blob: d93836a8940157f49190bb055cc6c8570709349f [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));
445static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000446static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000447static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
448static void set_ref_in_list __ARGS((list_T *l, int copyID));
449static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200450static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000451static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
453static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000454static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000455static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000456static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000458static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
459static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000461#ifdef FEAT_FLOAT
462static int string2float __ARGS((char_u *text, float_T *value));
463#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000464static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
465static int find_internal_func __ARGS((char_u *name));
466static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
467static 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 +0200468static 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 +0000469static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000470static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000471
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000472#ifdef FEAT_FLOAT
473static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200474static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000475#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200482static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000483static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200484static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#ifdef FEAT_FLOAT
498static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
499#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000500static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000503static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000505#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000506static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
509#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000512#ifdef FEAT_FLOAT
513static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200514static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000515#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000516static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
519static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200530#ifdef FEAT_FLOAT
531static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
532#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000533static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000535static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000536static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000541#ifdef FEAT_FLOAT
542static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200544static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000545#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000546static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000555static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000556static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000557static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000563static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000571static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000572static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000573static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000574static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000575static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200577static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000578static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000586static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000600static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000606static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000607static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000618#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200619static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000620static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
621#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000626static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000627static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000628static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000630static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000634#ifdef vim_mkdir
635static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
636#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100638#ifdef FEAT_MZSCHEME
639static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000643static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000644#ifdef FEAT_FLOAT
645static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000648static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000649static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000651static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000652static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000664#ifdef FEAT_FLOAT
665static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
666#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000668static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000670static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000672static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000677static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000678static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000679static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000680static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200682static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000683static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000684static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000685static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000687#ifdef FEAT_FLOAT
688static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200689static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000690#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000692static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000693static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000696#ifdef FEAT_FLOAT
697static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
699#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000700static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200701static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702#ifdef HAVE_STRFTIME
703static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
704#endif
705static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200711static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200712static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000713static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000718static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200719static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000721static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000722static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000723static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000724static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000725static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000726static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000727static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200728#ifdef FEAT_FLOAT
729static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
731#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000732static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
733static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000735#ifdef FEAT_FLOAT
736static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
737#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000738static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200739static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200740static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000750static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000752static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000753static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000755static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000756static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000757static int get_env_len __ARGS((char_u **arg));
758static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000759static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000760static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
761#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
762#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
763 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000764static 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 +0000765static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000766static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000767static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
768static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static typval_T *alloc_tv __ARGS((void));
770static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000771static void init_tv __ARGS((typval_T *varp));
772static long get_tv_number __ARGS((typval_T *varp));
773static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000774static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static char_u *get_tv_string __ARGS((typval_T *varp));
776static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000777static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000779static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
781static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
782static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000783static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
784static 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 +0000785static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
786static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000787static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200788static int var_check_func_name __ARGS((char_u *name, int new_var));
789static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000790static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000791static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000792static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
793static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
794static int eval_fname_script __ARGS((char_u *p));
795static int eval_fname_sid __ARGS((char_u *p));
796static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static ufunc_T *find_func __ARGS((char_u *name));
798static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000799static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000800#ifdef FEAT_PROFILE
801static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000802static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
803static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
804static int
805# ifdef __BORLANDC__
806 _RTLENTRYF
807# endif
808 prof_total_cmp __ARGS((const void *s1, const void *s2));
809static int
810# ifdef __BORLANDC__
811 _RTLENTRYF
812# endif
813 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000815static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000816static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000817static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000818static void func_free __ARGS((ufunc_T *fp));
819static void func_unref __ARGS((char_u *name));
820static void func_ref __ARGS((char_u *name));
821static 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 +0000822static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
823static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000824static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000825static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
826static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000827static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000828static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000829static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200831
832#ifdef EBCDIC
833static int compare_func_name __ARGS((const void *s1, const void *s2));
834static void sortFunctions __ARGS(());
835#endif
836
837
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000838/* Character used as separated in autoload function/variable names. */
839#define AUTOLOAD_CHAR '#'
840
Bram Moolenaar33570922005-01-25 22:26:29 +0000841/*
842 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000843 */
844 void
845eval_init()
846{
Bram Moolenaar33570922005-01-25 22:26:29 +0000847 int i;
848 struct vimvar *p;
849
850 init_var_dict(&globvardict, &globvars_var);
851 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200852 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000853 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000854 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000855
856 for (i = 0; i < VV_LEN; ++i)
857 {
858 p = &vimvars[i];
859 STRCPY(p->vv_di.di_key, p->vv_name);
860 if (p->vv_flags & VV_RO)
861 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
862 else if (p->vv_flags & VV_RO_SBX)
863 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
864 else
865 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000866
867 /* add to v: scope dict, unless the value is not always available */
868 if (p->vv_type != VAR_UNKNOWN)
869 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000870 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000871 /* add to compat scope dict */
872 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000873 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000874 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200875
876#ifdef EBCDIC
877 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100878 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200879 */
880 sortFunctions();
881#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000882}
883
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000884#if defined(EXITFREE) || defined(PROTO)
885 void
886eval_clear()
887{
888 int i;
889 struct vimvar *p;
890
891 for (i = 0; i < VV_LEN; ++i)
892 {
893 p = &vimvars[i];
894 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000895 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000896 vim_free(p->vv_str);
897 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000898 }
899 else if (p->vv_di.di_tv.v_type == VAR_LIST)
900 {
901 list_unref(p->vv_list);
902 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000903 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000904 }
905 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000906 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000907 hash_clear(&compat_hashtab);
908
Bram Moolenaard9fba312005-06-26 22:34:35 +0000909 free_scriptnames();
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200910 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911
912 /* global variables */
913 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000914
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000915 /* autoloaded script names */
916 ga_clear_strings(&ga_loaded);
917
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200918 /* script-local variables */
919 for (i = 1; i <= ga_scripts.ga_len; ++i)
920 {
921 vars_clear(&SCRIPT_VARS(i));
922 vim_free(SCRIPT_SV(i));
923 }
924 ga_clear(&ga_scripts);
925
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000926 /* unreferenced lists and dicts */
927 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000928
929 /* functions */
930 free_all_functions();
931 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000932}
933#endif
934
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 * Return the name of the executed function.
937 */
938 char_u *
939func_name(cookie)
940 void *cookie;
941{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000942 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943}
944
945/*
946 * Return the address holding the next breakpoint line for a funccall cookie.
947 */
948 linenr_T *
949func_breakpoint(cookie)
950 void *cookie;
951{
Bram Moolenaar33570922005-01-25 22:26:29 +0000952 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953}
954
955/*
956 * Return the address holding the debug tick for a funccall cookie.
957 */
958 int *
959func_dbg_tick(cookie)
960 void *cookie;
961{
Bram Moolenaar33570922005-01-25 22:26:29 +0000962 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the nesting level for a funccall cookie.
967 */
968 int
969func_level(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000976funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000978/* pointer to list of previously used funccal, still around because some
979 * item in it is still being used. */
980funccall_T *previous_funccal = NULL;
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982/*
983 * Return TRUE when a function was ended by a ":return" command.
984 */
985 int
986current_func_returned()
987{
988 return current_funccal->returned;
989}
990
991
992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 * Set an internal variable to a string value. Creates the variable if it does
994 * not already exist.
995 */
996 void
997set_internal_string_var(name, value)
998 char_u *name;
999 char_u *value;
1000{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001001 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001002 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
1004 val = vim_strsave(value);
1005 if (val != NULL)
1006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001007 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001010 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001011 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
1013 }
1014}
1015
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001017static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001018static char_u *redir_endp = NULL;
1019static char_u *redir_varname = NULL;
1020
1021/*
1022 * Start recording command output to a variable
1023 * Returns OK if successfully completed the setup. FAIL otherwise.
1024 */
1025 int
1026var_redir_start(name, append)
1027 char_u *name;
1028 int append; /* append to an existing variable */
1029{
1030 int save_emsg;
1031 int err;
1032 typval_T tv;
1033
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001034 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001035 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036 {
1037 EMSG(_(e_invarg));
1038 return FAIL;
1039 }
1040
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001041 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001042 redir_varname = vim_strsave(name);
1043 if (redir_varname == NULL)
1044 return FAIL;
1045
1046 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1047 if (redir_lval == NULL)
1048 {
1049 var_redir_stop();
1050 return FAIL;
1051 }
1052
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001053 /* The output is stored in growarray "redir_ga" until redirection ends. */
1054 ga_init2(&redir_ga, (int)sizeof(char), 500);
1055
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001057 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1058 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1060 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001061 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 if (redir_endp != NULL && *redir_endp != NUL)
1063 /* Trailing characters are present after the variable name */
1064 EMSG(_(e_trailing));
1065 else
1066 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001067 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001068 var_redir_stop();
1069 return FAIL;
1070 }
1071
1072 /* check if we can write to the variable: set it to or append an empty
1073 * string */
1074 save_emsg = did_emsg;
1075 did_emsg = FALSE;
1076 tv.v_type = VAR_STRING;
1077 tv.vval.v_string = (char_u *)"";
1078 if (append)
1079 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1080 else
1081 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001082 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001084 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085 if (err)
1086 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001087 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 var_redir_stop();
1089 return FAIL;
1090 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091
1092 return OK;
1093}
1094
1095/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001096 * Append "value[value_len]" to the variable set by var_redir_start().
1097 * The actual appending is postponed until redirection ends, because the value
1098 * appended may in fact be the string we write to, changing it may cause freed
1099 * memory to be used:
1100 * :redir => foo
1101 * :let foo
1102 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 */
1104 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001105var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001107 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001109 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110
1111 if (redir_lval == NULL)
1112 return;
1113
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001115 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001120 {
1121 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001123 }
1124 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126}
1127
1128/*
1129 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001130 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 */
1132 void
1133var_redir_stop()
1134{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001135 typval_T tv;
1136
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 if (redir_lval != NULL)
1138 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001139 /* If there was no error: assign the text to the variable. */
1140 if (redir_endp != NULL)
1141 {
1142 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1143 tv.v_type = VAR_STRING;
1144 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001145 /* Call get_lval() again, if it's inside a Dict or List it may
1146 * have changed. */
1147 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1148 FALSE, FALSE, FALSE, FNE_CHECK_START);
1149 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1150 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1151 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001153
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001154 /* free the collected output */
1155 vim_free(redir_ga.ga_data);
1156 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 vim_free(redir_lval);
1159 redir_lval = NULL;
1160 }
1161 vim_free(redir_varname);
1162 redir_varname = NULL;
1163}
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165# if defined(FEAT_MBYTE) || defined(PROTO)
1166 int
1167eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1168 char_u *enc_from;
1169 char_u *enc_to;
1170 char_u *fname_from;
1171 char_u *fname_to;
1172{
1173 int err = FALSE;
1174
1175 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1176 set_vim_var_string(VV_CC_TO, enc_to, -1);
1177 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1178 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1179 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1180 err = TRUE;
1181 set_vim_var_string(VV_CC_FROM, NULL, -1);
1182 set_vim_var_string(VV_CC_TO, NULL, -1);
1183 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1184 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1185
1186 if (err)
1187 return FAIL;
1188 return OK;
1189}
1190# endif
1191
1192# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1193 int
1194eval_printexpr(fname, args)
1195 char_u *fname;
1196 char_u *args;
1197{
1198 int err = FALSE;
1199
1200 set_vim_var_string(VV_FNAME_IN, fname, -1);
1201 set_vim_var_string(VV_CMDARG, args, -1);
1202 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1203 err = TRUE;
1204 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1205 set_vim_var_string(VV_CMDARG, NULL, -1);
1206
1207 if (err)
1208 {
1209 mch_remove(fname);
1210 return FAIL;
1211 }
1212 return OK;
1213}
1214# endif
1215
1216# if defined(FEAT_DIFF) || defined(PROTO)
1217 void
1218eval_diff(origfile, newfile, outfile)
1219 char_u *origfile;
1220 char_u *newfile;
1221 char_u *outfile;
1222{
1223 int err = FALSE;
1224
1225 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1226 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1227 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1228 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1229 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1230 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1231 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1232}
1233
1234 void
1235eval_patch(origfile, difffile, outfile)
1236 char_u *origfile;
1237 char_u *difffile;
1238 char_u *outfile;
1239{
1240 int err;
1241
1242 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1243 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1244 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1245 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1246 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1247 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1248 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1249}
1250# endif
1251
1252/*
1253 * Top level evaluation function, returning a boolean.
1254 * Sets "error" to TRUE if there was an error.
1255 * Return TRUE or FALSE.
1256 */
1257 int
1258eval_to_bool(arg, error, nextcmd, skip)
1259 char_u *arg;
1260 int *error;
1261 char_u **nextcmd;
1262 int skip; /* only parse, don't execute */
1263{
Bram Moolenaar33570922005-01-25 22:26:29 +00001264 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 int retval = FALSE;
1266
1267 if (skip)
1268 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001269 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 else
1272 {
1273 *error = FALSE;
1274 if (!skip)
1275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001276 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001277 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279 }
1280 if (skip)
1281 --emsg_skip;
1282
1283 return retval;
1284}
1285
1286/*
1287 * Top level evaluation function, returning a string. If "skip" is TRUE,
1288 * only parsing to "nextcmd" is done, without reporting errors. Return
1289 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1290 */
1291 char_u *
1292eval_to_string_skip(arg, nextcmd, skip)
1293 char_u *arg;
1294 char_u **nextcmd;
1295 int skip; /* only parse, don't execute */
1296{
Bram Moolenaar33570922005-01-25 22:26:29 +00001297 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 char_u *retval;
1299
1300 if (skip)
1301 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001302 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 retval = NULL;
1304 else
1305 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 retval = vim_strsave(get_tv_string(&tv));
1307 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 }
1309 if (skip)
1310 --emsg_skip;
1311
1312 return retval;
1313}
1314
1315/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001316 * Skip over an expression at "*pp".
1317 * Return FAIL for an error, OK otherwise.
1318 */
1319 int
1320skip_expr(pp)
1321 char_u **pp;
1322{
Bram Moolenaar33570922005-01-25 22:26:29 +00001323 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001324
1325 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001327}
1328
1329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001331 * When "convert" is TRUE convert a List into a sequence of lines and convert
1332 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 * Return pointer to allocated memory, or NULL for failure.
1334 */
1335 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001336eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 char_u *arg;
1338 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001339 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340{
Bram Moolenaar33570922005-01-25 22:26:29 +00001341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001343 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001344#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001345 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001348 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 retval = NULL;
1350 else
1351 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001353 {
1354 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001355 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001356 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001357 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001358 if (tv.vval.v_list->lv_len > 0)
1359 ga_append(&ga, NL);
1360 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001361 ga_append(&ga, NUL);
1362 retval = (char_u *)ga.ga_data;
1363 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001364#ifdef FEAT_FLOAT
1365 else if (convert && tv.v_type == VAR_FLOAT)
1366 {
1367 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1368 retval = vim_strsave(numbuf);
1369 }
1370#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001371 else
1372 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 }
1375
1376 return retval;
1377}
1378
1379/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001380 * Call eval_to_string() without using current local variables and using
1381 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 */
1383 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001384eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 char_u *arg;
1386 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001387 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388{
1389 char_u *retval;
1390 void *save_funccalp;
1391
1392 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393 if (use_sandbox)
1394 ++sandbox;
1395 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001396 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397 if (use_sandbox)
1398 --sandbox;
1399 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 restore_funccal(save_funccalp);
1401 return retval;
1402}
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404/*
1405 * Top level evaluation function, returning a number.
1406 * Evaluates "expr" silently.
1407 * Returns -1 for an error.
1408 */
1409 int
1410eval_to_number(expr)
1411 char_u *expr;
1412{
Bram Moolenaar33570922005-01-25 22:26:29 +00001413 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001415 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416
1417 ++emsg_off;
1418
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001419 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 retval = -1;
1421 else
1422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001423 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 }
1426 --emsg_off;
1427
1428 return retval;
1429}
1430
Bram Moolenaara40058a2005-07-11 22:42:07 +00001431/*
1432 * Prepare v: variable "idx" to be used.
1433 * Save the current typeval in "save_tv".
1434 * When not used yet add the variable to the v: hashtable.
1435 */
1436 static void
1437prepare_vimvar(idx, save_tv)
1438 int idx;
1439 typval_T *save_tv;
1440{
1441 *save_tv = vimvars[idx].vv_tv;
1442 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1443 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1444}
1445
1446/*
1447 * Restore v: variable "idx" to typeval "save_tv".
1448 * When no longer defined, remove the variable from the v: hashtable.
1449 */
1450 static void
1451restore_vimvar(idx, save_tv)
1452 int idx;
1453 typval_T *save_tv;
1454{
1455 hashitem_T *hi;
1456
Bram Moolenaara40058a2005-07-11 22:42:07 +00001457 vimvars[idx].vv_tv = *save_tv;
1458 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1459 {
1460 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1461 if (HASHITEM_EMPTY(hi))
1462 EMSG2(_(e_intern2), "restore_vimvar()");
1463 else
1464 hash_remove(&vimvarht, hi);
1465 }
1466}
1467
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001468#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001469/*
1470 * Evaluate an expression to a list with suggestions.
1471 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001472 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001473 */
1474 list_T *
1475eval_spell_expr(badword, expr)
1476 char_u *badword;
1477 char_u *expr;
1478{
1479 typval_T save_val;
1480 typval_T rettv;
1481 list_T *list = NULL;
1482 char_u *p = skipwhite(expr);
1483
1484 /* Set "v:val" to the bad word. */
1485 prepare_vimvar(VV_VAL, &save_val);
1486 vimvars[VV_VAL].vv_type = VAR_STRING;
1487 vimvars[VV_VAL].vv_str = badword;
1488 if (p_verbose == 0)
1489 ++emsg_off;
1490
1491 if (eval1(&p, &rettv, TRUE) == OK)
1492 {
1493 if (rettv.v_type != VAR_LIST)
1494 clear_tv(&rettv);
1495 else
1496 list = rettv.vval.v_list;
1497 }
1498
1499 if (p_verbose == 0)
1500 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001501 restore_vimvar(VV_VAL, &save_val);
1502
1503 return list;
1504}
1505
1506/*
1507 * "list" is supposed to contain two items: a word and a number. Return the
1508 * word in "pp" and the number as the return value.
1509 * Return -1 if anything isn't right.
1510 * Used to get the good word and score from the eval_spell_expr() result.
1511 */
1512 int
1513get_spellword(list, pp)
1514 list_T *list;
1515 char_u **pp;
1516{
1517 listitem_T *li;
1518
1519 li = list->lv_first;
1520 if (li == NULL)
1521 return -1;
1522 *pp = get_tv_string(&li->li_tv);
1523
1524 li = li->li_next;
1525 if (li == NULL)
1526 return -1;
1527 return get_tv_number(&li->li_tv);
1528}
1529#endif
1530
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001531/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001532 * Top level evaluation function.
1533 * Returns an allocated typval_T with the result.
1534 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001535 */
1536 typval_T *
1537eval_expr(arg, nextcmd)
1538 char_u *arg;
1539 char_u **nextcmd;
1540{
1541 typval_T *tv;
1542
1543 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001544 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001545 {
1546 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001547 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001548 }
1549
1550 return tv;
1551}
1552
1553
Bram Moolenaar4f688582007-07-24 12:34:30 +00001554#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1555 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001557 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001558 * Uses argv[argc] for the function arguments. Only Number and String
1559 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001560 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001562 int
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001563call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 char_u *func;
1565 int argc;
1566 char_u **argv;
1567 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569{
Bram Moolenaar33570922005-01-25 22:26:29 +00001570 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 long n;
1572 int len;
1573 int i;
1574 int doesrange;
1575 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001576 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001578 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
1582 for (i = 0; i < argc; i++)
1583 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001584 /* Pass a NULL or empty argument as an empty string */
1585 if (argv[i] == NULL || *argv[i] == NUL)
1586 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001587 argvars[i].v_type = VAR_STRING;
1588 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001589 continue;
1590 }
1591
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 /* Recognize a number argument, the others must be strings. */
1593 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1594 if (len != 0 && len == (int)STRLEN(argv[i]))
1595 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001596 argvars[i].v_type = VAR_NUMBER;
1597 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 }
1599 else
1600 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001601 argvars[i].v_type = VAR_STRING;
1602 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 }
1604 }
1605
1606 if (safe)
1607 {
1608 save_funccalp = save_funccal();
1609 ++sandbox;
1610 }
1611
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001612 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1613 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (safe)
1617 {
1618 --sandbox;
1619 restore_funccal(save_funccalp);
1620 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001621 vim_free(argvars);
1622
1623 if (ret == FAIL)
1624 clear_tv(rettv);
1625
1626 return ret;
1627}
1628
Bram Moolenaar4f688582007-07-24 12:34:30 +00001629# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001631 * Call vimL function "func" and return the result as a string.
1632 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001633 * Uses argv[argc] for the function arguments.
1634 */
1635 void *
1636call_func_retstr(func, argc, argv, safe)
1637 char_u *func;
1638 int argc;
1639 char_u **argv;
1640 int safe; /* use the sandbox */
1641{
1642 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001643 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001644
1645 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1646 return NULL;
1647
1648 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 return retval;
1651}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001652# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653
Bram Moolenaar4f688582007-07-24 12:34:30 +00001654# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001655/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001656 * Call vimL function "func" and return the result as a number.
1657 * Returns -1 when calling the function fails.
1658 * Uses argv[argc] for the function arguments.
1659 */
1660 long
1661call_func_retnr(func, argc, argv, safe)
1662 char_u *func;
1663 int argc;
1664 char_u **argv;
1665 int safe; /* use the sandbox */
1666{
1667 typval_T rettv;
1668 long retval;
1669
1670 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1671 return -1;
1672
1673 retval = get_tv_number_chk(&rettv, NULL);
1674 clear_tv(&rettv);
1675 return retval;
1676}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001677# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001678
1679/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001680 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001681 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001682 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683 */
1684 void *
1685call_func_retlist(func, argc, argv, safe)
1686 char_u *func;
1687 int argc;
1688 char_u **argv;
1689 int safe; /* use the sandbox */
1690{
1691 typval_T rettv;
1692
1693 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1694 return NULL;
1695
1696 if (rettv.v_type != VAR_LIST)
1697 {
1698 clear_tv(&rettv);
1699 return NULL;
1700 }
1701
1702 return rettv.vval.v_list;
1703}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704#endif
1705
Bram Moolenaar4f688582007-07-24 12:34:30 +00001706
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707/*
1708 * Save the current function call pointer, and set it to NULL.
1709 * Used when executing autocommands and for ":source".
1710 */
1711 void *
1712save_funccal()
1713{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001714 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 current_funccal = NULL;
1717 return (void *)fc;
1718}
1719
1720 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001721restore_funccal(vfc)
1722 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001724 funccall_T *fc = (funccall_T *)vfc;
1725
1726 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727}
1728
Bram Moolenaar05159a02005-02-26 23:04:13 +00001729#if defined(FEAT_PROFILE) || defined(PROTO)
1730/*
1731 * Prepare profiling for entering a child or something else that is not
1732 * counted for the script/function itself.
1733 * Should always be called in pair with prof_child_exit().
1734 */
1735 void
1736prof_child_enter(tm)
1737 proftime_T *tm; /* place to store waittime */
1738{
1739 funccall_T *fc = current_funccal;
1740
1741 if (fc != NULL && fc->func->uf_profiling)
1742 profile_start(&fc->prof_child);
1743 script_prof_save(tm);
1744}
1745
1746/*
1747 * Take care of time spent in a child.
1748 * Should always be called after prof_child_enter().
1749 */
1750 void
1751prof_child_exit(tm)
1752 proftime_T *tm; /* where waittime was stored */
1753{
1754 funccall_T *fc = current_funccal;
1755
1756 if (fc != NULL && fc->func->uf_profiling)
1757 {
1758 profile_end(&fc->prof_child);
1759 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1760 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1761 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1762 }
1763 script_prof_restore(tm);
1764}
1765#endif
1766
1767
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768#ifdef FEAT_FOLDING
1769/*
1770 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1771 * it in "*cp". Doesn't give error messages.
1772 */
1773 int
1774eval_foldexpr(arg, cp)
1775 char_u *arg;
1776 int *cp;
1777{
Bram Moolenaar33570922005-01-25 22:26:29 +00001778 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 int retval;
1780 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001781 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1782 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
1784 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001785 if (use_sandbox)
1786 ++sandbox;
1787 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001789 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 retval = 0;
1791 else
1792 {
1793 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 if (tv.v_type == VAR_NUMBER)
1795 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001796 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 retval = 0;
1798 else
1799 {
1800 /* If the result is a string, check if there is a non-digit before
1801 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001802 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 if (!VIM_ISDIGIT(*s) && *s != '-')
1804 *cp = *s++;
1805 retval = atol((char *)s);
1806 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
1809 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001810 if (use_sandbox)
1811 --sandbox;
1812 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
1814 return retval;
1815}
1816#endif
1817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 * ":let" list all variable values
1820 * ":let var1 var2" list variable values
1821 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001822 * ":let var += expr" assignment command.
1823 * ":let var -= expr" assignment command.
1824 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 */
1827 void
1828ex_let(eap)
1829 exarg_T *eap;
1830{
1831 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001833 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 int var_count = 0;
1836 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001837 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001838 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001839 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
Bram Moolenaardb552d602006-03-23 22:59:57 +00001841 argend = skip_var_list(arg, &var_count, &semicolon);
1842 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001843 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001844 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1845 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001846 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001849 /*
1850 * ":let" without "=": list variables
1851 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 if (*arg == '[')
1853 EMSG(_(e_invarg));
1854 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001855 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001856 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001860 list_glob_vars(&first);
1861 list_buf_vars(&first);
1862 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001863#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001865#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001866 list_script_vars(&first);
1867 list_func_vars(&first);
1868 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 eap->nextcmd = check_nextcmd(arg);
1871 }
1872 else
1873 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001874 op[0] = '=';
1875 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001876 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001877 {
1878 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1879 op[0] = expr[-1]; /* +=, -= or .= */
1880 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 if (eap->skip)
1884 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (eap->skip)
1887 {
1888 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 --emsg_skip;
1891 }
1892 else if (i != FAIL)
1893 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001895 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 }
1898 }
1899}
1900
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901/*
1902 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1903 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001904 * When "nextchars" is not NULL it points to a string with characters that
1905 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1906 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907 * Returns OK or FAIL;
1908 */
1909 static int
1910ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1911 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001912 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 int copy; /* copy values from "tv", don't move */
1914 int semicolon; /* from skip_var_list() */
1915 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917{
1918 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001919 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001921 listitem_T *item;
1922 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923
1924 if (*arg != '[')
1925 {
1926 /*
1927 * ":let var = expr" or ":for var in list"
1928 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001929 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930 return FAIL;
1931 return OK;
1932 }
1933
1934 /*
1935 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1936 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001937 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 {
1939 EMSG(_(e_listreq));
1940 return FAIL;
1941 }
1942
1943 i = list_len(l);
1944 if (semicolon == 0 && var_count < i)
1945 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001946 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001947 return FAIL;
1948 }
1949 if (var_count - semicolon > i)
1950 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001951 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 return FAIL;
1953 }
1954
1955 item = l->lv_first;
1956 while (*arg != ']')
1957 {
1958 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001959 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 item = item->li_next;
1961 if (arg == NULL)
1962 return FAIL;
1963
1964 arg = skipwhite(arg);
1965 if (*arg == ';')
1966 {
1967 /* Put the rest of the list (may be empty) in the var after ';'.
1968 * Create a new list for this. */
1969 l = list_alloc();
1970 if (l == NULL)
1971 return FAIL;
1972 while (item != NULL)
1973 {
1974 list_append_tv(l, &item->li_tv);
1975 item = item->li_next;
1976 }
1977
1978 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001979 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 ltv.vval.v_list = l;
1981 l->lv_refcount = 1;
1982
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001983 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1984 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 clear_tv(&ltv);
1986 if (arg == NULL)
1987 return FAIL;
1988 break;
1989 }
1990 else if (*arg != ',' && *arg != ']')
1991 {
1992 EMSG2(_(e_intern2), "ex_let_vars()");
1993 return FAIL;
1994 }
1995 }
1996
1997 return OK;
1998}
1999
2000/*
2001 * Skip over assignable variable "var" or list of variables "[var, var]".
2002 * Used for ":let varvar = expr" and ":for varvar in expr".
2003 * For "[var, var]" increment "*var_count" for each variable.
2004 * for "[var, var; var]" set "semicolon".
2005 * Return NULL for an error.
2006 */
2007 static char_u *
2008skip_var_list(arg, var_count, semicolon)
2009 char_u *arg;
2010 int *var_count;
2011 int *semicolon;
2012{
2013 char_u *p, *s;
2014
2015 if (*arg == '[')
2016 {
2017 /* "[var, var]": find the matching ']'. */
2018 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002019 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002020 {
2021 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2022 s = skip_var_one(p);
2023 if (s == p)
2024 {
2025 EMSG2(_(e_invarg2), p);
2026 return NULL;
2027 }
2028 ++*var_count;
2029
2030 p = skipwhite(s);
2031 if (*p == ']')
2032 break;
2033 else if (*p == ';')
2034 {
2035 if (*semicolon == 1)
2036 {
2037 EMSG(_("Double ; in list of variables"));
2038 return NULL;
2039 }
2040 *semicolon = 1;
2041 }
2042 else if (*p != ',')
2043 {
2044 EMSG2(_(e_invarg2), p);
2045 return NULL;
2046 }
2047 }
2048 return p + 1;
2049 }
2050 else
2051 return skip_var_one(arg);
2052}
2053
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002054/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002055 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002056 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002057 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002058 static char_u *
2059skip_var_one(arg)
2060 char_u *arg;
2061{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002062 if (*arg == '@' && arg[1] != NUL)
2063 return arg + 2;
2064 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2065 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002066}
2067
Bram Moolenaara7043832005-01-21 11:56:39 +00002068/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002069 * List variables for hashtab "ht" with prefix "prefix".
2070 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002071 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002073list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002074 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002075 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002076 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002077 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002078{
Bram Moolenaar33570922005-01-25 22:26:29 +00002079 hashitem_T *hi;
2080 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002081 int todo;
2082
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002083 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002084 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2085 {
2086 if (!HASHITEM_EMPTY(hi))
2087 {
2088 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002089 di = HI2DI(hi);
2090 if (empty || di->di_tv.v_type != VAR_STRING
2091 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002092 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 }
2094 }
2095}
2096
2097/*
2098 * List global variables.
2099 */
2100 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002101list_glob_vars(first)
2102 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002103{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002105}
2106
2107/*
2108 * List buffer variables.
2109 */
2110 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002111list_buf_vars(first)
2112 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002113{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002114 char_u numbuf[NUMBUFLEN];
2115
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2117 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118
2119 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2121 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002122}
2123
2124/*
2125 * List window variables.
2126 */
2127 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002128list_win_vars(first)
2129 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002130{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002131 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2132 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002133}
2134
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002135#ifdef FEAT_WINDOWS
2136/*
2137 * List tab page variables.
2138 */
2139 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140list_tab_vars(first)
2141 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002142{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002143 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2144 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002145}
2146#endif
2147
Bram Moolenaara7043832005-01-21 11:56:39 +00002148/*
2149 * List Vim variables.
2150 */
2151 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152list_vim_vars(first)
2153 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002154{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002156}
2157
2158/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002159 * List script-local variables, if there is a script.
2160 */
2161 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162list_script_vars(first)
2163 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002164{
2165 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2167 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002168}
2169
2170/*
2171 * List function variables, if there is a function.
2172 */
2173 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174list_func_vars(first)
2175 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002176{
2177 if (current_funccal != NULL)
2178 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180}
2181
2182/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183 * List variables in "arg".
2184 */
2185 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 exarg_T *eap;
2188 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190{
2191 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002192 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002194 char_u *name_start;
2195 char_u *arg_subsc;
2196 char_u *tofree;
2197 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198
2199 while (!ends_excmd(*arg) && !got_int)
2200 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002201 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002203 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2205 {
2206 emsg_severe = TRUE;
2207 EMSG(_(e_trailing));
2208 break;
2209 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002211 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 /* get_name_len() takes care of expanding curly braces */
2214 name_start = name = arg;
2215 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2216 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 /* This is mainly to keep test 49 working: when expanding
2219 * curly braces fails overrule the exception error message. */
2220 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 emsg_severe = TRUE;
2223 EMSG2(_(e_invarg2), arg);
2224 break;
2225 }
2226 error = TRUE;
2227 }
2228 else
2229 {
2230 if (tofree != NULL)
2231 name = tofree;
2232 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 else
2235 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 /* handle d.key, l[idx], f(expr) */
2237 arg_subsc = arg;
2238 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002239 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002241 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002245 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002246 case 'g': list_glob_vars(first); break;
2247 case 'b': list_buf_vars(first); break;
2248 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002249#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002251#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002252 case 'v': list_vim_vars(first); break;
2253 case 's': list_script_vars(first); break;
2254 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 default:
2256 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002258 }
2259 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 {
2261 char_u numbuf[NUMBUFLEN];
2262 char_u *tf;
2263 int c;
2264 char_u *s;
2265
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002266 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 c = *arg;
2268 *arg = NUL;
2269 list_one_var_a((char_u *)"",
2270 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002271 tv.v_type,
2272 s == NULL ? (char_u *)"" : s,
2273 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 *arg = c;
2275 vim_free(tf);
2276 }
2277 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 }
2280 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281
2282 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284
2285 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286 }
2287
2288 return arg;
2289}
2290
2291/*
2292 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2293 * Returns a pointer to the char just after the var name.
2294 * Returns NULL if there is an error.
2295 */
2296 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002297ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002299 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002301 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002302 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303{
2304 int c1;
2305 char_u *name;
2306 char_u *p;
2307 char_u *arg_end = NULL;
2308 int len;
2309 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002310 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311
2312 /*
2313 * ":let $VAR = expr": Set environment variable.
2314 */
2315 if (*arg == '$')
2316 {
2317 /* Find the end of the name. */
2318 ++arg;
2319 name = arg;
2320 len = get_env_len(&arg);
2321 if (len == 0)
2322 EMSG2(_(e_invarg2), name - 1);
2323 else
2324 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325 if (op != NULL && (*op == '+' || *op == '-'))
2326 EMSG2(_(e_letwrong), op);
2327 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002328 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002330 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 {
2332 c1 = name[len];
2333 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002334 p = get_tv_string_chk(tv);
2335 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002336 {
2337 int mustfree = FALSE;
2338 char_u *s = vim_getenv(name, &mustfree);
2339
2340 if (s != NULL)
2341 {
2342 p = tofree = concat_str(s, p);
2343 if (mustfree)
2344 vim_free(s);
2345 }
2346 }
2347 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002348 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002349 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002350 if (STRICMP(name, "HOME") == 0)
2351 init_homedir();
2352 else if (didset_vim && STRICMP(name, "VIM") == 0)
2353 didset_vim = FALSE;
2354 else if (didset_vimruntime
2355 && STRICMP(name, "VIMRUNTIME") == 0)
2356 didset_vimruntime = FALSE;
2357 arg_end = arg;
2358 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002360 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 }
2362 }
2363 }
2364
2365 /*
2366 * ":let &option = expr": Set option value.
2367 * ":let &l:option = expr": Set local option value.
2368 * ":let &g:option = expr": Set global option value.
2369 */
2370 else if (*arg == '&')
2371 {
2372 /* Find the end of the name. */
2373 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002374 if (p == NULL || (endchars != NULL
2375 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 EMSG(_(e_letunexp));
2377 else
2378 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002379 long n;
2380 int opt_type;
2381 long numval;
2382 char_u *stringval = NULL;
2383 char_u *s;
2384
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 c1 = *p;
2386 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387
2388 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002389 s = get_tv_string_chk(tv); /* != NULL if number or string */
2390 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 {
2392 opt_type = get_option_value(arg, &numval,
2393 &stringval, opt_flags);
2394 if ((opt_type == 1 && *op == '.')
2395 || (opt_type == 0 && *op != '.'))
2396 EMSG2(_(e_letwrong), op);
2397 else
2398 {
2399 if (opt_type == 1) /* number */
2400 {
2401 if (*op == '+')
2402 n = numval + n;
2403 else
2404 n = numval - n;
2405 }
2406 else if (opt_type == 0 && stringval != NULL) /* string */
2407 {
2408 s = concat_str(stringval, s);
2409 vim_free(stringval);
2410 stringval = s;
2411 }
2412 }
2413 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002414 if (s != NULL)
2415 {
2416 set_option_value(arg, n, s, opt_flags);
2417 arg_end = p;
2418 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002420 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 }
2422 }
2423
2424 /*
2425 * ":let @r = expr": Set register contents.
2426 */
2427 else if (*arg == '@')
2428 {
2429 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430 if (op != NULL && (*op == '+' || *op == '-'))
2431 EMSG2(_(e_letwrong), op);
2432 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002433 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 EMSG(_(e_letunexp));
2435 else
2436 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002437 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 char_u *s;
2439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440 p = get_tv_string_chk(tv);
2441 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002443 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 if (s != NULL)
2445 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002446 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 vim_free(s);
2448 }
2449 }
2450 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002453 arg_end = arg + 1;
2454 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002455 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002456 }
2457 }
2458
2459 /*
2460 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002463 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002465 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002467 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2471 EMSG(_(e_letunexp));
2472 else
2473 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 arg_end = p;
2476 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 }
2480
2481 else
2482 EMSG2(_(e_invarg2), arg);
2483
2484 return arg_end;
2485}
2486
2487/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002488 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2489 */
2490 static int
2491check_changedtick(arg)
2492 char_u *arg;
2493{
2494 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2495 {
2496 EMSG2(_(e_readonlyvar), arg);
2497 return TRUE;
2498 }
2499 return FALSE;
2500}
2501
2502/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 * Get an lval: variable, Dict item or List item that can be assigned a value
2504 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2505 * "name.key", "name.key[expr]" etc.
2506 * Indexing only works if "name" is an existing List or Dictionary.
2507 * "name" points to the start of the name.
2508 * If "rettv" is not NULL it points to the value to be assigned.
2509 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2510 * wrong; must end in space or cmd separator.
2511 *
2512 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002513 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 */
2516 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002517get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002519 typval_T *rettv;
2520 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 int unlet;
2522 int skip;
2523 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002524 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 char_u *expr_start, *expr_end;
2528 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002529 dictitem_T *v;
2530 typval_T var1;
2531 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002535 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002536 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002539 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540
2541 if (skip)
2542 {
2543 /* When skipping just find the end of the name. */
2544 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002545 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 }
2547
2548 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002549 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 if (expr_start != NULL)
2551 {
2552 /* Don't expand the name when we already know there is an error. */
2553 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2554 && *p != '[' && *p != '.')
2555 {
2556 EMSG(_(e_trailing));
2557 return NULL;
2558 }
2559
2560 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2561 if (lp->ll_exp_name == NULL)
2562 {
2563 /* Report an invalid expression in braces, unless the
2564 * expression evaluation has been cancelled due to an
2565 * aborting error, an interrupt, or an exception. */
2566 if (!aborting() && !quiet)
2567 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002568 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 EMSG2(_(e_invarg2), name);
2570 return NULL;
2571 }
2572 }
2573 lp->ll_name = lp->ll_exp_name;
2574 }
2575 else
2576 lp->ll_name = name;
2577
2578 /* Without [idx] or .key we are done. */
2579 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2580 return p;
2581
2582 cc = *p;
2583 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002584 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 if (v == NULL && !quiet)
2586 EMSG2(_(e_undefvar), lp->ll_name);
2587 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002588 if (v == NULL)
2589 return NULL;
2590
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 /*
2592 * Loop until no more [idx] or .key is following.
2593 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002594 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2598 && !(lp->ll_tv->v_type == VAR_DICT
2599 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (!quiet)
2602 EMSG(_("E689: Can only index a List or Dictionary"));
2603 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 if (!quiet)
2608 EMSG(_("E708: [:] must come last"));
2609 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002611
Bram Moolenaar8c711452005-01-14 21:53:12 +00002612 len = -1;
2613 if (*p == '.')
2614 {
2615 key = p + 1;
2616 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2617 ;
2618 if (len == 0)
2619 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 if (!quiet)
2621 EMSG(_(e_emptykey));
2622 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 }
2624 p = key + len;
2625 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002626 else
2627 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002630 if (*p == ':')
2631 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002632 else
2633 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 empty1 = FALSE;
2635 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002637 if (get_tv_string_chk(&var1) == NULL)
2638 {
2639 /* not a number or string */
2640 clear_tv(&var1);
2641 return NULL;
2642 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002643 }
2644
2645 /* Optionally get the second index [ :expr]. */
2646 if (*p == ':')
2647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002651 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652 if (!empty1)
2653 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002655 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (rettv != NULL && (rettv->v_type != VAR_LIST
2657 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (!quiet)
2660 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 if (!empty1)
2662 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 }
2665 p = skipwhite(p + 1);
2666 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 else
2669 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 if (!empty1)
2674 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002677 if (get_tv_string_chk(&var2) == NULL)
2678 {
2679 /* not a number or string */
2680 if (!empty1)
2681 clear_tv(&var1);
2682 clear_tv(&var2);
2683 return NULL;
2684 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002690
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 if (!quiet)
2694 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 if (!empty1)
2696 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 }
2701
2702 /* Skip to past ']'. */
2703 ++p;
2704 }
2705
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 {
2708 if (len == -1)
2709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002711 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 if (*key == NUL)
2713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (!quiet)
2715 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
2719 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002720 lp->ll_list = NULL;
2721 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002722 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002723
2724 /* When assigning to g: check that a function and variable name is
2725 * valid. */
2726 if (rettv != NULL && lp->ll_dict == &globvardict)
2727 {
2728 if (rettv->v_type == VAR_FUNC
2729 && var_check_func_name(key, lp->ll_di == NULL))
2730 return NULL;
2731 if (!valid_varname(key))
2732 return NULL;
2733 }
2734
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002737 /* Can't add "v:" variable. */
2738 if (lp->ll_dict == &vimvardict)
2739 {
2740 EMSG2(_(e_illvar), name);
2741 return NULL;
2742 }
2743
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002744 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002748 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 if (len == -1)
2750 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 }
2753 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 if (len == -1)
2758 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 p = NULL;
2761 break;
2762 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002763 /* existing variable, need to check if it can be changed */
2764 else if (var_check_ro(lp->ll_di->di_flags, name))
2765 return NULL;
2766
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 if (len == -1)
2768 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 }
2771 else
2772 {
2773 /*
2774 * Get the number and item for the only or first index of the List.
2775 */
2776 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 else
2779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002780 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 clear_tv(&var1);
2782 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002783 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 lp->ll_list = lp->ll_tv->vval.v_list;
2785 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2786 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002788 if (lp->ll_n1 < 0)
2789 {
2790 lp->ll_n1 = 0;
2791 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2792 }
2793 }
2794 if (lp->ll_li == NULL)
2795 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002798 if (!quiet)
2799 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 }
2802
2803 /*
2804 * May need to find the item or absolute index for the second
2805 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 * When no index given: "lp->ll_empty2" is TRUE.
2807 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002811 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002817 {
2818 if (!quiet)
2819 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002821 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 }
2824
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2826 if (lp->ll_n1 < 0)
2827 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2828 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002829 {
2830 if (!quiet)
2831 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002833 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002834 }
2835
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002837 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002838 }
2839
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 return p;
2841}
2842
2843/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002844 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 */
2846 static void
2847clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002848 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849{
2850 vim_free(lp->ll_exp_name);
2851 vim_free(lp->ll_newkey);
2852}
2853
2854/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002855 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002857 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 */
2859 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002860set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002861 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002863 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002865 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866{
2867 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002868 listitem_T *ri;
2869 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870
2871 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002872 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002874 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 cc = *endp;
2876 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002877 if (op != NULL && *op != '=')
2878 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002880
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002881 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002882 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002883 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002884 {
2885 if (tv_op(&tv, rettv, op) == OK)
2886 set_var(lp->ll_name, &tv, FALSE);
2887 clear_tv(&tv);
2888 }
2889 }
2890 else
2891 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002893 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002895 else if (tv_check_lock(lp->ll_newkey == NULL
2896 ? lp->ll_tv->v_lock
2897 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2898 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 else if (lp->ll_range)
2900 {
2901 /*
2902 * Assign the List values to the list items.
2903 */
2904 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002905 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002906 if (op != NULL && *op != '=')
2907 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2908 else
2909 {
2910 clear_tv(&lp->ll_li->li_tv);
2911 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2912 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 ri = ri->li_next;
2914 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2915 break;
2916 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002917 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002919 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002920 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 ri = NULL;
2922 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002923 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002924 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 lp->ll_li = lp->ll_li->li_next;
2926 ++lp->ll_n1;
2927 }
2928 if (ri != NULL)
2929 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002930 else if (lp->ll_empty2
2931 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 : lp->ll_n1 != lp->ll_n2)
2933 EMSG(_("E711: List value has not enough items"));
2934 }
2935 else
2936 {
2937 /*
2938 * Assign to a List or Dictionary item.
2939 */
2940 if (lp->ll_newkey != NULL)
2941 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002942 if (op != NULL && *op != '=')
2943 {
2944 EMSG2(_(e_letwrong), op);
2945 return;
2946 }
2947
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002949 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 if (di == NULL)
2951 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002952 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2953 {
2954 vim_free(di);
2955 return;
2956 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002958 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002959 else if (op != NULL && *op != '=')
2960 {
2961 tv_op(lp->ll_tv, rettv, op);
2962 return;
2963 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002964 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002966
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 /*
2968 * Assign the value to the variable or list item.
2969 */
2970 if (copy)
2971 copy_tv(rettv, lp->ll_tv);
2972 else
2973 {
2974 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002975 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002977 }
2978 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002979}
2980
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002981/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002982 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2983 * Returns OK or FAIL.
2984 */
2985 static int
2986tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002987 typval_T *tv1;
2988 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002989 char_u *op;
2990{
2991 long n;
2992 char_u numbuf[NUMBUFLEN];
2993 char_u *s;
2994
2995 /* Can't do anything with a Funcref or a Dict on the right. */
2996 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2997 {
2998 switch (tv1->v_type)
2999 {
3000 case VAR_DICT:
3001 case VAR_FUNC:
3002 break;
3003
3004 case VAR_LIST:
3005 if (*op != '+' || tv2->v_type != VAR_LIST)
3006 break;
3007 /* List += List */
3008 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3009 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3010 return OK;
3011
3012 case VAR_NUMBER:
3013 case VAR_STRING:
3014 if (tv2->v_type == VAR_LIST)
3015 break;
3016 if (*op == '+' || *op == '-')
3017 {
3018 /* nr += nr or nr -= nr*/
3019 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003020#ifdef FEAT_FLOAT
3021 if (tv2->v_type == VAR_FLOAT)
3022 {
3023 float_T f = n;
3024
3025 if (*op == '+')
3026 f += tv2->vval.v_float;
3027 else
3028 f -= tv2->vval.v_float;
3029 clear_tv(tv1);
3030 tv1->v_type = VAR_FLOAT;
3031 tv1->vval.v_float = f;
3032 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003033 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003034#endif
3035 {
3036 if (*op == '+')
3037 n += get_tv_number(tv2);
3038 else
3039 n -= get_tv_number(tv2);
3040 clear_tv(tv1);
3041 tv1->v_type = VAR_NUMBER;
3042 tv1->vval.v_number = n;
3043 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003044 }
3045 else
3046 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003047 if (tv2->v_type == VAR_FLOAT)
3048 break;
3049
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003050 /* str .= str */
3051 s = get_tv_string(tv1);
3052 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3053 clear_tv(tv1);
3054 tv1->v_type = VAR_STRING;
3055 tv1->vval.v_string = s;
3056 }
3057 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003058
3059#ifdef FEAT_FLOAT
3060 case VAR_FLOAT:
3061 {
3062 float_T f;
3063
3064 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3065 && tv2->v_type != VAR_NUMBER
3066 && tv2->v_type != VAR_STRING))
3067 break;
3068 if (tv2->v_type == VAR_FLOAT)
3069 f = tv2->vval.v_float;
3070 else
3071 f = get_tv_number(tv2);
3072 if (*op == '+')
3073 tv1->vval.v_float += f;
3074 else
3075 tv1->vval.v_float -= f;
3076 }
3077 return OK;
3078#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003079 }
3080 }
3081
3082 EMSG2(_(e_letwrong), op);
3083 return FAIL;
3084}
3085
3086/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003087 * Add a watcher to a list.
3088 */
3089 static void
3090list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003091 list_T *l;
3092 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003093{
3094 lw->lw_next = l->lv_watch;
3095 l->lv_watch = lw;
3096}
3097
3098/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003099 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003100 * No warning when it isn't found...
3101 */
3102 static void
3103list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003104 list_T *l;
3105 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003106{
Bram Moolenaar33570922005-01-25 22:26:29 +00003107 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003108
3109 lwp = &l->lv_watch;
3110 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3111 {
3112 if (lw == lwrem)
3113 {
3114 *lwp = lw->lw_next;
3115 break;
3116 }
3117 lwp = &lw->lw_next;
3118 }
3119}
3120
3121/*
3122 * Just before removing an item from a list: advance watchers to the next
3123 * item.
3124 */
3125 static void
3126list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003127 list_T *l;
3128 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003129{
Bram Moolenaar33570922005-01-25 22:26:29 +00003130 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003131
3132 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3133 if (lw->lw_item == item)
3134 lw->lw_item = item->li_next;
3135}
3136
3137/*
3138 * Evaluate the expression used in a ":for var in expr" command.
3139 * "arg" points to "var".
3140 * Set "*errp" to TRUE for an error, FALSE otherwise;
3141 * Return a pointer that holds the info. Null when there is an error.
3142 */
3143 void *
3144eval_for_line(arg, errp, nextcmdp, skip)
3145 char_u *arg;
3146 int *errp;
3147 char_u **nextcmdp;
3148 int skip;
3149{
Bram Moolenaar33570922005-01-25 22:26:29 +00003150 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003151 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003152 typval_T tv;
3153 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003154
3155 *errp = TRUE; /* default: there is an error */
3156
Bram Moolenaar33570922005-01-25 22:26:29 +00003157 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003158 if (fi == NULL)
3159 return NULL;
3160
3161 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3162 if (expr == NULL)
3163 return fi;
3164
3165 expr = skipwhite(expr);
3166 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3167 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003168 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169 return fi;
3170 }
3171
3172 if (skip)
3173 ++emsg_skip;
3174 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3175 {
3176 *errp = FALSE;
3177 if (!skip)
3178 {
3179 l = tv.vval.v_list;
3180 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003181 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003182 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003183 clear_tv(&tv);
3184 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185 else
3186 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003187 /* No need to increment the refcount, it's already set for the
3188 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 fi->fi_list = l;
3190 list_add_watch(l, &fi->fi_lw);
3191 fi->fi_lw.lw_item = l->lv_first;
3192 }
3193 }
3194 }
3195 if (skip)
3196 --emsg_skip;
3197
3198 return fi;
3199}
3200
3201/*
3202 * Use the first item in a ":for" list. Advance to the next.
3203 * Assign the values to the variable (list). "arg" points to the first one.
3204 * Return TRUE when a valid item was found, FALSE when at end of list or
3205 * something wrong.
3206 */
3207 int
3208next_for_item(fi_void, arg)
3209 void *fi_void;
3210 char_u *arg;
3211{
Bram Moolenaar33570922005-01-25 22:26:29 +00003212 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003213 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003214 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215
3216 item = fi->fi_lw.lw_item;
3217 if (item == NULL)
3218 result = FALSE;
3219 else
3220 {
3221 fi->fi_lw.lw_item = item->li_next;
3222 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3223 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3224 }
3225 return result;
3226}
3227
3228/*
3229 * Free the structure used to store info used by ":for".
3230 */
3231 void
3232free_for_info(fi_void)
3233 void *fi_void;
3234{
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003237 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003238 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003240 list_unref(fi->fi_list);
3241 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003242 vim_free(fi);
3243}
3244
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3246
3247 void
3248set_context_for_expression(xp, arg, cmdidx)
3249 expand_T *xp;
3250 char_u *arg;
3251 cmdidx_T cmdidx;
3252{
3253 int got_eq = FALSE;
3254 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003257 if (cmdidx == CMD_let)
3258 {
3259 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003260 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003261 {
3262 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003263 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264 {
3265 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003266 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267 if (vim_iswhite(*p))
3268 break;
3269 }
3270 return;
3271 }
3272 }
3273 else
3274 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3275 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 while ((xp->xp_pattern = vim_strpbrk(arg,
3277 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3278 {
3279 c = *xp->xp_pattern;
3280 if (c == '&')
3281 {
3282 c = xp->xp_pattern[1];
3283 if (c == '&')
3284 {
3285 ++xp->xp_pattern;
3286 xp->xp_context = cmdidx != CMD_let || got_eq
3287 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3288 }
3289 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003290 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003292 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3293 xp->xp_pattern += 2;
3294
3295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 }
3297 else if (c == '$')
3298 {
3299 /* environment variable */
3300 xp->xp_context = EXPAND_ENV_VARS;
3301 }
3302 else if (c == '=')
3303 {
3304 got_eq = TRUE;
3305 xp->xp_context = EXPAND_EXPRESSION;
3306 }
3307 else if (c == '<'
3308 && xp->xp_context == EXPAND_FUNCTIONS
3309 && vim_strchr(xp->xp_pattern, '(') == NULL)
3310 {
3311 /* Function name can start with "<SNR>" */
3312 break;
3313 }
3314 else if (cmdidx != CMD_let || got_eq)
3315 {
3316 if (c == '"') /* string */
3317 {
3318 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3319 if (c == '\\' && xp->xp_pattern[1] != NUL)
3320 ++xp->xp_pattern;
3321 xp->xp_context = EXPAND_NOTHING;
3322 }
3323 else if (c == '\'') /* literal string */
3324 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003325 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3327 /* skip */ ;
3328 xp->xp_context = EXPAND_NOTHING;
3329 }
3330 else if (c == '|')
3331 {
3332 if (xp->xp_pattern[1] == '|')
3333 {
3334 ++xp->xp_pattern;
3335 xp->xp_context = EXPAND_EXPRESSION;
3336 }
3337 else
3338 xp->xp_context = EXPAND_COMMANDS;
3339 }
3340 else
3341 xp->xp_context = EXPAND_EXPRESSION;
3342 }
3343 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344 /* Doesn't look like something valid, expand as an expression
3345 * anyway. */
3346 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 arg = xp->xp_pattern;
3348 if (*arg != NUL)
3349 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3350 /* skip */ ;
3351 }
3352 xp->xp_pattern = arg;
3353}
3354
3355#endif /* FEAT_CMDL_COMPL */
3356
3357/*
3358 * ":1,25call func(arg1, arg2)" function call.
3359 */
3360 void
3361ex_call(eap)
3362 exarg_T *eap;
3363{
3364 char_u *arg = eap->arg;
3365 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003367 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003369 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 linenr_T lnum;
3371 int doesrange;
3372 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003373 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003375 if (eap->skip)
3376 {
3377 /* trans_function_name() doesn't work well when skipping, use eval0()
3378 * instead to skip to any following command, e.g. for:
3379 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003380 ++emsg_skip;
3381 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3382 clear_tv(&rettv);
3383 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003384 return;
3385 }
3386
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003387 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003388 if (fudi.fd_newkey != NULL)
3389 {
3390 /* Still need to give an error message for missing key. */
3391 EMSG2(_(e_dictkey), fudi.fd_newkey);
3392 vim_free(fudi.fd_newkey);
3393 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003394 if (tofree == NULL)
3395 return;
3396
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003397 /* Increase refcount on dictionary, it could get deleted when evaluating
3398 * the arguments. */
3399 if (fudi.fd_dict != NULL)
3400 ++fudi.fd_dict->dv_refcount;
3401
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003402 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003403 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003404 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
Bram Moolenaar532c7802005-01-27 14:44:31 +00003406 /* Skip white space to allow ":call func ()". Not good, but required for
3407 * backward compatibility. */
3408 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003409 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410
3411 if (*startarg != '(')
3412 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003413 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 goto end;
3415 }
3416
3417 /*
3418 * When skipping, evaluate the function once, to find the end of the
3419 * arguments.
3420 * When the function takes a range, this is discovered after the first
3421 * call, and the loop is broken.
3422 */
3423 if (eap->skip)
3424 {
3425 ++emsg_skip;
3426 lnum = eap->line2; /* do it once, also with an invalid range */
3427 }
3428 else
3429 lnum = eap->line1;
3430 for ( ; lnum <= eap->line2; ++lnum)
3431 {
3432 if (!eap->skip && eap->addr_count > 0)
3433 {
3434 curwin->w_cursor.lnum = lnum;
3435 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003436#ifdef FEAT_VIRTUALEDIT
3437 curwin->w_cursor.coladd = 0;
3438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 }
3440 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003441 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003442 eap->line1, eap->line2, &doesrange,
3443 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 {
3445 failed = TRUE;
3446 break;
3447 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003448
3449 /* Handle a function returning a Funcref, Dictionary or List. */
3450 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3451 {
3452 failed = TRUE;
3453 break;
3454 }
3455
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003456 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 if (doesrange || eap->skip)
3458 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003459
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003461 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003462 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003463 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 if (aborting())
3465 break;
3466 }
3467 if (eap->skip)
3468 --emsg_skip;
3469
3470 if (!failed)
3471 {
3472 /* Check for trailing illegal characters and a following command. */
3473 if (!ends_excmd(*arg))
3474 {
3475 emsg_severe = TRUE;
3476 EMSG(_(e_trailing));
3477 }
3478 else
3479 eap->nextcmd = check_nextcmd(arg);
3480 }
3481
3482end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003483 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003484 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485}
3486
3487/*
3488 * ":unlet[!] var1 ... " command.
3489 */
3490 void
3491ex_unlet(eap)
3492 exarg_T *eap;
3493{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003494 ex_unletlock(eap, eap->arg, 0);
3495}
3496
3497/*
3498 * ":lockvar" and ":unlockvar" commands
3499 */
3500 void
3501ex_lockvar(eap)
3502 exarg_T *eap;
3503{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003505 int deep = 2;
3506
3507 if (eap->forceit)
3508 deep = -1;
3509 else if (vim_isdigit(*arg))
3510 {
3511 deep = getdigits(&arg);
3512 arg = skipwhite(arg);
3513 }
3514
3515 ex_unletlock(eap, arg, deep);
3516}
3517
3518/*
3519 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3520 */
3521 static void
3522ex_unletlock(eap, argstart, deep)
3523 exarg_T *eap;
3524 char_u *argstart;
3525 int deep;
3526{
3527 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003530 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531
3532 do
3533 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003534 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003535 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3536 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003537 if (lv.ll_name == NULL)
3538 error = TRUE; /* error but continue parsing */
3539 if (name_end == NULL || (!vim_iswhite(*name_end)
3540 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003542 if (name_end != NULL)
3543 {
3544 emsg_severe = TRUE;
3545 EMSG(_(e_trailing));
3546 }
3547 if (!(eap->skip || error))
3548 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 break;
3550 }
3551
3552 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003553 {
3554 if (eap->cmdidx == CMD_unlet)
3555 {
3556 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3557 error = TRUE;
3558 }
3559 else
3560 {
3561 if (do_lock_var(&lv, name_end, deep,
3562 eap->cmdidx == CMD_lockvar) == FAIL)
3563 error = TRUE;
3564 }
3565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003567 if (!eap->skip)
3568 clear_lval(&lv);
3569
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 arg = skipwhite(name_end);
3571 } while (!ends_excmd(*arg));
3572
3573 eap->nextcmd = check_nextcmd(arg);
3574}
3575
Bram Moolenaar8c711452005-01-14 21:53:12 +00003576 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003577do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003578 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003579 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003580 int forceit;
3581{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003582 int ret = OK;
3583 int cc;
3584
3585 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003586 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003587 cc = *name_end;
3588 *name_end = NUL;
3589
3590 /* Normal name or expanded name. */
3591 if (check_changedtick(lp->ll_name))
3592 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003593 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003594 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003595 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003596 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003597 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3598 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003599 else if (lp->ll_range)
3600 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003601 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003602
3603 /* Delete a range of List items. */
3604 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3605 {
3606 li = lp->ll_li->li_next;
3607 listitem_remove(lp->ll_list, lp->ll_li);
3608 lp->ll_li = li;
3609 ++lp->ll_n1;
3610 }
3611 }
3612 else
3613 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003615 /* unlet a List item. */
3616 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003619 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003620 }
3621
3622 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003623}
3624
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625/*
3626 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003627 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 */
3629 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003630do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003632 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633{
Bram Moolenaar33570922005-01-25 22:26:29 +00003634 hashtab_T *ht;
3635 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003636 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003637 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
Bram Moolenaar33570922005-01-25 22:26:29 +00003639 ht = find_var_ht(name, &varname);
3640 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003642 hi = hash_find(ht, varname);
3643 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003644 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003645 di = HI2DI(hi);
3646 if (var_check_fixed(di->di_flags, name)
3647 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003648 return FAIL;
3649 delete_var(ht, hi);
3650 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003653 if (forceit)
3654 return OK;
3655 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 return FAIL;
3657}
3658
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003659/*
3660 * Lock or unlock variable indicated by "lp".
3661 * "deep" is the levels to go (-1 for unlimited);
3662 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3663 */
3664 static int
3665do_lock_var(lp, name_end, deep, lock)
3666 lval_T *lp;
3667 char_u *name_end;
3668 int deep;
3669 int lock;
3670{
3671 int ret = OK;
3672 int cc;
3673 dictitem_T *di;
3674
3675 if (deep == 0) /* nothing to do */
3676 return OK;
3677
3678 if (lp->ll_tv == NULL)
3679 {
3680 cc = *name_end;
3681 *name_end = NUL;
3682
3683 /* Normal name or expanded name. */
3684 if (check_changedtick(lp->ll_name))
3685 ret = FAIL;
3686 else
3687 {
3688 di = find_var(lp->ll_name, NULL);
3689 if (di == NULL)
3690 ret = FAIL;
3691 else
3692 {
3693 if (lock)
3694 di->di_flags |= DI_FLAGS_LOCK;
3695 else
3696 di->di_flags &= ~DI_FLAGS_LOCK;
3697 item_lock(&di->di_tv, deep, lock);
3698 }
3699 }
3700 *name_end = cc;
3701 }
3702 else if (lp->ll_range)
3703 {
3704 listitem_T *li = lp->ll_li;
3705
3706 /* (un)lock a range of List items. */
3707 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3708 {
3709 item_lock(&li->li_tv, deep, lock);
3710 li = li->li_next;
3711 ++lp->ll_n1;
3712 }
3713 }
3714 else if (lp->ll_list != NULL)
3715 /* (un)lock a List item. */
3716 item_lock(&lp->ll_li->li_tv, deep, lock);
3717 else
3718 /* un(lock) a Dictionary item. */
3719 item_lock(&lp->ll_di->di_tv, deep, lock);
3720
3721 return ret;
3722}
3723
3724/*
3725 * Lock or unlock an item. "deep" is nr of levels to go.
3726 */
3727 static void
3728item_lock(tv, deep, lock)
3729 typval_T *tv;
3730 int deep;
3731 int lock;
3732{
3733 static int recurse = 0;
3734 list_T *l;
3735 listitem_T *li;
3736 dict_T *d;
3737 hashitem_T *hi;
3738 int todo;
3739
3740 if (recurse >= DICT_MAXNEST)
3741 {
3742 EMSG(_("E743: variable nested too deep for (un)lock"));
3743 return;
3744 }
3745 if (deep == 0)
3746 return;
3747 ++recurse;
3748
3749 /* lock/unlock the item itself */
3750 if (lock)
3751 tv->v_lock |= VAR_LOCKED;
3752 else
3753 tv->v_lock &= ~VAR_LOCKED;
3754
3755 switch (tv->v_type)
3756 {
3757 case VAR_LIST:
3758 if ((l = tv->vval.v_list) != NULL)
3759 {
3760 if (lock)
3761 l->lv_lock |= VAR_LOCKED;
3762 else
3763 l->lv_lock &= ~VAR_LOCKED;
3764 if (deep < 0 || deep > 1)
3765 /* recursive: lock/unlock the items the List contains */
3766 for (li = l->lv_first; li != NULL; li = li->li_next)
3767 item_lock(&li->li_tv, deep - 1, lock);
3768 }
3769 break;
3770 case VAR_DICT:
3771 if ((d = tv->vval.v_dict) != NULL)
3772 {
3773 if (lock)
3774 d->dv_lock |= VAR_LOCKED;
3775 else
3776 d->dv_lock &= ~VAR_LOCKED;
3777 if (deep < 0 || deep > 1)
3778 {
3779 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003780 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003781 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3782 {
3783 if (!HASHITEM_EMPTY(hi))
3784 {
3785 --todo;
3786 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3787 }
3788 }
3789 }
3790 }
3791 }
3792 --recurse;
3793}
3794
Bram Moolenaara40058a2005-07-11 22:42:07 +00003795/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003796 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3797 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003798 */
3799 static int
3800tv_islocked(tv)
3801 typval_T *tv;
3802{
3803 return (tv->v_lock & VAR_LOCKED)
3804 || (tv->v_type == VAR_LIST
3805 && tv->vval.v_list != NULL
3806 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3807 || (tv->v_type == VAR_DICT
3808 && tv->vval.v_dict != NULL
3809 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3810}
3811
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3813/*
3814 * Delete all "menutrans_" variables.
3815 */
3816 void
3817del_menutrans_vars()
3818{
Bram Moolenaar33570922005-01-25 22:26:29 +00003819 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003820 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821
Bram Moolenaar33570922005-01-25 22:26:29 +00003822 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003823 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003824 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003825 {
3826 if (!HASHITEM_EMPTY(hi))
3827 {
3828 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003829 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3830 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003831 }
3832 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003833 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834}
3835#endif
3836
3837#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3838
3839/*
3840 * Local string buffer for the next two functions to store a variable name
3841 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3842 * get_user_var_name().
3843 */
3844
3845static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3846
3847static char_u *varnamebuf = NULL;
3848static int varnamebuflen = 0;
3849
3850/*
3851 * Function to concatenate a prefix and a variable name.
3852 */
3853 static char_u *
3854cat_prefix_varname(prefix, name)
3855 int prefix;
3856 char_u *name;
3857{
3858 int len;
3859
3860 len = (int)STRLEN(name) + 3;
3861 if (len > varnamebuflen)
3862 {
3863 vim_free(varnamebuf);
3864 len += 10; /* some additional space */
3865 varnamebuf = alloc(len);
3866 if (varnamebuf == NULL)
3867 {
3868 varnamebuflen = 0;
3869 return NULL;
3870 }
3871 varnamebuflen = len;
3872 }
3873 *varnamebuf = prefix;
3874 varnamebuf[1] = ':';
3875 STRCPY(varnamebuf + 2, name);
3876 return varnamebuf;
3877}
3878
3879/*
3880 * Function given to ExpandGeneric() to obtain the list of user defined
3881 * (global/buffer/window/built-in) variable names.
3882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 char_u *
3884get_user_var_name(xp, idx)
3885 expand_T *xp;
3886 int idx;
3887{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003888 static long_u gdone;
3889 static long_u bdone;
3890 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003891#ifdef FEAT_WINDOWS
3892 static long_u tdone;
3893#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003894 static int vidx;
3895 static hashitem_T *hi;
3896 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897
3898 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003899 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003900 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003901#ifdef FEAT_WINDOWS
3902 tdone = 0;
3903#endif
3904 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003905
3906 /* Global variables */
3907 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003909 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003910 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003911 else
3912 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003913 while (HASHITEM_EMPTY(hi))
3914 ++hi;
3915 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3916 return cat_prefix_varname('g', hi->hi_key);
3917 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003919
3920 /* b: variables */
3921 ht = &curbuf->b_vars.dv_hashtab;
3922 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003924 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003925 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003926 else
3927 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003928 while (HASHITEM_EMPTY(hi))
3929 ++hi;
3930 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003932 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003934 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 return (char_u *)"b:changedtick";
3936 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003937
3938 /* w: variables */
3939 ht = &curwin->w_vars.dv_hashtab;
3940 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003942 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003943 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003944 else
3945 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003946 while (HASHITEM_EMPTY(hi))
3947 ++hi;
3948 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003950
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003951#ifdef FEAT_WINDOWS
3952 /* t: variables */
3953 ht = &curtab->tp_vars.dv_hashtab;
3954 if (tdone < ht->ht_used)
3955 {
3956 if (tdone++ == 0)
3957 hi = ht->ht_array;
3958 else
3959 ++hi;
3960 while (HASHITEM_EMPTY(hi))
3961 ++hi;
3962 return cat_prefix_varname('t', hi->hi_key);
3963 }
3964#endif
3965
Bram Moolenaar33570922005-01-25 22:26:29 +00003966 /* v: variables */
3967 if (vidx < VV_LEN)
3968 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969
3970 vim_free(varnamebuf);
3971 varnamebuf = NULL;
3972 varnamebuflen = 0;
3973 return NULL;
3974}
3975
3976#endif /* FEAT_CMDL_COMPL */
3977
3978/*
3979 * types for expressions.
3980 */
3981typedef enum
3982{
3983 TYPE_UNKNOWN = 0
3984 , TYPE_EQUAL /* == */
3985 , TYPE_NEQUAL /* != */
3986 , TYPE_GREATER /* > */
3987 , TYPE_GEQUAL /* >= */
3988 , TYPE_SMALLER /* < */
3989 , TYPE_SEQUAL /* <= */
3990 , TYPE_MATCH /* =~ */
3991 , TYPE_NOMATCH /* !~ */
3992} exptype_T;
3993
3994/*
3995 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003996 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3998 */
3999
4000/*
4001 * Handle zero level expression.
4002 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004003 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004004 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 * Return OK or FAIL.
4006 */
4007 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004008eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004010 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 char_u **nextcmd;
4012 int evaluate;
4013{
4014 int ret;
4015 char_u *p;
4016
4017 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004018 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 if (ret == FAIL || !ends_excmd(*p))
4020 {
4021 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004022 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 /*
4024 * Report the invalid expression unless the expression evaluation has
4025 * been cancelled due to an aborting error, an interrupt, or an
4026 * exception.
4027 */
4028 if (!aborting())
4029 EMSG2(_(e_invexpr2), arg);
4030 ret = FAIL;
4031 }
4032 if (nextcmd != NULL)
4033 *nextcmd = check_nextcmd(p);
4034
4035 return ret;
4036}
4037
4038/*
4039 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004040 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 *
4042 * "arg" must point to the first non-white of the expression.
4043 * "arg" is advanced to the next non-white after the recognized expression.
4044 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004045 * Note: "rettv.v_lock" is not set.
4046 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 * Return OK or FAIL.
4048 */
4049 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004050eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 int evaluate;
4054{
4055 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004056 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057
4058 /*
4059 * Get the first variable.
4060 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004061 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 return FAIL;
4063
4064 if ((*arg)[0] == '?')
4065 {
4066 result = FALSE;
4067 if (evaluate)
4068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004069 int error = FALSE;
4070
4071 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004073 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004074 if (error)
4075 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
4077
4078 /*
4079 * Get the second variable.
4080 */
4081 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004082 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 return FAIL;
4084
4085 /*
4086 * Check for the ":".
4087 */
4088 if ((*arg)[0] != ':')
4089 {
4090 EMSG(_("E109: Missing ':' after '?'"));
4091 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004092 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 return FAIL;
4094 }
4095
4096 /*
4097 * Get the third variable.
4098 */
4099 *arg = skipwhite(*arg + 1);
4100 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4101 {
4102 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004103 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 return FAIL;
4105 }
4106 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004107 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 }
4109
4110 return OK;
4111}
4112
4113/*
4114 * Handle first level expression:
4115 * expr2 || expr2 || expr2 logical OR
4116 *
4117 * "arg" must point to the first non-white of the expression.
4118 * "arg" is advanced to the next non-white after the recognized expression.
4119 *
4120 * Return OK or FAIL.
4121 */
4122 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 int evaluate;
4127{
Bram Moolenaar33570922005-01-25 22:26:29 +00004128 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 long result;
4130 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004131 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
4133 /*
4134 * Get the first variable.
4135 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004136 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 return FAIL;
4138
4139 /*
4140 * Repeat until there is no following "||".
4141 */
4142 first = TRUE;
4143 result = FALSE;
4144 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4145 {
4146 if (evaluate && first)
4147 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004148 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004150 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004151 if (error)
4152 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 first = FALSE;
4154 }
4155
4156 /*
4157 * Get the second variable.
4158 */
4159 *arg = skipwhite(*arg + 2);
4160 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4161 return FAIL;
4162
4163 /*
4164 * Compute the result.
4165 */
4166 if (evaluate && !result)
4167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004170 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004171 if (error)
4172 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 }
4174 if (evaluate)
4175 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 rettv->v_type = VAR_NUMBER;
4177 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
4179 }
4180
4181 return OK;
4182}
4183
4184/*
4185 * Handle second level expression:
4186 * expr3 && expr3 && expr3 logical AND
4187 *
4188 * "arg" must point to the first non-white of the expression.
4189 * "arg" is advanced to the next non-white after the recognized expression.
4190 *
4191 * Return OK or FAIL.
4192 */
4193 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004194eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 int evaluate;
4198{
Bram Moolenaar33570922005-01-25 22:26:29 +00004199 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 long result;
4201 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004202 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
4204 /*
4205 * Get the first variable.
4206 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 return FAIL;
4209
4210 /*
4211 * Repeat until there is no following "&&".
4212 */
4213 first = TRUE;
4214 result = TRUE;
4215 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4216 {
4217 if (evaluate && first)
4218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004219 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004221 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004222 if (error)
4223 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 first = FALSE;
4225 }
4226
4227 /*
4228 * Get the second variable.
4229 */
4230 *arg = skipwhite(*arg + 2);
4231 if (eval4(arg, &var2, evaluate && result) == FAIL)
4232 return FAIL;
4233
4234 /*
4235 * Compute the result.
4236 */
4237 if (evaluate && result)
4238 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004239 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004241 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004242 if (error)
4243 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 }
4245 if (evaluate)
4246 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 rettv->v_type = VAR_NUMBER;
4248 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
4250 }
4251
4252 return OK;
4253}
4254
4255/*
4256 * Handle third level expression:
4257 * var1 == var2
4258 * var1 =~ var2
4259 * var1 != var2
4260 * var1 !~ var2
4261 * var1 > var2
4262 * var1 >= var2
4263 * var1 < var2
4264 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004265 * var1 is var2
4266 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 *
4268 * "arg" must point to the first non-white of the expression.
4269 * "arg" is advanced to the next non-white after the recognized expression.
4270 *
4271 * Return OK or FAIL.
4272 */
4273 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004274eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 int evaluate;
4278{
Bram Moolenaar33570922005-01-25 22:26:29 +00004279 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 char_u *p;
4281 int i;
4282 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004283 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 int len = 2;
4285 long n1, n2;
4286 char_u *s1, *s2;
4287 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4288 regmatch_T regmatch;
4289 int ic;
4290 char_u *save_cpo;
4291
4292 /*
4293 * Get the first variable.
4294 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004295 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 return FAIL;
4297
4298 p = *arg;
4299 switch (p[0])
4300 {
4301 case '=': if (p[1] == '=')
4302 type = TYPE_EQUAL;
4303 else if (p[1] == '~')
4304 type = TYPE_MATCH;
4305 break;
4306 case '!': if (p[1] == '=')
4307 type = TYPE_NEQUAL;
4308 else if (p[1] == '~')
4309 type = TYPE_NOMATCH;
4310 break;
4311 case '>': if (p[1] != '=')
4312 {
4313 type = TYPE_GREATER;
4314 len = 1;
4315 }
4316 else
4317 type = TYPE_GEQUAL;
4318 break;
4319 case '<': if (p[1] != '=')
4320 {
4321 type = TYPE_SMALLER;
4322 len = 1;
4323 }
4324 else
4325 type = TYPE_SEQUAL;
4326 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004327 case 'i': if (p[1] == 's')
4328 {
4329 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4330 len = 5;
4331 if (!vim_isIDc(p[len]))
4332 {
4333 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4334 type_is = TRUE;
4335 }
4336 }
4337 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 }
4339
4340 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004341 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 */
4343 if (type != TYPE_UNKNOWN)
4344 {
4345 /* extra question mark appended: ignore case */
4346 if (p[len] == '?')
4347 {
4348 ic = TRUE;
4349 ++len;
4350 }
4351 /* extra '#' appended: match case */
4352 else if (p[len] == '#')
4353 {
4354 ic = FALSE;
4355 ++len;
4356 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004357 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 else
4359 ic = p_ic;
4360
4361 /*
4362 * Get the second variable.
4363 */
4364 *arg = skipwhite(p + len);
4365 if (eval5(arg, &var2, evaluate) == FAIL)
4366 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004367 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 return FAIL;
4369 }
4370
4371 if (evaluate)
4372 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004373 if (type_is && rettv->v_type != var2.v_type)
4374 {
4375 /* For "is" a different type always means FALSE, for "notis"
4376 * it means TRUE. */
4377 n1 = (type == TYPE_NEQUAL);
4378 }
4379 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4380 {
4381 if (type_is)
4382 {
4383 n1 = (rettv->v_type == var2.v_type
4384 && rettv->vval.v_list == var2.vval.v_list);
4385 if (type == TYPE_NEQUAL)
4386 n1 = !n1;
4387 }
4388 else if (rettv->v_type != var2.v_type
4389 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4390 {
4391 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004392 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004393 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004394 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004395 clear_tv(rettv);
4396 clear_tv(&var2);
4397 return FAIL;
4398 }
4399 else
4400 {
4401 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004402 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4403 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004404 if (type == TYPE_NEQUAL)
4405 n1 = !n1;
4406 }
4407 }
4408
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004409 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4410 {
4411 if (type_is)
4412 {
4413 n1 = (rettv->v_type == var2.v_type
4414 && rettv->vval.v_dict == var2.vval.v_dict);
4415 if (type == TYPE_NEQUAL)
4416 n1 = !n1;
4417 }
4418 else if (rettv->v_type != var2.v_type
4419 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4420 {
4421 if (rettv->v_type != var2.v_type)
4422 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4423 else
4424 EMSG(_("E736: Invalid operation for Dictionary"));
4425 clear_tv(rettv);
4426 clear_tv(&var2);
4427 return FAIL;
4428 }
4429 else
4430 {
4431 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004432 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4433 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004434 if (type == TYPE_NEQUAL)
4435 n1 = !n1;
4436 }
4437 }
4438
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004439 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4440 {
4441 if (rettv->v_type != var2.v_type
4442 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4443 {
4444 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004445 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004446 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004447 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004448 clear_tv(rettv);
4449 clear_tv(&var2);
4450 return FAIL;
4451 }
4452 else
4453 {
4454 /* Compare two Funcrefs for being equal or unequal. */
4455 if (rettv->vval.v_string == NULL
4456 || var2.vval.v_string == NULL)
4457 n1 = FALSE;
4458 else
4459 n1 = STRCMP(rettv->vval.v_string,
4460 var2.vval.v_string) == 0;
4461 if (type == TYPE_NEQUAL)
4462 n1 = !n1;
4463 }
4464 }
4465
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004466#ifdef FEAT_FLOAT
4467 /*
4468 * If one of the two variables is a float, compare as a float.
4469 * When using "=~" or "!~", always compare as string.
4470 */
4471 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4472 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4473 {
4474 float_T f1, f2;
4475
4476 if (rettv->v_type == VAR_FLOAT)
4477 f1 = rettv->vval.v_float;
4478 else
4479 f1 = get_tv_number(rettv);
4480 if (var2.v_type == VAR_FLOAT)
4481 f2 = var2.vval.v_float;
4482 else
4483 f2 = get_tv_number(&var2);
4484 n1 = FALSE;
4485 switch (type)
4486 {
4487 case TYPE_EQUAL: n1 = (f1 == f2); break;
4488 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4489 case TYPE_GREATER: n1 = (f1 > f2); break;
4490 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4491 case TYPE_SMALLER: n1 = (f1 < f2); break;
4492 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4493 case TYPE_UNKNOWN:
4494 case TYPE_MATCH:
4495 case TYPE_NOMATCH: break; /* avoid gcc warning */
4496 }
4497 }
4498#endif
4499
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 /*
4501 * If one of the two variables is a number, compare as a number.
4502 * When using "=~" or "!~", always compare as string.
4503 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004504 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4506 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004507 n1 = get_tv_number(rettv);
4508 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 switch (type)
4510 {
4511 case TYPE_EQUAL: n1 = (n1 == n2); break;
4512 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4513 case TYPE_GREATER: n1 = (n1 > n2); break;
4514 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4515 case TYPE_SMALLER: n1 = (n1 < n2); break;
4516 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4517 case TYPE_UNKNOWN:
4518 case TYPE_MATCH:
4519 case TYPE_NOMATCH: break; /* avoid gcc warning */
4520 }
4521 }
4522 else
4523 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004524 s1 = get_tv_string_buf(rettv, buf1);
4525 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4527 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4528 else
4529 i = 0;
4530 n1 = FALSE;
4531 switch (type)
4532 {
4533 case TYPE_EQUAL: n1 = (i == 0); break;
4534 case TYPE_NEQUAL: n1 = (i != 0); break;
4535 case TYPE_GREATER: n1 = (i > 0); break;
4536 case TYPE_GEQUAL: n1 = (i >= 0); break;
4537 case TYPE_SMALLER: n1 = (i < 0); break;
4538 case TYPE_SEQUAL: n1 = (i <= 0); break;
4539
4540 case TYPE_MATCH:
4541 case TYPE_NOMATCH:
4542 /* avoid 'l' flag in 'cpoptions' */
4543 save_cpo = p_cpo;
4544 p_cpo = (char_u *)"";
4545 regmatch.regprog = vim_regcomp(s2,
4546 RE_MAGIC + RE_STRING);
4547 regmatch.rm_ic = ic;
4548 if (regmatch.regprog != NULL)
4549 {
4550 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4551 vim_free(regmatch.regprog);
4552 if (type == TYPE_NOMATCH)
4553 n1 = !n1;
4554 }
4555 p_cpo = save_cpo;
4556 break;
4557
4558 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4559 }
4560 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004561 clear_tv(rettv);
4562 clear_tv(&var2);
4563 rettv->v_type = VAR_NUMBER;
4564 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 }
4566 }
4567
4568 return OK;
4569}
4570
4571/*
4572 * Handle fourth level expression:
4573 * + number addition
4574 * - number subtraction
4575 * . string concatenation
4576 *
4577 * "arg" must point to the first non-white of the expression.
4578 * "arg" is advanced to the next non-white after the recognized expression.
4579 *
4580 * Return OK or FAIL.
4581 */
4582 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004583eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004585 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 int evaluate;
4587{
Bram Moolenaar33570922005-01-25 22:26:29 +00004588 typval_T var2;
4589 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 int op;
4591 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004592#ifdef FEAT_FLOAT
4593 float_T f1 = 0, f2 = 0;
4594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 char_u *s1, *s2;
4596 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4597 char_u *p;
4598
4599 /*
4600 * Get the first variable.
4601 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004602 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 return FAIL;
4604
4605 /*
4606 * Repeat computing, until no '+', '-' or '.' is following.
4607 */
4608 for (;;)
4609 {
4610 op = **arg;
4611 if (op != '+' && op != '-' && op != '.')
4612 break;
4613
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004614 if ((op != '+' || rettv->v_type != VAR_LIST)
4615#ifdef FEAT_FLOAT
4616 && (op == '.' || rettv->v_type != VAR_FLOAT)
4617#endif
4618 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004619 {
4620 /* For "list + ...", an illegal use of the first operand as
4621 * a number cannot be determined before evaluating the 2nd
4622 * operand: if this is also a list, all is ok.
4623 * For "something . ...", "something - ..." or "non-list + ...",
4624 * we know that the first operand needs to be a string or number
4625 * without evaluating the 2nd operand. So check before to avoid
4626 * side effects after an error. */
4627 if (evaluate && get_tv_string_chk(rettv) == NULL)
4628 {
4629 clear_tv(rettv);
4630 return FAIL;
4631 }
4632 }
4633
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 /*
4635 * Get the second variable.
4636 */
4637 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004638 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004640 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 return FAIL;
4642 }
4643
4644 if (evaluate)
4645 {
4646 /*
4647 * Compute the result.
4648 */
4649 if (op == '.')
4650 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004651 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4652 s2 = get_tv_string_buf_chk(&var2, buf2);
4653 if (s2 == NULL) /* type error ? */
4654 {
4655 clear_tv(rettv);
4656 clear_tv(&var2);
4657 return FAIL;
4658 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004659 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004660 clear_tv(rettv);
4661 rettv->v_type = VAR_STRING;
4662 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004664 else if (op == '+' && rettv->v_type == VAR_LIST
4665 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004666 {
4667 /* concatenate Lists */
4668 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4669 &var3) == FAIL)
4670 {
4671 clear_tv(rettv);
4672 clear_tv(&var2);
4673 return FAIL;
4674 }
4675 clear_tv(rettv);
4676 *rettv = var3;
4677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 else
4679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004680 int error = FALSE;
4681
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004682#ifdef FEAT_FLOAT
4683 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004684 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004685 f1 = rettv->vval.v_float;
4686 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004687 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004688 else
4689#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004690 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004691 n1 = get_tv_number_chk(rettv, &error);
4692 if (error)
4693 {
4694 /* This can only happen for "list + non-list". For
4695 * "non-list + ..." or "something - ...", we returned
4696 * before evaluating the 2nd operand. */
4697 clear_tv(rettv);
4698 return FAIL;
4699 }
4700#ifdef FEAT_FLOAT
4701 if (var2.v_type == VAR_FLOAT)
4702 f1 = n1;
4703#endif
4704 }
4705#ifdef FEAT_FLOAT
4706 if (var2.v_type == VAR_FLOAT)
4707 {
4708 f2 = var2.vval.v_float;
4709 n2 = 0;
4710 }
4711 else
4712#endif
4713 {
4714 n2 = get_tv_number_chk(&var2, &error);
4715 if (error)
4716 {
4717 clear_tv(rettv);
4718 clear_tv(&var2);
4719 return FAIL;
4720 }
4721#ifdef FEAT_FLOAT
4722 if (rettv->v_type == VAR_FLOAT)
4723 f2 = n2;
4724#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004725 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004726 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004727
4728#ifdef FEAT_FLOAT
4729 /* If there is a float on either side the result is a float. */
4730 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4731 {
4732 if (op == '+')
4733 f1 = f1 + f2;
4734 else
4735 f1 = f1 - f2;
4736 rettv->v_type = VAR_FLOAT;
4737 rettv->vval.v_float = f1;
4738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004740#endif
4741 {
4742 if (op == '+')
4743 n1 = n1 + n2;
4744 else
4745 n1 = n1 - n2;
4746 rettv->v_type = VAR_NUMBER;
4747 rettv->vval.v_number = n1;
4748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004750 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 }
4752 }
4753 return OK;
4754}
4755
4756/*
4757 * Handle fifth level expression:
4758 * * number multiplication
4759 * / number division
4760 * % number modulo
4761 *
4762 * "arg" must point to the first non-white of the expression.
4763 * "arg" is advanced to the next non-white after the recognized expression.
4764 *
4765 * Return OK or FAIL.
4766 */
4767 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004768eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004772 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773{
Bram Moolenaar33570922005-01-25 22:26:29 +00004774 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 int op;
4776 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004777#ifdef FEAT_FLOAT
4778 int use_float = FALSE;
4779 float_T f1 = 0, f2;
4780#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004781 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782
4783 /*
4784 * Get the first variable.
4785 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004786 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 return FAIL;
4788
4789 /*
4790 * Repeat computing, until no '*', '/' or '%' is following.
4791 */
4792 for (;;)
4793 {
4794 op = **arg;
4795 if (op != '*' && op != '/' && op != '%')
4796 break;
4797
4798 if (evaluate)
4799 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004800#ifdef FEAT_FLOAT
4801 if (rettv->v_type == VAR_FLOAT)
4802 {
4803 f1 = rettv->vval.v_float;
4804 use_float = TRUE;
4805 n1 = 0;
4806 }
4807 else
4808#endif
4809 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004810 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004811 if (error)
4812 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 }
4814 else
4815 n1 = 0;
4816
4817 /*
4818 * Get the second variable.
4819 */
4820 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004821 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 return FAIL;
4823
4824 if (evaluate)
4825 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004826#ifdef FEAT_FLOAT
4827 if (var2.v_type == VAR_FLOAT)
4828 {
4829 if (!use_float)
4830 {
4831 f1 = n1;
4832 use_float = TRUE;
4833 }
4834 f2 = var2.vval.v_float;
4835 n2 = 0;
4836 }
4837 else
4838#endif
4839 {
4840 n2 = get_tv_number_chk(&var2, &error);
4841 clear_tv(&var2);
4842 if (error)
4843 return FAIL;
4844#ifdef FEAT_FLOAT
4845 if (use_float)
4846 f2 = n2;
4847#endif
4848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849
4850 /*
4851 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004852 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004854#ifdef FEAT_FLOAT
4855 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004857 if (op == '*')
4858 f1 = f1 * f2;
4859 else if (op == '/')
4860 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004861# ifdef VMS
4862 /* VMS crashes on divide by zero, work around it */
4863 if (f2 == 0.0)
4864 {
4865 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004866 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004867 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004868 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004869 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004870 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004871 }
4872 else
4873 f1 = f1 / f2;
4874# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004875 /* We rely on the floating point library to handle divide
4876 * by zero to result in "inf" and not a crash. */
4877 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004878# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004881 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004882 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004883 return FAIL;
4884 }
4885 rettv->v_type = VAR_FLOAT;
4886 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 }
4888 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004891 if (op == '*')
4892 n1 = n1 * n2;
4893 else if (op == '/')
4894 {
4895 if (n2 == 0) /* give an error message? */
4896 {
4897 if (n1 == 0)
4898 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4899 else if (n1 < 0)
4900 n1 = -0x7fffffffL;
4901 else
4902 n1 = 0x7fffffffL;
4903 }
4904 else
4905 n1 = n1 / n2;
4906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004908 {
4909 if (n2 == 0) /* give an error message? */
4910 n1 = 0;
4911 else
4912 n1 = n1 % n2;
4913 }
4914 rettv->v_type = VAR_NUMBER;
4915 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 }
4919
4920 return OK;
4921}
4922
4923/*
4924 * Handle sixth level expression:
4925 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004926 * "string" string constant
4927 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 * &option-name option value
4929 * @r register contents
4930 * identifier variable value
4931 * function() function call
4932 * $VAR environment variable
4933 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004934 * [expr, expr] List
4935 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 *
4937 * Also handle:
4938 * ! in front logical NOT
4939 * - in front unary minus
4940 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004941 * trailing [] subscript in String or List
4942 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 *
4944 * "arg" must point to the first non-white of the expression.
4945 * "arg" is advanced to the next non-white after the recognized expression.
4946 *
4947 * Return OK or FAIL.
4948 */
4949 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004950eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004952 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004954 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 long n;
4957 int len;
4958 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 char_u *start_leader, *end_leader;
4960 int ret = OK;
4961 char_u *alias;
4962
4963 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004964 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004965 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004967 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968
4969 /*
4970 * Skip '!' and '-' characters. They are handled later.
4971 */
4972 start_leader = *arg;
4973 while (**arg == '!' || **arg == '-' || **arg == '+')
4974 *arg = skipwhite(*arg + 1);
4975 end_leader = *arg;
4976
4977 switch (**arg)
4978 {
4979 /*
4980 * Number constant.
4981 */
4982 case '0':
4983 case '1':
4984 case '2':
4985 case '3':
4986 case '4':
4987 case '5':
4988 case '6':
4989 case '7':
4990 case '8':
4991 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992 {
4993#ifdef FEAT_FLOAT
4994 char_u *p = skipdigits(*arg + 1);
4995 int get_float = FALSE;
4996
4997 /* We accept a float when the format matches
4998 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004999 * strict to avoid backwards compatibility problems.
5000 * Don't look for a float after the "." operator, so that
5001 * ":let vers = 1.2.3" doesn't fail. */
5002 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005004 get_float = TRUE;
5005 p = skipdigits(p + 2);
5006 if (*p == 'e' || *p == 'E')
5007 {
5008 ++p;
5009 if (*p == '-' || *p == '+')
5010 ++p;
5011 if (!vim_isdigit(*p))
5012 get_float = FALSE;
5013 else
5014 p = skipdigits(p + 1);
5015 }
5016 if (ASCII_ISALPHA(*p) || *p == '.')
5017 get_float = FALSE;
5018 }
5019 if (get_float)
5020 {
5021 float_T f;
5022
5023 *arg += string2float(*arg, &f);
5024 if (evaluate)
5025 {
5026 rettv->v_type = VAR_FLOAT;
5027 rettv->vval.v_float = f;
5028 }
5029 }
5030 else
5031#endif
5032 {
5033 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5034 *arg += len;
5035 if (evaluate)
5036 {
5037 rettv->v_type = VAR_NUMBER;
5038 rettv->vval.v_number = n;
5039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 }
5041 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043
5044 /*
5045 * String constant: "string".
5046 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005047 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 break;
5049
5050 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005051 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005053 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005054 break;
5055
5056 /*
5057 * List: [expr, expr]
5058 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005059 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 break;
5061
5062 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005063 * Dictionary: {key: val, key: val}
5064 */
5065 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5066 break;
5067
5068 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005069 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005071 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 break;
5073
5074 /*
5075 * Environment variable: $VAR.
5076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005077 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 break;
5079
5080 /*
5081 * Register contents: @r.
5082 */
5083 case '@': ++*arg;
5084 if (evaluate)
5085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005086 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005087 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 }
5089 if (**arg != NUL)
5090 ++*arg;
5091 break;
5092
5093 /*
5094 * nested expression: (expression).
5095 */
5096 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005097 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 if (**arg == ')')
5099 ++*arg;
5100 else if (ret == OK)
5101 {
5102 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005103 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 ret = FAIL;
5105 }
5106 break;
5107
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 break;
5110 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005111
5112 if (ret == NOTDONE)
5113 {
5114 /*
5115 * Must be a variable or function name.
5116 * Can also be a curly-braces kind of name: {expr}.
5117 */
5118 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005119 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005120 if (alias != NULL)
5121 s = alias;
5122
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005123 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005124 ret = FAIL;
5125 else
5126 {
5127 if (**arg == '(') /* recursive! */
5128 {
5129 /* If "s" is the name of a variable of type VAR_FUNC
5130 * use its contents. */
5131 s = deref_func_name(s, &len);
5132
5133 /* Invoke the function. */
5134 ret = get_func_tv(s, len, rettv, arg,
5135 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005136 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005137 /* Stop the expression evaluation when immediately
5138 * aborting on error, or when an interrupt occurred or
5139 * an exception was thrown but not caught. */
5140 if (aborting())
5141 {
5142 if (ret == OK)
5143 clear_tv(rettv);
5144 ret = FAIL;
5145 }
5146 }
5147 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005148 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005149 else
5150 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005151 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005152 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005153 }
5154
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 *arg = skipwhite(*arg);
5156
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005157 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5158 * expr(expr). */
5159 if (ret == OK)
5160 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161
5162 /*
5163 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5164 */
5165 if (ret == OK && evaluate && end_leader > start_leader)
5166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005167 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005168 int val = 0;
5169#ifdef FEAT_FLOAT
5170 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005171
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005172 if (rettv->v_type == VAR_FLOAT)
5173 f = rettv->vval.v_float;
5174 else
5175#endif
5176 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005177 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005179 clear_tv(rettv);
5180 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005182 else
5183 {
5184 while (end_leader > start_leader)
5185 {
5186 --end_leader;
5187 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005188 {
5189#ifdef FEAT_FLOAT
5190 if (rettv->v_type == VAR_FLOAT)
5191 f = !f;
5192 else
5193#endif
5194 val = !val;
5195 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005196 else 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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005206#ifdef FEAT_FLOAT
5207 if (rettv->v_type == VAR_FLOAT)
5208 {
5209 clear_tv(rettv);
5210 rettv->vval.v_float = f;
5211 }
5212 else
5213#endif
5214 {
5215 clear_tv(rettv);
5216 rettv->v_type = VAR_NUMBER;
5217 rettv->vval.v_number = val;
5218 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 }
5221
5222 return ret;
5223}
5224
5225/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005226 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5227 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5229 */
5230 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005231eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005232 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005233 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005234 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005235 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005236{
5237 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005238 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005239 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005240 long len = -1;
5241 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005242 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005243 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005245 if (rettv->v_type == VAR_FUNC
5246#ifdef FEAT_FLOAT
5247 || rettv->v_type == VAR_FLOAT
5248#endif
5249 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005251 if (verbose)
5252 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253 return FAIL;
5254 }
5255
Bram Moolenaar8c711452005-01-14 21:53:12 +00005256 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005258 /*
5259 * dict.name
5260 */
5261 key = *arg + 1;
5262 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5263 ;
5264 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005265 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005267 }
5268 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005270 /*
5271 * something[idx]
5272 *
5273 * Get the (first) variable from inside the [].
5274 */
5275 *arg = skipwhite(*arg + 1);
5276 if (**arg == ':')
5277 empty1 = TRUE;
5278 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5279 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005280 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5281 {
5282 /* not a number or string */
5283 clear_tv(&var1);
5284 return FAIL;
5285 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286
5287 /*
5288 * Get the second variable from inside the [:].
5289 */
5290 if (**arg == ':')
5291 {
5292 range = TRUE;
5293 *arg = skipwhite(*arg + 1);
5294 if (**arg == ']')
5295 empty2 = TRUE;
5296 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005298 if (!empty1)
5299 clear_tv(&var1);
5300 return FAIL;
5301 }
5302 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5303 {
5304 /* not a number or string */
5305 if (!empty1)
5306 clear_tv(&var1);
5307 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005308 return FAIL;
5309 }
5310 }
5311
5312 /* Check for the ']'. */
5313 if (**arg != ']')
5314 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005315 if (verbose)
5316 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005317 clear_tv(&var1);
5318 if (range)
5319 clear_tv(&var2);
5320 return FAIL;
5321 }
5322 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005323 }
5324
5325 if (evaluate)
5326 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005327 n1 = 0;
5328 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005329 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005330 n1 = get_tv_number(&var1);
5331 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005332 }
5333 if (range)
5334 {
5335 if (empty2)
5336 n2 = -1;
5337 else
5338 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005339 n2 = get_tv_number(&var2);
5340 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 }
5342 }
5343
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005344 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 {
5346 case VAR_NUMBER:
5347 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005348 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 len = (long)STRLEN(s);
5350 if (range)
5351 {
5352 /* The resulting variable is a substring. If the indexes
5353 * are out of range the result is empty. */
5354 if (n1 < 0)
5355 {
5356 n1 = len + n1;
5357 if (n1 < 0)
5358 n1 = 0;
5359 }
5360 if (n2 < 0)
5361 n2 = len + n2;
5362 else if (n2 >= len)
5363 n2 = len;
5364 if (n1 >= len || n2 < 0 || n1 > n2)
5365 s = NULL;
5366 else
5367 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5368 }
5369 else
5370 {
5371 /* The resulting variable is a string of a single
5372 * character. If the index is too big or negative the
5373 * result is empty. */
5374 if (n1 >= len || n1 < 0)
5375 s = NULL;
5376 else
5377 s = vim_strnsave(s + n1, 1);
5378 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005379 clear_tv(rettv);
5380 rettv->v_type = VAR_STRING;
5381 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 break;
5383
5384 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005385 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 if (n1 < 0)
5387 n1 = len + n1;
5388 if (!empty1 && (n1 < 0 || n1 >= len))
5389 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005390 /* For a range we allow invalid values and return an empty
5391 * list. A list index out of range is an error. */
5392 if (!range)
5393 {
5394 if (verbose)
5395 EMSGN(_(e_listidx), n1);
5396 return FAIL;
5397 }
5398 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 }
5400 if (range)
5401 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005402 list_T *l;
5403 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404
5405 if (n2 < 0)
5406 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005407 else if (n2 >= len)
5408 n2 = len - 1;
5409 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005410 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411 l = list_alloc();
5412 if (l == NULL)
5413 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005414 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415 n1 <= n2; ++n1)
5416 {
5417 if (list_append_tv(l, &item->li_tv) == FAIL)
5418 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005419 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 return FAIL;
5421 }
5422 item = item->li_next;
5423 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005424 clear_tv(rettv);
5425 rettv->v_type = VAR_LIST;
5426 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005427 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428 }
5429 else
5430 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005431 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005432 clear_tv(rettv);
5433 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 }
5435 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005436
5437 case VAR_DICT:
5438 if (range)
5439 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005440 if (verbose)
5441 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005442 if (len == -1)
5443 clear_tv(&var1);
5444 return FAIL;
5445 }
5446 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005447 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005448
5449 if (len == -1)
5450 {
5451 key = get_tv_string(&var1);
5452 if (*key == NUL)
5453 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005454 if (verbose)
5455 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005456 clear_tv(&var1);
5457 return FAIL;
5458 }
5459 }
5460
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005461 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005462
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005463 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005464 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005465 if (len == -1)
5466 clear_tv(&var1);
5467 if (item == NULL)
5468 return FAIL;
5469
5470 copy_tv(&item->di_tv, &var1);
5471 clear_tv(rettv);
5472 *rettv = var1;
5473 }
5474 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 }
5476 }
5477
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 return OK;
5479}
5480
5481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 * Get an option value.
5483 * "arg" points to the '&' or '+' before the option name.
5484 * "arg" is advanced to character after the option name.
5485 * Return OK or FAIL.
5486 */
5487 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005488get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005490 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 int evaluate;
5492{
5493 char_u *option_end;
5494 long numval;
5495 char_u *stringval;
5496 int opt_type;
5497 int c;
5498 int working = (**arg == '+'); /* has("+option") */
5499 int ret = OK;
5500 int opt_flags;
5501
5502 /*
5503 * Isolate the option name and find its value.
5504 */
5505 option_end = find_option_end(arg, &opt_flags);
5506 if (option_end == NULL)
5507 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005508 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 EMSG2(_("E112: Option name missing: %s"), *arg);
5510 return FAIL;
5511 }
5512
5513 if (!evaluate)
5514 {
5515 *arg = option_end;
5516 return OK;
5517 }
5518
5519 c = *option_end;
5520 *option_end = NUL;
5521 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005522 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523
5524 if (opt_type == -3) /* invalid name */
5525 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005526 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 EMSG2(_("E113: Unknown option: %s"), *arg);
5528 ret = FAIL;
5529 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005530 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 {
5532 if (opt_type == -2) /* hidden string option */
5533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005534 rettv->v_type = VAR_STRING;
5535 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 }
5537 else if (opt_type == -1) /* hidden number option */
5538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005539 rettv->v_type = VAR_NUMBER;
5540 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 }
5542 else if (opt_type == 1) /* number option */
5543 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005544 rettv->v_type = VAR_NUMBER;
5545 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 }
5547 else /* string option */
5548 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005549 rettv->v_type = VAR_STRING;
5550 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 }
5552 }
5553 else if (working && (opt_type == -2 || opt_type == -1))
5554 ret = FAIL;
5555
5556 *option_end = c; /* put back for error messages */
5557 *arg = option_end;
5558
5559 return ret;
5560}
5561
5562/*
5563 * Allocate a variable for a string constant.
5564 * Return OK or FAIL.
5565 */
5566 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 int evaluate;
5571{
5572 char_u *p;
5573 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 int extra = 0;
5575
5576 /*
5577 * Find the end of the string, skipping backslashed characters.
5578 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005579 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 {
5581 if (*p == '\\' && p[1] != NUL)
5582 {
5583 ++p;
5584 /* A "\<x>" form occupies at least 4 characters, and produces up
5585 * to 6 characters: reserve space for 2 extra */
5586 if (*p == '<')
5587 extra += 2;
5588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 }
5590
5591 if (*p != '"')
5592 {
5593 EMSG2(_("E114: Missing quote: %s"), *arg);
5594 return FAIL;
5595 }
5596
5597 /* If only parsing, set *arg and return here */
5598 if (!evaluate)
5599 {
5600 *arg = p + 1;
5601 return OK;
5602 }
5603
5604 /*
5605 * Copy the string into allocated memory, handling backslashed
5606 * characters.
5607 */
5608 name = alloc((unsigned)(p - *arg + extra));
5609 if (name == NULL)
5610 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005611 rettv->v_type = VAR_STRING;
5612 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
Bram Moolenaar8c711452005-01-14 21:53:12 +00005614 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 {
5616 if (*p == '\\')
5617 {
5618 switch (*++p)
5619 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005620 case 'b': *name++ = BS; ++p; break;
5621 case 'e': *name++ = ESC; ++p; break;
5622 case 'f': *name++ = FF; ++p; break;
5623 case 'n': *name++ = NL; ++p; break;
5624 case 'r': *name++ = CAR; ++p; break;
5625 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626
5627 case 'X': /* hex: "\x1", "\x12" */
5628 case 'x':
5629 case 'u': /* Unicode: "\u0023" */
5630 case 'U':
5631 if (vim_isxdigit(p[1]))
5632 {
5633 int n, nr;
5634 int c = toupper(*p);
5635
5636 if (c == 'X')
5637 n = 2;
5638 else
5639 n = 4;
5640 nr = 0;
5641 while (--n >= 0 && vim_isxdigit(p[1]))
5642 {
5643 ++p;
5644 nr = (nr << 4) + hex2nr(*p);
5645 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005646 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647#ifdef FEAT_MBYTE
5648 /* For "\u" store the number according to
5649 * 'encoding'. */
5650 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005651 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 else
5653#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005654 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 break;
5657
5658 /* octal: "\1", "\12", "\123" */
5659 case '0':
5660 case '1':
5661 case '2':
5662 case '3':
5663 case '4':
5664 case '5':
5665 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005666 case '7': *name = *p++ - '0';
5667 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005669 *name = (*name << 3) + *p++ - '0';
5670 if (*p >= '0' && *p <= '7')
5671 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005673 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 break;
5675
5676 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 if (extra != 0)
5679 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005680 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 break;
5682 }
5683 /* FALLTHROUGH */
5684
Bram Moolenaar8c711452005-01-14 21:53:12 +00005685 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 break;
5687 }
5688 }
5689 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005690 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005693 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 *arg = p + 1;
5695
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 return OK;
5697}
5698
5699/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005700 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 * Return OK or FAIL.
5702 */
5703 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005704get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 int evaluate;
5708{
5709 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005710 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005711 int reduce = 0;
5712
5713 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005714 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005715 */
5716 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5717 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005718 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005719 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005720 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005721 break;
5722 ++reduce;
5723 ++p;
5724 }
5725 }
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 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005730 return FAIL;
5731 }
5732
Bram Moolenaar8c711452005-01-14 21:53:12 +00005733 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005734 if (!evaluate)
5735 {
5736 *arg = p + 1;
5737 return OK;
5738 }
5739
5740 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005742 */
5743 str = alloc((unsigned)((p - *arg) - reduce));
5744 if (str == NULL)
5745 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 rettv->v_type = VAR_STRING;
5747 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005748
Bram Moolenaar8c711452005-01-14 21:53:12 +00005749 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005750 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005752 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005754 break;
5755 ++p;
5756 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005758 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005760 *arg = p + 1;
5761
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005762 return OK;
5763}
5764
5765/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005766 * Allocate a variable for a List and fill it from "*arg".
5767 * Return OK or FAIL.
5768 */
5769 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005770get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005771 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005772 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005773 int evaluate;
5774{
Bram Moolenaar33570922005-01-25 22:26:29 +00005775 list_T *l = NULL;
5776 typval_T tv;
5777 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005778
5779 if (evaluate)
5780 {
5781 l = list_alloc();
5782 if (l == NULL)
5783 return FAIL;
5784 }
5785
5786 *arg = skipwhite(*arg + 1);
5787 while (**arg != ']' && **arg != NUL)
5788 {
5789 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5790 goto failret;
5791 if (evaluate)
5792 {
5793 item = listitem_alloc();
5794 if (item != NULL)
5795 {
5796 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005797 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005798 list_append(l, item);
5799 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005800 else
5801 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802 }
5803
5804 if (**arg == ']')
5805 break;
5806 if (**arg != ',')
5807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005809 goto failret;
5810 }
5811 *arg = skipwhite(*arg + 1);
5812 }
5813
5814 if (**arg != ']')
5815 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005817failret:
5818 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005819 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005820 return FAIL;
5821 }
5822
5823 *arg = skipwhite(*arg + 1);
5824 if (evaluate)
5825 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005826 rettv->v_type = VAR_LIST;
5827 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 ++l->lv_refcount;
5829 }
5830
5831 return OK;
5832}
5833
5834/*
5835 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005836 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005837 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005838 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005839list_alloc()
5840{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005841 list_T *l;
5842
5843 l = (list_T *)alloc_clear(sizeof(list_T));
5844 if (l != NULL)
5845 {
5846 /* Prepend the list to the list of lists for garbage collection. */
5847 if (first_list != NULL)
5848 first_list->lv_used_prev = l;
5849 l->lv_used_prev = NULL;
5850 l->lv_used_next = first_list;
5851 first_list = l;
5852 }
5853 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854}
5855
5856/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005857 * Allocate an empty list for a return value.
5858 * Returns OK or FAIL.
5859 */
5860 static int
5861rettv_list_alloc(rettv)
5862 typval_T *rettv;
5863{
5864 list_T *l = list_alloc();
5865
5866 if (l == NULL)
5867 return FAIL;
5868
5869 rettv->vval.v_list = l;
5870 rettv->v_type = VAR_LIST;
5871 ++l->lv_refcount;
5872 return OK;
5873}
5874
5875/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005876 * Unreference a list: decrement the reference count and free it when it
5877 * becomes zero.
5878 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005879 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005881 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005882{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005883 if (l != NULL && --l->lv_refcount <= 0)
5884 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005885}
5886
5887/*
5888 * Free a list, including all items it points to.
5889 * Ignores the reference count.
5890 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005891 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005892list_free(l, recurse)
5893 list_T *l;
5894 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005895{
Bram Moolenaar33570922005-01-25 22:26:29 +00005896 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005897
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005898 /* Remove the list from the list of lists for garbage collection. */
5899 if (l->lv_used_prev == NULL)
5900 first_list = l->lv_used_next;
5901 else
5902 l->lv_used_prev->lv_used_next = l->lv_used_next;
5903 if (l->lv_used_next != NULL)
5904 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5905
Bram Moolenaard9fba312005-06-26 22:34:35 +00005906 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005908 /* Remove the item before deleting it. */
5909 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005910 if (recurse || (item->li_tv.v_type != VAR_LIST
5911 && item->li_tv.v_type != VAR_DICT))
5912 clear_tv(&item->li_tv);
5913 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914 }
5915 vim_free(l);
5916}
5917
5918/*
5919 * Allocate a list item.
5920 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005921 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922listitem_alloc()
5923{
Bram Moolenaar33570922005-01-25 22:26:29 +00005924 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925}
5926
5927/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005928 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929 */
5930 static void
5931listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005932 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005934 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935 vim_free(item);
5936}
5937
5938/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005939 * Remove a list item from a List and free it. Also clears the value.
5940 */
5941 static void
5942listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005943 list_T *l;
5944 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005945{
5946 list_remove(l, item, item);
5947 listitem_free(item);
5948}
5949
5950/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951 * Get the number of items in a list.
5952 */
5953 static long
5954list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005955 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957 if (l == NULL)
5958 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005959 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960}
5961
5962/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005963 * Return TRUE when two lists have exactly the same values.
5964 */
5965 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005966list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005967 list_T *l1;
5968 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005969 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005970 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005971{
Bram Moolenaar33570922005-01-25 22:26:29 +00005972 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005973
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005974 if (l1 == NULL || l2 == NULL)
5975 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005976 if (l1 == l2)
5977 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005978 if (list_len(l1) != list_len(l2))
5979 return FALSE;
5980
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005981 for (item1 = l1->lv_first, item2 = l2->lv_first;
5982 item1 != NULL && item2 != NULL;
5983 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005984 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005985 return FALSE;
5986 return item1 == NULL && item2 == NULL;
5987}
5988
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005989#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5990 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005991/*
5992 * Return the dictitem that an entry in a hashtable points to.
5993 */
5994 dictitem_T *
5995dict_lookup(hi)
5996 hashitem_T *hi;
5997{
5998 return HI2DI(hi);
5999}
6000#endif
6001
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006002/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006003 * Return TRUE when two dictionaries have exactly the same key/values.
6004 */
6005 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006006dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006007 dict_T *d1;
6008 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006009 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006010 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006011{
Bram Moolenaar33570922005-01-25 22:26:29 +00006012 hashitem_T *hi;
6013 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006014 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006015
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006016 if (d1 == NULL || d2 == NULL)
6017 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006018 if (d1 == d2)
6019 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006020 if (dict_len(d1) != dict_len(d2))
6021 return FALSE;
6022
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006023 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006024 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006025 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006026 if (!HASHITEM_EMPTY(hi))
6027 {
6028 item2 = dict_find(d2, hi->hi_key, -1);
6029 if (item2 == NULL)
6030 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006031 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006032 return FALSE;
6033 --todo;
6034 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006035 }
6036 return TRUE;
6037}
6038
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006039static int tv_equal_recurse_limit;
6040
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006041/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006042 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006043 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006044 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006045 */
6046 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006047tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006048 typval_T *tv1;
6049 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006050 int ic; /* ignore case */
6051 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006052{
6053 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006054 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006055 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006056 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006057
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006058 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006059 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006060
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006061 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006062 * recursiveness to a limit. We guess they are equal then.
6063 * A fixed limit has the problem of still taking an awful long time.
6064 * Reduce the limit every time running into it. That should work fine for
6065 * deeply linked structures that are not recursively linked and catch
6066 * recursiveness quickly. */
6067 if (!recursive)
6068 tv_equal_recurse_limit = 1000;
6069 if (recursive_cnt >= tv_equal_recurse_limit)
6070 {
6071 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006072 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006073 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006074
6075 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006076 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006077 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006078 ++recursive_cnt;
6079 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6080 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006081 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006082
6083 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006084 ++recursive_cnt;
6085 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6086 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006087 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006088
6089 case VAR_FUNC:
6090 return (tv1->vval.v_string != NULL
6091 && tv2->vval.v_string != NULL
6092 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6093
6094 case VAR_NUMBER:
6095 return tv1->vval.v_number == tv2->vval.v_number;
6096
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006097#ifdef FEAT_FLOAT
6098 case VAR_FLOAT:
6099 return tv1->vval.v_float == tv2->vval.v_float;
6100#endif
6101
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006102 case VAR_STRING:
6103 s1 = get_tv_string_buf(tv1, buf1);
6104 s2 = get_tv_string_buf(tv2, buf2);
6105 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006106 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006107
6108 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006109 return TRUE;
6110}
6111
6112/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006113 * Locate item with index "n" in list "l" and return it.
6114 * A negative index is counted from the end; -1 is the last item.
6115 * Returns NULL when "n" is out of range.
6116 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006117 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006118list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006119 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006120 long n;
6121{
Bram Moolenaar33570922005-01-25 22:26:29 +00006122 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006123 long idx;
6124
6125 if (l == NULL)
6126 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006127
6128 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006129 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006130 n = l->lv_len + n;
6131
6132 /* Check for index out of range. */
6133 if (n < 0 || n >= l->lv_len)
6134 return NULL;
6135
6136 /* When there is a cached index may start search from there. */
6137 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006138 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006139 if (n < l->lv_idx / 2)
6140 {
6141 /* closest to the start of the list */
6142 item = l->lv_first;
6143 idx = 0;
6144 }
6145 else if (n > (l->lv_idx + l->lv_len) / 2)
6146 {
6147 /* closest to the end of the list */
6148 item = l->lv_last;
6149 idx = l->lv_len - 1;
6150 }
6151 else
6152 {
6153 /* closest to the cached index */
6154 item = l->lv_idx_item;
6155 idx = l->lv_idx;
6156 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006157 }
6158 else
6159 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006160 if (n < l->lv_len / 2)
6161 {
6162 /* closest to the start of the list */
6163 item = l->lv_first;
6164 idx = 0;
6165 }
6166 else
6167 {
6168 /* closest to the end of the list */
6169 item = l->lv_last;
6170 idx = l->lv_len - 1;
6171 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006172 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006173
6174 while (n > idx)
6175 {
6176 /* search forward */
6177 item = item->li_next;
6178 ++idx;
6179 }
6180 while (n < idx)
6181 {
6182 /* search backward */
6183 item = item->li_prev;
6184 --idx;
6185 }
6186
6187 /* cache the used index */
6188 l->lv_idx = idx;
6189 l->lv_idx_item = item;
6190
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006191 return item;
6192}
6193
6194/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006195 * Get list item "l[idx]" as a number.
6196 */
6197 static long
6198list_find_nr(l, idx, errorp)
6199 list_T *l;
6200 long idx;
6201 int *errorp; /* set to TRUE when something wrong */
6202{
6203 listitem_T *li;
6204
6205 li = list_find(l, idx);
6206 if (li == NULL)
6207 {
6208 if (errorp != NULL)
6209 *errorp = TRUE;
6210 return -1L;
6211 }
6212 return get_tv_number_chk(&li->li_tv, errorp);
6213}
6214
6215/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006216 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6217 */
6218 char_u *
6219list_find_str(l, idx)
6220 list_T *l;
6221 long idx;
6222{
6223 listitem_T *li;
6224
6225 li = list_find(l, idx - 1);
6226 if (li == NULL)
6227 {
6228 EMSGN(_(e_listidx), idx);
6229 return NULL;
6230 }
6231 return get_tv_string(&li->li_tv);
6232}
6233
6234/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006235 * Locate "item" list "l" and return its index.
6236 * Returns -1 when "item" is not in the list.
6237 */
6238 static long
6239list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006240 list_T *l;
6241 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006242{
6243 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006244 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006245
6246 if (l == NULL)
6247 return -1;
6248 idx = 0;
6249 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6250 ++idx;
6251 if (li == NULL)
6252 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006253 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006254}
6255
6256/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006257 * Append item "item" to the end of list "l".
6258 */
6259 static void
6260list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006261 list_T *l;
6262 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263{
6264 if (l->lv_last == NULL)
6265 {
6266 /* empty list */
6267 l->lv_first = item;
6268 l->lv_last = item;
6269 item->li_prev = NULL;
6270 }
6271 else
6272 {
6273 l->lv_last->li_next = item;
6274 item->li_prev = l->lv_last;
6275 l->lv_last = item;
6276 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006277 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006278 item->li_next = NULL;
6279}
6280
6281/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006282 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006283 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006284 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006285 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006287 list_T *l;
6288 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006289{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006290 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006291
Bram Moolenaar05159a02005-02-26 23:04:13 +00006292 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006293 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006294 copy_tv(tv, &li->li_tv);
6295 list_append(l, li);
6296 return OK;
6297}
6298
6299/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006300 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006301 * Return FAIL when out of memory.
6302 */
6303 int
6304list_append_dict(list, dict)
6305 list_T *list;
6306 dict_T *dict;
6307{
6308 listitem_T *li = listitem_alloc();
6309
6310 if (li == NULL)
6311 return FAIL;
6312 li->li_tv.v_type = VAR_DICT;
6313 li->li_tv.v_lock = 0;
6314 li->li_tv.vval.v_dict = dict;
6315 list_append(list, li);
6316 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006317 return OK;
6318}
6319
6320/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006321 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006322 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006323 * Returns FAIL when out of memory.
6324 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006325 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006326list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006327 list_T *l;
6328 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006329 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006330{
6331 listitem_T *li = listitem_alloc();
6332
6333 if (li == NULL)
6334 return FAIL;
6335 list_append(l, li);
6336 li->li_tv.v_type = VAR_STRING;
6337 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006338 if (str == NULL)
6339 li->li_tv.vval.v_string = NULL;
6340 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006341 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006342 return FAIL;
6343 return OK;
6344}
6345
6346/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006347 * Append "n" to list "l".
6348 * Returns FAIL when out of memory.
6349 */
6350 static int
6351list_append_number(l, n)
6352 list_T *l;
6353 varnumber_T n;
6354{
6355 listitem_T *li;
6356
6357 li = listitem_alloc();
6358 if (li == NULL)
6359 return FAIL;
6360 li->li_tv.v_type = VAR_NUMBER;
6361 li->li_tv.v_lock = 0;
6362 li->li_tv.vval.v_number = n;
6363 list_append(l, li);
6364 return OK;
6365}
6366
6367/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006368 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006369 * If "item" is NULL append at the end.
6370 * Return FAIL when out of memory.
6371 */
6372 static int
6373list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006374 list_T *l;
6375 typval_T *tv;
6376 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006377{
Bram Moolenaar33570922005-01-25 22:26:29 +00006378 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006379
6380 if (ni == NULL)
6381 return FAIL;
6382 copy_tv(tv, &ni->li_tv);
6383 if (item == NULL)
6384 /* Append new item at end of list. */
6385 list_append(l, ni);
6386 else
6387 {
6388 /* Insert new item before existing item. */
6389 ni->li_prev = item->li_prev;
6390 ni->li_next = item;
6391 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006392 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006393 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006394 ++l->lv_idx;
6395 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006396 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006397 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006398 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006399 l->lv_idx_item = NULL;
6400 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006401 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006402 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403 }
6404 return OK;
6405}
6406
6407/*
6408 * Extend "l1" with "l2".
6409 * If "bef" is NULL append at the end, otherwise insert before this item.
6410 * Returns FAIL when out of memory.
6411 */
6412 static int
6413list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006414 list_T *l1;
6415 list_T *l2;
6416 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006417{
Bram Moolenaar33570922005-01-25 22:26:29 +00006418 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006419 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006420
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006421 /* We also quit the loop when we have inserted the original item count of
6422 * the list, avoid a hang when we extend a list with itself. */
6423 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6425 return FAIL;
6426 return OK;
6427}
6428
6429/*
6430 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6431 * Return FAIL when out of memory.
6432 */
6433 static int
6434list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006435 list_T *l1;
6436 list_T *l2;
6437 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006438{
Bram Moolenaar33570922005-01-25 22:26:29 +00006439 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006440
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006441 if (l1 == NULL || l2 == NULL)
6442 return FAIL;
6443
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006444 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006445 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006446 if (l == NULL)
6447 return FAIL;
6448 tv->v_type = VAR_LIST;
6449 tv->vval.v_list = l;
6450
6451 /* append all items from the second list */
6452 return list_extend(l, l2, NULL);
6453}
6454
6455/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006456 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006457 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006458 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006459 * Returns NULL when out of memory.
6460 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006461 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006462list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006463 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006464 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006465 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006466{
Bram Moolenaar33570922005-01-25 22:26:29 +00006467 list_T *copy;
6468 listitem_T *item;
6469 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006470
6471 if (orig == NULL)
6472 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006473
6474 copy = list_alloc();
6475 if (copy != NULL)
6476 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006477 if (copyID != 0)
6478 {
6479 /* Do this before adding the items, because one of the items may
6480 * refer back to this list. */
6481 orig->lv_copyID = copyID;
6482 orig->lv_copylist = copy;
6483 }
6484 for (item = orig->lv_first; item != NULL && !got_int;
6485 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006486 {
6487 ni = listitem_alloc();
6488 if (ni == NULL)
6489 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006490 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006491 {
6492 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6493 {
6494 vim_free(ni);
6495 break;
6496 }
6497 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006498 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006499 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006500 list_append(copy, ni);
6501 }
6502 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006503 if (item != NULL)
6504 {
6505 list_unref(copy);
6506 copy = NULL;
6507 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006508 }
6509
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006510 return copy;
6511}
6512
6513/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006514 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006515 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006516 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006517 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006518list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006519 list_T *l;
6520 listitem_T *item;
6521 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522{
Bram Moolenaar33570922005-01-25 22:26:29 +00006523 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525 /* notify watchers */
6526 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006527 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006528 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006529 list_fix_watch(l, ip);
6530 if (ip == item2)
6531 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006532 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533
6534 if (item2->li_next == NULL)
6535 l->lv_last = item->li_prev;
6536 else
6537 item2->li_next->li_prev = item->li_prev;
6538 if (item->li_prev == NULL)
6539 l->lv_first = item2->li_next;
6540 else
6541 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006542 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006543}
6544
6545/*
6546 * Return an allocated string with the string representation of a list.
6547 * May return NULL.
6548 */
6549 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006550list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006551 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006552 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006553{
6554 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006555
6556 if (tv->vval.v_list == NULL)
6557 return NULL;
6558 ga_init2(&ga, (int)sizeof(char), 80);
6559 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006560 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006561 {
6562 vim_free(ga.ga_data);
6563 return NULL;
6564 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006565 ga_append(&ga, ']');
6566 ga_append(&ga, NUL);
6567 return (char_u *)ga.ga_data;
6568}
6569
6570/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006571 * Join list "l" into a string in "*gap", using separator "sep".
6572 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006573 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006574 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006575 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006576list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006577 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006578 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006579 char_u *sep;
6580 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006581 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006582{
6583 int first = TRUE;
6584 char_u *tofree;
6585 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006586 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006587 char_u *s;
6588
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006589 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006590 {
6591 if (first)
6592 first = FALSE;
6593 else
6594 ga_concat(gap, sep);
6595
6596 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006597 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006598 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006599 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006600 if (s != NULL)
6601 ga_concat(gap, s);
6602 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006603 if (s == NULL)
6604 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006605 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006606 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006608}
6609
6610/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006611 * Garbage collection for lists and dictionaries.
6612 *
6613 * We use reference counts to be able to free most items right away when they
6614 * are no longer used. But for composite items it's possible that it becomes
6615 * unused while the reference count is > 0: When there is a recursive
6616 * reference. Example:
6617 * :let l = [1, 2, 3]
6618 * :let d = {9: l}
6619 * :let l[1] = d
6620 *
6621 * Since this is quite unusual we handle this with garbage collection: every
6622 * once in a while find out which lists and dicts are not referenced from any
6623 * variable.
6624 *
6625 * Here is a good reference text about garbage collection (refers to Python
6626 * but it applies to all reference-counting mechanisms):
6627 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006628 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006629
6630/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006631 * Do garbage collection for lists and dicts.
6632 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006633 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006634 int
6635garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006636{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006637 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006638 buf_T *buf;
6639 win_T *wp;
6640 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006641 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006642 int did_free;
6643 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006644#ifdef FEAT_WINDOWS
6645 tabpage_T *tp;
6646#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006647
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006648 /* Only do this once. */
6649 want_garbage_collect = FALSE;
6650 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006651 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006652
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006653 /* We advance by two because we add one for items referenced through
6654 * previous_funccal. */
6655 current_copyID += COPYID_INC;
6656 copyID = current_copyID;
6657
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006658 /*
6659 * 1. Go through all accessible variables and mark all lists and dicts
6660 * with copyID.
6661 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006662
6663 /* Don't free variables in the previous_funccal list unless they are only
6664 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006665 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006666 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6667 {
6668 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6669 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6670 }
6671
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006672 /* script-local variables */
6673 for (i = 1; i <= ga_scripts.ga_len; ++i)
6674 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6675
6676 /* buffer-local variables */
6677 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6678 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6679
6680 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006681 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006682 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6683
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006684#ifdef FEAT_WINDOWS
6685 /* tabpage-local variables */
6686 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6687 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6688#endif
6689
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006690 /* global variables */
6691 set_ref_in_ht(&globvarht, copyID);
6692
6693 /* function-local variables */
6694 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6695 {
6696 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6697 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6698 }
6699
Bram Moolenaard812df62008-11-09 12:46:09 +00006700 /* v: vars */
6701 set_ref_in_ht(&vimvarht, copyID);
6702
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006703 /*
6704 * 2. Free lists and dictionaries that are not referenced.
6705 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006706 did_free = free_unref_items(copyID);
6707
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006708 /*
6709 * 3. Check if any funccal can be freed now.
6710 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006711 for (pfc = &previous_funccal; *pfc != NULL; )
6712 {
6713 if (can_free_funccal(*pfc, copyID))
6714 {
6715 fc = *pfc;
6716 *pfc = fc->caller;
6717 free_funccal(fc, TRUE);
6718 did_free = TRUE;
6719 did_free_funccal = TRUE;
6720 }
6721 else
6722 pfc = &(*pfc)->caller;
6723 }
6724 if (did_free_funccal)
6725 /* When a funccal was freed some more items might be garbage
6726 * collected, so run again. */
6727 (void)garbage_collect();
6728
6729 return did_free;
6730}
6731
6732/*
6733 * Free lists and dictionaries that are no longer referenced.
6734 */
6735 static int
6736free_unref_items(copyID)
6737 int copyID;
6738{
6739 dict_T *dd;
6740 list_T *ll;
6741 int did_free = FALSE;
6742
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006743 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006744 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006745 */
6746 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006747 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006748 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006749 /* Free the Dictionary and ordinary items it contains, but don't
6750 * recurse into Lists and Dictionaries, they will be in the list
6751 * of dicts or list of lists. */
6752 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006753 did_free = TRUE;
6754
6755 /* restart, next dict may also have been freed */
6756 dd = first_dict;
6757 }
6758 else
6759 dd = dd->dv_used_next;
6760
6761 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006762 * Go through the list of lists and free items without the copyID.
6763 * But don't free a list that has a watcher (used in a for loop), these
6764 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006765 */
6766 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006767 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6768 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006769 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006770 /* Free the List and ordinary items it contains, but don't recurse
6771 * into Lists and Dictionaries, they will be in the list of dicts
6772 * or list of lists. */
6773 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006774 did_free = TRUE;
6775
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006776 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006777 ll = first_list;
6778 }
6779 else
6780 ll = ll->lv_used_next;
6781
6782 return did_free;
6783}
6784
6785/*
6786 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6787 */
6788 static void
6789set_ref_in_ht(ht, copyID)
6790 hashtab_T *ht;
6791 int copyID;
6792{
6793 int todo;
6794 hashitem_T *hi;
6795
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006796 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006797 for (hi = ht->ht_array; todo > 0; ++hi)
6798 if (!HASHITEM_EMPTY(hi))
6799 {
6800 --todo;
6801 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6802 }
6803}
6804
6805/*
6806 * Mark all lists and dicts referenced through list "l" with "copyID".
6807 */
6808 static void
6809set_ref_in_list(l, copyID)
6810 list_T *l;
6811 int copyID;
6812{
6813 listitem_T *li;
6814
6815 for (li = l->lv_first; li != NULL; li = li->li_next)
6816 set_ref_in_item(&li->li_tv, copyID);
6817}
6818
6819/*
6820 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6821 */
6822 static void
6823set_ref_in_item(tv, copyID)
6824 typval_T *tv;
6825 int copyID;
6826{
6827 dict_T *dd;
6828 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006829
6830 switch (tv->v_type)
6831 {
6832 case VAR_DICT:
6833 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006834 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006835 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006836 /* Didn't see this dict yet. */
6837 dd->dv_copyID = copyID;
6838 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006839 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006840 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006841
6842 case VAR_LIST:
6843 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006844 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006845 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006846 /* Didn't see this list yet. */
6847 ll->lv_copyID = copyID;
6848 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006849 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006850 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006851 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006852 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006853}
6854
6855/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006856 * Allocate an empty header for a dictionary.
6857 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006858 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006859dict_alloc()
6860{
Bram Moolenaar33570922005-01-25 22:26:29 +00006861 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006862
Bram Moolenaar33570922005-01-25 22:26:29 +00006863 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006864 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006865 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006866 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006867 if (first_dict != NULL)
6868 first_dict->dv_used_prev = d;
6869 d->dv_used_next = first_dict;
6870 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006871 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006872
Bram Moolenaar33570922005-01-25 22:26:29 +00006873 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006874 d->dv_lock = 0;
6875 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006876 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006877 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006878 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006879}
6880
6881/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006882 * Allocate an empty dict for a return value.
6883 * Returns OK or FAIL.
6884 */
6885 static int
6886rettv_dict_alloc(rettv)
6887 typval_T *rettv;
6888{
6889 dict_T *d = dict_alloc();
6890
6891 if (d == NULL)
6892 return FAIL;
6893
6894 rettv->vval.v_dict = d;
6895 rettv->v_type = VAR_DICT;
6896 ++d->dv_refcount;
6897 return OK;
6898}
6899
6900
6901/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006902 * Unreference a Dictionary: decrement the reference count and free it when it
6903 * becomes zero.
6904 */
Bram Moolenaar82139082011-09-14 16:52:09 +02006905 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006906dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006907 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006908{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006909 if (d != NULL && --d->dv_refcount <= 0)
6910 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006911}
6912
6913/*
6914 * Free a Dictionary, including all items it contains.
6915 * Ignores the reference count.
6916 */
6917 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006918dict_free(d, recurse)
6919 dict_T *d;
6920 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006921{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006922 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006923 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006924 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006925
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006926 /* Remove the dict from the list of dicts for garbage collection. */
6927 if (d->dv_used_prev == NULL)
6928 first_dict = d->dv_used_next;
6929 else
6930 d->dv_used_prev->dv_used_next = d->dv_used_next;
6931 if (d->dv_used_next != NULL)
6932 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6933
6934 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006935 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006936 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006937 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006938 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006939 if (!HASHITEM_EMPTY(hi))
6940 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006941 /* Remove the item before deleting it, just in case there is
6942 * something recursive causing trouble. */
6943 di = HI2DI(hi);
6944 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006945 if (recurse || (di->di_tv.v_type != VAR_LIST
6946 && di->di_tv.v_type != VAR_DICT))
6947 clear_tv(&di->di_tv);
6948 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006949 --todo;
6950 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006951 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006952 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006953 vim_free(d);
6954}
6955
6956/*
6957 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006958 * The "key" is copied to the new item.
6959 * Note that the value of the item "di_tv" still needs to be initialized!
6960 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006961 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006962 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006963dictitem_alloc(key)
6964 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006965{
Bram Moolenaar33570922005-01-25 22:26:29 +00006966 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006967
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006968 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006969 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006970 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006971 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006972 di->di_flags = 0;
6973 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006974 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006975}
6976
6977/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006978 * Make a copy of a Dictionary item.
6979 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006980 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006981dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006982 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006983{
Bram Moolenaar33570922005-01-25 22:26:29 +00006984 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006985
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006986 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6987 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006988 if (di != NULL)
6989 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006990 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006991 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006992 copy_tv(&org->di_tv, &di->di_tv);
6993 }
6994 return di;
6995}
6996
6997/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006998 * Remove item "item" from Dictionary "dict" and free it.
6999 */
7000 static void
7001dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007002 dict_T *dict;
7003 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007004{
Bram Moolenaar33570922005-01-25 22:26:29 +00007005 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007006
Bram Moolenaar33570922005-01-25 22:26:29 +00007007 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007008 if (HASHITEM_EMPTY(hi))
7009 EMSG2(_(e_intern2), "dictitem_remove()");
7010 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007011 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007012 dictitem_free(item);
7013}
7014
7015/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007016 * Free a dict item. Also clears the value.
7017 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007018 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007019dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007020 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007021{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007022 clear_tv(&item->di_tv);
7023 vim_free(item);
7024}
7025
7026/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007027 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7028 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007029 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007030 * Returns NULL when out of memory.
7031 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007032 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007033dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007034 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007035 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007036 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007037{
Bram Moolenaar33570922005-01-25 22:26:29 +00007038 dict_T *copy;
7039 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007040 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007041 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007042
7043 if (orig == NULL)
7044 return NULL;
7045
7046 copy = dict_alloc();
7047 if (copy != NULL)
7048 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007049 if (copyID != 0)
7050 {
7051 orig->dv_copyID = copyID;
7052 orig->dv_copydict = copy;
7053 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007054 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007055 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007056 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007057 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007058 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007059 --todo;
7060
7061 di = dictitem_alloc(hi->hi_key);
7062 if (di == NULL)
7063 break;
7064 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007065 {
7066 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7067 copyID) == FAIL)
7068 {
7069 vim_free(di);
7070 break;
7071 }
7072 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007073 else
7074 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7075 if (dict_add(copy, di) == FAIL)
7076 {
7077 dictitem_free(di);
7078 break;
7079 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007080 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007081 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007082
Bram Moolenaare9a41262005-01-15 22:18:47 +00007083 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007084 if (todo > 0)
7085 {
7086 dict_unref(copy);
7087 copy = NULL;
7088 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007089 }
7090
7091 return copy;
7092}
7093
7094/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007095 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007096 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007097 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007098 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007099dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007100 dict_T *d;
7101 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007102{
Bram Moolenaar33570922005-01-25 22:26:29 +00007103 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007104}
7105
Bram Moolenaar8c711452005-01-14 21:53:12 +00007106/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007107 * Add a number or string entry to dictionary "d".
7108 * When "str" is NULL use number "nr", otherwise use "str".
7109 * Returns FAIL when out of memory and when key already exists.
7110 */
7111 int
7112dict_add_nr_str(d, key, nr, str)
7113 dict_T *d;
7114 char *key;
7115 long nr;
7116 char_u *str;
7117{
7118 dictitem_T *item;
7119
7120 item = dictitem_alloc((char_u *)key);
7121 if (item == NULL)
7122 return FAIL;
7123 item->di_tv.v_lock = 0;
7124 if (str == NULL)
7125 {
7126 item->di_tv.v_type = VAR_NUMBER;
7127 item->di_tv.vval.v_number = nr;
7128 }
7129 else
7130 {
7131 item->di_tv.v_type = VAR_STRING;
7132 item->di_tv.vval.v_string = vim_strsave(str);
7133 }
7134 if (dict_add(d, item) == FAIL)
7135 {
7136 dictitem_free(item);
7137 return FAIL;
7138 }
7139 return OK;
7140}
7141
7142/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007143 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007144 * Returns FAIL when out of memory and when key already exists.
7145 */
7146 int
7147dict_add_list(d, key, list)
7148 dict_T *d;
7149 char *key;
7150 list_T *list;
7151{
7152 dictitem_T *item;
7153
7154 item = dictitem_alloc((char_u *)key);
7155 if (item == NULL)
7156 return FAIL;
7157 item->di_tv.v_lock = 0;
7158 item->di_tv.v_type = VAR_LIST;
7159 item->di_tv.vval.v_list = list;
7160 if (dict_add(d, item) == FAIL)
7161 {
7162 dictitem_free(item);
7163 return FAIL;
7164 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007165 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007166 return OK;
7167}
7168
7169/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007170 * Get the number of items in a Dictionary.
7171 */
7172 static long
7173dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007174 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007175{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007176 if (d == NULL)
7177 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007178 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007179}
7180
7181/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007182 * Find item "key[len]" in Dictionary "d".
7183 * If "len" is negative use strlen(key).
7184 * Returns NULL when not found.
7185 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007186 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007187dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007188 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007189 char_u *key;
7190 int len;
7191{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007192#define AKEYLEN 200
7193 char_u buf[AKEYLEN];
7194 char_u *akey;
7195 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007196 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007197
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007198 if (len < 0)
7199 akey = key;
7200 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007201 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007202 tofree = akey = vim_strnsave(key, len);
7203 if (akey == NULL)
7204 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007205 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007206 else
7207 {
7208 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007209 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007210 akey = buf;
7211 }
7212
Bram Moolenaar33570922005-01-25 22:26:29 +00007213 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007214 vim_free(tofree);
7215 if (HASHITEM_EMPTY(hi))
7216 return NULL;
7217 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218}
7219
7220/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007221 * Get a string item from a dictionary.
7222 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007223 * Returns NULL if the entry doesn't exist or out of memory.
7224 */
7225 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007226get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007227 dict_T *d;
7228 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007229 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007230{
7231 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007232 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007233
7234 di = dict_find(d, key, -1);
7235 if (di == NULL)
7236 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007237 s = get_tv_string(&di->di_tv);
7238 if (save && s != NULL)
7239 s = vim_strsave(s);
7240 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007241}
7242
7243/*
7244 * Get a number item from a dictionary.
7245 * Returns 0 if the entry doesn't exist or out of memory.
7246 */
7247 long
7248get_dict_number(d, key)
7249 dict_T *d;
7250 char_u *key;
7251{
7252 dictitem_T *di;
7253
7254 di = dict_find(d, key, -1);
7255 if (di == NULL)
7256 return 0;
7257 return get_tv_number(&di->di_tv);
7258}
7259
7260/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261 * Return an allocated string with the string representation of a Dictionary.
7262 * May return NULL.
7263 */
7264 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007265dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007266 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007267 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268{
7269 garray_T ga;
7270 int first = TRUE;
7271 char_u *tofree;
7272 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007273 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007274 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007275 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007276 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007277
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007278 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007279 return NULL;
7280 ga_init2(&ga, (int)sizeof(char), 80);
7281 ga_append(&ga, '{');
7282
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007283 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007284 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007285 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007287 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007288 --todo;
7289
7290 if (first)
7291 first = FALSE;
7292 else
7293 ga_concat(&ga, (char_u *)", ");
7294
7295 tofree = string_quote(hi->hi_key, FALSE);
7296 if (tofree != NULL)
7297 {
7298 ga_concat(&ga, tofree);
7299 vim_free(tofree);
7300 }
7301 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007302 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303 if (s != NULL)
7304 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007306 if (s == NULL)
7307 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007308 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007309 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007310 if (todo > 0)
7311 {
7312 vim_free(ga.ga_data);
7313 return NULL;
7314 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007315
7316 ga_append(&ga, '}');
7317 ga_append(&ga, NUL);
7318 return (char_u *)ga.ga_data;
7319}
7320
7321/*
7322 * Allocate a variable for a Dictionary and fill it from "*arg".
7323 * Return OK or FAIL. Returns NOTDONE for {expr}.
7324 */
7325 static int
7326get_dict_tv(arg, rettv, evaluate)
7327 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007329 int evaluate;
7330{
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 dict_T *d = NULL;
7332 typval_T tvkey;
7333 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007334 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007337 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338
7339 /*
7340 * First check if it's not a curly-braces thing: {expr}.
7341 * Must do this without evaluating, otherwise a function may be called
7342 * twice. Unfortunately this means we need to call eval1() twice for the
7343 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007344 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007346 if (*start != '}')
7347 {
7348 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7349 return FAIL;
7350 if (*start == '}')
7351 return NOTDONE;
7352 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007353
7354 if (evaluate)
7355 {
7356 d = dict_alloc();
7357 if (d == NULL)
7358 return FAIL;
7359 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007360 tvkey.v_type = VAR_UNKNOWN;
7361 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007362
7363 *arg = skipwhite(*arg + 1);
7364 while (**arg != '}' && **arg != NUL)
7365 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007366 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007367 goto failret;
7368 if (**arg != ':')
7369 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007370 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007371 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007372 goto failret;
7373 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007374 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007375 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007376 key = get_tv_string_buf_chk(&tvkey, buf);
7377 if (key == NULL || *key == NUL)
7378 {
7379 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7380 if (key != NULL)
7381 EMSG(_(e_emptykey));
7382 clear_tv(&tvkey);
7383 goto failret;
7384 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007385 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007386
7387 *arg = skipwhite(*arg + 1);
7388 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7389 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007390 if (evaluate)
7391 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007392 goto failret;
7393 }
7394 if (evaluate)
7395 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007396 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007397 if (item != NULL)
7398 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007399 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007400 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007401 clear_tv(&tv);
7402 goto failret;
7403 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007404 item = dictitem_alloc(key);
7405 clear_tv(&tvkey);
7406 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007408 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007409 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007410 if (dict_add(d, item) == FAIL)
7411 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007412 }
7413 }
7414
7415 if (**arg == '}')
7416 break;
7417 if (**arg != ',')
7418 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007419 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420 goto failret;
7421 }
7422 *arg = skipwhite(*arg + 1);
7423 }
7424
7425 if (**arg != '}')
7426 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007427 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428failret:
7429 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007430 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007431 return FAIL;
7432 }
7433
7434 *arg = skipwhite(*arg + 1);
7435 if (evaluate)
7436 {
7437 rettv->v_type = VAR_DICT;
7438 rettv->vval.v_dict = d;
7439 ++d->dv_refcount;
7440 }
7441
7442 return OK;
7443}
7444
Bram Moolenaar8c711452005-01-14 21:53:12 +00007445/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007446 * Return a string with the string representation of a variable.
7447 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007448 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007449 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007450 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007451 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007452 */
7453 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007454echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007455 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007456 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007457 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007458 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007459{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007460 static int recurse = 0;
7461 char_u *r = NULL;
7462
Bram Moolenaar33570922005-01-25 22:26:29 +00007463 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007464 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007465 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007466 *tofree = NULL;
7467 return NULL;
7468 }
7469 ++recurse;
7470
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007471 switch (tv->v_type)
7472 {
7473 case VAR_FUNC:
7474 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007475 r = tv->vval.v_string;
7476 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007477
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007478 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007479 if (tv->vval.v_list == NULL)
7480 {
7481 *tofree = NULL;
7482 r = NULL;
7483 }
7484 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7485 {
7486 *tofree = NULL;
7487 r = (char_u *)"[...]";
7488 }
7489 else
7490 {
7491 tv->vval.v_list->lv_copyID = copyID;
7492 *tofree = list2string(tv, copyID);
7493 r = *tofree;
7494 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007495 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007496
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007498 if (tv->vval.v_dict == NULL)
7499 {
7500 *tofree = NULL;
7501 r = NULL;
7502 }
7503 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7504 {
7505 *tofree = NULL;
7506 r = (char_u *)"{...}";
7507 }
7508 else
7509 {
7510 tv->vval.v_dict->dv_copyID = copyID;
7511 *tofree = dict2string(tv, copyID);
7512 r = *tofree;
7513 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007514 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007515
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007516 case VAR_STRING:
7517 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007518 *tofree = NULL;
7519 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007520 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007521
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007522#ifdef FEAT_FLOAT
7523 case VAR_FLOAT:
7524 *tofree = NULL;
7525 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7526 r = numbuf;
7527 break;
7528#endif
7529
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007530 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007531 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007532 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007533 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007534
7535 --recurse;
7536 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007537}
7538
7539/*
7540 * Return a string with the string representation of a variable.
7541 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7542 * "numbuf" is used for a number.
7543 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007544 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007545 */
7546 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007547tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007548 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007549 char_u **tofree;
7550 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007551 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007552{
7553 switch (tv->v_type)
7554 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007555 case VAR_FUNC:
7556 *tofree = string_quote(tv->vval.v_string, TRUE);
7557 return *tofree;
7558 case VAR_STRING:
7559 *tofree = string_quote(tv->vval.v_string, FALSE);
7560 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007561#ifdef FEAT_FLOAT
7562 case VAR_FLOAT:
7563 *tofree = NULL;
7564 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7565 return numbuf;
7566#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007567 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007568 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007569 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007570 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007571 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007572 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007573 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007574 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007575}
7576
7577/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007578 * Return string "str" in ' quotes, doubling ' characters.
7579 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007581 */
7582 static char_u *
7583string_quote(str, function)
7584 char_u *str;
7585 int function;
7586{
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007588 char_u *p, *r, *s;
7589
Bram Moolenaar33570922005-01-25 22:26:29 +00007590 len = (function ? 13 : 3);
7591 if (str != NULL)
7592 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007593 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007594 for (p = str; *p != NUL; mb_ptr_adv(p))
7595 if (*p == '\'')
7596 ++len;
7597 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007598 s = r = alloc(len);
7599 if (r != NULL)
7600 {
7601 if (function)
7602 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007603 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007604 r += 10;
7605 }
7606 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007608 if (str != NULL)
7609 for (p = str; *p != NUL; )
7610 {
7611 if (*p == '\'')
7612 *r++ = '\'';
7613 MB_COPY_CHAR(p, r);
7614 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007615 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007616 if (function)
7617 *r++ = ')';
7618 *r++ = NUL;
7619 }
7620 return s;
7621}
7622
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007623#ifdef FEAT_FLOAT
7624/*
7625 * Convert the string "text" to a floating point number.
7626 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7627 * this always uses a decimal point.
7628 * Returns the length of the text that was consumed.
7629 */
7630 static int
7631string2float(text, value)
7632 char_u *text;
7633 float_T *value; /* result stored here */
7634{
7635 char *s = (char *)text;
7636 float_T f;
7637
7638 f = strtod(s, &s);
7639 *value = f;
7640 return (int)((char_u *)s - text);
7641}
7642#endif
7643
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007644/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 * Get the value of an environment variable.
7646 * "arg" is pointing to the '$'. It is advanced to after the name.
7647 * If the environment variable was not set, silently assume it is empty.
7648 * Always return OK.
7649 */
7650 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007651get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 int evaluate;
7655{
7656 char_u *string = NULL;
7657 int len;
7658 int cc;
7659 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007660 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661
7662 ++*arg;
7663 name = *arg;
7664 len = get_env_len(arg);
7665 if (evaluate)
7666 {
7667 if (len != 0)
7668 {
7669 cc = name[len];
7670 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007671 /* first try vim_getenv(), fast for normal environment vars */
7672 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007674 {
7675 if (!mustfree)
7676 string = vim_strsave(string);
7677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 else
7679 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007680 if (mustfree)
7681 vim_free(string);
7682
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 /* next try expanding things like $VIM and ${HOME} */
7684 string = expand_env_save(name - 1);
7685 if (string != NULL && *string == '$')
7686 {
7687 vim_free(string);
7688 string = NULL;
7689 }
7690 }
7691 name[len] = cc;
7692 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007693 rettv->v_type = VAR_STRING;
7694 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 }
7696
7697 return OK;
7698}
7699
7700/*
7701 * Array with names and number of arguments of all internal functions
7702 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7703 */
7704static struct fst
7705{
7706 char *f_name; /* function name */
7707 char f_min_argc; /* minimal number of arguments */
7708 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007710 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711} functions[] =
7712{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007713#ifdef FEAT_FLOAT
7714 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007715 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007716#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007717 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 {"append", 2, 2, f_append},
7719 {"argc", 0, 0, f_argc},
7720 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007721 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007722#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007723 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007724 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007725 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007728 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 {"bufexists", 1, 1, f_bufexists},
7730 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7731 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7732 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7733 {"buflisted", 1, 1, f_buflisted},
7734 {"bufloaded", 1, 1, f_bufloaded},
7735 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007736 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 {"bufwinnr", 1, 1, f_bufwinnr},
7738 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007739 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007740 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007741#ifdef FEAT_FLOAT
7742 {"ceil", 1, 1, f_ceil},
7743#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007744 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 {"char2nr", 1, 1, f_char2nr},
7746 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007747 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007749#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007750 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007751 {"complete_add", 1, 1, f_complete_add},
7752 {"complete_check", 0, 0, f_complete_check},
7753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007755 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007756#ifdef FEAT_FLOAT
7757 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007758 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007759#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007760 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007762 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007763 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 {"delete", 1, 1, f_delete},
7765 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007766 {"diff_filler", 1, 1, f_diff_filler},
7767 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007768 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007770 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 {"eventhandler", 0, 0, f_eventhandler},
7772 {"executable", 1, 1, f_executable},
7773 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007774#ifdef FEAT_FLOAT
7775 {"exp", 1, 1, f_exp},
7776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007778 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007779 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7781 {"filereadable", 1, 1, f_filereadable},
7782 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007783 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007784 {"finddir", 1, 3, f_finddir},
7785 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007786#ifdef FEAT_FLOAT
7787 {"float2nr", 1, 1, f_float2nr},
7788 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007789 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007790#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007791 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 {"fnamemodify", 2, 2, f_fnamemodify},
7793 {"foldclosed", 1, 1, f_foldclosed},
7794 {"foldclosedend", 1, 1, f_foldclosedend},
7795 {"foldlevel", 1, 1, f_foldlevel},
7796 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007797 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007799 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007800 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007801 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007802 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 {"getbufvar", 2, 2, f_getbufvar},
7804 {"getchar", 0, 1, f_getchar},
7805 {"getcharmod", 0, 0, f_getcharmod},
7806 {"getcmdline", 0, 0, f_getcmdline},
7807 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007808 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007810 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007811 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 {"getfsize", 1, 1, f_getfsize},
7813 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007814 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007815 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007816 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007817 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007818 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007819 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007820 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007821 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007823 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007824 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 {"getwinposx", 0, 0, f_getwinposx},
7826 {"getwinposy", 0, 0, f_getwinposy},
7827 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007828 {"glob", 1, 2, f_glob},
7829 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007831 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007832 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007833 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7835 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7836 {"histadd", 2, 2, f_histadd},
7837 {"histdel", 1, 2, f_histdel},
7838 {"histget", 1, 2, f_histget},
7839 {"histnr", 1, 1, f_histnr},
7840 {"hlID", 1, 1, f_hlID},
7841 {"hlexists", 1, 1, f_hlexists},
7842 {"hostname", 0, 0, f_hostname},
7843 {"iconv", 3, 3, f_iconv},
7844 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007845 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007846 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007848 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 {"inputrestore", 0, 0, f_inputrestore},
7850 {"inputsave", 0, 0, f_inputsave},
7851 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007852 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007854 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007855 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007856 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007857 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007859 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 {"libcall", 3, 3, f_libcall},
7861 {"libcallnr", 3, 3, f_libcallnr},
7862 {"line", 1, 1, f_line},
7863 {"line2byte", 1, 1, f_line2byte},
7864 {"lispindent", 1, 1, f_lispindent},
7865 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007866#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007867 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007868 {"log10", 1, 1, f_log10},
7869#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007870 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007871 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007872 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007873 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007874 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007875 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007876 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007877 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007878 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007879 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007880 {"max", 1, 1, f_max},
7881 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007882#ifdef vim_mkdir
7883 {"mkdir", 1, 3, f_mkdir},
7884#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007885 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007886#ifdef FEAT_MZSCHEME
7887 {"mzeval", 1, 1, f_mzeval},
7888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 {"nextnonblank", 1, 1, f_nextnonblank},
7890 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007891 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007892#ifdef FEAT_FLOAT
7893 {"pow", 2, 2, f_pow},
7894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007896 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007897 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007898 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007899 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007900 {"reltime", 0, 2, f_reltime},
7901 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 {"remote_expr", 2, 3, f_remote_expr},
7903 {"remote_foreground", 1, 1, f_remote_foreground},
7904 {"remote_peek", 1, 2, f_remote_peek},
7905 {"remote_read", 1, 1, f_remote_read},
7906 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007907 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007909 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007911 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007912#ifdef FEAT_FLOAT
7913 {"round", 1, 1, f_round},
7914#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007915 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007916 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007917 {"searchpair", 3, 7, f_searchpair},
7918 {"searchpairpos", 3, 7, f_searchpairpos},
7919 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 {"server2client", 2, 2, f_server2client},
7921 {"serverlist", 0, 0, f_serverlist},
7922 {"setbufvar", 3, 3, f_setbufvar},
7923 {"setcmdpos", 1, 1, f_setcmdpos},
7924 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007925 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007926 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007927 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007928 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007930 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007931 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007933 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007935#ifdef FEAT_FLOAT
7936 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007937 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007938#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02007939 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007940 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007941 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007942 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007943 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007944#ifdef FEAT_FLOAT
7945 {"sqrt", 1, 1, f_sqrt},
7946 {"str2float", 1, 1, f_str2float},
7947#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007948 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007949 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007950 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951#ifdef HAVE_STRFTIME
7952 {"strftime", 1, 2, f_strftime},
7953#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007954 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007955 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 {"strlen", 1, 1, f_strlen},
7957 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007958 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007960 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {"submatch", 1, 1, f_submatch},
7962 {"substitute", 4, 4, f_substitute},
7963 {"synID", 3, 3, f_synID},
7964 {"synIDattr", 2, 3, f_synIDattr},
7965 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007966 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007967 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007968 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007969 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007970 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007971 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007972 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007973 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007974#ifdef FEAT_FLOAT
7975 {"tan", 1, 1, f_tan},
7976 {"tanh", 1, 1, f_tanh},
7977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007979 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 {"tolower", 1, 1, f_tolower},
7981 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007982 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007983#ifdef FEAT_FLOAT
7984 {"trunc", 1, 1, f_trunc},
7985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007987 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007988 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007989 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {"virtcol", 1, 1, f_virtcol},
7991 {"visualmode", 0, 1, f_visualmode},
7992 {"winbufnr", 1, 1, f_winbufnr},
7993 {"wincol", 0, 0, f_wincol},
7994 {"winheight", 1, 1, f_winheight},
7995 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007996 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007998 {"winrestview", 1, 1, f_winrestview},
7999 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008001 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002};
8003
8004#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8005
8006/*
8007 * Function given to ExpandGeneric() to obtain the list of internal
8008 * or user defined function names.
8009 */
8010 char_u *
8011get_function_name(xp, idx)
8012 expand_T *xp;
8013 int idx;
8014{
8015 static int intidx = -1;
8016 char_u *name;
8017
8018 if (idx == 0)
8019 intidx = -1;
8020 if (intidx < 0)
8021 {
8022 name = get_user_func_name(xp, idx);
8023 if (name != NULL)
8024 return name;
8025 }
8026 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8027 {
8028 STRCPY(IObuff, functions[intidx].f_name);
8029 STRCAT(IObuff, "(");
8030 if (functions[intidx].f_max_argc == 0)
8031 STRCAT(IObuff, ")");
8032 return IObuff;
8033 }
8034
8035 return NULL;
8036}
8037
8038/*
8039 * Function given to ExpandGeneric() to obtain the list of internal or
8040 * user defined variable or function names.
8041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 char_u *
8043get_expr_name(xp, idx)
8044 expand_T *xp;
8045 int idx;
8046{
8047 static int intidx = -1;
8048 char_u *name;
8049
8050 if (idx == 0)
8051 intidx = -1;
8052 if (intidx < 0)
8053 {
8054 name = get_function_name(xp, idx);
8055 if (name != NULL)
8056 return name;
8057 }
8058 return get_user_var_name(xp, ++intidx);
8059}
8060
8061#endif /* FEAT_CMDL_COMPL */
8062
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008063#if defined(EBCDIC) || defined(PROTO)
8064/*
8065 * Compare struct fst by function name.
8066 */
8067 static int
8068compare_func_name(s1, s2)
8069 const void *s1;
8070 const void *s2;
8071{
8072 struct fst *p1 = (struct fst *)s1;
8073 struct fst *p2 = (struct fst *)s2;
8074
8075 return STRCMP(p1->f_name, p2->f_name);
8076}
8077
8078/*
8079 * Sort the function table by function name.
8080 * The sorting of the table above is ASCII dependant.
8081 * On machines using EBCDIC we have to sort it.
8082 */
8083 static void
8084sortFunctions()
8085{
8086 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8087
8088 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8089}
8090#endif
8091
8092
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093/*
8094 * Find internal function in table above.
8095 * Return index, or -1 if not found
8096 */
8097 static int
8098find_internal_func(name)
8099 char_u *name; /* name of the function */
8100{
8101 int first = 0;
8102 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8103 int cmp;
8104 int x;
8105
8106 /*
8107 * Find the function name in the table. Binary search.
8108 */
8109 while (first <= last)
8110 {
8111 x = first + ((unsigned)(last - first) >> 1);
8112 cmp = STRCMP(name, functions[x].f_name);
8113 if (cmp < 0)
8114 last = x - 1;
8115 else if (cmp > 0)
8116 first = x + 1;
8117 else
8118 return x;
8119 }
8120 return -1;
8121}
8122
8123/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008124 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8125 * name it contains, otherwise return "name".
8126 */
8127 static char_u *
8128deref_func_name(name, lenp)
8129 char_u *name;
8130 int *lenp;
8131{
Bram Moolenaar33570922005-01-25 22:26:29 +00008132 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008133 int cc;
8134
8135 cc = name[*lenp];
8136 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008137 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008138 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008139 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008140 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008141 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008142 {
8143 *lenp = 0;
8144 return (char_u *)""; /* just in case */
8145 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008146 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008147 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008148 }
8149
8150 return name;
8151}
8152
8153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 * Allocate a variable for the result of a function.
8155 * Return OK or FAIL.
8156 */
8157 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008158get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8159 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 char_u *name; /* name of the function */
8161 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 char_u **arg; /* argument, pointing to the '(' */
8164 linenr_T firstline; /* first line of range */
8165 linenr_T lastline; /* last line of range */
8166 int *doesrange; /* return: function handled range */
8167 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008168 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169{
8170 char_u *argp;
8171 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008172 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 int argcount = 0; /* number of arguments found */
8174
8175 /*
8176 * Get the arguments.
8177 */
8178 argp = *arg;
8179 while (argcount < MAX_FUNC_ARGS)
8180 {
8181 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8182 if (*argp == ')' || *argp == ',' || *argp == NUL)
8183 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8185 {
8186 ret = FAIL;
8187 break;
8188 }
8189 ++argcount;
8190 if (*argp != ',')
8191 break;
8192 }
8193 if (*argp == ')')
8194 ++argp;
8195 else
8196 ret = FAIL;
8197
8198 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008199 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008200 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008202 {
8203 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008204 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008205 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008206 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208
8209 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008210 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211
8212 *arg = skipwhite(argp);
8213 return ret;
8214}
8215
8216
8217/*
8218 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008219 * Return OK when the function can't be called, FAIL otherwise.
8220 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 */
8222 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008223call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008224 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008225 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008227 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008229 typval_T *argvars; /* vars for arguments, must have "argcount"
8230 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 linenr_T firstline; /* first line of range */
8232 linenr_T lastline; /* last line of range */
8233 int *doesrange; /* return: function handled range */
8234 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008235 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236{
8237 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238#define ERROR_UNKNOWN 0
8239#define ERROR_TOOMANY 1
8240#define ERROR_TOOFEW 2
8241#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008242#define ERROR_DICT 4
8243#define ERROR_NONE 5
8244#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 int error = ERROR_NONE;
8246 int i;
8247 int llen;
8248 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249#define FLEN_FIXED 40
8250 char_u fname_buf[FLEN_FIXED + 1];
8251 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008252 char_u *name;
8253
8254 /* Make a copy of the name, if it comes from a funcref variable it could
8255 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008256 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008257 if (name == NULL)
8258 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259
8260 /*
8261 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8262 * Change <SNR>123_name() to K_SNR 123_name().
8263 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 llen = eval_fname_script(name);
8266 if (llen > 0)
8267 {
8268 fname_buf[0] = K_SPECIAL;
8269 fname_buf[1] = KS_EXTRA;
8270 fname_buf[2] = (int)KE_SNR;
8271 i = 3;
8272 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8273 {
8274 if (current_SID <= 0)
8275 error = ERROR_SCRIPT;
8276 else
8277 {
8278 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8279 i = (int)STRLEN(fname_buf);
8280 }
8281 }
8282 if (i + STRLEN(name + llen) < FLEN_FIXED)
8283 {
8284 STRCPY(fname_buf + i, name + llen);
8285 fname = fname_buf;
8286 }
8287 else
8288 {
8289 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8290 if (fname == NULL)
8291 error = ERROR_OTHER;
8292 else
8293 {
8294 mch_memmove(fname, fname_buf, (size_t)i);
8295 STRCPY(fname + i, name + llen);
8296 }
8297 }
8298 }
8299 else
8300 fname = name;
8301
8302 *doesrange = FALSE;
8303
8304
8305 /* execute the function if no errors detected and executing */
8306 if (evaluate && error == ERROR_NONE)
8307 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008308 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8309 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 error = ERROR_UNKNOWN;
8311
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008312 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 {
8314 /*
8315 * User defined function.
8316 */
8317 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008318
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008320 /* Trigger FuncUndefined event, may load the function. */
8321 if (fp == NULL
8322 && apply_autocmds(EVENT_FUNCUNDEFINED,
8323 fname, fname, TRUE, NULL)
8324 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008326 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 fp = find_func(fname);
8328 }
8329#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008330 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008331 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008332 {
8333 /* loaded a package, search for the function again */
8334 fp = find_func(fname);
8335 }
8336
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 if (fp != NULL)
8338 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008339 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008341 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008343 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008345 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008346 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 else
8348 {
8349 /*
8350 * Call the user function.
8351 * Save and restore search patterns, script variables and
8352 * redo buffer.
8353 */
8354 save_search_patterns();
8355 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008356 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008357 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008358 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008359 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8360 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8361 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008362 /* Function was unreferenced while being used, free it
8363 * now. */
8364 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 restoreRedobuff();
8366 restore_search_patterns();
8367 error = ERROR_NONE;
8368 }
8369 }
8370 }
8371 else
8372 {
8373 /*
8374 * Find the function name in the table, call its implementation.
8375 */
8376 i = find_internal_func(fname);
8377 if (i >= 0)
8378 {
8379 if (argcount < functions[i].f_min_argc)
8380 error = ERROR_TOOFEW;
8381 else if (argcount > functions[i].f_max_argc)
8382 error = ERROR_TOOMANY;
8383 else
8384 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008385 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008386 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 error = ERROR_NONE;
8388 }
8389 }
8390 }
8391 /*
8392 * The function call (or "FuncUndefined" autocommand sequence) might
8393 * have been aborted by an error, an interrupt, or an explicitly thrown
8394 * exception that has not been caught so far. This situation can be
8395 * tested for by calling aborting(). For an error in an internal
8396 * function or for the "E132" error in call_user_func(), however, the
8397 * throw point at which the "force_abort" flag (temporarily reset by
8398 * emsg()) is normally updated has not been reached yet. We need to
8399 * update that flag first to make aborting() reliable.
8400 */
8401 update_force_abort();
8402 }
8403 if (error == ERROR_NONE)
8404 ret = OK;
8405
8406 /*
8407 * Report an error unless the argument evaluation or function call has been
8408 * cancelled due to an aborting error, an interrupt, or an exception.
8409 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008410 if (!aborting())
8411 {
8412 switch (error)
8413 {
8414 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008415 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008416 break;
8417 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008418 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008419 break;
8420 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008421 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008422 name);
8423 break;
8424 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008425 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008426 name);
8427 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008428 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008429 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008430 name);
8431 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008432 }
8433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 if (fname != name && fname != fname_buf)
8436 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008437 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438
8439 return ret;
8440}
8441
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008442/*
8443 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008444 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008445 */
8446 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008447emsg_funcname(ermsg, name)
8448 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008449 char_u *name;
8450{
8451 char_u *p;
8452
8453 if (*name == K_SPECIAL)
8454 p = concat_str((char_u *)"<SNR>", name + 3);
8455 else
8456 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008457 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008458 if (p != name)
8459 vim_free(p);
8460}
8461
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008462/*
8463 * Return TRUE for a non-zero Number and a non-empty String.
8464 */
8465 static int
8466non_zero_arg(argvars)
8467 typval_T *argvars;
8468{
8469 return ((argvars[0].v_type == VAR_NUMBER
8470 && argvars[0].vval.v_number != 0)
8471 || (argvars[0].v_type == VAR_STRING
8472 && argvars[0].vval.v_string != NULL
8473 && *argvars[0].vval.v_string != NUL));
8474}
8475
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476/*********************************************
8477 * Implementation of the built-in functions
8478 */
8479
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008480#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008481static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8482
8483/*
8484 * Get the float value of "argvars[0]" into "f".
8485 * Returns FAIL when the argument is not a Number or Float.
8486 */
8487 static int
8488get_float_arg(argvars, f)
8489 typval_T *argvars;
8490 float_T *f;
8491{
8492 if (argvars[0].v_type == VAR_FLOAT)
8493 {
8494 *f = argvars[0].vval.v_float;
8495 return OK;
8496 }
8497 if (argvars[0].v_type == VAR_NUMBER)
8498 {
8499 *f = (float_T)argvars[0].vval.v_number;
8500 return OK;
8501 }
8502 EMSG(_("E808: Number or Float required"));
8503 return FAIL;
8504}
8505
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008506/*
8507 * "abs(expr)" function
8508 */
8509 static void
8510f_abs(argvars, rettv)
8511 typval_T *argvars;
8512 typval_T *rettv;
8513{
8514 if (argvars[0].v_type == VAR_FLOAT)
8515 {
8516 rettv->v_type = VAR_FLOAT;
8517 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8518 }
8519 else
8520 {
8521 varnumber_T n;
8522 int error = FALSE;
8523
8524 n = get_tv_number_chk(&argvars[0], &error);
8525 if (error)
8526 rettv->vval.v_number = -1;
8527 else if (n > 0)
8528 rettv->vval.v_number = n;
8529 else
8530 rettv->vval.v_number = -n;
8531 }
8532}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008533
8534/*
8535 * "acos()" function
8536 */
8537 static void
8538f_acos(argvars, rettv)
8539 typval_T *argvars;
8540 typval_T *rettv;
8541{
8542 float_T f;
8543
8544 rettv->v_type = VAR_FLOAT;
8545 if (get_float_arg(argvars, &f) == OK)
8546 rettv->vval.v_float = acos(f);
8547 else
8548 rettv->vval.v_float = 0.0;
8549}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008550#endif
8551
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008553 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 */
8555 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008556f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008557 typval_T *argvars;
8558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559{
Bram Moolenaar33570922005-01-25 22:26:29 +00008560 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008562 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008563 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008565 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008566 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008567 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008568 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008569 }
8570 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008571 EMSG(_(e_listreq));
8572}
8573
8574/*
8575 * "append(lnum, string/list)" function
8576 */
8577 static void
8578f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008579 typval_T *argvars;
8580 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008581{
8582 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008583 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008584 list_T *l = NULL;
8585 listitem_T *li = NULL;
8586 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008587 long added = 0;
8588
Bram Moolenaar0d660222005-01-07 21:51:51 +00008589 lnum = get_tv_lnum(argvars);
8590 if (lnum >= 0
8591 && lnum <= curbuf->b_ml.ml_line_count
8592 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008593 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008594 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008595 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008596 l = argvars[1].vval.v_list;
8597 if (l == NULL)
8598 return;
8599 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008600 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008601 for (;;)
8602 {
8603 if (l == NULL)
8604 tv = &argvars[1]; /* append a string */
8605 else if (li == NULL)
8606 break; /* end of list */
8607 else
8608 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008609 line = get_tv_string_chk(tv);
8610 if (line == NULL) /* type error */
8611 {
8612 rettv->vval.v_number = 1; /* Failed */
8613 break;
8614 }
8615 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008616 ++added;
8617 if (l == NULL)
8618 break;
8619 li = li->li_next;
8620 }
8621
8622 appended_lines_mark(lnum, added);
8623 if (curwin->w_cursor.lnum > lnum)
8624 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008626 else
8627 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628}
8629
8630/*
8631 * "argc()" function
8632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008634f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008635 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008638 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639}
8640
8641/*
8642 * "argidx()" function
8643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008645f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008646 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008649 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650}
8651
8652/*
8653 * "argv(nr)" function
8654 */
8655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008656f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008657 typval_T *argvars;
8658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659{
8660 int idx;
8661
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008662 if (argvars[0].v_type != VAR_UNKNOWN)
8663 {
8664 idx = get_tv_number_chk(&argvars[0], NULL);
8665 if (idx >= 0 && idx < ARGCOUNT)
8666 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8667 else
8668 rettv->vval.v_string = NULL;
8669 rettv->v_type = VAR_STRING;
8670 }
8671 else if (rettv_list_alloc(rettv) == OK)
8672 for (idx = 0; idx < ARGCOUNT; ++idx)
8673 list_append_string(rettv->vval.v_list,
8674 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675}
8676
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008677#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008678/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008679 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008680 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008681 static void
8682f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008683 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008684 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008685{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008686 float_T f;
8687
8688 rettv->v_type = VAR_FLOAT;
8689 if (get_float_arg(argvars, &f) == OK)
8690 rettv->vval.v_float = asin(f);
8691 else
8692 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008693}
8694
8695/*
8696 * "atan()" function
8697 */
8698 static void
8699f_atan(argvars, rettv)
8700 typval_T *argvars;
8701 typval_T *rettv;
8702{
8703 float_T f;
8704
8705 rettv->v_type = VAR_FLOAT;
8706 if (get_float_arg(argvars, &f) == OK)
8707 rettv->vval.v_float = atan(f);
8708 else
8709 rettv->vval.v_float = 0.0;
8710}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008711
8712/*
8713 * "atan2()" function
8714 */
8715 static void
8716f_atan2(argvars, rettv)
8717 typval_T *argvars;
8718 typval_T *rettv;
8719{
8720 float_T fx, fy;
8721
8722 rettv->v_type = VAR_FLOAT;
8723 if (get_float_arg(argvars, &fx) == OK
8724 && get_float_arg(&argvars[1], &fy) == OK)
8725 rettv->vval.v_float = atan2(fx, fy);
8726 else
8727 rettv->vval.v_float = 0.0;
8728}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008729#endif
8730
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731/*
8732 * "browse(save, title, initdir, default)" function
8733 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008735f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008736 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738{
8739#ifdef FEAT_BROWSE
8740 int save;
8741 char_u *title;
8742 char_u *initdir;
8743 char_u *defname;
8744 char_u buf[NUMBUFLEN];
8745 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008746 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008748 save = get_tv_number_chk(&argvars[0], &error);
8749 title = get_tv_string_chk(&argvars[1]);
8750 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8751 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008753 if (error || title == NULL || initdir == NULL || defname == NULL)
8754 rettv->vval.v_string = NULL;
8755 else
8756 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008757 do_browse(save ? BROWSE_SAVE : 0,
8758 title, defname, NULL, initdir, NULL, curbuf);
8759#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008760 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008761#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008762 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008763}
8764
8765/*
8766 * "browsedir(title, initdir)" function
8767 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008769f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008770 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008771 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008772{
8773#ifdef FEAT_BROWSE
8774 char_u *title;
8775 char_u *initdir;
8776 char_u buf[NUMBUFLEN];
8777
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008778 title = get_tv_string_chk(&argvars[0]);
8779 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008781 if (title == NULL || initdir == NULL)
8782 rettv->vval.v_string = NULL;
8783 else
8784 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008785 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008787 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008789 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790}
8791
Bram Moolenaar33570922005-01-25 22:26:29 +00008792static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008793
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794/*
8795 * Find a buffer by number or exact name.
8796 */
8797 static buf_T *
8798find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008799 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800{
8801 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008803 if (avar->v_type == VAR_NUMBER)
8804 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008805 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008807 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008808 if (buf == NULL)
8809 {
8810 /* No full path name match, try a match with a URL or a "nofile"
8811 * buffer, these don't use the full path. */
8812 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8813 if (buf->b_fname != NULL
8814 && (path_with_url(buf->b_fname)
8815#ifdef FEAT_QUICKFIX
8816 || bt_nofile(buf)
8817#endif
8818 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008819 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008820 break;
8821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 }
8823 return buf;
8824}
8825
8826/*
8827 * "bufexists(expr)" function
8828 */
8829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008830f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008831 typval_T *argvars;
8832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008834 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835}
8836
8837/*
8838 * "buflisted(expr)" function
8839 */
8840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008841f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008842 typval_T *argvars;
8843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844{
8845 buf_T *buf;
8846
8847 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008848 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849}
8850
8851/*
8852 * "bufloaded(expr)" function
8853 */
8854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008855f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008856 typval_T *argvars;
8857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858{
8859 buf_T *buf;
8860
8861 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008862 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863}
8864
Bram Moolenaar33570922005-01-25 22:26:29 +00008865static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008866
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867/*
8868 * Get buffer by number or pattern.
8869 */
8870 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008871get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008872 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008874 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 int save_magic;
8876 char_u *save_cpo;
8877 buf_T *buf;
8878
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008879 if (tv->v_type == VAR_NUMBER)
8880 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008881 if (tv->v_type != VAR_STRING)
8882 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 if (name == NULL || *name == NUL)
8884 return curbuf;
8885 if (name[0] == '$' && name[1] == NUL)
8886 return lastbuf;
8887
8888 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8889 save_magic = p_magic;
8890 p_magic = TRUE;
8891 save_cpo = p_cpo;
8892 p_cpo = (char_u *)"";
8893
8894 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8895 TRUE, FALSE));
8896
8897 p_magic = save_magic;
8898 p_cpo = save_cpo;
8899
8900 /* If not found, try expanding the name, like done for bufexists(). */
8901 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008902 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903
8904 return buf;
8905}
8906
8907/*
8908 * "bufname(expr)" function
8909 */
8910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008911f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008912 typval_T *argvars;
8913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914{
8915 buf_T *buf;
8916
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008917 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008919 buf = get_buf_tv(&argvars[0]);
8920 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008922 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008924 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 --emsg_off;
8926}
8927
8928/*
8929 * "bufnr(expr)" function
8930 */
8931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008932f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008933 typval_T *argvars;
8934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935{
8936 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008937 int error = FALSE;
8938 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008940 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008942 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008943 --emsg_off;
8944
8945 /* If the buffer isn't found and the second argument is not zero create a
8946 * new buffer. */
8947 if (buf == NULL
8948 && argvars[1].v_type != VAR_UNKNOWN
8949 && get_tv_number_chk(&argvars[1], &error) != 0
8950 && !error
8951 && (name = get_tv_string_chk(&argvars[0])) != NULL
8952 && !error)
8953 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8954
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008956 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959}
8960
8961/*
8962 * "bufwinnr(nr)" function
8963 */
8964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008965f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008966 typval_T *argvars;
8967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968{
8969#ifdef FEAT_WINDOWS
8970 win_T *wp;
8971 int winnr = 0;
8972#endif
8973 buf_T *buf;
8974
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008975 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008977 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978#ifdef FEAT_WINDOWS
8979 for (wp = firstwin; wp; wp = wp->w_next)
8980 {
8981 ++winnr;
8982 if (wp->w_buffer == buf)
8983 break;
8984 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008985 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008987 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988#endif
8989 --emsg_off;
8990}
8991
8992/*
8993 * "byte2line(byte)" function
8994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008996f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008997 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999{
9000#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009001 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002#else
9003 long boff = 0;
9004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009005 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009007 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009009 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 (linenr_T)0, &boff);
9011#endif
9012}
9013
9014/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009015 * "byteidx()" function
9016 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009018f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009019 typval_T *argvars;
9020 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009021{
9022#ifdef FEAT_MBYTE
9023 char_u *t;
9024#endif
9025 char_u *str;
9026 long idx;
9027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009028 str = get_tv_string_chk(&argvars[0]);
9029 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009030 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009031 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009032 return;
9033
9034#ifdef FEAT_MBYTE
9035 t = str;
9036 for ( ; idx > 0; idx--)
9037 {
9038 if (*t == NUL) /* EOL reached */
9039 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009040 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009041 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009042 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009043#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009044 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009045 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009046#endif
9047}
9048
9049/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009050 * "call(func, arglist)" function
9051 */
9052 static void
9053f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009054 typval_T *argvars;
9055 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009056{
9057 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009058 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009059 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00009060 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009061 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00009062 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009063
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009064 if (argvars[1].v_type != VAR_LIST)
9065 {
9066 EMSG(_(e_listreq));
9067 return;
9068 }
9069 if (argvars[1].vval.v_list == NULL)
9070 return;
9071
9072 if (argvars[0].v_type == VAR_FUNC)
9073 func = argvars[0].vval.v_string;
9074 else
9075 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009076 if (*func == NUL)
9077 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009078
Bram Moolenaare9a41262005-01-15 22:18:47 +00009079 if (argvars[2].v_type != VAR_UNKNOWN)
9080 {
9081 if (argvars[2].v_type != VAR_DICT)
9082 {
9083 EMSG(_(e_dictreq));
9084 return;
9085 }
9086 selfdict = argvars[2].vval.v_dict;
9087 }
9088
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009089 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9090 item = item->li_next)
9091 {
9092 if (argc == MAX_FUNC_ARGS)
9093 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009094 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009095 break;
9096 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009097 /* Make a copy of each argument. This is needed to be able to set
9098 * v_lock to VAR_FIXED in the copy without changing the original list.
9099 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009100 copy_tv(&item->li_tv, &argv[argc++]);
9101 }
9102
9103 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009104 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009105 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9106 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009107
9108 /* Free the arguments. */
9109 while (argc > 0)
9110 clear_tv(&argv[--argc]);
9111}
9112
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009113#ifdef FEAT_FLOAT
9114/*
9115 * "ceil({float})" function
9116 */
9117 static void
9118f_ceil(argvars, rettv)
9119 typval_T *argvars;
9120 typval_T *rettv;
9121{
9122 float_T f;
9123
9124 rettv->v_type = VAR_FLOAT;
9125 if (get_float_arg(argvars, &f) == OK)
9126 rettv->vval.v_float = ceil(f);
9127 else
9128 rettv->vval.v_float = 0.0;
9129}
9130#endif
9131
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009132/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009133 * "changenr()" function
9134 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009135 static void
9136f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009137 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009138 typval_T *rettv;
9139{
9140 rettv->vval.v_number = curbuf->b_u_seq_cur;
9141}
9142
9143/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 * "char2nr(string)" function
9145 */
9146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009147f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009148 typval_T *argvars;
9149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150{
9151#ifdef FEAT_MBYTE
9152 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009153 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 else
9155#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009156 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157}
9158
9159/*
9160 * "cindent(lnum)" function
9161 */
9162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009163f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009164 typval_T *argvars;
9165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166{
9167#ifdef FEAT_CINDENT
9168 pos_T pos;
9169 linenr_T lnum;
9170
9171 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009172 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9174 {
9175 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009176 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 curwin->w_cursor = pos;
9178 }
9179 else
9180#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009181 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182}
9183
9184/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009185 * "clearmatches()" function
9186 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009187 static void
9188f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009189 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009190 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009191{
9192#ifdef FEAT_SEARCH_EXTRA
9193 clear_matches(curwin);
9194#endif
9195}
9196
9197/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 * "col(string)" function
9199 */
9200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009201f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009202 typval_T *argvars;
9203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204{
9205 colnr_T col = 0;
9206 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009207 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009209 fp = var2fpos(&argvars[0], FALSE, &fnum);
9210 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 {
9212 if (fp->col == MAXCOL)
9213 {
9214 /* '> can be MAXCOL, get the length of the line then */
9215 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009216 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 else
9218 col = MAXCOL;
9219 }
9220 else
9221 {
9222 col = fp->col + 1;
9223#ifdef FEAT_VIRTUALEDIT
9224 /* col(".") when the cursor is on the NUL at the end of the line
9225 * because of "coladd" can be seen as an extra column. */
9226 if (virtual_active() && fp == &curwin->w_cursor)
9227 {
9228 char_u *p = ml_get_cursor();
9229
9230 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9231 curwin->w_virtcol - curwin->w_cursor.coladd))
9232 {
9233# ifdef FEAT_MBYTE
9234 int l;
9235
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009236 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 col += l;
9238# else
9239 if (*p != NUL && p[1] == NUL)
9240 ++col;
9241# endif
9242 }
9243 }
9244#endif
9245 }
9246 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009247 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248}
9249
Bram Moolenaar572cb562005-08-05 21:35:02 +00009250#if defined(FEAT_INS_EXPAND)
9251/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009252 * "complete()" function
9253 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009254 static void
9255f_complete(argvars, rettv)
9256 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009257 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009258{
9259 int startcol;
9260
9261 if ((State & INSERT) == 0)
9262 {
9263 EMSG(_("E785: complete() can only be used in Insert mode"));
9264 return;
9265 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009266
9267 /* Check for undo allowed here, because if something was already inserted
9268 * the line was already saved for undo and this check isn't done. */
9269 if (!undo_allowed())
9270 return;
9271
Bram Moolenaarade00832006-03-10 21:46:58 +00009272 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9273 {
9274 EMSG(_(e_invarg));
9275 return;
9276 }
9277
9278 startcol = get_tv_number_chk(&argvars[0], NULL);
9279 if (startcol <= 0)
9280 return;
9281
9282 set_completion(startcol - 1, argvars[1].vval.v_list);
9283}
9284
9285/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009286 * "complete_add()" function
9287 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009288 static void
9289f_complete_add(argvars, rettv)
9290 typval_T *argvars;
9291 typval_T *rettv;
9292{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009293 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009294}
9295
9296/*
9297 * "complete_check()" function
9298 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009299 static void
9300f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009301 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009302 typval_T *rettv;
9303{
9304 int saved = RedrawingDisabled;
9305
9306 RedrawingDisabled = 0;
9307 ins_compl_check_keys(0);
9308 rettv->vval.v_number = compl_interrupted;
9309 RedrawingDisabled = saved;
9310}
9311#endif
9312
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313/*
9314 * "confirm(message, buttons[, default [, type]])" function
9315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009317f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009318 typval_T *argvars UNUSED;
9319 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320{
9321#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9322 char_u *message;
9323 char_u *buttons = NULL;
9324 char_u buf[NUMBUFLEN];
9325 char_u buf2[NUMBUFLEN];
9326 int def = 1;
9327 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009328 char_u *typestr;
9329 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009331 message = get_tv_string_chk(&argvars[0]);
9332 if (message == NULL)
9333 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009334 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009336 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9337 if (buttons == NULL)
9338 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009339 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009341 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009342 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009344 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9345 if (typestr == NULL)
9346 error = TRUE;
9347 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009349 switch (TOUPPER_ASC(*typestr))
9350 {
9351 case 'E': type = VIM_ERROR; break;
9352 case 'Q': type = VIM_QUESTION; break;
9353 case 'I': type = VIM_INFO; break;
9354 case 'W': type = VIM_WARNING; break;
9355 case 'G': type = VIM_GENERIC; break;
9356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 }
9358 }
9359 }
9360 }
9361
9362 if (buttons == NULL || *buttons == NUL)
9363 buttons = (char_u *)_("&Ok");
9364
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009365 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009366 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009367 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368#endif
9369}
9370
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009371/*
9372 * "copy()" function
9373 */
9374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009375f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009376 typval_T *argvars;
9377 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009378{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009379 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009380}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009382#ifdef FEAT_FLOAT
9383/*
9384 * "cos()" function
9385 */
9386 static void
9387f_cos(argvars, rettv)
9388 typval_T *argvars;
9389 typval_T *rettv;
9390{
9391 float_T f;
9392
9393 rettv->v_type = VAR_FLOAT;
9394 if (get_float_arg(argvars, &f) == OK)
9395 rettv->vval.v_float = cos(f);
9396 else
9397 rettv->vval.v_float = 0.0;
9398}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009399
9400/*
9401 * "cosh()" function
9402 */
9403 static void
9404f_cosh(argvars, rettv)
9405 typval_T *argvars;
9406 typval_T *rettv;
9407{
9408 float_T f;
9409
9410 rettv->v_type = VAR_FLOAT;
9411 if (get_float_arg(argvars, &f) == OK)
9412 rettv->vval.v_float = cosh(f);
9413 else
9414 rettv->vval.v_float = 0.0;
9415}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009416#endif
9417
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009419 * "count()" function
9420 */
9421 static void
9422f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009423 typval_T *argvars;
9424 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009425{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009426 long n = 0;
9427 int ic = FALSE;
9428
Bram Moolenaare9a41262005-01-15 22:18:47 +00009429 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009430 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009431 listitem_T *li;
9432 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009433 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009434
Bram Moolenaare9a41262005-01-15 22:18:47 +00009435 if ((l = argvars[0].vval.v_list) != NULL)
9436 {
9437 li = l->lv_first;
9438 if (argvars[2].v_type != VAR_UNKNOWN)
9439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009440 int error = FALSE;
9441
9442 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009443 if (argvars[3].v_type != VAR_UNKNOWN)
9444 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009445 idx = get_tv_number_chk(&argvars[3], &error);
9446 if (!error)
9447 {
9448 li = list_find(l, idx);
9449 if (li == NULL)
9450 EMSGN(_(e_listidx), idx);
9451 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009452 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009453 if (error)
9454 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009455 }
9456
9457 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009458 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009459 ++n;
9460 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009461 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009462 else if (argvars[0].v_type == VAR_DICT)
9463 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009464 int todo;
9465 dict_T *d;
9466 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009467
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009468 if ((d = argvars[0].vval.v_dict) != NULL)
9469 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009470 int error = FALSE;
9471
Bram Moolenaare9a41262005-01-15 22:18:47 +00009472 if (argvars[2].v_type != VAR_UNKNOWN)
9473 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009474 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009475 if (argvars[3].v_type != VAR_UNKNOWN)
9476 EMSG(_(e_invarg));
9477 }
9478
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009479 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009480 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009481 {
9482 if (!HASHITEM_EMPTY(hi))
9483 {
9484 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009485 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009486 ++n;
9487 }
9488 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009489 }
9490 }
9491 else
9492 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009493 rettv->vval.v_number = n;
9494}
9495
9496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9498 *
9499 * Checks the existence of a cscope connection.
9500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009502f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009503 typval_T *argvars UNUSED;
9504 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505{
9506#ifdef FEAT_CSCOPE
9507 int num = 0;
9508 char_u *dbpath = NULL;
9509 char_u *prepend = NULL;
9510 char_u buf[NUMBUFLEN];
9511
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009512 if (argvars[0].v_type != VAR_UNKNOWN
9513 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009515 num = (int)get_tv_number(&argvars[0]);
9516 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009517 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009518 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 }
9520
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009521 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522#endif
9523}
9524
9525/*
9526 * "cursor(lnum, col)" function
9527 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009528 * Moves the cursor to the specified line and column.
9529 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009532f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009533 typval_T *argvars;
9534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535{
9536 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009537#ifdef FEAT_VIRTUALEDIT
9538 long coladd = 0;
9539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009541 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009542 if (argvars[1].v_type == VAR_UNKNOWN)
9543 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009544 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009545
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009546 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009547 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009548 line = pos.lnum;
9549 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009550#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009551 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009552#endif
9553 }
9554 else
9555 {
9556 line = get_tv_lnum(argvars);
9557 col = get_tv_number_chk(&argvars[1], NULL);
9558#ifdef FEAT_VIRTUALEDIT
9559 if (argvars[2].v_type != VAR_UNKNOWN)
9560 coladd = get_tv_number_chk(&argvars[2], NULL);
9561#endif
9562 }
9563 if (line < 0 || col < 0
9564#ifdef FEAT_VIRTUALEDIT
9565 || coladd < 0
9566#endif
9567 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009568 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 if (line > 0)
9570 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 if (col > 0)
9572 curwin->w_cursor.col = col - 1;
9573#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009574 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575#endif
9576
9577 /* Make sure the cursor is in a valid position. */
9578 check_cursor();
9579#ifdef FEAT_MBYTE
9580 /* Correct cursor for multi-byte character. */
9581 if (has_mbyte)
9582 mb_adjust_cursor();
9583#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009584
9585 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009586 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587}
9588
9589/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009590 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 */
9592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009594 typval_T *argvars;
9595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009597 int noref = 0;
9598
9599 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009600 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009601 if (noref < 0 || noref > 1)
9602 EMSG(_(e_invarg));
9603 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009604 {
9605 current_copyID += COPYID_INC;
9606 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608}
9609
9610/*
9611 * "delete()" function
9612 */
9613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009614f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009615 typval_T *argvars;
9616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617{
9618 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009619 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009621 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622}
9623
9624/*
9625 * "did_filetype()" function
9626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009628f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009629 typval_T *argvars UNUSED;
9630 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631{
9632#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009633 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634#endif
9635}
9636
9637/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009638 * "diff_filler()" function
9639 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009641f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009642 typval_T *argvars UNUSED;
9643 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009644{
9645#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009646 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009647#endif
9648}
9649
9650/*
9651 * "diff_hlID()" function
9652 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009654f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009655 typval_T *argvars UNUSED;
9656 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009657{
9658#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009659 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009660 static linenr_T prev_lnum = 0;
9661 static int changedtick = 0;
9662 static int fnum = 0;
9663 static int change_start = 0;
9664 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009665 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009666 int filler_lines;
9667 int col;
9668
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009669 if (lnum < 0) /* ignore type error in {lnum} arg */
9670 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009671 if (lnum != prev_lnum
9672 || changedtick != curbuf->b_changedtick
9673 || fnum != curbuf->b_fnum)
9674 {
9675 /* New line, buffer, change: need to get the values. */
9676 filler_lines = diff_check(curwin, lnum);
9677 if (filler_lines < 0)
9678 {
9679 if (filler_lines == -1)
9680 {
9681 change_start = MAXCOL;
9682 change_end = -1;
9683 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9684 hlID = HLF_ADD; /* added line */
9685 else
9686 hlID = HLF_CHD; /* changed line */
9687 }
9688 else
9689 hlID = HLF_ADD; /* added line */
9690 }
9691 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009692 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009693 prev_lnum = lnum;
9694 changedtick = curbuf->b_changedtick;
9695 fnum = curbuf->b_fnum;
9696 }
9697
9698 if (hlID == HLF_CHD || hlID == HLF_TXD)
9699 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009700 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009701 if (col >= change_start && col <= change_end)
9702 hlID = HLF_TXD; /* changed text */
9703 else
9704 hlID = HLF_CHD; /* changed line */
9705 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009706 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009707#endif
9708}
9709
9710/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009711 * "empty({expr})" function
9712 */
9713 static void
9714f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009715 typval_T *argvars;
9716 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009717{
9718 int n;
9719
9720 switch (argvars[0].v_type)
9721 {
9722 case VAR_STRING:
9723 case VAR_FUNC:
9724 n = argvars[0].vval.v_string == NULL
9725 || *argvars[0].vval.v_string == NUL;
9726 break;
9727 case VAR_NUMBER:
9728 n = argvars[0].vval.v_number == 0;
9729 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009730#ifdef FEAT_FLOAT
9731 case VAR_FLOAT:
9732 n = argvars[0].vval.v_float == 0.0;
9733 break;
9734#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009735 case VAR_LIST:
9736 n = argvars[0].vval.v_list == NULL
9737 || argvars[0].vval.v_list->lv_first == NULL;
9738 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009739 case VAR_DICT:
9740 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009741 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009742 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009743 default:
9744 EMSG2(_(e_intern2), "f_empty()");
9745 n = 0;
9746 }
9747
9748 rettv->vval.v_number = n;
9749}
9750
9751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 * "escape({string}, {chars})" function
9753 */
9754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009755f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009756 typval_T *argvars;
9757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758{
9759 char_u buf[NUMBUFLEN];
9760
Bram Moolenaar758711c2005-02-02 23:11:38 +00009761 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9762 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009763 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764}
9765
9766/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009767 * "eval()" function
9768 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009769 static void
9770f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009771 typval_T *argvars;
9772 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009773{
9774 char_u *s;
9775
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009776 s = get_tv_string_chk(&argvars[0]);
9777 if (s != NULL)
9778 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009779
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009780 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9781 {
9782 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009783 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009784 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009785 else if (*s != NUL)
9786 EMSG(_(e_trailing));
9787}
9788
9789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 * "eventhandler()" function
9791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009793f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009794 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009797 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798}
9799
9800/*
9801 * "executable()" function
9802 */
9803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009804f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009805 typval_T *argvars;
9806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009808 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809}
9810
9811/*
9812 * "exists()" function
9813 */
9814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009815f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009816 typval_T *argvars;
9817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818{
9819 char_u *p;
9820 char_u *name;
9821 int n = FALSE;
9822 int len = 0;
9823
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009824 no_autoload = TRUE;
9825
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009826 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 if (*p == '$') /* environment variable */
9828 {
9829 /* first try "normal" environment variables (fast) */
9830 if (mch_getenv(p + 1) != NULL)
9831 n = TRUE;
9832 else
9833 {
9834 /* try expanding things like $VIM and ${HOME} */
9835 p = expand_env_save(p);
9836 if (p != NULL && *p != '$')
9837 n = TRUE;
9838 vim_free(p);
9839 }
9840 }
9841 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009842 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009843 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009844 if (*skipwhite(p) != NUL)
9845 n = FALSE; /* trailing garbage */
9846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 else if (*p == '*') /* internal or user defined function */
9848 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009849 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 }
9851 else if (*p == ':')
9852 {
9853 n = cmd_exists(p + 1);
9854 }
9855 else if (*p == '#')
9856 {
9857#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009858 if (p[1] == '#')
9859 n = autocmd_supported(p + 2);
9860 else
9861 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862#endif
9863 }
9864 else /* internal variable */
9865 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009866 char_u *tofree;
9867 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009869 /* get_name_len() takes care of expanding curly braces */
9870 name = p;
9871 len = get_name_len(&p, &tofree, TRUE, FALSE);
9872 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009874 if (tofree != NULL)
9875 name = tofree;
9876 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9877 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009879 /* handle d.key, l[idx], f(expr) */
9880 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9881 if (n)
9882 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 }
9884 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009885 if (*p != NUL)
9886 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009888 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 }
9890
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009891 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009892
9893 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894}
9895
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009896#ifdef FEAT_FLOAT
9897/*
9898 * "exp()" function
9899 */
9900 static void
9901f_exp(argvars, rettv)
9902 typval_T *argvars;
9903 typval_T *rettv;
9904{
9905 float_T f;
9906
9907 rettv->v_type = VAR_FLOAT;
9908 if (get_float_arg(argvars, &f) == OK)
9909 rettv->vval.v_float = exp(f);
9910 else
9911 rettv->vval.v_float = 0.0;
9912}
9913#endif
9914
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915/*
9916 * "expand()" function
9917 */
9918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009919f_expand(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 *s;
9924 int len;
9925 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009926 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009928 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009930 rettv->v_type = VAR_STRING;
9931 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 if (*s == '%' || *s == '#' || *s == '<')
9933 {
9934 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009935 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 --emsg_off;
9937 }
9938 else
9939 {
9940 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009941 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009942 if (argvars[1].v_type != VAR_UNKNOWN
9943 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009944 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009945 if (!error)
9946 {
9947 ExpandInit(&xpc);
9948 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009949 if (p_wic)
9950 options += WILD_ICASE;
9951 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, options, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009952 }
9953 else
9954 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 }
9956}
9957
9958/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009959 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009960 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009961 */
9962 static void
9963f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009964 typval_T *argvars;
9965 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009966{
Bram Moolenaar32f649e2011-04-11 13:46:13 +02009967 char *arg_errmsg = N_("extend() argument");
9968
Bram Moolenaare9a41262005-01-15 22:18:47 +00009969 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009970 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009971 list_T *l1, *l2;
9972 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009973 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009974 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009975
Bram Moolenaare9a41262005-01-15 22:18:47 +00009976 l1 = argvars[0].vval.v_list;
9977 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +02009978 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009979 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009980 {
9981 if (argvars[2].v_type != VAR_UNKNOWN)
9982 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009983 before = get_tv_number_chk(&argvars[2], &error);
9984 if (error)
9985 return; /* type error; errmsg already given */
9986
Bram Moolenaar758711c2005-02-02 23:11:38 +00009987 if (before == l1->lv_len)
9988 item = NULL;
9989 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009990 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009991 item = list_find(l1, before);
9992 if (item == NULL)
9993 {
9994 EMSGN(_(e_listidx), before);
9995 return;
9996 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009997 }
9998 }
9999 else
10000 item = NULL;
10001 list_extend(l1, l2, item);
10002
Bram Moolenaare9a41262005-01-15 22:18:47 +000010003 copy_tv(&argvars[0], rettv);
10004 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010005 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010006 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10007 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010008 dict_T *d1, *d2;
10009 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010010 char_u *action;
10011 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010012 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010013 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010014
10015 d1 = argvars[0].vval.v_dict;
10016 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010017 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010018 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010019 {
10020 /* Check the third argument. */
10021 if (argvars[2].v_type != VAR_UNKNOWN)
10022 {
10023 static char *(av[]) = {"keep", "force", "error"};
10024
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010025 action = get_tv_string_chk(&argvars[2]);
10026 if (action == NULL)
10027 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010028 for (i = 0; i < 3; ++i)
10029 if (STRCMP(action, av[i]) == 0)
10030 break;
10031 if (i == 3)
10032 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010033 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010034 return;
10035 }
10036 }
10037 else
10038 action = (char_u *)"force";
10039
10040 /* Go over all entries in the second dict and add them to the
10041 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010042 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010043 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010044 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010045 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010046 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010047 --todo;
10048 di1 = dict_find(d1, hi2->hi_key, -1);
10049 if (di1 == NULL)
10050 {
10051 di1 = dictitem_copy(HI2DI(hi2));
10052 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10053 dictitem_free(di1);
10054 }
10055 else if (*action == 'e')
10056 {
10057 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10058 break;
10059 }
10060 else if (*action == 'f')
10061 {
10062 clear_tv(&di1->di_tv);
10063 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10064 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010065 }
10066 }
10067
Bram Moolenaare9a41262005-01-15 22:18:47 +000010068 copy_tv(&argvars[0], rettv);
10069 }
10070 }
10071 else
10072 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010073}
10074
10075/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010076 * "feedkeys()" function
10077 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010078 static void
10079f_feedkeys(argvars, rettv)
10080 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010081 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010082{
10083 int remap = TRUE;
10084 char_u *keys, *flags;
10085 char_u nbuf[NUMBUFLEN];
10086 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010087 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010088
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010089 /* This is not allowed in the sandbox. If the commands would still be
10090 * executed in the sandbox it would be OK, but it probably happens later,
10091 * when "sandbox" is no longer set. */
10092 if (check_secure())
10093 return;
10094
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010095 keys = get_tv_string(&argvars[0]);
10096 if (*keys != NUL)
10097 {
10098 if (argvars[1].v_type != VAR_UNKNOWN)
10099 {
10100 flags = get_tv_string_buf(&argvars[1], nbuf);
10101 for ( ; *flags != NUL; ++flags)
10102 {
10103 switch (*flags)
10104 {
10105 case 'n': remap = FALSE; break;
10106 case 'm': remap = TRUE; break;
10107 case 't': typed = TRUE; break;
10108 }
10109 }
10110 }
10111
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010112 /* Need to escape K_SPECIAL and CSI before putting the string in the
10113 * typeahead buffer. */
10114 keys_esc = vim_strsave_escape_csi(keys);
10115 if (keys_esc != NULL)
10116 {
10117 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010118 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010119 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010120 if (vgetc_busy)
10121 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010122 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010123 }
10124}
10125
10126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 * "filereadable()" function
10128 */
10129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010130f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010131 typval_T *argvars;
10132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010134 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 char_u *p;
10136 int n;
10137
Bram Moolenaarc236c162008-07-13 17:41:49 +000010138#ifndef O_NONBLOCK
10139# define O_NONBLOCK 0
10140#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010141 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010142 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10143 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 {
10145 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010146 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 }
10148 else
10149 n = FALSE;
10150
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010151 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152}
10153
10154/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010155 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 * rights to write into.
10157 */
10158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010159f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010160 typval_T *argvars;
10161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010163 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164}
10165
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010166static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010167
10168 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010169findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010170 typval_T *argvars;
10171 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010172 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010173{
10174#ifdef FEAT_SEARCHPATH
10175 char_u *fname;
10176 char_u *fresult = NULL;
10177 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10178 char_u *p;
10179 char_u pathbuf[NUMBUFLEN];
10180 int count = 1;
10181 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010182 int error = FALSE;
10183#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010184
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010185 rettv->vval.v_string = NULL;
10186 rettv->v_type = VAR_STRING;
10187
10188#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010189 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010190
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010191 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010193 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10194 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010195 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010196 else
10197 {
10198 if (*p != NUL)
10199 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010200
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010201 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010202 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010203 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010204 }
10205
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010206 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10207 error = TRUE;
10208
10209 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010210 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010211 do
10212 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010213 if (rettv->v_type == VAR_STRING)
10214 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215 fresult = find_file_in_path_option(first ? fname : NULL,
10216 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010217 0, first, path,
10218 find_what,
10219 curbuf->b_ffname,
10220 find_what == FINDFILE_DIR
10221 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010222 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010223
10224 if (fresult != NULL && rettv->v_type == VAR_LIST)
10225 list_append_string(rettv->vval.v_list, fresult, -1);
10226
10227 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010228 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010229
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010230 if (rettv->v_type == VAR_STRING)
10231 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010232#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010233}
10234
Bram Moolenaar33570922005-01-25 22:26:29 +000010235static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10236static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010237
10238/*
10239 * Implementation of map() and filter().
10240 */
10241 static void
10242filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010243 typval_T *argvars;
10244 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010245 int map;
10246{
10247 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010248 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010249 listitem_T *li, *nli;
10250 list_T *l = NULL;
10251 dictitem_T *di;
10252 hashtab_T *ht;
10253 hashitem_T *hi;
10254 dict_T *d = NULL;
10255 typval_T save_val;
10256 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010257 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010258 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010259 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10260 char *arg_errmsg = (map ? N_("map() argument")
10261 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010262 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010263 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010264
Bram Moolenaare9a41262005-01-15 22:18:47 +000010265 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010266 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010267 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010268 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010269 return;
10270 }
10271 else if (argvars[0].v_type == VAR_DICT)
10272 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010273 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010274 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010275 return;
10276 }
10277 else
10278 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010279 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010280 return;
10281 }
10282
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010283 expr = get_tv_string_buf_chk(&argvars[1], buf);
10284 /* On type errors, the preceding call has already displayed an error
10285 * message. Avoid a misleading error message for an empty string that
10286 * was not passed as argument. */
10287 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010289 prepare_vimvar(VV_VAL, &save_val);
10290 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010291
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010292 /* We reset "did_emsg" to be able to detect whether an error
10293 * occurred during evaluation of the expression. */
10294 save_did_emsg = did_emsg;
10295 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010296
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010297 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010298 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010300 vimvars[VV_KEY].vv_type = VAR_STRING;
10301
10302 ht = &d->dv_hashtab;
10303 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010304 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010305 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010306 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010307 if (!HASHITEM_EMPTY(hi))
10308 {
10309 --todo;
10310 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010311 if (tv_check_lock(di->di_tv.v_lock,
10312 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010313 break;
10314 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010315 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010316 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010317 break;
10318 if (!map && rem)
10319 dictitem_remove(d, di);
10320 clear_tv(&vimvars[VV_KEY].vv_tv);
10321 }
10322 }
10323 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010324 }
10325 else
10326 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010327 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10328
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010329 for (li = l->lv_first; li != NULL; li = nli)
10330 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010331 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010332 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010333 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010334 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010335 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010336 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010337 break;
10338 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010339 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010340 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010341 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010342 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010343
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010344 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010345 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010346
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010347 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010348 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010349
10350 copy_tv(&argvars[0], rettv);
10351}
10352
10353 static int
10354filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010355 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010356 char_u *expr;
10357 int map;
10358 int *remp;
10359{
Bram Moolenaar33570922005-01-25 22:26:29 +000010360 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010361 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010362 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010363
Bram Moolenaar33570922005-01-25 22:26:29 +000010364 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010365 s = expr;
10366 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010367 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010368 if (*s != NUL) /* check for trailing chars after expr */
10369 {
10370 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010371 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010372 }
10373 if (map)
10374 {
10375 /* map(): replace the list item value */
10376 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010377 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010378 *tv = rettv;
10379 }
10380 else
10381 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010382 int error = FALSE;
10383
Bram Moolenaare9a41262005-01-15 22:18:47 +000010384 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010385 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010386 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010387 /* On type error, nothing has been removed; return FAIL to stop the
10388 * loop. The error message was given by get_tv_number_chk(). */
10389 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010390 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010391 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010392 retval = OK;
10393theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010394 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010395 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010396}
10397
10398/*
10399 * "filter()" function
10400 */
10401 static void
10402f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010403 typval_T *argvars;
10404 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010405{
10406 filter_map(argvars, rettv, FALSE);
10407}
10408
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010409/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010410 * "finddir({fname}[, {path}[, {count}]])" function
10411 */
10412 static void
10413f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010414 typval_T *argvars;
10415 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010416{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010417 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010418}
10419
10420/*
10421 * "findfile({fname}[, {path}[, {count}]])" function
10422 */
10423 static void
10424f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010425 typval_T *argvars;
10426 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010427{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010428 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010429}
10430
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010431#ifdef FEAT_FLOAT
10432/*
10433 * "float2nr({float})" function
10434 */
10435 static void
10436f_float2nr(argvars, rettv)
10437 typval_T *argvars;
10438 typval_T *rettv;
10439{
10440 float_T f;
10441
10442 if (get_float_arg(argvars, &f) == OK)
10443 {
10444 if (f < -0x7fffffff)
10445 rettv->vval.v_number = -0x7fffffff;
10446 else if (f > 0x7fffffff)
10447 rettv->vval.v_number = 0x7fffffff;
10448 else
10449 rettv->vval.v_number = (varnumber_T)f;
10450 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010451}
10452
10453/*
10454 * "floor({float})" function
10455 */
10456 static void
10457f_floor(argvars, rettv)
10458 typval_T *argvars;
10459 typval_T *rettv;
10460{
10461 float_T f;
10462
10463 rettv->v_type = VAR_FLOAT;
10464 if (get_float_arg(argvars, &f) == OK)
10465 rettv->vval.v_float = floor(f);
10466 else
10467 rettv->vval.v_float = 0.0;
10468}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010469
10470/*
10471 * "fmod()" function
10472 */
10473 static void
10474f_fmod(argvars, rettv)
10475 typval_T *argvars;
10476 typval_T *rettv;
10477{
10478 float_T fx, fy;
10479
10480 rettv->v_type = VAR_FLOAT;
10481 if (get_float_arg(argvars, &fx) == OK
10482 && get_float_arg(&argvars[1], &fy) == OK)
10483 rettv->vval.v_float = fmod(fx, fy);
10484 else
10485 rettv->vval.v_float = 0.0;
10486}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010487#endif
10488
Bram Moolenaar0d660222005-01-07 21:51:51 +000010489/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010490 * "fnameescape({string})" function
10491 */
10492 static void
10493f_fnameescape(argvars, rettv)
10494 typval_T *argvars;
10495 typval_T *rettv;
10496{
10497 rettv->vval.v_string = vim_strsave_fnameescape(
10498 get_tv_string(&argvars[0]), FALSE);
10499 rettv->v_type = VAR_STRING;
10500}
10501
10502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 * "fnamemodify({fname}, {mods})" function
10504 */
10505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010506f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010507 typval_T *argvars;
10508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509{
10510 char_u *fname;
10511 char_u *mods;
10512 int usedlen = 0;
10513 int len;
10514 char_u *fbuf = NULL;
10515 char_u buf[NUMBUFLEN];
10516
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010517 fname = get_tv_string_chk(&argvars[0]);
10518 mods = get_tv_string_buf_chk(&argvars[1], buf);
10519 if (fname == NULL || mods == NULL)
10520 fname = NULL;
10521 else
10522 {
10523 len = (int)STRLEN(fname);
10524 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010527 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010529 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010531 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 vim_free(fbuf);
10533}
10534
Bram Moolenaar33570922005-01-25 22:26:29 +000010535static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536
10537/*
10538 * "foldclosed()" function
10539 */
10540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010541foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010542 typval_T *argvars;
10543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 int end;
10545{
10546#ifdef FEAT_FOLDING
10547 linenr_T lnum;
10548 linenr_T first, last;
10549
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010550 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10552 {
10553 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10554 {
10555 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010556 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010558 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 return;
10560 }
10561 }
10562#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010563 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564}
10565
10566/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010567 * "foldclosed()" function
10568 */
10569 static void
10570f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010571 typval_T *argvars;
10572 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010573{
10574 foldclosed_both(argvars, rettv, FALSE);
10575}
10576
10577/*
10578 * "foldclosedend()" function
10579 */
10580 static void
10581f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010582 typval_T *argvars;
10583 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010584{
10585 foldclosed_both(argvars, rettv, TRUE);
10586}
10587
10588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 * "foldlevel()" function
10590 */
10591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010592f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010593 typval_T *argvars;
10594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595{
10596#ifdef FEAT_FOLDING
10597 linenr_T lnum;
10598
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010599 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010601 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603}
10604
10605/*
10606 * "foldtext()" function
10607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010609f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010610 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612{
10613#ifdef FEAT_FOLDING
10614 linenr_T lnum;
10615 char_u *s;
10616 char_u *r;
10617 int len;
10618 char *txt;
10619#endif
10620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010621 rettv->v_type = VAR_STRING;
10622 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010624 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10625 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10626 <= curbuf->b_ml.ml_line_count
10627 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628 {
10629 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010630 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10631 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 {
10633 if (!linewhite(lnum))
10634 break;
10635 ++lnum;
10636 }
10637
10638 /* Find interesting text in this line. */
10639 s = skipwhite(ml_get(lnum));
10640 /* skip C comment-start */
10641 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010642 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010644 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010645 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010646 {
10647 s = skipwhite(ml_get(lnum + 1));
10648 if (*s == '*')
10649 s = skipwhite(s + 1);
10650 }
10651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 txt = _("+-%s%3ld lines: ");
10653 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010654 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 + 20 /* for %3ld */
10656 + STRLEN(s))); /* concatenated */
10657 if (r != NULL)
10658 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010659 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10660 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10661 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 len = (int)STRLEN(r);
10663 STRCAT(r, s);
10664 /* remove 'foldmarker' and 'commentstring' */
10665 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010666 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 }
10668 }
10669#endif
10670}
10671
10672/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010673 * "foldtextresult(lnum)" function
10674 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010676f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010677 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010678 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010679{
10680#ifdef FEAT_FOLDING
10681 linenr_T lnum;
10682 char_u *text;
10683 char_u buf[51];
10684 foldinfo_T foldinfo;
10685 int fold_count;
10686#endif
10687
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010688 rettv->v_type = VAR_STRING;
10689 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010690#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010691 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010692 /* treat illegal types and illegal string values for {lnum} the same */
10693 if (lnum < 0)
10694 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010695 fold_count = foldedCount(curwin, lnum, &foldinfo);
10696 if (fold_count > 0)
10697 {
10698 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10699 &foldinfo, buf);
10700 if (text == buf)
10701 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010702 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010703 }
10704#endif
10705}
10706
10707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 * "foreground()" function
10709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010711f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010712 typval_T *argvars UNUSED;
10713 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715#ifdef FEAT_GUI
10716 if (gui.in_use)
10717 gui_mch_set_foreground();
10718#else
10719# ifdef WIN32
10720 win32_set_foreground();
10721# endif
10722#endif
10723}
10724
10725/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010726 * "function()" function
10727 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010729f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010730 typval_T *argvars;
10731 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010732{
10733 char_u *s;
10734
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010735 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010736 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010737 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010738 /* Don't check an autoload name for existence here. */
10739 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010740 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010741 else
10742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010743 rettv->vval.v_string = vim_strsave(s);
10744 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010745 }
10746}
10747
10748/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010749 * "garbagecollect()" function
10750 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010751 static void
10752f_garbagecollect(argvars, rettv)
10753 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010754 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010755{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010756 /* This is postponed until we are back at the toplevel, because we may be
10757 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10758 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010759
10760 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10761 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010762}
10763
10764/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010765 * "get()" function
10766 */
10767 static void
10768f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010769 typval_T *argvars;
10770 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010771{
Bram Moolenaar33570922005-01-25 22:26:29 +000010772 listitem_T *li;
10773 list_T *l;
10774 dictitem_T *di;
10775 dict_T *d;
10776 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010777
Bram Moolenaare9a41262005-01-15 22:18:47 +000010778 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010779 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010780 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010782 int error = FALSE;
10783
10784 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10785 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010786 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010787 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010788 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010789 else if (argvars[0].v_type == VAR_DICT)
10790 {
10791 if ((d = argvars[0].vval.v_dict) != NULL)
10792 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010793 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010794 if (di != NULL)
10795 tv = &di->di_tv;
10796 }
10797 }
10798 else
10799 EMSG2(_(e_listdictarg), "get()");
10800
10801 if (tv == NULL)
10802 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010803 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010804 copy_tv(&argvars[2], rettv);
10805 }
10806 else
10807 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010808}
10809
Bram Moolenaar342337a2005-07-21 21:11:17 +000010810static 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 +000010811
10812/*
10813 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010814 * Return a range (from start to end) of lines in rettv from the specified
10815 * buffer.
10816 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010817 */
10818 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010819get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010820 buf_T *buf;
10821 linenr_T start;
10822 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010823 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010824 typval_T *rettv;
10825{
10826 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010827
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010828 if (retlist && rettv_list_alloc(rettv) == FAIL)
10829 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010830
10831 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10832 return;
10833
10834 if (!retlist)
10835 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010836 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10837 p = ml_get_buf(buf, start, FALSE);
10838 else
10839 p = (char_u *)"";
10840
10841 rettv->v_type = VAR_STRING;
10842 rettv->vval.v_string = vim_strsave(p);
10843 }
10844 else
10845 {
10846 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010847 return;
10848
10849 if (start < 1)
10850 start = 1;
10851 if (end > buf->b_ml.ml_line_count)
10852 end = buf->b_ml.ml_line_count;
10853 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010854 if (list_append_string(rettv->vval.v_list,
10855 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010856 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010857 }
10858}
10859
10860/*
10861 * "getbufline()" function
10862 */
10863 static void
10864f_getbufline(argvars, rettv)
10865 typval_T *argvars;
10866 typval_T *rettv;
10867{
10868 linenr_T lnum;
10869 linenr_T end;
10870 buf_T *buf;
10871
10872 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10873 ++emsg_off;
10874 buf = get_buf_tv(&argvars[0]);
10875 --emsg_off;
10876
Bram Moolenaar661b1822005-07-28 22:36:45 +000010877 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010878 if (argvars[2].v_type == VAR_UNKNOWN)
10879 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010880 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010881 end = get_tv_lnum_buf(&argvars[2], buf);
10882
Bram Moolenaar342337a2005-07-21 21:11:17 +000010883 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010884}
10885
Bram Moolenaar0d660222005-01-07 21:51:51 +000010886/*
10887 * "getbufvar()" function
10888 */
10889 static void
10890f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010891 typval_T *argvars;
10892 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010893{
10894 buf_T *buf;
10895 buf_T *save_curbuf;
10896 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010897 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010898
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010899 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10900 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010901 ++emsg_off;
10902 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010903
10904 rettv->v_type = VAR_STRING;
10905 rettv->vval.v_string = NULL;
10906
10907 if (buf != NULL && varname != NULL)
10908 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010909 /* set curbuf to be our buf, temporarily */
10910 save_curbuf = curbuf;
10911 curbuf = buf;
10912
Bram Moolenaar0d660222005-01-07 21:51:51 +000010913 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010914 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010010915 else if (STRCMP(varname, "changedtick") == 0)
10916 {
10917 rettv->v_type = VAR_NUMBER;
10918 rettv->vval.v_number = curbuf->b_changedtick;
10919 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010920 else
10921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010922 if (*varname == NUL)
10923 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10924 * scope prefix before the NUL byte is required by
10925 * find_var_in_ht(). */
10926 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010927 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010928 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010929 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010930 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010931 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010932
10933 /* restore previous notion of curbuf */
10934 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010935 }
10936
10937 --emsg_off;
10938}
10939
10940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 * "getchar()" function
10942 */
10943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010944f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010945 typval_T *argvars;
10946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947{
10948 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010949 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010951 /* Position the cursor. Needed after a message that ends in a space. */
10952 windgoto(msg_row, msg_col);
10953
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 ++no_mapping;
10955 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010956 for (;;)
10957 {
10958 if (argvars[0].v_type == VAR_UNKNOWN)
10959 /* getchar(): blocking wait. */
10960 n = safe_vgetc();
10961 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10962 /* getchar(1): only check if char avail */
10963 n = vpeekc();
10964 else if (error || vpeekc() == NUL)
10965 /* illegal argument or getchar(0) and no char avail: return zero */
10966 n = 0;
10967 else
10968 /* getchar(0) and char avail: return char */
10969 n = safe_vgetc();
10970 if (n == K_IGNORE)
10971 continue;
10972 break;
10973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 --no_mapping;
10975 --allow_keys;
10976
Bram Moolenaar219b8702006-11-01 14:32:36 +000010977 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10978 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10979 vimvars[VV_MOUSE_COL].vv_nr = 0;
10980
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010981 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 if (IS_SPECIAL(n) || mod_mask != 0)
10983 {
10984 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10985 int i = 0;
10986
10987 /* Turn a special key into three bytes, plus modifier. */
10988 if (mod_mask != 0)
10989 {
10990 temp[i++] = K_SPECIAL;
10991 temp[i++] = KS_MODIFIER;
10992 temp[i++] = mod_mask;
10993 }
10994 if (IS_SPECIAL(n))
10995 {
10996 temp[i++] = K_SPECIAL;
10997 temp[i++] = K_SECOND(n);
10998 temp[i++] = K_THIRD(n);
10999 }
11000#ifdef FEAT_MBYTE
11001 else if (has_mbyte)
11002 i += (*mb_char2bytes)(n, temp + i);
11003#endif
11004 else
11005 temp[i++] = n;
11006 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011007 rettv->v_type = VAR_STRING;
11008 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011009
11010#ifdef FEAT_MOUSE
11011 if (n == K_LEFTMOUSE
11012 || n == K_LEFTMOUSE_NM
11013 || n == K_LEFTDRAG
11014 || n == K_LEFTRELEASE
11015 || n == K_LEFTRELEASE_NM
11016 || n == K_MIDDLEMOUSE
11017 || n == K_MIDDLEDRAG
11018 || n == K_MIDDLERELEASE
11019 || n == K_RIGHTMOUSE
11020 || n == K_RIGHTDRAG
11021 || n == K_RIGHTRELEASE
11022 || n == K_X1MOUSE
11023 || n == K_X1DRAG
11024 || n == K_X1RELEASE
11025 || n == K_X2MOUSE
11026 || n == K_X2DRAG
11027 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011028 || n == K_MOUSELEFT
11029 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011030 || n == K_MOUSEDOWN
11031 || n == K_MOUSEUP)
11032 {
11033 int row = mouse_row;
11034 int col = mouse_col;
11035 win_T *win;
11036 linenr_T lnum;
11037# ifdef FEAT_WINDOWS
11038 win_T *wp;
11039# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011040 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011041
11042 if (row >= 0 && col >= 0)
11043 {
11044 /* Find the window at the mouse coordinates and compute the
11045 * text position. */
11046 win = mouse_find_win(&row, &col);
11047 (void)mouse_comp_pos(win, &row, &col, &lnum);
11048# ifdef FEAT_WINDOWS
11049 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011050 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011051# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011052 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011053 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11054 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11055 }
11056 }
11057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 }
11059}
11060
11061/*
11062 * "getcharmod()" function
11063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011065f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011066 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011069 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070}
11071
11072/*
11073 * "getcmdline()" function
11074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011076f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011077 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011080 rettv->v_type = VAR_STRING;
11081 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082}
11083
11084/*
11085 * "getcmdpos()" function
11086 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011088f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011089 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011092 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093}
11094
11095/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011096 * "getcmdtype()" function
11097 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011098 static void
11099f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011100 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011101 typval_T *rettv;
11102{
11103 rettv->v_type = VAR_STRING;
11104 rettv->vval.v_string = alloc(2);
11105 if (rettv->vval.v_string != NULL)
11106 {
11107 rettv->vval.v_string[0] = get_cmdline_type();
11108 rettv->vval.v_string[1] = NUL;
11109 }
11110}
11111
11112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113 * "getcwd()" function
11114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011116f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011117 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011120 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011122 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011123 rettv->vval.v_string = NULL;
11124 cwd = alloc(MAXPATHL);
11125 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011127 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11128 {
11129 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011131 if (rettv->vval.v_string != NULL)
11132 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011134 }
11135 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136 }
11137}
11138
11139/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011140 * "getfontname()" function
11141 */
11142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011143f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011144 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011145 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011146{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011147 rettv->v_type = VAR_STRING;
11148 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011149#ifdef FEAT_GUI
11150 if (gui.in_use)
11151 {
11152 GuiFont font;
11153 char_u *name = NULL;
11154
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011155 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011156 {
11157 /* Get the "Normal" font. Either the name saved by
11158 * hl_set_font_name() or from the font ID. */
11159 font = gui.norm_font;
11160 name = hl_get_font_name();
11161 }
11162 else
11163 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011165 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11166 return;
11167 font = gui_mch_get_font(name, FALSE);
11168 if (font == NOFONT)
11169 return; /* Invalid font name, return empty string. */
11170 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011171 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011172 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011173 gui_mch_free_font(font);
11174 }
11175#endif
11176}
11177
11178/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011179 * "getfperm({fname})" function
11180 */
11181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011182f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011183 typval_T *argvars;
11184 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011185{
11186 char_u *fname;
11187 struct stat st;
11188 char_u *perm = NULL;
11189 char_u flags[] = "rwx";
11190 int i;
11191
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011192 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011193
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011194 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011195 if (mch_stat((char *)fname, &st) >= 0)
11196 {
11197 perm = vim_strsave((char_u *)"---------");
11198 if (perm != NULL)
11199 {
11200 for (i = 0; i < 9; i++)
11201 {
11202 if (st.st_mode & (1 << (8 - i)))
11203 perm[i] = flags[i % 3];
11204 }
11205 }
11206 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011207 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011208}
11209
11210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 * "getfsize({fname})" function
11212 */
11213 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011214f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011215 typval_T *argvars;
11216 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217{
11218 char_u *fname;
11219 struct stat st;
11220
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011221 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011223 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224
11225 if (mch_stat((char *)fname, &st) >= 0)
11226 {
11227 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011228 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011230 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011231 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011232
11233 /* non-perfect check for overflow */
11234 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11235 rettv->vval.v_number = -2;
11236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 }
11238 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011239 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240}
11241
11242/*
11243 * "getftime({fname})" function
11244 */
11245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011246f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011247 typval_T *argvars;
11248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249{
11250 char_u *fname;
11251 struct stat st;
11252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011253 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254
11255 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011256 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011258 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259}
11260
11261/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011262 * "getftype({fname})" function
11263 */
11264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011265f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011266 typval_T *argvars;
11267 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011268{
11269 char_u *fname;
11270 struct stat st;
11271 char_u *type = NULL;
11272 char *t;
11273
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011274 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011275
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011276 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011277 if (mch_lstat((char *)fname, &st) >= 0)
11278 {
11279#ifdef S_ISREG
11280 if (S_ISREG(st.st_mode))
11281 t = "file";
11282 else if (S_ISDIR(st.st_mode))
11283 t = "dir";
11284# ifdef S_ISLNK
11285 else if (S_ISLNK(st.st_mode))
11286 t = "link";
11287# endif
11288# ifdef S_ISBLK
11289 else if (S_ISBLK(st.st_mode))
11290 t = "bdev";
11291# endif
11292# ifdef S_ISCHR
11293 else if (S_ISCHR(st.st_mode))
11294 t = "cdev";
11295# endif
11296# ifdef S_ISFIFO
11297 else if (S_ISFIFO(st.st_mode))
11298 t = "fifo";
11299# endif
11300# ifdef S_ISSOCK
11301 else if (S_ISSOCK(st.st_mode))
11302 t = "fifo";
11303# endif
11304 else
11305 t = "other";
11306#else
11307# ifdef S_IFMT
11308 switch (st.st_mode & S_IFMT)
11309 {
11310 case S_IFREG: t = "file"; break;
11311 case S_IFDIR: t = "dir"; break;
11312# ifdef S_IFLNK
11313 case S_IFLNK: t = "link"; break;
11314# endif
11315# ifdef S_IFBLK
11316 case S_IFBLK: t = "bdev"; break;
11317# endif
11318# ifdef S_IFCHR
11319 case S_IFCHR: t = "cdev"; break;
11320# endif
11321# ifdef S_IFIFO
11322 case S_IFIFO: t = "fifo"; break;
11323# endif
11324# ifdef S_IFSOCK
11325 case S_IFSOCK: t = "socket"; break;
11326# endif
11327 default: t = "other";
11328 }
11329# else
11330 if (mch_isdir(fname))
11331 t = "dir";
11332 else
11333 t = "file";
11334# endif
11335#endif
11336 type = vim_strsave((char_u *)t);
11337 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011338 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011339}
11340
11341/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011342 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011343 */
11344 static void
11345f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011346 typval_T *argvars;
11347 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011348{
11349 linenr_T lnum;
11350 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011351 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011352
11353 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011354 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011355 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011356 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011357 retlist = FALSE;
11358 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011359 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011360 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011361 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011362 retlist = TRUE;
11363 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011364
Bram Moolenaar342337a2005-07-21 21:11:17 +000011365 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011366}
11367
11368/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011369 * "getmatches()" function
11370 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011371 static void
11372f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011373 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011374 typval_T *rettv;
11375{
11376#ifdef FEAT_SEARCH_EXTRA
11377 dict_T *dict;
11378 matchitem_T *cur = curwin->w_match_head;
11379
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011380 if (rettv_list_alloc(rettv) == OK)
11381 {
11382 while (cur != NULL)
11383 {
11384 dict = dict_alloc();
11385 if (dict == NULL)
11386 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011387 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11388 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11389 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11390 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11391 list_append_dict(rettv->vval.v_list, dict);
11392 cur = cur->next;
11393 }
11394 }
11395#endif
11396}
11397
11398/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011399 * "getpid()" function
11400 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011401 static void
11402f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011403 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011404 typval_T *rettv;
11405{
11406 rettv->vval.v_number = mch_get_pid();
11407}
11408
11409/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011410 * "getpos(string)" function
11411 */
11412 static void
11413f_getpos(argvars, rettv)
11414 typval_T *argvars;
11415 typval_T *rettv;
11416{
11417 pos_T *fp;
11418 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011419 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011420
11421 if (rettv_list_alloc(rettv) == OK)
11422 {
11423 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011424 fp = var2fpos(&argvars[0], TRUE, &fnum);
11425 if (fnum != -1)
11426 list_append_number(l, (varnumber_T)fnum);
11427 else
11428 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011429 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11430 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011431 list_append_number(l, (fp != NULL)
11432 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011433 : (varnumber_T)0);
11434 list_append_number(l,
11435#ifdef FEAT_VIRTUALEDIT
11436 (fp != NULL) ? (varnumber_T)fp->coladd :
11437#endif
11438 (varnumber_T)0);
11439 }
11440 else
11441 rettv->vval.v_number = FALSE;
11442}
11443
11444/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011445 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011446 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011447 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011448f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011449 typval_T *argvars UNUSED;
11450 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011451{
11452#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011453 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011454#endif
11455
Bram Moolenaar2641f772005-03-25 21:58:17 +000011456#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011457 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011458 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011459 wp = NULL;
11460 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11461 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011462 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011463 if (wp == NULL)
11464 return;
11465 }
11466
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011467 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011468 }
11469#endif
11470}
11471
11472/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 * "getreg()" function
11474 */
11475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011476f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011477 typval_T *argvars;
11478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479{
11480 char_u *strregname;
11481 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011482 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011483 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011485 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011487 strregname = get_tv_string_chk(&argvars[0]);
11488 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011489 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011490 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011493 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494 regname = (strregname == NULL ? '"' : *strregname);
11495 if (regname == 0)
11496 regname = '"';
11497
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011499 rettv->vval.v_string = error ? NULL :
11500 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501}
11502
11503/*
11504 * "getregtype()" function
11505 */
11506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011507f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011508 typval_T *argvars;
11509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510{
11511 char_u *strregname;
11512 int regname;
11513 char_u buf[NUMBUFLEN + 2];
11514 long reglen = 0;
11515
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011516 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011517 {
11518 strregname = get_tv_string_chk(&argvars[0]);
11519 if (strregname == NULL) /* type error; errmsg already given */
11520 {
11521 rettv->v_type = VAR_STRING;
11522 rettv->vval.v_string = NULL;
11523 return;
11524 }
11525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526 else
11527 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011528 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011529
11530 regname = (strregname == NULL ? '"' : *strregname);
11531 if (regname == 0)
11532 regname = '"';
11533
11534 buf[0] = NUL;
11535 buf[1] = NUL;
11536 switch (get_reg_type(regname, &reglen))
11537 {
11538 case MLINE: buf[0] = 'V'; break;
11539 case MCHAR: buf[0] = 'v'; break;
11540#ifdef FEAT_VISUAL
11541 case MBLOCK:
11542 buf[0] = Ctrl_V;
11543 sprintf((char *)buf + 1, "%ld", reglen + 1);
11544 break;
11545#endif
11546 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011547 rettv->v_type = VAR_STRING;
11548 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549}
11550
11551/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011552 * "gettabvar()" function
11553 */
11554 static void
11555f_gettabvar(argvars, rettv)
11556 typval_T *argvars;
11557 typval_T *rettv;
11558{
11559 tabpage_T *tp;
11560 dictitem_T *v;
11561 char_u *varname;
11562
11563 rettv->v_type = VAR_STRING;
11564 rettv->vval.v_string = NULL;
11565
11566 varname = get_tv_string_chk(&argvars[1]);
11567 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11568 if (tp != NULL && varname != NULL)
11569 {
11570 /* look up the variable */
11571 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11572 if (v != NULL)
11573 copy_tv(&v->di_tv, rettv);
11574 }
11575}
11576
11577/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011578 * "gettabwinvar()" function
11579 */
11580 static void
11581f_gettabwinvar(argvars, rettv)
11582 typval_T *argvars;
11583 typval_T *rettv;
11584{
11585 getwinvar(argvars, rettv, 1);
11586}
11587
11588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589 * "getwinposx()" function
11590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011592f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011593 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011596 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597#ifdef FEAT_GUI
11598 if (gui.in_use)
11599 {
11600 int x, y;
11601
11602 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011603 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604 }
11605#endif
11606}
11607
11608/*
11609 * "getwinposy()" function
11610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011612f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011613 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011616 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617#ifdef FEAT_GUI
11618 if (gui.in_use)
11619 {
11620 int x, y;
11621
11622 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011623 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011624 }
11625#endif
11626}
11627
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011628/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011629 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011630 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011631 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011632find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011633 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011634 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011635{
11636#ifdef FEAT_WINDOWS
11637 win_T *wp;
11638#endif
11639 int nr;
11640
11641 nr = get_tv_number_chk(vp, NULL);
11642
11643#ifdef FEAT_WINDOWS
11644 if (nr < 0)
11645 return NULL;
11646 if (nr == 0)
11647 return curwin;
11648
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011649 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11650 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011651 if (--nr <= 0)
11652 break;
11653 return wp;
11654#else
11655 if (nr == 0 || nr == 1)
11656 return curwin;
11657 return NULL;
11658#endif
11659}
11660
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661/*
11662 * "getwinvar()" function
11663 */
11664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011665f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011666 typval_T *argvars;
11667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011669 getwinvar(argvars, rettv, 0);
11670}
11671
11672/*
11673 * getwinvar() and gettabwinvar()
11674 */
11675 static void
11676getwinvar(argvars, rettv, off)
11677 typval_T *argvars;
11678 typval_T *rettv;
11679 int off; /* 1 for gettabwinvar() */
11680{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 win_T *win, *oldcurwin;
11682 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011683 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011684 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011686#ifdef FEAT_WINDOWS
11687 if (off == 1)
11688 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11689 else
11690 tp = curtab;
11691#endif
11692 win = find_win_by_nr(&argvars[off], tp);
11693 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011694 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011695
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011696 rettv->v_type = VAR_STRING;
11697 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698
11699 if (win != NULL && varname != NULL)
11700 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011701 /* Set curwin to be our win, temporarily. Also set curbuf, so
11702 * that we can get buffer-local options. */
11703 oldcurwin = curwin;
11704 curwin = win;
11705 curbuf = win->w_buffer;
11706
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011708 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709 else
11710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011711 if (*varname == NUL)
11712 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11713 * scope prefix before the NUL byte is required by
11714 * find_var_in_ht(). */
11715 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011717 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011719 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011721
11722 /* restore previous notion of curwin */
11723 curwin = oldcurwin;
11724 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011725 }
11726
11727 --emsg_off;
11728}
11729
11730/*
11731 * "glob()" function
11732 */
11733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011734f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011735 typval_T *argvars;
11736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011738 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011740 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011742 /* When the optional second argument is non-zero, don't remove matches
11743 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11744 if (argvars[1].v_type != VAR_UNKNOWN
11745 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011746 options |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011747 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011748 if (!error)
11749 {
11750 ExpandInit(&xpc);
11751 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011752 if (p_wic)
11753 options += WILD_ICASE;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011754 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011755 NULL, options, WILD_ALL);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011756 }
11757 else
11758 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759}
11760
11761/*
11762 * "globpath()" function
11763 */
11764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011765f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011766 typval_T *argvars;
11767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011769 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011771 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011772 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011773
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011774 /* When the optional second argument is non-zero, don't remove matches
11775 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11776 if (argvars[2].v_type != VAR_UNKNOWN
11777 && get_tv_number_chk(&argvars[2], &error))
11778 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011779 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011780 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011781 rettv->vval.v_string = NULL;
11782 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011783 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11784 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785}
11786
11787/*
11788 * "has()" function
11789 */
11790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011791f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011792 typval_T *argvars;
11793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011794{
11795 int i;
11796 char_u *name;
11797 int n = FALSE;
11798 static char *(has_list[]) =
11799 {
11800#ifdef AMIGA
11801 "amiga",
11802# ifdef FEAT_ARP
11803 "arp",
11804# endif
11805#endif
11806#ifdef __BEOS__
11807 "beos",
11808#endif
11809#ifdef MSDOS
11810# ifdef DJGPP
11811 "dos32",
11812# else
11813 "dos16",
11814# endif
11815#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011816#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817 "mac",
11818#endif
11819#if defined(MACOS_X_UNIX)
11820 "macunix",
11821#endif
11822#ifdef OS2
11823 "os2",
11824#endif
11825#ifdef __QNX__
11826 "qnx",
11827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828#ifdef UNIX
11829 "unix",
11830#endif
11831#ifdef VMS
11832 "vms",
11833#endif
11834#ifdef WIN16
11835 "win16",
11836#endif
11837#ifdef WIN32
11838 "win32",
11839#endif
11840#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11841 "win32unix",
11842#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011843#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011844 "win64",
11845#endif
11846#ifdef EBCDIC
11847 "ebcdic",
11848#endif
11849#ifndef CASE_INSENSITIVE_FILENAME
11850 "fname_case",
11851#endif
11852#ifdef FEAT_ARABIC
11853 "arabic",
11854#endif
11855#ifdef FEAT_AUTOCMD
11856 "autocmd",
11857#endif
11858#ifdef FEAT_BEVAL
11859 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011860# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11861 "balloon_multiline",
11862# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863#endif
11864#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11865 "builtin_terms",
11866# ifdef ALL_BUILTIN_TCAPS
11867 "all_builtin_terms",
11868# endif
11869#endif
11870#ifdef FEAT_BYTEOFF
11871 "byte_offset",
11872#endif
11873#ifdef FEAT_CINDENT
11874 "cindent",
11875#endif
11876#ifdef FEAT_CLIENTSERVER
11877 "clientserver",
11878#endif
11879#ifdef FEAT_CLIPBOARD
11880 "clipboard",
11881#endif
11882#ifdef FEAT_CMDL_COMPL
11883 "cmdline_compl",
11884#endif
11885#ifdef FEAT_CMDHIST
11886 "cmdline_hist",
11887#endif
11888#ifdef FEAT_COMMENTS
11889 "comments",
11890#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011891#ifdef FEAT_CONCEAL
11892 "conceal",
11893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894#ifdef FEAT_CRYPT
11895 "cryptv",
11896#endif
11897#ifdef FEAT_CSCOPE
11898 "cscope",
11899#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011900#ifdef FEAT_CURSORBIND
11901 "cursorbind",
11902#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011903#ifdef CURSOR_SHAPE
11904 "cursorshape",
11905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906#ifdef DEBUG
11907 "debug",
11908#endif
11909#ifdef FEAT_CON_DIALOG
11910 "dialog_con",
11911#endif
11912#ifdef FEAT_GUI_DIALOG
11913 "dialog_gui",
11914#endif
11915#ifdef FEAT_DIFF
11916 "diff",
11917#endif
11918#ifdef FEAT_DIGRAPHS
11919 "digraphs",
11920#endif
11921#ifdef FEAT_DND
11922 "dnd",
11923#endif
11924#ifdef FEAT_EMACS_TAGS
11925 "emacs_tags",
11926#endif
11927 "eval", /* always present, of course! */
11928#ifdef FEAT_EX_EXTRA
11929 "ex_extra",
11930#endif
11931#ifdef FEAT_SEARCH_EXTRA
11932 "extra_search",
11933#endif
11934#ifdef FEAT_FKMAP
11935 "farsi",
11936#endif
11937#ifdef FEAT_SEARCHPATH
11938 "file_in_path",
11939#endif
Bram Moolenaar4b9669f2011-07-07 16:20:52 +020011940#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011941 "filterpipe",
11942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943#ifdef FEAT_FIND_ID
11944 "find_in_path",
11945#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011946#ifdef FEAT_FLOAT
11947 "float",
11948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949#ifdef FEAT_FOLDING
11950 "folding",
11951#endif
11952#ifdef FEAT_FOOTER
11953 "footer",
11954#endif
11955#if !defined(USE_SYSTEM) && defined(UNIX)
11956 "fork",
11957#endif
11958#ifdef FEAT_GETTEXT
11959 "gettext",
11960#endif
11961#ifdef FEAT_GUI
11962 "gui",
11963#endif
11964#ifdef FEAT_GUI_ATHENA
11965# ifdef FEAT_GUI_NEXTAW
11966 "gui_neXtaw",
11967# else
11968 "gui_athena",
11969# endif
11970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971#ifdef FEAT_GUI_GTK
11972 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011975#ifdef FEAT_GUI_GNOME
11976 "gui_gnome",
11977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978#ifdef FEAT_GUI_MAC
11979 "gui_mac",
11980#endif
11981#ifdef FEAT_GUI_MOTIF
11982 "gui_motif",
11983#endif
11984#ifdef FEAT_GUI_PHOTON
11985 "gui_photon",
11986#endif
11987#ifdef FEAT_GUI_W16
11988 "gui_win16",
11989#endif
11990#ifdef FEAT_GUI_W32
11991 "gui_win32",
11992#endif
11993#ifdef FEAT_HANGULIN
11994 "hangul_input",
11995#endif
11996#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11997 "iconv",
11998#endif
11999#ifdef FEAT_INS_EXPAND
12000 "insert_expand",
12001#endif
12002#ifdef FEAT_JUMPLIST
12003 "jumplist",
12004#endif
12005#ifdef FEAT_KEYMAP
12006 "keymap",
12007#endif
12008#ifdef FEAT_LANGMAP
12009 "langmap",
12010#endif
12011#ifdef FEAT_LIBCALL
12012 "libcall",
12013#endif
12014#ifdef FEAT_LINEBREAK
12015 "linebreak",
12016#endif
12017#ifdef FEAT_LISP
12018 "lispindent",
12019#endif
12020#ifdef FEAT_LISTCMDS
12021 "listcmds",
12022#endif
12023#ifdef FEAT_LOCALMAP
12024 "localmap",
12025#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012026#ifdef FEAT_LUA
12027# ifndef DYNAMIC_LUA
12028 "lua",
12029# endif
12030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031#ifdef FEAT_MENU
12032 "menu",
12033#endif
12034#ifdef FEAT_SESSION
12035 "mksession",
12036#endif
12037#ifdef FEAT_MODIFY_FNAME
12038 "modify_fname",
12039#endif
12040#ifdef FEAT_MOUSE
12041 "mouse",
12042#endif
12043#ifdef FEAT_MOUSESHAPE
12044 "mouseshape",
12045#endif
12046#if defined(UNIX) || defined(VMS)
12047# ifdef FEAT_MOUSE_DEC
12048 "mouse_dec",
12049# endif
12050# ifdef FEAT_MOUSE_GPM
12051 "mouse_gpm",
12052# endif
12053# ifdef FEAT_MOUSE_JSB
12054 "mouse_jsbterm",
12055# endif
12056# ifdef FEAT_MOUSE_NET
12057 "mouse_netterm",
12058# endif
12059# ifdef FEAT_MOUSE_PTERM
12060 "mouse_pterm",
12061# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012062# ifdef FEAT_SYSMOUSE
12063 "mouse_sysmouse",
12064# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065# ifdef FEAT_MOUSE_XTERM
12066 "mouse_xterm",
12067# endif
12068#endif
12069#ifdef FEAT_MBYTE
12070 "multi_byte",
12071#endif
12072#ifdef FEAT_MBYTE_IME
12073 "multi_byte_ime",
12074#endif
12075#ifdef FEAT_MULTI_LANG
12076 "multi_lang",
12077#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012078#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012079#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012080 "mzscheme",
12081#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083#ifdef FEAT_OLE
12084 "ole",
12085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086#ifdef FEAT_PATH_EXTRA
12087 "path_extra",
12088#endif
12089#ifdef FEAT_PERL
12090#ifndef DYNAMIC_PERL
12091 "perl",
12092#endif
12093#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012094#ifdef FEAT_PERSISTENT_UNDO
12095 "persistent_undo",
12096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097#ifdef FEAT_PYTHON
12098#ifndef DYNAMIC_PYTHON
12099 "python",
12100#endif
12101#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012102#ifdef FEAT_PYTHON3
12103#ifndef DYNAMIC_PYTHON3
12104 "python3",
12105#endif
12106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107#ifdef FEAT_POSTSCRIPT
12108 "postscript",
12109#endif
12110#ifdef FEAT_PRINTER
12111 "printer",
12112#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012113#ifdef FEAT_PROFILE
12114 "profile",
12115#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012116#ifdef FEAT_RELTIME
12117 "reltime",
12118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119#ifdef FEAT_QUICKFIX
12120 "quickfix",
12121#endif
12122#ifdef FEAT_RIGHTLEFT
12123 "rightleft",
12124#endif
12125#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12126 "ruby",
12127#endif
12128#ifdef FEAT_SCROLLBIND
12129 "scrollbind",
12130#endif
12131#ifdef FEAT_CMDL_INFO
12132 "showcmd",
12133 "cmdline_info",
12134#endif
12135#ifdef FEAT_SIGNS
12136 "signs",
12137#endif
12138#ifdef FEAT_SMARTINDENT
12139 "smartindent",
12140#endif
12141#ifdef FEAT_SNIFF
12142 "sniff",
12143#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012144#ifdef STARTUPTIME
12145 "startuptime",
12146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147#ifdef FEAT_STL_OPT
12148 "statusline",
12149#endif
12150#ifdef FEAT_SUN_WORKSHOP
12151 "sun_workshop",
12152#endif
12153#ifdef FEAT_NETBEANS_INTG
12154 "netbeans_intg",
12155#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012156#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012157 "spell",
12158#endif
12159#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160 "syntax",
12161#endif
12162#if defined(USE_SYSTEM) || !defined(UNIX)
12163 "system",
12164#endif
12165#ifdef FEAT_TAG_BINS
12166 "tag_binary",
12167#endif
12168#ifdef FEAT_TAG_OLDSTATIC
12169 "tag_old_static",
12170#endif
12171#ifdef FEAT_TAG_ANYWHITE
12172 "tag_any_white",
12173#endif
12174#ifdef FEAT_TCL
12175# ifndef DYNAMIC_TCL
12176 "tcl",
12177# endif
12178#endif
12179#ifdef TERMINFO
12180 "terminfo",
12181#endif
12182#ifdef FEAT_TERMRESPONSE
12183 "termresponse",
12184#endif
12185#ifdef FEAT_TEXTOBJ
12186 "textobjects",
12187#endif
12188#ifdef HAVE_TGETENT
12189 "tgetent",
12190#endif
12191#ifdef FEAT_TITLE
12192 "title",
12193#endif
12194#ifdef FEAT_TOOLBAR
12195 "toolbar",
12196#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012197#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12198 "unnamedplus",
12199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200#ifdef FEAT_USR_CMDS
12201 "user-commands", /* was accidentally included in 5.4 */
12202 "user_commands",
12203#endif
12204#ifdef FEAT_VIMINFO
12205 "viminfo",
12206#endif
12207#ifdef FEAT_VERTSPLIT
12208 "vertsplit",
12209#endif
12210#ifdef FEAT_VIRTUALEDIT
12211 "virtualedit",
12212#endif
12213#ifdef FEAT_VISUAL
12214 "visual",
12215#endif
12216#ifdef FEAT_VISUALEXTRA
12217 "visualextra",
12218#endif
12219#ifdef FEAT_VREPLACE
12220 "vreplace",
12221#endif
12222#ifdef FEAT_WILDIGN
12223 "wildignore",
12224#endif
12225#ifdef FEAT_WILDMENU
12226 "wildmenu",
12227#endif
12228#ifdef FEAT_WINDOWS
12229 "windows",
12230#endif
12231#ifdef FEAT_WAK
12232 "winaltkeys",
12233#endif
12234#ifdef FEAT_WRITEBACKUP
12235 "writebackup",
12236#endif
12237#ifdef FEAT_XIM
12238 "xim",
12239#endif
12240#ifdef FEAT_XFONTSET
12241 "xfontset",
12242#endif
12243#ifdef USE_XSMP
12244 "xsmp",
12245#endif
12246#ifdef USE_XSMP_INTERACT
12247 "xsmp_interact",
12248#endif
12249#ifdef FEAT_XCLIPBOARD
12250 "xterm_clipboard",
12251#endif
12252#ifdef FEAT_XTERM_SAVE
12253 "xterm_save",
12254#endif
12255#if defined(UNIX) && defined(FEAT_X11)
12256 "X11",
12257#endif
12258 NULL
12259 };
12260
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012261 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262 for (i = 0; has_list[i] != NULL; ++i)
12263 if (STRICMP(name, has_list[i]) == 0)
12264 {
12265 n = TRUE;
12266 break;
12267 }
12268
12269 if (n == FALSE)
12270 {
12271 if (STRNICMP(name, "patch", 5) == 0)
12272 n = has_patch(atoi((char *)name + 5));
12273 else if (STRICMP(name, "vim_starting") == 0)
12274 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012275#ifdef FEAT_MBYTE
12276 else if (STRICMP(name, "multi_byte_encoding") == 0)
12277 n = has_mbyte;
12278#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012279#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12280 else if (STRICMP(name, "balloon_multiline") == 0)
12281 n = multiline_balloon_available();
12282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283#ifdef DYNAMIC_TCL
12284 else if (STRICMP(name, "tcl") == 0)
12285 n = tcl_enabled(FALSE);
12286#endif
12287#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12288 else if (STRICMP(name, "iconv") == 0)
12289 n = iconv_enabled(FALSE);
12290#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012291#ifdef DYNAMIC_LUA
12292 else if (STRICMP(name, "lua") == 0)
12293 n = lua_enabled(FALSE);
12294#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012295#ifdef DYNAMIC_MZSCHEME
12296 else if (STRICMP(name, "mzscheme") == 0)
12297 n = mzscheme_enabled(FALSE);
12298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299#ifdef DYNAMIC_RUBY
12300 else if (STRICMP(name, "ruby") == 0)
12301 n = ruby_enabled(FALSE);
12302#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012303#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012304#ifdef DYNAMIC_PYTHON
12305 else if (STRICMP(name, "python") == 0)
12306 n = python_enabled(FALSE);
12307#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012308#endif
12309#ifdef FEAT_PYTHON3
12310#ifdef DYNAMIC_PYTHON3
12311 else if (STRICMP(name, "python3") == 0)
12312 n = python3_enabled(FALSE);
12313#endif
12314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315#ifdef DYNAMIC_PERL
12316 else if (STRICMP(name, "perl") == 0)
12317 n = perl_enabled(FALSE);
12318#endif
12319#ifdef FEAT_GUI
12320 else if (STRICMP(name, "gui_running") == 0)
12321 n = (gui.in_use || gui.starting);
12322# ifdef FEAT_GUI_W32
12323 else if (STRICMP(name, "gui_win32s") == 0)
12324 n = gui_is_win32s();
12325# endif
12326# ifdef FEAT_BROWSE
12327 else if (STRICMP(name, "browse") == 0)
12328 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12329# endif
12330#endif
12331#ifdef FEAT_SYN_HL
12332 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012333 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334#endif
12335#if defined(WIN3264)
12336 else if (STRICMP(name, "win95") == 0)
12337 n = mch_windows95();
12338#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012339#ifdef FEAT_NETBEANS_INTG
12340 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012341 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343 }
12344
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012345 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012346}
12347
12348/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012349 * "has_key()" function
12350 */
12351 static void
12352f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012353 typval_T *argvars;
12354 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012355{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012356 if (argvars[0].v_type != VAR_DICT)
12357 {
12358 EMSG(_(e_dictreq));
12359 return;
12360 }
12361 if (argvars[0].vval.v_dict == NULL)
12362 return;
12363
12364 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012365 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012366}
12367
12368/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012369 * "haslocaldir()" function
12370 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012371 static void
12372f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012373 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012374 typval_T *rettv;
12375{
12376 rettv->vval.v_number = (curwin->w_localdir != NULL);
12377}
12378
12379/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380 * "hasmapto()" function
12381 */
12382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012383f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012384 typval_T *argvars;
12385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386{
12387 char_u *name;
12388 char_u *mode;
12389 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012390 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012391
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012392 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012393 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394 mode = (char_u *)"nvo";
12395 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012396 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012397 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012398 if (argvars[2].v_type != VAR_UNKNOWN)
12399 abbr = get_tv_number(&argvars[2]);
12400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401
Bram Moolenaar2c932302006-03-18 21:42:09 +000012402 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012403 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012405 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406}
12407
12408/*
12409 * "histadd()" function
12410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012412f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012413 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415{
12416#ifdef FEAT_CMDHIST
12417 int histype;
12418 char_u *str;
12419 char_u buf[NUMBUFLEN];
12420#endif
12421
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012422 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012423 if (check_restricted() || check_secure())
12424 return;
12425#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012426 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12427 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428 if (histype >= 0)
12429 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012430 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431 if (*str != NUL)
12432 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012433 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012435 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436 return;
12437 }
12438 }
12439#endif
12440}
12441
12442/*
12443 * "histdel()" function
12444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012446f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012447 typval_T *argvars UNUSED;
12448 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449{
12450#ifdef FEAT_CMDHIST
12451 int n;
12452 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012453 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012454
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012455 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12456 if (str == NULL)
12457 n = 0;
12458 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012460 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012461 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012462 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012463 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012464 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465 else
12466 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012467 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012468 get_tv_string_buf(&argvars[1], buf));
12469 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470#endif
12471}
12472
12473/*
12474 * "histget()" function
12475 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012477f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012478 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480{
12481#ifdef FEAT_CMDHIST
12482 int type;
12483 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012484 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012486 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12487 if (str == NULL)
12488 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012490 {
12491 type = get_histtype(str);
12492 if (argvars[1].v_type == VAR_UNKNOWN)
12493 idx = get_history_idx(type);
12494 else
12495 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12496 /* -1 on type error */
12497 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012500 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012502 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503}
12504
12505/*
12506 * "histnr()" function
12507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012509f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012510 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512{
12513 int i;
12514
12515#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012516 char_u *history = get_tv_string_chk(&argvars[0]);
12517
12518 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519 if (i >= HIST_CMD && i < HIST_COUNT)
12520 i = get_history_idx(i);
12521 else
12522#endif
12523 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012524 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012525}
12526
12527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528 * "highlightID(name)" function
12529 */
12530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012531f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012532 typval_T *argvars;
12533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012535 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012536}
12537
12538/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012539 * "highlight_exists()" function
12540 */
12541 static void
12542f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012543 typval_T *argvars;
12544 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012545{
12546 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12547}
12548
12549/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012550 * "hostname()" function
12551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012553f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012554 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556{
12557 char_u hostname[256];
12558
12559 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012560 rettv->v_type = VAR_STRING;
12561 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562}
12563
12564/*
12565 * iconv() function
12566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012568f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012569 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571{
12572#ifdef FEAT_MBYTE
12573 char_u buf1[NUMBUFLEN];
12574 char_u buf2[NUMBUFLEN];
12575 char_u *from, *to, *str;
12576 vimconv_T vimconv;
12577#endif
12578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012579 rettv->v_type = VAR_STRING;
12580 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012581
12582#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012583 str = get_tv_string(&argvars[0]);
12584 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12585 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012586 vimconv.vc_type = CONV_NONE;
12587 convert_setup(&vimconv, from, to);
12588
12589 /* If the encodings are equal, no conversion needed. */
12590 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012591 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012593 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012594
12595 convert_setup(&vimconv, NULL, NULL);
12596 vim_free(from);
12597 vim_free(to);
12598#endif
12599}
12600
12601/*
12602 * "indent()" function
12603 */
12604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012605f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012606 typval_T *argvars;
12607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608{
12609 linenr_T lnum;
12610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012611 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012613 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012615 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012616}
12617
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012618/*
12619 * "index()" function
12620 */
12621 static void
12622f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012623 typval_T *argvars;
12624 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012625{
Bram Moolenaar33570922005-01-25 22:26:29 +000012626 list_T *l;
12627 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012628 long idx = 0;
12629 int ic = FALSE;
12630
12631 rettv->vval.v_number = -1;
12632 if (argvars[0].v_type != VAR_LIST)
12633 {
12634 EMSG(_(e_listreq));
12635 return;
12636 }
12637 l = argvars[0].vval.v_list;
12638 if (l != NULL)
12639 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012640 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012641 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012642 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012643 int error = FALSE;
12644
Bram Moolenaar758711c2005-02-02 23:11:38 +000012645 /* Start at specified item. Use the cached index that list_find()
12646 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012647 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012648 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012649 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012650 ic = get_tv_number_chk(&argvars[3], &error);
12651 if (error)
12652 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012653 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012654
Bram Moolenaar758711c2005-02-02 23:11:38 +000012655 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012656 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012657 {
12658 rettv->vval.v_number = idx;
12659 break;
12660 }
12661 }
12662}
12663
Bram Moolenaar071d4272004-06-13 20:20:40 +000012664static int inputsecret_flag = 0;
12665
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012666static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12667
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012669 * This function is used by f_input() and f_inputdialog() functions. The third
12670 * argument to f_input() specifies the type of completion to use at the
12671 * prompt. The third argument to f_inputdialog() specifies the value to return
12672 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012673 */
12674 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012675get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012676 typval_T *argvars;
12677 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012678 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012680 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681 char_u *p = NULL;
12682 int c;
12683 char_u buf[NUMBUFLEN];
12684 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012685 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012686 int xp_type = EXPAND_NOTHING;
12687 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012689 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012690 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691
12692#ifdef NO_CONSOLE_INPUT
12693 /* While starting up, there is no place to enter text. */
12694 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696#endif
12697
12698 cmd_silent = FALSE; /* Want to see the prompt. */
12699 if (prompt != NULL)
12700 {
12701 /* Only the part of the message after the last NL is considered as
12702 * prompt for the command line */
12703 p = vim_strrchr(prompt, '\n');
12704 if (p == NULL)
12705 p = prompt;
12706 else
12707 {
12708 ++p;
12709 c = *p;
12710 *p = NUL;
12711 msg_start();
12712 msg_clr_eos();
12713 msg_puts_attr(prompt, echo_attr);
12714 msg_didout = FALSE;
12715 msg_starthere();
12716 *p = c;
12717 }
12718 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012720 if (argvars[1].v_type != VAR_UNKNOWN)
12721 {
12722 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12723 if (defstr != NULL)
12724 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012726 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012727 {
12728 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012729 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012730 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012731
Bram Moolenaar4463f292005-09-25 22:20:24 +000012732 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012733
Bram Moolenaar4463f292005-09-25 22:20:24 +000012734 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12735 if (xp_name == NULL)
12736 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012737
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012738 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012739
Bram Moolenaar4463f292005-09-25 22:20:24 +000012740 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12741 &xp_arg) == FAIL)
12742 return;
12743 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012744 }
12745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012746 if (defstr != NULL)
12747 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012748 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12749 xp_type, xp_arg);
12750
12751 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012753 /* since the user typed this, no need to wait for return */
12754 need_wait_return = FALSE;
12755 msg_didout = FALSE;
12756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757 cmd_silent = cmd_silent_save;
12758}
12759
12760/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012761 * "input()" function
12762 * Also handles inputsecret() when inputsecret is set.
12763 */
12764 static void
12765f_input(argvars, rettv)
12766 typval_T *argvars;
12767 typval_T *rettv;
12768{
12769 get_user_input(argvars, rettv, FALSE);
12770}
12771
12772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773 * "inputdialog()" function
12774 */
12775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012776f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012777 typval_T *argvars;
12778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779{
12780#if defined(FEAT_GUI_TEXTDIALOG)
12781 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12782 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12783 {
12784 char_u *message;
12785 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012786 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012788 message = get_tv_string_chk(&argvars[0]);
12789 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012790 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012791 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 else
12793 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012794 if (message != NULL && defstr != NULL
12795 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010012796 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012797 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798 else
12799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012800 if (message != NULL && defstr != NULL
12801 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012802 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012803 rettv->vval.v_string = vim_strsave(
12804 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012806 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012808 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809 }
12810 else
12811#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012812 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813}
12814
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012815/*
12816 * "inputlist()" function
12817 */
12818 static void
12819f_inputlist(argvars, rettv)
12820 typval_T *argvars;
12821 typval_T *rettv;
12822{
12823 listitem_T *li;
12824 int selected;
12825 int mouse_used;
12826
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012827#ifdef NO_CONSOLE_INPUT
12828 /* While starting up, there is no place to enter text. */
12829 if (no_console_input())
12830 return;
12831#endif
12832 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12833 {
12834 EMSG2(_(e_listarg), "inputlist()");
12835 return;
12836 }
12837
12838 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012839 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012840 lines_left = Rows; /* avoid more prompt */
12841 msg_scroll = TRUE;
12842 msg_clr_eos();
12843
12844 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12845 {
12846 msg_puts(get_tv_string(&li->li_tv));
12847 msg_putchar('\n');
12848 }
12849
12850 /* Ask for choice. */
12851 selected = prompt_for_number(&mouse_used);
12852 if (mouse_used)
12853 selected -= lines_left;
12854
12855 rettv->vval.v_number = selected;
12856}
12857
12858
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12860
12861/*
12862 * "inputrestore()" function
12863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012865f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012866 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868{
12869 if (ga_userinput.ga_len > 0)
12870 {
12871 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12873 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012874 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875 }
12876 else if (p_verbose > 1)
12877 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012878 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880 }
12881}
12882
12883/*
12884 * "inputsave()" function
12885 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012887f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012888 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012891 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892 if (ga_grow(&ga_userinput, 1) == OK)
12893 {
12894 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12895 + ga_userinput.ga_len);
12896 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012897 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898 }
12899 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012900 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901}
12902
12903/*
12904 * "inputsecret()" function
12905 */
12906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012907f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012908 typval_T *argvars;
12909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910{
12911 ++cmdline_star;
12912 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012913 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914 --cmdline_star;
12915 --inputsecret_flag;
12916}
12917
12918/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012919 * "insert()" function
12920 */
12921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012922f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012923 typval_T *argvars;
12924 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012925{
12926 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012927 listitem_T *item;
12928 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012929 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012930
12931 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012932 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012933 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012934 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012935 {
12936 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012937 before = get_tv_number_chk(&argvars[2], &error);
12938 if (error)
12939 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012940
Bram Moolenaar758711c2005-02-02 23:11:38 +000012941 if (before == l->lv_len)
12942 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012943 else
12944 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012945 item = list_find(l, before);
12946 if (item == NULL)
12947 {
12948 EMSGN(_(e_listidx), before);
12949 l = NULL;
12950 }
12951 }
12952 if (l != NULL)
12953 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012954 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012955 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012956 }
12957 }
12958}
12959
12960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 * "isdirectory()" function
12962 */
12963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012964f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012965 typval_T *argvars;
12966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012968 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012969}
12970
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012971/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012972 * "islocked()" function
12973 */
12974 static void
12975f_islocked(argvars, rettv)
12976 typval_T *argvars;
12977 typval_T *rettv;
12978{
12979 lval_T lv;
12980 char_u *end;
12981 dictitem_T *di;
12982
12983 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012984 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12985 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012986 if (end != NULL && lv.ll_name != NULL)
12987 {
12988 if (*end != NUL)
12989 EMSG(_(e_trailing));
12990 else
12991 {
12992 if (lv.ll_tv == NULL)
12993 {
12994 if (check_changedtick(lv.ll_name))
12995 rettv->vval.v_number = 1; /* always locked */
12996 else
12997 {
12998 di = find_var(lv.ll_name, NULL);
12999 if (di != NULL)
13000 {
13001 /* Consider a variable locked when:
13002 * 1. the variable itself is locked
13003 * 2. the value of the variable is locked.
13004 * 3. the List or Dict value is locked.
13005 */
13006 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13007 || tv_islocked(&di->di_tv));
13008 }
13009 }
13010 }
13011 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013012 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013013 else if (lv.ll_newkey != NULL)
13014 EMSG2(_(e_dictkey), lv.ll_newkey);
13015 else if (lv.ll_list != NULL)
13016 /* List item. */
13017 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13018 else
13019 /* Dictionary item. */
13020 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13021 }
13022 }
13023
13024 clear_lval(&lv);
13025}
13026
Bram Moolenaar33570922005-01-25 22:26:29 +000013027static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013028
13029/*
13030 * Turn a dict into a list:
13031 * "what" == 0: list of keys
13032 * "what" == 1: list of values
13033 * "what" == 2: list of items
13034 */
13035 static void
13036dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013037 typval_T *argvars;
13038 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013039 int what;
13040{
Bram Moolenaar33570922005-01-25 22:26:29 +000013041 list_T *l2;
13042 dictitem_T *di;
13043 hashitem_T *hi;
13044 listitem_T *li;
13045 listitem_T *li2;
13046 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013047 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013048
Bram Moolenaar8c711452005-01-14 21:53:12 +000013049 if (argvars[0].v_type != VAR_DICT)
13050 {
13051 EMSG(_(e_dictreq));
13052 return;
13053 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013054 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013055 return;
13056
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013057 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013058 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013059
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013060 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013061 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013062 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013063 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013064 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013065 --todo;
13066 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013067
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013068 li = listitem_alloc();
13069 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013070 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013071 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013072
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013073 if (what == 0)
13074 {
13075 /* keys() */
13076 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013077 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013078 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13079 }
13080 else if (what == 1)
13081 {
13082 /* values() */
13083 copy_tv(&di->di_tv, &li->li_tv);
13084 }
13085 else
13086 {
13087 /* items() */
13088 l2 = list_alloc();
13089 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013090 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013091 li->li_tv.vval.v_list = l2;
13092 if (l2 == NULL)
13093 break;
13094 ++l2->lv_refcount;
13095
13096 li2 = listitem_alloc();
13097 if (li2 == NULL)
13098 break;
13099 list_append(l2, li2);
13100 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013101 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013102 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13103
13104 li2 = listitem_alloc();
13105 if (li2 == NULL)
13106 break;
13107 list_append(l2, li2);
13108 copy_tv(&di->di_tv, &li2->li_tv);
13109 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013110 }
13111 }
13112}
13113
13114/*
13115 * "items(dict)" function
13116 */
13117 static void
13118f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013119 typval_T *argvars;
13120 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013121{
13122 dict_list(argvars, rettv, 2);
13123}
13124
Bram Moolenaar071d4272004-06-13 20:20:40 +000013125/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013126 * "join()" function
13127 */
13128 static void
13129f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013130 typval_T *argvars;
13131 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013132{
13133 garray_T ga;
13134 char_u *sep;
13135
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013136 if (argvars[0].v_type != VAR_LIST)
13137 {
13138 EMSG(_(e_listreq));
13139 return;
13140 }
13141 if (argvars[0].vval.v_list == NULL)
13142 return;
13143 if (argvars[1].v_type == VAR_UNKNOWN)
13144 sep = (char_u *)" ";
13145 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013146 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013147
13148 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013149
13150 if (sep != NULL)
13151 {
13152 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013153 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013154 ga_append(&ga, NUL);
13155 rettv->vval.v_string = (char_u *)ga.ga_data;
13156 }
13157 else
13158 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013159}
13160
13161/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013162 * "keys()" function
13163 */
13164 static void
13165f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013166 typval_T *argvars;
13167 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013168{
13169 dict_list(argvars, rettv, 0);
13170}
13171
13172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173 * "last_buffer_nr()" function.
13174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013176f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013177 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013179{
13180 int n = 0;
13181 buf_T *buf;
13182
13183 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13184 if (n < buf->b_fnum)
13185 n = buf->b_fnum;
13186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013187 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013188}
13189
13190/*
13191 * "len()" function
13192 */
13193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013194f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013195 typval_T *argvars;
13196 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013197{
13198 switch (argvars[0].v_type)
13199 {
13200 case VAR_STRING:
13201 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013202 rettv->vval.v_number = (varnumber_T)STRLEN(
13203 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013204 break;
13205 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013206 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013207 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013208 case VAR_DICT:
13209 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13210 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013211 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013212 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013213 break;
13214 }
13215}
13216
Bram Moolenaar33570922005-01-25 22:26:29 +000013217static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013218
13219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013220libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013221 typval_T *argvars;
13222 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013223 int type;
13224{
13225#ifdef FEAT_LIBCALL
13226 char_u *string_in;
13227 char_u **string_result;
13228 int nr_result;
13229#endif
13230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013231 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013232 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013233 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013234
13235 if (check_restricted() || check_secure())
13236 return;
13237
13238#ifdef FEAT_LIBCALL
13239 /* The first two args must be strings, otherwise its meaningless */
13240 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13241 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013242 string_in = NULL;
13243 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013244 string_in = argvars[2].vval.v_string;
13245 if (type == VAR_NUMBER)
13246 string_result = NULL;
13247 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013248 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013249 if (mch_libcall(argvars[0].vval.v_string,
13250 argvars[1].vval.v_string,
13251 string_in,
13252 argvars[2].vval.v_number,
13253 string_result,
13254 &nr_result) == OK
13255 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013256 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013257 }
13258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259}
13260
13261/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013262 * "libcall()" function
13263 */
13264 static void
13265f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013266 typval_T *argvars;
13267 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013268{
13269 libcall_common(argvars, rettv, VAR_STRING);
13270}
13271
13272/*
13273 * "libcallnr()" function
13274 */
13275 static void
13276f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013277 typval_T *argvars;
13278 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013279{
13280 libcall_common(argvars, rettv, VAR_NUMBER);
13281}
13282
13283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284 * "line(string)" function
13285 */
13286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013287f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013288 typval_T *argvars;
13289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290{
13291 linenr_T lnum = 0;
13292 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013293 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013295 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296 if (fp != NULL)
13297 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013298 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299}
13300
13301/*
13302 * "line2byte(lnum)" function
13303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013305f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013306 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308{
13309#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013310 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311#else
13312 linenr_T lnum;
13313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013314 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013316 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013318 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13319 if (rettv->vval.v_number >= 0)
13320 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321#endif
13322}
13323
13324/*
13325 * "lispindent(lnum)" function
13326 */
13327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013328f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013329 typval_T *argvars;
13330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331{
13332#ifdef FEAT_LISP
13333 pos_T pos;
13334 linenr_T lnum;
13335
13336 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013337 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13339 {
13340 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013341 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342 curwin->w_cursor = pos;
13343 }
13344 else
13345#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013346 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347}
13348
13349/*
13350 * "localtime()" function
13351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013353f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013354 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013357 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358}
13359
Bram Moolenaar33570922005-01-25 22:26:29 +000013360static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361
13362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013363get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013364 typval_T *argvars;
13365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366 int exact;
13367{
13368 char_u *keys;
13369 char_u *which;
13370 char_u buf[NUMBUFLEN];
13371 char_u *keys_buf = NULL;
13372 char_u *rhs;
13373 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013374 int abbr = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013375 int get_dict = FALSE;
13376 mapblock_T *mp;
13377 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378
13379 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013380 rettv->v_type = VAR_STRING;
13381 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013383 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384 if (*keys == NUL)
13385 return;
13386
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013387 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013388 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013389 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013390 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013391 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013392 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013393 if (argvars[3].v_type != VAR_UNKNOWN)
13394 get_dict = get_tv_number(&argvars[3]);
13395 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013397 else
13398 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013399 if (which == NULL)
13400 return;
13401
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402 mode = get_map_mode(&which, 0);
13403
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013404 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013405 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013407
13408 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013410 /* Return a string. */
13411 if (rhs != NULL)
13412 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413
Bram Moolenaarbd743252010-10-20 21:23:33 +020013414 }
13415 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13416 {
13417 /* Return a dictionary. */
13418 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13419 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13420 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421
Bram Moolenaarbd743252010-10-20 21:23:33 +020013422 dict_add_nr_str(dict, "lhs", 0L, lhs);
13423 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13424 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13425 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13426 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13427 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13428 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13429 dict_add_nr_str(dict, "mode", 0L, mapmode);
13430
13431 vim_free(lhs);
13432 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433 }
13434}
13435
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013436#ifdef FEAT_FLOAT
13437/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013438 * "log()" function
13439 */
13440 static void
13441f_log(argvars, rettv)
13442 typval_T *argvars;
13443 typval_T *rettv;
13444{
13445 float_T f;
13446
13447 rettv->v_type = VAR_FLOAT;
13448 if (get_float_arg(argvars, &f) == OK)
13449 rettv->vval.v_float = log(f);
13450 else
13451 rettv->vval.v_float = 0.0;
13452}
13453
13454/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013455 * "log10()" function
13456 */
13457 static void
13458f_log10(argvars, rettv)
13459 typval_T *argvars;
13460 typval_T *rettv;
13461{
13462 float_T f;
13463
13464 rettv->v_type = VAR_FLOAT;
13465 if (get_float_arg(argvars, &f) == OK)
13466 rettv->vval.v_float = log10(f);
13467 else
13468 rettv->vval.v_float = 0.0;
13469}
13470#endif
13471
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013473 * "map()" function
13474 */
13475 static void
13476f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013477 typval_T *argvars;
13478 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013479{
13480 filter_map(argvars, rettv, TRUE);
13481}
13482
13483/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013484 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485 */
13486 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013487f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013488 typval_T *argvars;
13489 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013491 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492}
13493
13494/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013495 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496 */
13497 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013498f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013499 typval_T *argvars;
13500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013502 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503}
13504
Bram Moolenaar33570922005-01-25 22:26:29 +000013505static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506
13507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013508find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013509 typval_T *argvars;
13510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511 int type;
13512{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013513 char_u *str = NULL;
13514 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515 char_u *pat;
13516 regmatch_T regmatch;
13517 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013518 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519 char_u *save_cpo;
13520 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013521 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013522 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013523 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013524 list_T *l = NULL;
13525 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013526 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013527 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528
13529 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13530 save_cpo = p_cpo;
13531 p_cpo = (char_u *)"";
13532
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013533 rettv->vval.v_number = -1;
13534 if (type == 3)
13535 {
13536 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013537 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013538 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013539 }
13540 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013542 rettv->v_type = VAR_STRING;
13543 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013546 if (argvars[0].v_type == VAR_LIST)
13547 {
13548 if ((l = argvars[0].vval.v_list) == NULL)
13549 goto theend;
13550 li = l->lv_first;
13551 }
13552 else
13553 expr = str = get_tv_string(&argvars[0]);
13554
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013555 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13556 if (pat == NULL)
13557 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013558
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013559 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013561 int error = FALSE;
13562
13563 start = get_tv_number_chk(&argvars[2], &error);
13564 if (error)
13565 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013566 if (l != NULL)
13567 {
13568 li = list_find(l, start);
13569 if (li == NULL)
13570 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013571 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013572 }
13573 else
13574 {
13575 if (start < 0)
13576 start = 0;
13577 if (start > (long)STRLEN(str))
13578 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013579 /* When "count" argument is there ignore matches before "start",
13580 * otherwise skip part of the string. Differs when pattern is "^"
13581 * or "\<". */
13582 if (argvars[3].v_type != VAR_UNKNOWN)
13583 startcol = start;
13584 else
13585 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013586 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013587
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013588 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013589 nth = get_tv_number_chk(&argvars[3], &error);
13590 if (error)
13591 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592 }
13593
13594 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13595 if (regmatch.regprog != NULL)
13596 {
13597 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013598
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013599 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013600 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013601 if (l != NULL)
13602 {
13603 if (li == NULL)
13604 {
13605 match = FALSE;
13606 break;
13607 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013608 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013609 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013610 if (str == NULL)
13611 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013612 }
13613
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013614 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013615
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013616 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013617 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013618 if (l == NULL && !match)
13619 break;
13620
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013621 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013622 if (l != NULL)
13623 {
13624 li = li->li_next;
13625 ++idx;
13626 }
13627 else
13628 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013629#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013630 startcol = (colnr_T)(regmatch.startp[0]
13631 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013632#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013633 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013634#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013635 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013636 }
13637
13638 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013640 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013641 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013642 int i;
13643
13644 /* return list with matched string and submatches */
13645 for (i = 0; i < NSUBEXP; ++i)
13646 {
13647 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013648 {
13649 if (list_append_string(rettv->vval.v_list,
13650 (char_u *)"", 0) == FAIL)
13651 break;
13652 }
13653 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013654 regmatch.startp[i],
13655 (int)(regmatch.endp[i] - regmatch.startp[i]))
13656 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013657 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013658 }
13659 }
13660 else if (type == 2)
13661 {
13662 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013663 if (l != NULL)
13664 copy_tv(&li->li_tv, rettv);
13665 else
13666 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013668 }
13669 else if (l != NULL)
13670 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671 else
13672 {
13673 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013674 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675 (varnumber_T)(regmatch.startp[0] - str);
13676 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013677 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013679 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680 }
13681 }
13682 vim_free(regmatch.regprog);
13683 }
13684
13685theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013686 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687 p_cpo = save_cpo;
13688}
13689
13690/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013691 * "match()" function
13692 */
13693 static void
13694f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013695 typval_T *argvars;
13696 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013697{
13698 find_some_match(argvars, rettv, 1);
13699}
13700
13701/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013702 * "matchadd()" function
13703 */
13704 static void
13705f_matchadd(argvars, rettv)
13706 typval_T *argvars;
13707 typval_T *rettv;
13708{
13709#ifdef FEAT_SEARCH_EXTRA
13710 char_u buf[NUMBUFLEN];
13711 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13712 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13713 int prio = 10; /* default priority */
13714 int id = -1;
13715 int error = FALSE;
13716
13717 rettv->vval.v_number = -1;
13718
13719 if (grp == NULL || pat == NULL)
13720 return;
13721 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013722 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013723 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013724 if (argvars[3].v_type != VAR_UNKNOWN)
13725 id = get_tv_number_chk(&argvars[3], &error);
13726 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013727 if (error == TRUE)
13728 return;
13729 if (id >= 1 && id <= 3)
13730 {
13731 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13732 return;
13733 }
13734
13735 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13736#endif
13737}
13738
13739/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013740 * "matcharg()" function
13741 */
13742 static void
13743f_matcharg(argvars, rettv)
13744 typval_T *argvars;
13745 typval_T *rettv;
13746{
13747 if (rettv_list_alloc(rettv) == OK)
13748 {
13749#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013750 int id = get_tv_number(&argvars[0]);
13751 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013752
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013753 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013754 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013755 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13756 {
13757 list_append_string(rettv->vval.v_list,
13758 syn_id2name(m->hlg_id), -1);
13759 list_append_string(rettv->vval.v_list, m->pattern, -1);
13760 }
13761 else
13762 {
13763 list_append_string(rettv->vval.v_list, NUL, -1);
13764 list_append_string(rettv->vval.v_list, NUL, -1);
13765 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013766 }
13767#endif
13768 }
13769}
13770
13771/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013772 * "matchdelete()" function
13773 */
13774 static void
13775f_matchdelete(argvars, rettv)
13776 typval_T *argvars;
13777 typval_T *rettv;
13778{
13779#ifdef FEAT_SEARCH_EXTRA
13780 rettv->vval.v_number = match_delete(curwin,
13781 (int)get_tv_number(&argvars[0]), TRUE);
13782#endif
13783}
13784
13785/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013786 * "matchend()" function
13787 */
13788 static void
13789f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013790 typval_T *argvars;
13791 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013792{
13793 find_some_match(argvars, rettv, 0);
13794}
13795
13796/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013797 * "matchlist()" function
13798 */
13799 static void
13800f_matchlist(argvars, rettv)
13801 typval_T *argvars;
13802 typval_T *rettv;
13803{
13804 find_some_match(argvars, rettv, 3);
13805}
13806
13807/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013808 * "matchstr()" function
13809 */
13810 static void
13811f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013812 typval_T *argvars;
13813 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013814{
13815 find_some_match(argvars, rettv, 2);
13816}
13817
Bram Moolenaar33570922005-01-25 22:26:29 +000013818static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013819
13820 static void
13821max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013822 typval_T *argvars;
13823 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013824 int domax;
13825{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013826 long n = 0;
13827 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013828 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013829
13830 if (argvars[0].v_type == VAR_LIST)
13831 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013832 list_T *l;
13833 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013834
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013835 l = argvars[0].vval.v_list;
13836 if (l != NULL)
13837 {
13838 li = l->lv_first;
13839 if (li != NULL)
13840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013841 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013842 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013843 {
13844 li = li->li_next;
13845 if (li == NULL)
13846 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013847 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013848 if (domax ? i > n : i < n)
13849 n = i;
13850 }
13851 }
13852 }
13853 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013854 else if (argvars[0].v_type == VAR_DICT)
13855 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013856 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013857 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013858 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013859 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013860
13861 d = argvars[0].vval.v_dict;
13862 if (d != NULL)
13863 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013864 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013865 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013866 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013867 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013868 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013869 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013870 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013871 if (first)
13872 {
13873 n = i;
13874 first = FALSE;
13875 }
13876 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013877 n = i;
13878 }
13879 }
13880 }
13881 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013882 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013883 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013884 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013885}
13886
13887/*
13888 * "max()" function
13889 */
13890 static void
13891f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013892 typval_T *argvars;
13893 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013894{
13895 max_min(argvars, rettv, TRUE);
13896}
13897
13898/*
13899 * "min()" function
13900 */
13901 static void
13902f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013903 typval_T *argvars;
13904 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013905{
13906 max_min(argvars, rettv, FALSE);
13907}
13908
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013909static int mkdir_recurse __ARGS((char_u *dir, int prot));
13910
13911/*
13912 * Create the directory in which "dir" is located, and higher levels when
13913 * needed.
13914 */
13915 static int
13916mkdir_recurse(dir, prot)
13917 char_u *dir;
13918 int prot;
13919{
13920 char_u *p;
13921 char_u *updir;
13922 int r = FAIL;
13923
13924 /* Get end of directory name in "dir".
13925 * We're done when it's "/" or "c:/". */
13926 p = gettail_sep(dir);
13927 if (p <= get_past_head(dir))
13928 return OK;
13929
13930 /* If the directory exists we're done. Otherwise: create it.*/
13931 updir = vim_strnsave(dir, (int)(p - dir));
13932 if (updir == NULL)
13933 return FAIL;
13934 if (mch_isdir(updir))
13935 r = OK;
13936 else if (mkdir_recurse(updir, prot) == OK)
13937 r = vim_mkdir_emsg(updir, prot);
13938 vim_free(updir);
13939 return r;
13940}
13941
13942#ifdef vim_mkdir
13943/*
13944 * "mkdir()" function
13945 */
13946 static void
13947f_mkdir(argvars, rettv)
13948 typval_T *argvars;
13949 typval_T *rettv;
13950{
13951 char_u *dir;
13952 char_u buf[NUMBUFLEN];
13953 int prot = 0755;
13954
13955 rettv->vval.v_number = FAIL;
13956 if (check_restricted() || check_secure())
13957 return;
13958
13959 dir = get_tv_string_buf(&argvars[0], buf);
13960 if (argvars[1].v_type != VAR_UNKNOWN)
13961 {
13962 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013963 prot = get_tv_number_chk(&argvars[2], NULL);
13964 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013965 mkdir_recurse(dir, prot);
13966 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013967 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013968}
13969#endif
13970
Bram Moolenaar0d660222005-01-07 21:51:51 +000013971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972 * "mode()" function
13973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013975f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013976 typval_T *argvars;
13977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013979 char_u buf[3];
13980
13981 buf[1] = NUL;
13982 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983
13984#ifdef FEAT_VISUAL
13985 if (VIsual_active)
13986 {
13987 if (VIsual_select)
13988 buf[0] = VIsual_mode + 's' - 'v';
13989 else
13990 buf[0] = VIsual_mode;
13991 }
13992 else
13993#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013994 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13995 || State == CONFIRM)
13996 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013998 if (State == ASKMORE)
13999 buf[1] = 'm';
14000 else if (State == CONFIRM)
14001 buf[1] = '?';
14002 }
14003 else if (State == EXTERNCMD)
14004 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005 else if (State & INSERT)
14006 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014007#ifdef FEAT_VREPLACE
14008 if (State & VREPLACE_FLAG)
14009 {
14010 buf[0] = 'R';
14011 buf[1] = 'v';
14012 }
14013 else
14014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015 if (State & REPLACE_FLAG)
14016 buf[0] = 'R';
14017 else
14018 buf[0] = 'i';
14019 }
14020 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014021 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014023 if (exmode_active)
14024 buf[1] = 'v';
14025 }
14026 else if (exmode_active)
14027 {
14028 buf[0] = 'c';
14029 buf[1] = 'e';
14030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014032 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014034 if (finish_op)
14035 buf[1] = 'o';
14036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014038 /* Clear out the minor mode when the argument is not a non-zero number or
14039 * non-empty string. */
14040 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014041 buf[1] = NUL;
14042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014043 rettv->vval.v_string = vim_strsave(buf);
14044 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045}
14046
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014047#ifdef FEAT_MZSCHEME
14048/*
14049 * "mzeval()" function
14050 */
14051 static void
14052f_mzeval(argvars, rettv)
14053 typval_T *argvars;
14054 typval_T *rettv;
14055{
14056 char_u *str;
14057 char_u buf[NUMBUFLEN];
14058
14059 str = get_tv_string_buf(&argvars[0], buf);
14060 do_mzeval(str, rettv);
14061}
14062#endif
14063
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014065 * "nextnonblank()" function
14066 */
14067 static void
14068f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014069 typval_T *argvars;
14070 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014071{
14072 linenr_T lnum;
14073
14074 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14075 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014076 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014077 {
14078 lnum = 0;
14079 break;
14080 }
14081 if (*skipwhite(ml_get(lnum)) != NUL)
14082 break;
14083 }
14084 rettv->vval.v_number = lnum;
14085}
14086
14087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088 * "nr2char()" function
14089 */
14090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014091f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014092 typval_T *argvars;
14093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094{
14095 char_u buf[NUMBUFLEN];
14096
14097#ifdef FEAT_MBYTE
14098 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014099 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100 else
14101#endif
14102 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014103 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104 buf[1] = NUL;
14105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014106 rettv->v_type = VAR_STRING;
14107 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014108}
14109
14110/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014111 * "pathshorten()" function
14112 */
14113 static void
14114f_pathshorten(argvars, rettv)
14115 typval_T *argvars;
14116 typval_T *rettv;
14117{
14118 char_u *p;
14119
14120 rettv->v_type = VAR_STRING;
14121 p = get_tv_string_chk(&argvars[0]);
14122 if (p == NULL)
14123 rettv->vval.v_string = NULL;
14124 else
14125 {
14126 p = vim_strsave(p);
14127 rettv->vval.v_string = p;
14128 if (p != NULL)
14129 shorten_dir(p);
14130 }
14131}
14132
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014133#ifdef FEAT_FLOAT
14134/*
14135 * "pow()" function
14136 */
14137 static void
14138f_pow(argvars, rettv)
14139 typval_T *argvars;
14140 typval_T *rettv;
14141{
14142 float_T fx, fy;
14143
14144 rettv->v_type = VAR_FLOAT;
14145 if (get_float_arg(argvars, &fx) == OK
14146 && get_float_arg(&argvars[1], &fy) == OK)
14147 rettv->vval.v_float = pow(fx, fy);
14148 else
14149 rettv->vval.v_float = 0.0;
14150}
14151#endif
14152
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014153/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014154 * "prevnonblank()" function
14155 */
14156 static void
14157f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014158 typval_T *argvars;
14159 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014160{
14161 linenr_T lnum;
14162
14163 lnum = get_tv_lnum(argvars);
14164 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14165 lnum = 0;
14166 else
14167 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14168 --lnum;
14169 rettv->vval.v_number = lnum;
14170}
14171
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014172#ifdef HAVE_STDARG_H
14173/* This dummy va_list is here because:
14174 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14175 * - locally in the function results in a "used before set" warning
14176 * - using va_start() to initialize it gives "function with fixed args" error */
14177static va_list ap;
14178#endif
14179
Bram Moolenaar8c711452005-01-14 21:53:12 +000014180/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014181 * "printf()" function
14182 */
14183 static void
14184f_printf(argvars, rettv)
14185 typval_T *argvars;
14186 typval_T *rettv;
14187{
14188 rettv->v_type = VAR_STRING;
14189 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014190#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014191 {
14192 char_u buf[NUMBUFLEN];
14193 int len;
14194 char_u *s;
14195 int saved_did_emsg = did_emsg;
14196 char *fmt;
14197
14198 /* Get the required length, allocate the buffer and do it for real. */
14199 did_emsg = FALSE;
14200 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014201 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014202 if (!did_emsg)
14203 {
14204 s = alloc(len + 1);
14205 if (s != NULL)
14206 {
14207 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014208 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014209 }
14210 }
14211 did_emsg |= saved_did_emsg;
14212 }
14213#endif
14214}
14215
14216/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014217 * "pumvisible()" function
14218 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014219 static void
14220f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014221 typval_T *argvars UNUSED;
14222 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014223{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014224#ifdef FEAT_INS_EXPAND
14225 if (pum_visible())
14226 rettv->vval.v_number = 1;
14227#endif
14228}
14229
14230/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014231 * "range()" function
14232 */
14233 static void
14234f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014235 typval_T *argvars;
14236 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014237{
14238 long start;
14239 long end;
14240 long stride = 1;
14241 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014242 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014243
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014244 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014245 if (argvars[1].v_type == VAR_UNKNOWN)
14246 {
14247 end = start - 1;
14248 start = 0;
14249 }
14250 else
14251 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014252 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014253 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014254 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014255 }
14256
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014257 if (error)
14258 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014259 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014260 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014261 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014262 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014263 else
14264 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014265 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014266 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014267 if (list_append_number(rettv->vval.v_list,
14268 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014269 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014270 }
14271}
14272
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014273/*
14274 * "readfile()" function
14275 */
14276 static void
14277f_readfile(argvars, rettv)
14278 typval_T *argvars;
14279 typval_T *rettv;
14280{
14281 int binary = FALSE;
14282 char_u *fname;
14283 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014284 listitem_T *li;
14285#define FREAD_SIZE 200 /* optimized for text lines */
14286 char_u buf[FREAD_SIZE];
14287 int readlen; /* size of last fread() */
14288 int buflen; /* nr of valid chars in buf[] */
14289 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14290 int tolist; /* first byte in buf[] still to be put in list */
14291 int chop; /* how many CR to chop off */
14292 char_u *prev = NULL; /* previously read bytes, if any */
14293 int prevlen = 0; /* length of "prev" if not NULL */
14294 char_u *s;
14295 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014296 long maxline = MAXLNUM;
14297 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014298
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014299 if (argvars[1].v_type != VAR_UNKNOWN)
14300 {
14301 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14302 binary = TRUE;
14303 if (argvars[2].v_type != VAR_UNKNOWN)
14304 maxline = get_tv_number(&argvars[2]);
14305 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014306
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014307 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014308 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014309
14310 /* Always open the file in binary mode, library functions have a mind of
14311 * their own about CR-LF conversion. */
14312 fname = get_tv_string(&argvars[0]);
14313 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14314 {
14315 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14316 return;
14317 }
14318
14319 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014320 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014321 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014322 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014323 buflen = filtd + readlen;
14324 tolist = 0;
14325 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14326 {
Bram Moolenaarf56a6de2011-07-07 17:36:56 +020014327 if (readlen <= 0 || buf[filtd] == '\n')
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014328 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014329 /* In binary mode add an empty list item when the last
14330 * non-empty line ends in a '\n'. */
14331 if (!binary && readlen == 0 && filtd == 0 && prev == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014332 break;
14333
14334 /* Found end-of-line or end-of-file: add a text line to the
14335 * list. */
14336 chop = 0;
14337 if (!binary)
14338 while (filtd - chop - 1 >= tolist
14339 && buf[filtd - chop - 1] == '\r')
14340 ++chop;
14341 len = filtd - tolist - chop;
14342 if (prev == NULL)
14343 s = vim_strnsave(buf + tolist, len);
14344 else
14345 {
14346 s = alloc((unsigned)(prevlen + len + 1));
14347 if (s != NULL)
14348 {
14349 mch_memmove(s, prev, prevlen);
14350 vim_free(prev);
14351 prev = NULL;
14352 mch_memmove(s + prevlen, buf + tolist, len);
14353 s[prevlen + len] = NUL;
14354 }
14355 }
14356 tolist = filtd + 1;
14357
14358 li = listitem_alloc();
14359 if (li == NULL)
14360 {
14361 vim_free(s);
14362 break;
14363 }
14364 li->li_tv.v_type = VAR_STRING;
14365 li->li_tv.v_lock = 0;
14366 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014367 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014368
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014369 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014370 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014371 if (readlen <= 0)
14372 break;
14373 }
14374 else if (buf[filtd] == NUL)
14375 buf[filtd] = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014376#ifdef FEAT_MBYTE
14377 else if (buf[filtd] == 0xef
14378 && enc_utf8
14379 && filtd + 2 < buflen
14380 && !binary
14381 && buf[filtd + 1] == 0xbb
14382 && buf[filtd + 2] == 0xbf)
14383 {
14384 /* remove utf-8 byte order mark */
14385 mch_memmove(buf + filtd, buf + filtd + 3, buflen - filtd - 3);
14386 --filtd;
14387 buflen -= 3;
14388 }
14389#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014390 }
14391 if (readlen <= 0)
14392 break;
14393
14394 if (tolist == 0)
14395 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014396 if (buflen >= FREAD_SIZE / 2)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014397 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014398 /* "buf" is full, need to move text to an allocated buffer */
14399 if (prev == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014400 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014401 prev = vim_strnsave(buf, buflen);
14402 prevlen = buflen;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014403 }
Bram Moolenaar27b60562011-04-01 16:07:46 +020014404 else
14405 {
14406 s = alloc((unsigned)(prevlen + buflen));
14407 if (s != NULL)
14408 {
14409 mch_memmove(s, prev, prevlen);
14410 mch_memmove(s + prevlen, buf, buflen);
14411 vim_free(prev);
14412 prev = s;
14413 prevlen += buflen;
14414 }
14415 }
14416 filtd = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014417 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014418 }
14419 else
14420 {
14421 mch_memmove(buf, buf + tolist, buflen - tolist);
14422 filtd -= tolist;
14423 }
14424 }
14425
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014426 /*
14427 * For a negative line count use only the lines at the end of the file,
14428 * free the rest.
14429 */
14430 if (maxline < 0)
14431 while (cnt > -maxline)
14432 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014433 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014434 --cnt;
14435 }
14436
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014437 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014438 fclose(fd);
14439}
14440
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014441#if defined(FEAT_RELTIME)
14442static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14443
14444/*
14445 * Convert a List to proftime_T.
14446 * Return FAIL when there is something wrong.
14447 */
14448 static int
14449list2proftime(arg, tm)
14450 typval_T *arg;
14451 proftime_T *tm;
14452{
14453 long n1, n2;
14454 int error = FALSE;
14455
14456 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14457 || arg->vval.v_list->lv_len != 2)
14458 return FAIL;
14459 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14460 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14461# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014462 tm->HighPart = n1;
14463 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014464# else
14465 tm->tv_sec = n1;
14466 tm->tv_usec = n2;
14467# endif
14468 return error ? FAIL : OK;
14469}
14470#endif /* FEAT_RELTIME */
14471
14472/*
14473 * "reltime()" function
14474 */
14475 static void
14476f_reltime(argvars, rettv)
14477 typval_T *argvars;
14478 typval_T *rettv;
14479{
14480#ifdef FEAT_RELTIME
14481 proftime_T res;
14482 proftime_T start;
14483
14484 if (argvars[0].v_type == VAR_UNKNOWN)
14485 {
14486 /* No arguments: get current time. */
14487 profile_start(&res);
14488 }
14489 else if (argvars[1].v_type == VAR_UNKNOWN)
14490 {
14491 if (list2proftime(&argvars[0], &res) == FAIL)
14492 return;
14493 profile_end(&res);
14494 }
14495 else
14496 {
14497 /* Two arguments: compute the difference. */
14498 if (list2proftime(&argvars[0], &start) == FAIL
14499 || list2proftime(&argvars[1], &res) == FAIL)
14500 return;
14501 profile_sub(&res, &start);
14502 }
14503
14504 if (rettv_list_alloc(rettv) == OK)
14505 {
14506 long n1, n2;
14507
14508# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014509 n1 = res.HighPart;
14510 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014511# else
14512 n1 = res.tv_sec;
14513 n2 = res.tv_usec;
14514# endif
14515 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14516 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14517 }
14518#endif
14519}
14520
14521/*
14522 * "reltimestr()" function
14523 */
14524 static void
14525f_reltimestr(argvars, rettv)
14526 typval_T *argvars;
14527 typval_T *rettv;
14528{
14529#ifdef FEAT_RELTIME
14530 proftime_T tm;
14531#endif
14532
14533 rettv->v_type = VAR_STRING;
14534 rettv->vval.v_string = NULL;
14535#ifdef FEAT_RELTIME
14536 if (list2proftime(&argvars[0], &tm) == OK)
14537 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14538#endif
14539}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014540
Bram Moolenaar0d660222005-01-07 21:51:51 +000014541#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14542static void make_connection __ARGS((void));
14543static int check_connection __ARGS((void));
14544
14545 static void
14546make_connection()
14547{
14548 if (X_DISPLAY == NULL
14549# ifdef FEAT_GUI
14550 && !gui.in_use
14551# endif
14552 )
14553 {
14554 x_force_connect = TRUE;
14555 setup_term_clip();
14556 x_force_connect = FALSE;
14557 }
14558}
14559
14560 static int
14561check_connection()
14562{
14563 make_connection();
14564 if (X_DISPLAY == NULL)
14565 {
14566 EMSG(_("E240: No connection to Vim server"));
14567 return FAIL;
14568 }
14569 return OK;
14570}
14571#endif
14572
14573#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014574static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014575
14576 static void
14577remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014578 typval_T *argvars;
14579 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014580 int expr;
14581{
14582 char_u *server_name;
14583 char_u *keys;
14584 char_u *r = NULL;
14585 char_u buf[NUMBUFLEN];
14586# ifdef WIN32
14587 HWND w;
14588# else
14589 Window w;
14590# endif
14591
14592 if (check_restricted() || check_secure())
14593 return;
14594
14595# ifdef FEAT_X11
14596 if (check_connection() == FAIL)
14597 return;
14598# endif
14599
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014600 server_name = get_tv_string_chk(&argvars[0]);
14601 if (server_name == NULL)
14602 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014603 keys = get_tv_string_buf(&argvars[1], buf);
14604# ifdef WIN32
14605 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14606# else
14607 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14608 < 0)
14609# endif
14610 {
14611 if (r != NULL)
14612 EMSG(r); /* sending worked but evaluation failed */
14613 else
14614 EMSG2(_("E241: Unable to send to %s"), server_name);
14615 return;
14616 }
14617
14618 rettv->vval.v_string = r;
14619
14620 if (argvars[2].v_type != VAR_UNKNOWN)
14621 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014622 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014623 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014624 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014625
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014626 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014627 v.di_tv.v_type = VAR_STRING;
14628 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014629 idvar = get_tv_string_chk(&argvars[2]);
14630 if (idvar != NULL)
14631 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014632 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014633 }
14634}
14635#endif
14636
14637/*
14638 * "remote_expr()" function
14639 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014640 static void
14641f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014642 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014643 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014644{
14645 rettv->v_type = VAR_STRING;
14646 rettv->vval.v_string = NULL;
14647#ifdef FEAT_CLIENTSERVER
14648 remote_common(argvars, rettv, TRUE);
14649#endif
14650}
14651
14652/*
14653 * "remote_foreground()" function
14654 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014655 static void
14656f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014657 typval_T *argvars UNUSED;
14658 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014659{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014660#ifdef FEAT_CLIENTSERVER
14661# ifdef WIN32
14662 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014663 {
14664 char_u *server_name = get_tv_string_chk(&argvars[0]);
14665
14666 if (server_name != NULL)
14667 serverForeground(server_name);
14668 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014669# else
14670 /* Send a foreground() expression to the server. */
14671 argvars[1].v_type = VAR_STRING;
14672 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14673 argvars[2].v_type = VAR_UNKNOWN;
14674 remote_common(argvars, rettv, TRUE);
14675 vim_free(argvars[1].vval.v_string);
14676# endif
14677#endif
14678}
14679
Bram Moolenaar0d660222005-01-07 21:51:51 +000014680 static void
14681f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014682 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014683 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014684{
14685#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014686 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014687 char_u *s = NULL;
14688# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014689 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014690# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014691 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014692
14693 if (check_restricted() || check_secure())
14694 {
14695 rettv->vval.v_number = -1;
14696 return;
14697 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014698 serverid = get_tv_string_chk(&argvars[0]);
14699 if (serverid == NULL)
14700 {
14701 rettv->vval.v_number = -1;
14702 return; /* type error; errmsg already given */
14703 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014704# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014705 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014706 if (n == 0)
14707 rettv->vval.v_number = -1;
14708 else
14709 {
14710 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14711 rettv->vval.v_number = (s != NULL);
14712 }
14713# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014714 if (check_connection() == FAIL)
14715 return;
14716
14717 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014718 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014719# endif
14720
14721 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14722 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014723 char_u *retvar;
14724
Bram Moolenaar33570922005-01-25 22:26:29 +000014725 v.di_tv.v_type = VAR_STRING;
14726 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014727 retvar = get_tv_string_chk(&argvars[1]);
14728 if (retvar != NULL)
14729 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014730 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014731 }
14732#else
14733 rettv->vval.v_number = -1;
14734#endif
14735}
14736
Bram Moolenaar0d660222005-01-07 21:51:51 +000014737 static void
14738f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014739 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014740 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014741{
14742 char_u *r = NULL;
14743
14744#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014745 char_u *serverid = get_tv_string_chk(&argvars[0]);
14746
14747 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014748 {
14749# ifdef WIN32
14750 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014751 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014752
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014753 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014754 if (n != 0)
14755 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14756 if (r == NULL)
14757# else
14758 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014759 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014760# endif
14761 EMSG(_("E277: Unable to read a server reply"));
14762 }
14763#endif
14764 rettv->v_type = VAR_STRING;
14765 rettv->vval.v_string = r;
14766}
14767
14768/*
14769 * "remote_send()" function
14770 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014771 static void
14772f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014773 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014774 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014775{
14776 rettv->v_type = VAR_STRING;
14777 rettv->vval.v_string = NULL;
14778#ifdef FEAT_CLIENTSERVER
14779 remote_common(argvars, rettv, FALSE);
14780#endif
14781}
14782
14783/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014784 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014785 */
14786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014787f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014788 typval_T *argvars;
14789 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014790{
Bram Moolenaar33570922005-01-25 22:26:29 +000014791 list_T *l;
14792 listitem_T *item, *item2;
14793 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014794 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014795 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014796 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014797 dict_T *d;
14798 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014799 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014800
Bram Moolenaar8c711452005-01-14 21:53:12 +000014801 if (argvars[0].v_type == VAR_DICT)
14802 {
14803 if (argvars[2].v_type != VAR_UNKNOWN)
14804 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014805 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014806 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014807 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014808 key = get_tv_string_chk(&argvars[1]);
14809 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014811 di = dict_find(d, key, -1);
14812 if (di == NULL)
14813 EMSG2(_(e_dictkey), key);
14814 else
14815 {
14816 *rettv = di->di_tv;
14817 init_tv(&di->di_tv);
14818 dictitem_remove(d, di);
14819 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014820 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014821 }
14822 }
14823 else if (argvars[0].v_type != VAR_LIST)
14824 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014825 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014826 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014828 int error = FALSE;
14829
14830 idx = get_tv_number_chk(&argvars[1], &error);
14831 if (error)
14832 ; /* type error: do nothing, errmsg already given */
14833 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014834 EMSGN(_(e_listidx), idx);
14835 else
14836 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014837 if (argvars[2].v_type == VAR_UNKNOWN)
14838 {
14839 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014840 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014841 *rettv = item->li_tv;
14842 vim_free(item);
14843 }
14844 else
14845 {
14846 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014847 end = get_tv_number_chk(&argvars[2], &error);
14848 if (error)
14849 ; /* type error: do nothing */
14850 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014851 EMSGN(_(e_listidx), end);
14852 else
14853 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014854 int cnt = 0;
14855
14856 for (li = item; li != NULL; li = li->li_next)
14857 {
14858 ++cnt;
14859 if (li == item2)
14860 break;
14861 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014862 if (li == NULL) /* didn't find "item2" after "item" */
14863 EMSG(_(e_invrange));
14864 else
14865 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014866 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014867 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014868 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014869 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014870 l->lv_first = item;
14871 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014872 item->li_prev = NULL;
14873 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014874 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014875 }
14876 }
14877 }
14878 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014879 }
14880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014881}
14882
14883/*
14884 * "rename({from}, {to})" function
14885 */
14886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014887f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014888 typval_T *argvars;
14889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014890{
14891 char_u buf[NUMBUFLEN];
14892
14893 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014894 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014896 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14897 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014898}
14899
14900/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014901 * "repeat()" function
14902 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014903 static void
14904f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014905 typval_T *argvars;
14906 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014907{
14908 char_u *p;
14909 int n;
14910 int slen;
14911 int len;
14912 char_u *r;
14913 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014914
14915 n = get_tv_number(&argvars[1]);
14916 if (argvars[0].v_type == VAR_LIST)
14917 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014918 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014919 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014920 if (list_extend(rettv->vval.v_list,
14921 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014922 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014923 }
14924 else
14925 {
14926 p = get_tv_string(&argvars[0]);
14927 rettv->v_type = VAR_STRING;
14928 rettv->vval.v_string = NULL;
14929
14930 slen = (int)STRLEN(p);
14931 len = slen * n;
14932 if (len <= 0)
14933 return;
14934
14935 r = alloc(len + 1);
14936 if (r != NULL)
14937 {
14938 for (i = 0; i < n; i++)
14939 mch_memmove(r + i * slen, p, (size_t)slen);
14940 r[len] = NUL;
14941 }
14942
14943 rettv->vval.v_string = r;
14944 }
14945}
14946
14947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014948 * "resolve()" function
14949 */
14950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014951f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014952 typval_T *argvars;
14953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014954{
14955 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020014956#ifdef HAVE_READLINK
14957 char_u *buf = NULL;
14958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014960 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961#ifdef FEAT_SHORTCUT
14962 {
14963 char_u *v = NULL;
14964
14965 v = mch_resolve_shortcut(p);
14966 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014967 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014969 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970 }
14971#else
14972# ifdef HAVE_READLINK
14973 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974 char_u *cpy;
14975 int len;
14976 char_u *remain = NULL;
14977 char_u *q;
14978 int is_relative_to_current = FALSE;
14979 int has_trailing_pathsep = FALSE;
14980 int limit = 100;
14981
14982 p = vim_strsave(p);
14983
14984 if (p[0] == '.' && (vim_ispathsep(p[1])
14985 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14986 is_relative_to_current = TRUE;
14987
14988 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014989 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020014990 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020014992 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
14993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994
14995 q = getnextcomp(p);
14996 if (*q != NUL)
14997 {
14998 /* Separate the first path component in "p", and keep the
14999 * remainder (beginning with the path separator). */
15000 remain = vim_strsave(q - 1);
15001 q[-1] = NUL;
15002 }
15003
Bram Moolenaard9462e32011-04-11 21:35:11 +020015004 buf = alloc(MAXPATHL + 1);
15005 if (buf == NULL)
15006 goto fail;
15007
Bram Moolenaar071d4272004-06-13 20:20:40 +000015008 for (;;)
15009 {
15010 for (;;)
15011 {
15012 len = readlink((char *)p, (char *)buf, MAXPATHL);
15013 if (len <= 0)
15014 break;
15015 buf[len] = NUL;
15016
15017 if (limit-- == 0)
15018 {
15019 vim_free(p);
15020 vim_free(remain);
15021 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015022 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015023 goto fail;
15024 }
15025
15026 /* Ensure that the result will have a trailing path separator
15027 * if the argument has one. */
15028 if (remain == NULL && has_trailing_pathsep)
15029 add_pathsep(buf);
15030
15031 /* Separate the first path component in the link value and
15032 * concatenate the remainders. */
15033 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15034 if (*q != NUL)
15035 {
15036 if (remain == NULL)
15037 remain = vim_strsave(q - 1);
15038 else
15039 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015040 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041 if (cpy != NULL)
15042 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043 vim_free(remain);
15044 remain = cpy;
15045 }
15046 }
15047 q[-1] = NUL;
15048 }
15049
15050 q = gettail(p);
15051 if (q > p && *q == NUL)
15052 {
15053 /* Ignore trailing path separator. */
15054 q[-1] = NUL;
15055 q = gettail(p);
15056 }
15057 if (q > p && !mch_isFullName(buf))
15058 {
15059 /* symlink is relative to directory of argument */
15060 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15061 if (cpy != NULL)
15062 {
15063 STRCPY(cpy, p);
15064 STRCPY(gettail(cpy), buf);
15065 vim_free(p);
15066 p = cpy;
15067 }
15068 }
15069 else
15070 {
15071 vim_free(p);
15072 p = vim_strsave(buf);
15073 }
15074 }
15075
15076 if (remain == NULL)
15077 break;
15078
15079 /* Append the first path component of "remain" to "p". */
15080 q = getnextcomp(remain + 1);
15081 len = q - remain - (*q != NUL);
15082 cpy = vim_strnsave(p, STRLEN(p) + len);
15083 if (cpy != NULL)
15084 {
15085 STRNCAT(cpy, remain, len);
15086 vim_free(p);
15087 p = cpy;
15088 }
15089 /* Shorten "remain". */
15090 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015091 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015092 else
15093 {
15094 vim_free(remain);
15095 remain = NULL;
15096 }
15097 }
15098
15099 /* If the result is a relative path name, make it explicitly relative to
15100 * the current directory if and only if the argument had this form. */
15101 if (!vim_ispathsep(*p))
15102 {
15103 if (is_relative_to_current
15104 && *p != NUL
15105 && !(p[0] == '.'
15106 && (p[1] == NUL
15107 || vim_ispathsep(p[1])
15108 || (p[1] == '.'
15109 && (p[2] == NUL
15110 || vim_ispathsep(p[2]))))))
15111 {
15112 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015113 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015114 if (cpy != NULL)
15115 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015116 vim_free(p);
15117 p = cpy;
15118 }
15119 }
15120 else if (!is_relative_to_current)
15121 {
15122 /* Strip leading "./". */
15123 q = p;
15124 while (q[0] == '.' && vim_ispathsep(q[1]))
15125 q += 2;
15126 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015127 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015128 }
15129 }
15130
15131 /* Ensure that the result will have no trailing path separator
15132 * if the argument had none. But keep "/" or "//". */
15133 if (!has_trailing_pathsep)
15134 {
15135 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015136 if (after_pathsep(p, q))
15137 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138 }
15139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015140 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141 }
15142# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015143 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144# endif
15145#endif
15146
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015147 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015148
15149#ifdef HAVE_READLINK
15150fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015151 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015153 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015154}
15155
15156/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015157 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015158 */
15159 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015160f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015161 typval_T *argvars;
15162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015163{
Bram Moolenaar33570922005-01-25 22:26:29 +000015164 list_T *l;
15165 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015166
Bram Moolenaar0d660222005-01-07 21:51:51 +000015167 if (argvars[0].v_type != VAR_LIST)
15168 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015169 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015170 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015171 {
15172 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015173 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015174 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015175 while (li != NULL)
15176 {
15177 ni = li->li_prev;
15178 list_append(l, li);
15179 li = ni;
15180 }
15181 rettv->vval.v_list = l;
15182 rettv->v_type = VAR_LIST;
15183 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015184 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186}
15187
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015188#define SP_NOMOVE 0x01 /* don't move cursor */
15189#define SP_REPEAT 0x02 /* repeat to find outer pair */
15190#define SP_RETCOUNT 0x04 /* return matchcount */
15191#define SP_SETPCMARK 0x08 /* set previous context mark */
15192#define SP_START 0x10 /* accept match at start position */
15193#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15194#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015195
Bram Moolenaar33570922005-01-25 22:26:29 +000015196static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015197
15198/*
15199 * Get flags for a search function.
15200 * Possibly sets "p_ws".
15201 * Returns BACKWARD, FORWARD or zero (for an error).
15202 */
15203 static int
15204get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015205 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015206 int *flagsp;
15207{
15208 int dir = FORWARD;
15209 char_u *flags;
15210 char_u nbuf[NUMBUFLEN];
15211 int mask;
15212
15213 if (varp->v_type != VAR_UNKNOWN)
15214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015215 flags = get_tv_string_buf_chk(varp, nbuf);
15216 if (flags == NULL)
15217 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015218 while (*flags != NUL)
15219 {
15220 switch (*flags)
15221 {
15222 case 'b': dir = BACKWARD; break;
15223 case 'w': p_ws = TRUE; break;
15224 case 'W': p_ws = FALSE; break;
15225 default: mask = 0;
15226 if (flagsp != NULL)
15227 switch (*flags)
15228 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015229 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015230 case 'e': mask = SP_END; break;
15231 case 'm': mask = SP_RETCOUNT; break;
15232 case 'n': mask = SP_NOMOVE; break;
15233 case 'p': mask = SP_SUBPAT; break;
15234 case 'r': mask = SP_REPEAT; break;
15235 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015236 }
15237 if (mask == 0)
15238 {
15239 EMSG2(_(e_invarg2), flags);
15240 dir = 0;
15241 }
15242 else
15243 *flagsp |= mask;
15244 }
15245 if (dir == 0)
15246 break;
15247 ++flags;
15248 }
15249 }
15250 return dir;
15251}
15252
Bram Moolenaar071d4272004-06-13 20:20:40 +000015253/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015254 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015255 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015256 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015257search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015258 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015259 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015260 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015261{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015262 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015263 char_u *pat;
15264 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015265 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015266 int save_p_ws = p_ws;
15267 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015268 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015269 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015270 proftime_T tm;
15271#ifdef FEAT_RELTIME
15272 long time_limit = 0;
15273#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015274 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015275 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015276
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015277 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015278 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015279 if (dir == 0)
15280 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015281 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015282 if (flags & SP_START)
15283 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015284 if (flags & SP_END)
15285 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015286
Bram Moolenaar76929292008-01-06 19:07:36 +000015287 /* Optional arguments: line number to stop searching and timeout. */
15288 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015289 {
15290 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15291 if (lnum_stop < 0)
15292 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015293#ifdef FEAT_RELTIME
15294 if (argvars[3].v_type != VAR_UNKNOWN)
15295 {
15296 time_limit = get_tv_number_chk(&argvars[3], NULL);
15297 if (time_limit < 0)
15298 goto theend;
15299 }
15300#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015301 }
15302
Bram Moolenaar76929292008-01-06 19:07:36 +000015303#ifdef FEAT_RELTIME
15304 /* Set the time limit, if there is one. */
15305 profile_setlimit(time_limit, &tm);
15306#endif
15307
Bram Moolenaar231334e2005-07-25 20:46:57 +000015308 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015309 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015310 * Check to make sure only those flags are set.
15311 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15312 * flags cannot be set. Check for that condition also.
15313 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015314 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015315 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015316 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015317 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015318 goto theend;
15319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015321 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015322 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015323 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015324 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015326 if (flags & SP_SUBPAT)
15327 retval = subpatnum;
15328 else
15329 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015330 if (flags & SP_SETPCMARK)
15331 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015333 if (match_pos != NULL)
15334 {
15335 /* Store the match cursor position */
15336 match_pos->lnum = pos.lnum;
15337 match_pos->col = pos.col + 1;
15338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339 /* "/$" will put the cursor after the end of the line, may need to
15340 * correct that here */
15341 check_cursor();
15342 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015343
15344 /* If 'n' flag is used: restore cursor position. */
15345 if (flags & SP_NOMOVE)
15346 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015347 else
15348 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015349theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015351
15352 return retval;
15353}
15354
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015355#ifdef FEAT_FLOAT
15356/*
15357 * "round({float})" function
15358 */
15359 static void
15360f_round(argvars, rettv)
15361 typval_T *argvars;
15362 typval_T *rettv;
15363{
15364 float_T f;
15365
15366 rettv->v_type = VAR_FLOAT;
15367 if (get_float_arg(argvars, &f) == OK)
15368 /* round() is not in C90, use ceil() or floor() instead. */
15369 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15370 else
15371 rettv->vval.v_float = 0.0;
15372}
15373#endif
15374
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015375/*
15376 * "search()" function
15377 */
15378 static void
15379f_search(argvars, rettv)
15380 typval_T *argvars;
15381 typval_T *rettv;
15382{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015383 int flags = 0;
15384
15385 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015386}
15387
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015389 * "searchdecl()" function
15390 */
15391 static void
15392f_searchdecl(argvars, rettv)
15393 typval_T *argvars;
15394 typval_T *rettv;
15395{
15396 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015397 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015398 int error = FALSE;
15399 char_u *name;
15400
15401 rettv->vval.v_number = 1; /* default: FAIL */
15402
15403 name = get_tv_string_chk(&argvars[0]);
15404 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015405 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015406 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015407 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15408 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15409 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015410 if (!error && name != NULL)
15411 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015412 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015413}
15414
15415/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015416 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015417 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015418 static int
15419searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015420 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015421 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015422{
15423 char_u *spat, *mpat, *epat;
15424 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015425 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015426 int dir;
15427 int flags = 0;
15428 char_u nbuf1[NUMBUFLEN];
15429 char_u nbuf2[NUMBUFLEN];
15430 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015431 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015432 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015433 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015436 spat = get_tv_string_chk(&argvars[0]);
15437 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15438 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15439 if (spat == NULL || mpat == NULL || epat == NULL)
15440 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015441
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442 /* Handle the optional fourth argument: flags */
15443 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015444 if (dir == 0)
15445 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015446
15447 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015448 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15449 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015450 if ((flags & (SP_END | SP_SUBPAT)) != 0
15451 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015452 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015453 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015454 goto theend;
15455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015456
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015457 /* Using 'r' implies 'W', otherwise it doesn't work. */
15458 if (flags & SP_REPEAT)
15459 p_ws = FALSE;
15460
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015461 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015462 if (argvars[3].v_type == VAR_UNKNOWN
15463 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464 skip = (char_u *)"";
15465 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015467 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015468 if (argvars[5].v_type != VAR_UNKNOWN)
15469 {
15470 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15471 if (lnum_stop < 0)
15472 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015473#ifdef FEAT_RELTIME
15474 if (argvars[6].v_type != VAR_UNKNOWN)
15475 {
15476 time_limit = get_tv_number_chk(&argvars[6], NULL);
15477 if (time_limit < 0)
15478 goto theend;
15479 }
15480#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015481 }
15482 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015483 if (skip == NULL)
15484 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015486 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015487 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015488
15489theend:
15490 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015491
15492 return retval;
15493}
15494
15495/*
15496 * "searchpair()" function
15497 */
15498 static void
15499f_searchpair(argvars, rettv)
15500 typval_T *argvars;
15501 typval_T *rettv;
15502{
15503 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15504}
15505
15506/*
15507 * "searchpairpos()" function
15508 */
15509 static void
15510f_searchpairpos(argvars, rettv)
15511 typval_T *argvars;
15512 typval_T *rettv;
15513{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015514 pos_T match_pos;
15515 int lnum = 0;
15516 int col = 0;
15517
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015518 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015519 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015520
15521 if (searchpair_cmn(argvars, &match_pos) > 0)
15522 {
15523 lnum = match_pos.lnum;
15524 col = match_pos.col;
15525 }
15526
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015527 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15528 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015529}
15530
15531/*
15532 * Search for a start/middle/end thing.
15533 * Used by searchpair(), see its documentation for the details.
15534 * Returns 0 or -1 for no match,
15535 */
15536 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015537do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15538 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015539 char_u *spat; /* start pattern */
15540 char_u *mpat; /* middle pattern */
15541 char_u *epat; /* end pattern */
15542 int dir; /* BACKWARD or FORWARD */
15543 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015544 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015545 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015546 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015547 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015548{
15549 char_u *save_cpo;
15550 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15551 long retval = 0;
15552 pos_T pos;
15553 pos_T firstpos;
15554 pos_T foundpos;
15555 pos_T save_cursor;
15556 pos_T save_pos;
15557 int n;
15558 int r;
15559 int nest = 1;
15560 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015561 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015562 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015563
15564 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15565 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015566 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015567
Bram Moolenaar76929292008-01-06 19:07:36 +000015568#ifdef FEAT_RELTIME
15569 /* Set the time limit, if there is one. */
15570 profile_setlimit(time_limit, &tm);
15571#endif
15572
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015573 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15574 * start/middle/end (pat3, for the top pair). */
15575 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15576 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15577 if (pat2 == NULL || pat3 == NULL)
15578 goto theend;
15579 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15580 if (*mpat == NUL)
15581 STRCPY(pat3, pat2);
15582 else
15583 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15584 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015585 if (flags & SP_START)
15586 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015587
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588 save_cursor = curwin->w_cursor;
15589 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015590 clearpos(&firstpos);
15591 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592 pat = pat3;
15593 for (;;)
15594 {
15595 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015596 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015597 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15598 /* didn't find it or found the first match again: FAIL */
15599 break;
15600
15601 if (firstpos.lnum == 0)
15602 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015603 if (equalpos(pos, foundpos))
15604 {
15605 /* Found the same position again. Can happen with a pattern that
15606 * has "\zs" at the end and searching backwards. Advance one
15607 * character and try again. */
15608 if (dir == BACKWARD)
15609 decl(&pos);
15610 else
15611 incl(&pos);
15612 }
15613 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015615 /* clear the start flag to avoid getting stuck here */
15616 options &= ~SEARCH_START;
15617
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618 /* If the skip pattern matches, ignore this match. */
15619 if (*skip != NUL)
15620 {
15621 save_pos = curwin->w_cursor;
15622 curwin->w_cursor = pos;
15623 r = eval_to_bool(skip, &err, NULL, FALSE);
15624 curwin->w_cursor = save_pos;
15625 if (err)
15626 {
15627 /* Evaluating {skip} caused an error, break here. */
15628 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015629 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630 break;
15631 }
15632 if (r)
15633 continue;
15634 }
15635
15636 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15637 {
15638 /* Found end when searching backwards or start when searching
15639 * forward: nested pair. */
15640 ++nest;
15641 pat = pat2; /* nested, don't search for middle */
15642 }
15643 else
15644 {
15645 /* Found end when searching forward or start when searching
15646 * backward: end of (nested) pair; or found middle in outer pair. */
15647 if (--nest == 1)
15648 pat = pat3; /* outer level, search for middle */
15649 }
15650
15651 if (nest == 0)
15652 {
15653 /* Found the match: return matchcount or line number. */
15654 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015655 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015656 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015657 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015658 if (flags & SP_SETPCMARK)
15659 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660 curwin->w_cursor = pos;
15661 if (!(flags & SP_REPEAT))
15662 break;
15663 nest = 1; /* search for next unmatched */
15664 }
15665 }
15666
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015667 if (match_pos != NULL)
15668 {
15669 /* Store the match cursor position */
15670 match_pos->lnum = curwin->w_cursor.lnum;
15671 match_pos->col = curwin->w_cursor.col + 1;
15672 }
15673
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015675 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676 curwin->w_cursor = save_cursor;
15677
15678theend:
15679 vim_free(pat2);
15680 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015681 if (p_cpo == empty_option)
15682 p_cpo = save_cpo;
15683 else
15684 /* Darn, evaluating the {skip} expression changed the value. */
15685 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015686
15687 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688}
15689
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015690/*
15691 * "searchpos()" function
15692 */
15693 static void
15694f_searchpos(argvars, rettv)
15695 typval_T *argvars;
15696 typval_T *rettv;
15697{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015698 pos_T match_pos;
15699 int lnum = 0;
15700 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015701 int n;
15702 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015703
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015704 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015705 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015706
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015707 n = search_cmn(argvars, &match_pos, &flags);
15708 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015709 {
15710 lnum = match_pos.lnum;
15711 col = match_pos.col;
15712 }
15713
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015714 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15715 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015716 if (flags & SP_SUBPAT)
15717 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015718}
15719
15720
Bram Moolenaar0d660222005-01-07 21:51:51 +000015721 static void
15722f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015723 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015726#ifdef FEAT_CLIENTSERVER
15727 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015728 char_u *server = get_tv_string_chk(&argvars[0]);
15729 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730
Bram Moolenaar0d660222005-01-07 21:51:51 +000015731 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015732 if (server == NULL || reply == NULL)
15733 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015734 if (check_restricted() || check_secure())
15735 return;
15736# ifdef FEAT_X11
15737 if (check_connection() == FAIL)
15738 return;
15739# endif
15740
15741 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015743 EMSG(_("E258: Unable to send to client"));
15744 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015746 rettv->vval.v_number = 0;
15747#else
15748 rettv->vval.v_number = -1;
15749#endif
15750}
15751
Bram Moolenaar0d660222005-01-07 21:51:51 +000015752 static void
15753f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015754 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015755 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015756{
15757 char_u *r = NULL;
15758
15759#ifdef FEAT_CLIENTSERVER
15760# ifdef WIN32
15761 r = serverGetVimNames();
15762# else
15763 make_connection();
15764 if (X_DISPLAY != NULL)
15765 r = serverGetVimNames(X_DISPLAY);
15766# endif
15767#endif
15768 rettv->v_type = VAR_STRING;
15769 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770}
15771
15772/*
15773 * "setbufvar()" function
15774 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015776f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015777 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015778 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779{
15780 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015783 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015784 char_u nbuf[NUMBUFLEN];
15785
15786 if (check_restricted() || check_secure())
15787 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015788 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15789 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015790 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015791 varp = &argvars[2];
15792
15793 if (buf != NULL && varname != NULL && varp != NULL)
15794 {
15795 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015796 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015797
15798 if (*varname == '&')
15799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015800 long numval;
15801 char_u *strval;
15802 int error = FALSE;
15803
Bram Moolenaar071d4272004-06-13 20:20:40 +000015804 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015805 numval = get_tv_number_chk(varp, &error);
15806 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015807 if (!error && strval != NULL)
15808 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809 }
15810 else
15811 {
15812 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15813 if (bufvarname != NULL)
15814 {
15815 STRCPY(bufvarname, "b:");
15816 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015817 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015818 vim_free(bufvarname);
15819 }
15820 }
15821
15822 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015825}
15826
15827/*
15828 * "setcmdpos()" function
15829 */
15830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015831f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015832 typval_T *argvars;
15833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015834{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015835 int pos = (int)get_tv_number(&argvars[0]) - 1;
15836
15837 if (pos >= 0)
15838 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839}
15840
15841/*
15842 * "setline()" function
15843 */
15844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015845f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015846 typval_T *argvars;
15847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848{
15849 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015850 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015851 list_T *l = NULL;
15852 listitem_T *li = NULL;
15853 long added = 0;
15854 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015856 lnum = get_tv_lnum(&argvars[0]);
15857 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015859 l = argvars[1].vval.v_list;
15860 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015861 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015862 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015863 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015864
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015865 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015866 for (;;)
15867 {
15868 if (l != NULL)
15869 {
15870 /* list argument, get next string */
15871 if (li == NULL)
15872 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015873 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015874 li = li->li_next;
15875 }
15876
15877 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015878 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015879 break;
15880 if (lnum <= curbuf->b_ml.ml_line_count)
15881 {
15882 /* existing line, replace it */
15883 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15884 {
15885 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015886 if (lnum == curwin->w_cursor.lnum)
15887 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015888 rettv->vval.v_number = 0; /* OK */
15889 }
15890 }
15891 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15892 {
15893 /* lnum is one past the last line, append the line */
15894 ++added;
15895 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15896 rettv->vval.v_number = 0; /* OK */
15897 }
15898
15899 if (l == NULL) /* only one string argument */
15900 break;
15901 ++lnum;
15902 }
15903
15904 if (added > 0)
15905 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015906}
15907
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015908static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15909
Bram Moolenaar071d4272004-06-13 20:20:40 +000015910/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015911 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015912 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015913 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015914set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015915 win_T *wp UNUSED;
15916 typval_T *list_arg UNUSED;
15917 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015918 typval_T *rettv;
15919{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015920#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015921 char_u *act;
15922 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015923#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015924
Bram Moolenaar2641f772005-03-25 21:58:17 +000015925 rettv->vval.v_number = -1;
15926
15927#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015928 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015929 EMSG(_(e_listreq));
15930 else
15931 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015932 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015933
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015934 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015935 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015936 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015937 if (act == NULL)
15938 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015939 if (*act == 'a' || *act == 'r')
15940 action = *act;
15941 }
15942
Bram Moolenaarbc226b62010-08-09 22:14:48 +020015943 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015944 rettv->vval.v_number = 0;
15945 }
15946#endif
15947}
15948
15949/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015950 * "setloclist()" function
15951 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015952 static void
15953f_setloclist(argvars, rettv)
15954 typval_T *argvars;
15955 typval_T *rettv;
15956{
15957 win_T *win;
15958
15959 rettv->vval.v_number = -1;
15960
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015961 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015962 if (win != NULL)
15963 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15964}
15965
15966/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015967 * "setmatches()" function
15968 */
15969 static void
15970f_setmatches(argvars, rettv)
15971 typval_T *argvars;
15972 typval_T *rettv;
15973{
15974#ifdef FEAT_SEARCH_EXTRA
15975 list_T *l;
15976 listitem_T *li;
15977 dict_T *d;
15978
15979 rettv->vval.v_number = -1;
15980 if (argvars[0].v_type != VAR_LIST)
15981 {
15982 EMSG(_(e_listreq));
15983 return;
15984 }
15985 if ((l = argvars[0].vval.v_list) != NULL)
15986 {
15987
15988 /* To some extent make sure that we are dealing with a list from
15989 * "getmatches()". */
15990 li = l->lv_first;
15991 while (li != NULL)
15992 {
15993 if (li->li_tv.v_type != VAR_DICT
15994 || (d = li->li_tv.vval.v_dict) == NULL)
15995 {
15996 EMSG(_(e_invarg));
15997 return;
15998 }
15999 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16000 && dict_find(d, (char_u *)"pattern", -1) != NULL
16001 && dict_find(d, (char_u *)"priority", -1) != NULL
16002 && dict_find(d, (char_u *)"id", -1) != NULL))
16003 {
16004 EMSG(_(e_invarg));
16005 return;
16006 }
16007 li = li->li_next;
16008 }
16009
16010 clear_matches(curwin);
16011 li = l->lv_first;
16012 while (li != NULL)
16013 {
16014 d = li->li_tv.vval.v_dict;
16015 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16016 get_dict_string(d, (char_u *)"pattern", FALSE),
16017 (int)get_dict_number(d, (char_u *)"priority"),
16018 (int)get_dict_number(d, (char_u *)"id"));
16019 li = li->li_next;
16020 }
16021 rettv->vval.v_number = 0;
16022 }
16023#endif
16024}
16025
16026/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016027 * "setpos()" function
16028 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016029 static void
16030f_setpos(argvars, rettv)
16031 typval_T *argvars;
16032 typval_T *rettv;
16033{
16034 pos_T pos;
16035 int fnum;
16036 char_u *name;
16037
Bram Moolenaar08250432008-02-13 11:42:46 +000016038 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016039 name = get_tv_string_chk(argvars);
16040 if (name != NULL)
16041 {
16042 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16043 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016044 if (--pos.col < 0)
16045 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016046 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016047 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016048 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016049 if (fnum == curbuf->b_fnum)
16050 {
16051 curwin->w_cursor = pos;
16052 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016053 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016054 }
16055 else
16056 EMSG(_(e_invarg));
16057 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016058 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16059 {
16060 /* set mark */
16061 if (setmark_pos(name[1], &pos, fnum) == OK)
16062 rettv->vval.v_number = 0;
16063 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016064 else
16065 EMSG(_(e_invarg));
16066 }
16067 }
16068}
16069
16070/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016071 * "setqflist()" function
16072 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016073 static void
16074f_setqflist(argvars, rettv)
16075 typval_T *argvars;
16076 typval_T *rettv;
16077{
16078 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16079}
16080
16081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082 * "setreg()" function
16083 */
16084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016085f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016086 typval_T *argvars;
16087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088{
16089 int regname;
16090 char_u *strregname;
16091 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016092 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093 int append;
16094 char_u yank_type;
16095 long block_len;
16096
16097 block_len = -1;
16098 yank_type = MAUTO;
16099 append = FALSE;
16100
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016101 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016102 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016104 if (strregname == NULL)
16105 return; /* type error; errmsg already given */
16106 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016107 if (regname == 0 || regname == '@')
16108 regname = '"';
16109 else if (regname == '=')
16110 return;
16111
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016112 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016114 stropt = get_tv_string_chk(&argvars[2]);
16115 if (stropt == NULL)
16116 return; /* type error */
16117 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118 switch (*stropt)
16119 {
16120 case 'a': case 'A': /* append */
16121 append = TRUE;
16122 break;
16123 case 'v': case 'c': /* character-wise selection */
16124 yank_type = MCHAR;
16125 break;
16126 case 'V': case 'l': /* line-wise selection */
16127 yank_type = MLINE;
16128 break;
16129#ifdef FEAT_VISUAL
16130 case 'b': case Ctrl_V: /* block-wise selection */
16131 yank_type = MBLOCK;
16132 if (VIM_ISDIGIT(stropt[1]))
16133 {
16134 ++stropt;
16135 block_len = getdigits(&stropt) - 1;
16136 --stropt;
16137 }
16138 break;
16139#endif
16140 }
16141 }
16142
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016143 strval = get_tv_string_chk(&argvars[1]);
16144 if (strval != NULL)
16145 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016146 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016147 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148}
16149
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016150/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016151 * "settabvar()" function
16152 */
16153 static void
16154f_settabvar(argvars, rettv)
16155 typval_T *argvars;
16156 typval_T *rettv;
16157{
16158 tabpage_T *save_curtab;
16159 char_u *varname, *tabvarname;
16160 typval_T *varp;
16161 tabpage_T *tp;
16162
16163 rettv->vval.v_number = 0;
16164
16165 if (check_restricted() || check_secure())
16166 return;
16167
16168 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16169 varname = get_tv_string_chk(&argvars[1]);
16170 varp = &argvars[2];
16171
16172 if (tp != NULL && varname != NULL && varp != NULL)
16173 {
16174 save_curtab = curtab;
16175 goto_tabpage_tp(tp);
16176
16177 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16178 if (tabvarname != NULL)
16179 {
16180 STRCPY(tabvarname, "t:");
16181 STRCPY(tabvarname + 2, varname);
16182 set_var(tabvarname, varp, TRUE);
16183 vim_free(tabvarname);
16184 }
16185
16186 /* Restore current tabpage */
16187 if (valid_tabpage(save_curtab))
16188 goto_tabpage_tp(save_curtab);
16189 }
16190}
16191
16192/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016193 * "settabwinvar()" function
16194 */
16195 static void
16196f_settabwinvar(argvars, rettv)
16197 typval_T *argvars;
16198 typval_T *rettv;
16199{
16200 setwinvar(argvars, rettv, 1);
16201}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202
16203/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016204 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016207f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016208 typval_T *argvars;
16209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016211 setwinvar(argvars, rettv, 0);
16212}
16213
16214/*
16215 * "setwinvar()" and "settabwinvar()" functions
16216 */
16217 static void
16218setwinvar(argvars, rettv, off)
16219 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016220 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016221 int off;
16222{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223 win_T *win;
16224#ifdef FEAT_WINDOWS
16225 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016226 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227#endif
16228 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016229 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016231 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232
16233 if (check_restricted() || check_secure())
16234 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016235
16236#ifdef FEAT_WINDOWS
16237 if (off == 1)
16238 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16239 else
16240 tp = curtab;
16241#endif
16242 win = find_win_by_nr(&argvars[off], tp);
16243 varname = get_tv_string_chk(&argvars[off + 1]);
16244 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016245
16246 if (win != NULL && varname != NULL && varp != NULL)
16247 {
16248#ifdef FEAT_WINDOWS
16249 /* set curwin to be our win, temporarily */
16250 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016251 save_curtab = curtab;
16252 goto_tabpage_tp(tp);
16253 if (!win_valid(win))
16254 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255 curwin = win;
16256 curbuf = curwin->w_buffer;
16257#endif
16258
16259 if (*varname == '&')
16260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016261 long numval;
16262 char_u *strval;
16263 int error = FALSE;
16264
Bram Moolenaar071d4272004-06-13 20:20:40 +000016265 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016266 numval = get_tv_number_chk(varp, &error);
16267 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016268 if (!error && strval != NULL)
16269 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270 }
16271 else
16272 {
16273 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16274 if (winvarname != NULL)
16275 {
16276 STRCPY(winvarname, "w:");
16277 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016278 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279 vim_free(winvarname);
16280 }
16281 }
16282
16283#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016284 /* Restore current tabpage and window, if still valid (autocomands can
16285 * make them invalid). */
16286 if (valid_tabpage(save_curtab))
16287 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016288 if (win_valid(save_curwin))
16289 {
16290 curwin = save_curwin;
16291 curbuf = curwin->w_buffer;
16292 }
16293#endif
16294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295}
16296
16297/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016298 * "shellescape({string})" function
16299 */
16300 static void
16301f_shellescape(argvars, rettv)
16302 typval_T *argvars;
16303 typval_T *rettv;
16304{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016305 rettv->vval.v_string = vim_strsave_shellescape(
16306 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016307 rettv->v_type = VAR_STRING;
16308}
16309
16310/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016311 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312 */
16313 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016314f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016315 typval_T *argvars;
16316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016318 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016319
Bram Moolenaar0d660222005-01-07 21:51:51 +000016320 p = get_tv_string(&argvars[0]);
16321 rettv->vval.v_string = vim_strsave(p);
16322 simplify_filename(rettv->vval.v_string); /* simplify in place */
16323 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016324}
16325
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016326#ifdef FEAT_FLOAT
16327/*
16328 * "sin()" function
16329 */
16330 static void
16331f_sin(argvars, rettv)
16332 typval_T *argvars;
16333 typval_T *rettv;
16334{
16335 float_T f;
16336
16337 rettv->v_type = VAR_FLOAT;
16338 if (get_float_arg(argvars, &f) == OK)
16339 rettv->vval.v_float = sin(f);
16340 else
16341 rettv->vval.v_float = 0.0;
16342}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016343
16344/*
16345 * "sinh()" function
16346 */
16347 static void
16348f_sinh(argvars, rettv)
16349 typval_T *argvars;
16350 typval_T *rettv;
16351{
16352 float_T f;
16353
16354 rettv->v_type = VAR_FLOAT;
16355 if (get_float_arg(argvars, &f) == OK)
16356 rettv->vval.v_float = sinh(f);
16357 else
16358 rettv->vval.v_float = 0.0;
16359}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016360#endif
16361
Bram Moolenaar0d660222005-01-07 21:51:51 +000016362static int
16363#ifdef __BORLANDC__
16364 _RTLENTRYF
16365#endif
16366 item_compare __ARGS((const void *s1, const void *s2));
16367static int
16368#ifdef __BORLANDC__
16369 _RTLENTRYF
16370#endif
16371 item_compare2 __ARGS((const void *s1, const void *s2));
16372
16373static int item_compare_ic;
16374static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016375static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016376static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016377#define ITEM_COMPARE_FAIL 999
16378
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016380 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016382 static int
16383#ifdef __BORLANDC__
16384_RTLENTRYF
16385#endif
16386item_compare(s1, s2)
16387 const void *s1;
16388 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016390 char_u *p1, *p2;
16391 char_u *tofree1, *tofree2;
16392 int res;
16393 char_u numbuf1[NUMBUFLEN];
16394 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016395
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016396 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16397 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016398 if (p1 == NULL)
16399 p1 = (char_u *)"";
16400 if (p2 == NULL)
16401 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016402 if (item_compare_ic)
16403 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016405 res = STRCMP(p1, p2);
16406 vim_free(tofree1);
16407 vim_free(tofree2);
16408 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409}
16410
16411 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016412#ifdef __BORLANDC__
16413_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016415item_compare2(s1, s2)
16416 const void *s1;
16417 const void *s2;
16418{
16419 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016420 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016421 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016422 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016424 /* shortcut after failure in previous call; compare all items equal */
16425 if (item_compare_func_err)
16426 return 0;
16427
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016428 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16429 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016430 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16431 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016432
16433 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016434 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016435 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16436 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016437 clear_tv(&argv[0]);
16438 clear_tv(&argv[1]);
16439
16440 if (res == FAIL)
16441 res = ITEM_COMPARE_FAIL;
16442 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016443 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16444 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016445 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016446 clear_tv(&rettv);
16447 return res;
16448}
16449
16450/*
16451 * "sort({list})" function
16452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016454f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016455 typval_T *argvars;
16456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457{
Bram Moolenaar33570922005-01-25 22:26:29 +000016458 list_T *l;
16459 listitem_T *li;
16460 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016461 long len;
16462 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463
Bram Moolenaar0d660222005-01-07 21:51:51 +000016464 if (argvars[0].v_type != VAR_LIST)
16465 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016466 else
16467 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016468 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016469 if (l == NULL || tv_check_lock(l->lv_lock,
16470 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016471 return;
16472 rettv->vval.v_list = l;
16473 rettv->v_type = VAR_LIST;
16474 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475
Bram Moolenaar0d660222005-01-07 21:51:51 +000016476 len = list_len(l);
16477 if (len <= 1)
16478 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016479
Bram Moolenaar0d660222005-01-07 21:51:51 +000016480 item_compare_ic = FALSE;
16481 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016482 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016483 if (argvars[1].v_type != VAR_UNKNOWN)
16484 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016485 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016486 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016487 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016488 else
16489 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016490 int error = FALSE;
16491
16492 i = get_tv_number_chk(&argvars[1], &error);
16493 if (error)
16494 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016495 if (i == 1)
16496 item_compare_ic = TRUE;
16497 else
16498 item_compare_func = get_tv_string(&argvars[1]);
16499 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016500
16501 if (argvars[2].v_type != VAR_UNKNOWN)
16502 {
16503 /* optional third argument: {dict} */
16504 if (argvars[2].v_type != VAR_DICT)
16505 {
16506 EMSG(_(e_dictreq));
16507 return;
16508 }
16509 item_compare_selfdict = argvars[2].vval.v_dict;
16510 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016512
Bram Moolenaar0d660222005-01-07 21:51:51 +000016513 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016514 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016515 if (ptrs == NULL)
16516 return;
16517 i = 0;
16518 for (li = l->lv_first; li != NULL; li = li->li_next)
16519 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016521 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016522 /* test the compare function */
16523 if (item_compare_func != NULL
16524 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16525 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016526 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016527 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016528 {
16529 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016530 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016531 item_compare_func == NULL ? item_compare : item_compare2);
16532
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016533 if (!item_compare_func_err)
16534 {
16535 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016536 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016537 l->lv_len = 0;
16538 for (i = 0; i < len; ++i)
16539 list_append(l, ptrs[i]);
16540 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016541 }
16542
16543 vim_free(ptrs);
16544 }
16545}
16546
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016547/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016548 * "soundfold({word})" function
16549 */
16550 static void
16551f_soundfold(argvars, rettv)
16552 typval_T *argvars;
16553 typval_T *rettv;
16554{
16555 char_u *s;
16556
16557 rettv->v_type = VAR_STRING;
16558 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016559#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016560 rettv->vval.v_string = eval_soundfold(s);
16561#else
16562 rettv->vval.v_string = vim_strsave(s);
16563#endif
16564}
16565
16566/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016567 * "spellbadword()" function
16568 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016569 static void
16570f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016571 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016572 typval_T *rettv;
16573{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016574 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016575 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016576 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016577
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016578 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016579 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016580
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016581#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016582 if (argvars[0].v_type == VAR_UNKNOWN)
16583 {
16584 /* Find the start and length of the badly spelled word. */
16585 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16586 if (len != 0)
16587 word = ml_get_cursor();
16588 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016589 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016590 {
16591 char_u *str = get_tv_string_chk(&argvars[0]);
16592 int capcol = -1;
16593
16594 if (str != NULL)
16595 {
16596 /* Check the argument for spelling. */
16597 while (*str != NUL)
16598 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016599 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016600 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016601 {
16602 word = str;
16603 break;
16604 }
16605 str += len;
16606 }
16607 }
16608 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016609#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016610
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016611 list_append_string(rettv->vval.v_list, word, len);
16612 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016613 attr == HLF_SPB ? "bad" :
16614 attr == HLF_SPR ? "rare" :
16615 attr == HLF_SPL ? "local" :
16616 attr == HLF_SPC ? "caps" :
16617 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016618}
16619
16620/*
16621 * "spellsuggest()" function
16622 */
16623 static void
16624f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016625 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016626 typval_T *rettv;
16627{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016628#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016629 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016630 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016631 int maxcount;
16632 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016633 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016634 listitem_T *li;
16635 int need_capital = FALSE;
16636#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016637
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016638 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016639 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016640
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016641#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016642 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016643 {
16644 str = get_tv_string(&argvars[0]);
16645 if (argvars[1].v_type != VAR_UNKNOWN)
16646 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016647 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016648 if (maxcount <= 0)
16649 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016650 if (argvars[2].v_type != VAR_UNKNOWN)
16651 {
16652 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16653 if (typeerr)
16654 return;
16655 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016656 }
16657 else
16658 maxcount = 25;
16659
Bram Moolenaar4770d092006-01-12 23:22:24 +000016660 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016661
16662 for (i = 0; i < ga.ga_len; ++i)
16663 {
16664 str = ((char_u **)ga.ga_data)[i];
16665
16666 li = listitem_alloc();
16667 if (li == NULL)
16668 vim_free(str);
16669 else
16670 {
16671 li->li_tv.v_type = VAR_STRING;
16672 li->li_tv.v_lock = 0;
16673 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016674 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016675 }
16676 }
16677 ga_clear(&ga);
16678 }
16679#endif
16680}
16681
Bram Moolenaar0d660222005-01-07 21:51:51 +000016682 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016683f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016684 typval_T *argvars;
16685 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016686{
16687 char_u *str;
16688 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016689 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016690 regmatch_T regmatch;
16691 char_u patbuf[NUMBUFLEN];
16692 char_u *save_cpo;
16693 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016694 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016695 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016696 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016697
16698 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16699 save_cpo = p_cpo;
16700 p_cpo = (char_u *)"";
16701
16702 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016703 if (argvars[1].v_type != VAR_UNKNOWN)
16704 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016705 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16706 if (pat == NULL)
16707 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016708 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016709 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016710 }
16711 if (pat == NULL || *pat == NUL)
16712 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016713
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016714 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016716 if (typeerr)
16717 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718
Bram Moolenaar0d660222005-01-07 21:51:51 +000016719 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16720 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016722 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016723 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016724 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016725 if (*str == NUL)
16726 match = FALSE; /* empty item at the end */
16727 else
16728 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016729 if (match)
16730 end = regmatch.startp[0];
16731 else
16732 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016733 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16734 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016735 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016736 if (list_append_string(rettv->vval.v_list, str,
16737 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016738 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016739 }
16740 if (!match)
16741 break;
16742 /* Advance to just after the match. */
16743 if (regmatch.endp[0] > str)
16744 col = 0;
16745 else
16746 {
16747 /* Don't get stuck at the same match. */
16748#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016749 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016750#else
16751 col = 1;
16752#endif
16753 }
16754 str = regmatch.endp[0];
16755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756
Bram Moolenaar0d660222005-01-07 21:51:51 +000016757 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759
Bram Moolenaar0d660222005-01-07 21:51:51 +000016760 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761}
16762
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016763#ifdef FEAT_FLOAT
16764/*
16765 * "sqrt()" function
16766 */
16767 static void
16768f_sqrt(argvars, rettv)
16769 typval_T *argvars;
16770 typval_T *rettv;
16771{
16772 float_T f;
16773
16774 rettv->v_type = VAR_FLOAT;
16775 if (get_float_arg(argvars, &f) == OK)
16776 rettv->vval.v_float = sqrt(f);
16777 else
16778 rettv->vval.v_float = 0.0;
16779}
16780
16781/*
16782 * "str2float()" function
16783 */
16784 static void
16785f_str2float(argvars, rettv)
16786 typval_T *argvars;
16787 typval_T *rettv;
16788{
16789 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16790
16791 if (*p == '+')
16792 p = skipwhite(p + 1);
16793 (void)string2float(p, &rettv->vval.v_float);
16794 rettv->v_type = VAR_FLOAT;
16795}
16796#endif
16797
Bram Moolenaar2c932302006-03-18 21:42:09 +000016798/*
16799 * "str2nr()" function
16800 */
16801 static void
16802f_str2nr(argvars, rettv)
16803 typval_T *argvars;
16804 typval_T *rettv;
16805{
16806 int base = 10;
16807 char_u *p;
16808 long n;
16809
16810 if (argvars[1].v_type != VAR_UNKNOWN)
16811 {
16812 base = get_tv_number(&argvars[1]);
16813 if (base != 8 && base != 10 && base != 16)
16814 {
16815 EMSG(_(e_invarg));
16816 return;
16817 }
16818 }
16819
16820 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016821 if (*p == '+')
16822 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016823 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16824 rettv->vval.v_number = n;
16825}
16826
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827#ifdef HAVE_STRFTIME
16828/*
16829 * "strftime({format}[, {time}])" function
16830 */
16831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016832f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016833 typval_T *argvars;
16834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835{
16836 char_u result_buf[256];
16837 struct tm *curtime;
16838 time_t seconds;
16839 char_u *p;
16840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016841 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016843 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016844 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016845 seconds = time(NULL);
16846 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016847 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848 curtime = localtime(&seconds);
16849 /* MSVC returns NULL for an invalid value of seconds. */
16850 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016851 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852 else
16853 {
16854# ifdef FEAT_MBYTE
16855 vimconv_T conv;
16856 char_u *enc;
16857
16858 conv.vc_type = CONV_NONE;
16859 enc = enc_locale();
16860 convert_setup(&conv, p_enc, enc);
16861 if (conv.vc_type != CONV_NONE)
16862 p = string_convert(&conv, p, NULL);
16863# endif
16864 if (p != NULL)
16865 (void)strftime((char *)result_buf, sizeof(result_buf),
16866 (char *)p, curtime);
16867 else
16868 result_buf[0] = NUL;
16869
16870# ifdef FEAT_MBYTE
16871 if (conv.vc_type != CONV_NONE)
16872 vim_free(p);
16873 convert_setup(&conv, enc, p_enc);
16874 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016875 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876 else
16877# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016878 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879
16880# ifdef FEAT_MBYTE
16881 /* Release conversion descriptors */
16882 convert_setup(&conv, NULL, NULL);
16883 vim_free(enc);
16884# endif
16885 }
16886}
16887#endif
16888
16889/*
16890 * "stridx()" function
16891 */
16892 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016893f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016894 typval_T *argvars;
16895 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896{
16897 char_u buf[NUMBUFLEN];
16898 char_u *needle;
16899 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016900 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016902 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016904 needle = get_tv_string_chk(&argvars[1]);
16905 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016906 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016907 if (needle == NULL || haystack == NULL)
16908 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016909
Bram Moolenaar33570922005-01-25 22:26:29 +000016910 if (argvars[2].v_type != VAR_UNKNOWN)
16911 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016912 int error = FALSE;
16913
16914 start_idx = get_tv_number_chk(&argvars[2], &error);
16915 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016916 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016917 if (start_idx >= 0)
16918 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016919 }
16920
16921 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16922 if (pos != NULL)
16923 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924}
16925
16926/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016927 * "string()" function
16928 */
16929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016930f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016931 typval_T *argvars;
16932 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016933{
16934 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016935 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016936
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016937 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016938 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016939 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016940 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016941 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016942}
16943
16944/*
16945 * "strlen()" function
16946 */
16947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016948f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016949 typval_T *argvars;
16950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016952 rettv->vval.v_number = (varnumber_T)(STRLEN(
16953 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954}
16955
16956/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016957 * "strchars()" function
16958 */
16959 static void
16960f_strchars(argvars, rettv)
16961 typval_T *argvars;
16962 typval_T *rettv;
16963{
16964 char_u *s = get_tv_string(&argvars[0]);
16965#ifdef FEAT_MBYTE
16966 varnumber_T len = 0;
16967
16968 while (*s != NUL)
16969 {
16970 mb_cptr2char_adv(&s);
16971 ++len;
16972 }
16973 rettv->vval.v_number = len;
16974#else
16975 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16976#endif
16977}
16978
16979/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016980 * "strdisplaywidth()" function
16981 */
16982 static void
16983f_strdisplaywidth(argvars, rettv)
16984 typval_T *argvars;
16985 typval_T *rettv;
16986{
16987 char_u *s = get_tv_string(&argvars[0]);
16988 int col = 0;
16989
16990 if (argvars[1].v_type != VAR_UNKNOWN)
16991 col = get_tv_number(&argvars[1]);
16992
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016993 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016994}
16995
16996/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016997 * "strwidth()" function
16998 */
16999 static void
17000f_strwidth(argvars, rettv)
17001 typval_T *argvars;
17002 typval_T *rettv;
17003{
17004 char_u *s = get_tv_string(&argvars[0]);
17005
17006 rettv->vval.v_number = (varnumber_T)(
17007#ifdef FEAT_MBYTE
17008 mb_string2cells(s, -1)
17009#else
17010 STRLEN(s)
17011#endif
17012 );
17013}
17014
17015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 * "strpart()" function
17017 */
17018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017019f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017020 typval_T *argvars;
17021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022{
17023 char_u *p;
17024 int n;
17025 int len;
17026 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017027 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017029 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030 slen = (int)STRLEN(p);
17031
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017032 n = get_tv_number_chk(&argvars[1], &error);
17033 if (error)
17034 len = 0;
17035 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017036 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037 else
17038 len = slen - n; /* default len: all bytes that are available. */
17039
17040 /*
17041 * Only return the overlap between the specified part and the actual
17042 * string.
17043 */
17044 if (n < 0)
17045 {
17046 len += n;
17047 n = 0;
17048 }
17049 else if (n > slen)
17050 n = slen;
17051 if (len < 0)
17052 len = 0;
17053 else if (n + len > slen)
17054 len = slen - n;
17055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017056 rettv->v_type = VAR_STRING;
17057 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058}
17059
17060/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017061 * "strridx()" function
17062 */
17063 static void
17064f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017065 typval_T *argvars;
17066 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017067{
17068 char_u buf[NUMBUFLEN];
17069 char_u *needle;
17070 char_u *haystack;
17071 char_u *rest;
17072 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017073 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017074
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017075 needle = get_tv_string_chk(&argvars[1]);
17076 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017077
17078 rettv->vval.v_number = -1;
17079 if (needle == NULL || haystack == NULL)
17080 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017081
17082 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017083 if (argvars[2].v_type != VAR_UNKNOWN)
17084 {
17085 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017086 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017087 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017088 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017089 }
17090 else
17091 end_idx = haystack_len;
17092
Bram Moolenaar0d660222005-01-07 21:51:51 +000017093 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017094 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017095 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017096 lastmatch = haystack + end_idx;
17097 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017098 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017099 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017100 for (rest = haystack; *rest != '\0'; ++rest)
17101 {
17102 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017103 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017104 break;
17105 lastmatch = rest;
17106 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017107 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017108
17109 if (lastmatch == NULL)
17110 rettv->vval.v_number = -1;
17111 else
17112 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17113}
17114
17115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017116 * "strtrans()" function
17117 */
17118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017119f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017120 typval_T *argvars;
17121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017123 rettv->v_type = VAR_STRING;
17124 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017125}
17126
17127/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017128 * "submatch()" function
17129 */
17130 static void
17131f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017132 typval_T *argvars;
17133 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017134{
17135 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017136 rettv->vval.v_string =
17137 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017138}
17139
17140/*
17141 * "substitute()" function
17142 */
17143 static void
17144f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017145 typval_T *argvars;
17146 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017147{
17148 char_u patbuf[NUMBUFLEN];
17149 char_u subbuf[NUMBUFLEN];
17150 char_u flagsbuf[NUMBUFLEN];
17151
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017152 char_u *str = get_tv_string_chk(&argvars[0]);
17153 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17154 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17155 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17156
Bram Moolenaar0d660222005-01-07 21:51:51 +000017157 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017158 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17159 rettv->vval.v_string = NULL;
17160 else
17161 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017162}
17163
17164/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017165 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017168f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017169 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017170 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171{
17172 int id = 0;
17173#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017174 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175 long col;
17176 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017177 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017179 lnum = get_tv_lnum(argvars); /* -1 on type error */
17180 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17181 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017183 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017184 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017185 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186#endif
17187
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017188 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189}
17190
17191/*
17192 * "synIDattr(id, what [, mode])" function
17193 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017195f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017196 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017198{
17199 char_u *p = NULL;
17200#ifdef FEAT_SYN_HL
17201 int id;
17202 char_u *what;
17203 char_u *mode;
17204 char_u modebuf[NUMBUFLEN];
17205 int modec;
17206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017207 id = get_tv_number(&argvars[0]);
17208 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017209 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017210 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017211 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017212 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017213 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017214 modec = 0; /* replace invalid with current */
17215 }
17216 else
17217 {
17218#ifdef FEAT_GUI
17219 if (gui.in_use)
17220 modec = 'g';
17221 else
17222#endif
17223 if (t_colors > 1)
17224 modec = 'c';
17225 else
17226 modec = 't';
17227 }
17228
17229
17230 switch (TOLOWER_ASC(what[0]))
17231 {
17232 case 'b':
17233 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17234 p = highlight_color(id, what, modec);
17235 else /* bold */
17236 p = highlight_has_attr(id, HL_BOLD, modec);
17237 break;
17238
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017239 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240 p = highlight_color(id, what, modec);
17241 break;
17242
17243 case 'i':
17244 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17245 p = highlight_has_attr(id, HL_INVERSE, modec);
17246 else /* italic */
17247 p = highlight_has_attr(id, HL_ITALIC, modec);
17248 break;
17249
17250 case 'n': /* name */
17251 p = get_highlight_name(NULL, id - 1);
17252 break;
17253
17254 case 'r': /* reverse */
17255 p = highlight_has_attr(id, HL_INVERSE, modec);
17256 break;
17257
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017258 case 's':
17259 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17260 p = highlight_color(id, what, modec);
17261 else /* standout */
17262 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263 break;
17264
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017265 case 'u':
17266 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17267 /* underline */
17268 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17269 else
17270 /* undercurl */
17271 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017272 break;
17273 }
17274
17275 if (p != NULL)
17276 p = vim_strsave(p);
17277#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017278 rettv->v_type = VAR_STRING;
17279 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280}
17281
17282/*
17283 * "synIDtrans(id)" function
17284 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017286f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017287 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289{
17290 int id;
17291
17292#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017293 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294
17295 if (id > 0)
17296 id = syn_get_final_id(id);
17297 else
17298#endif
17299 id = 0;
17300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017301 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017302}
17303
17304/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017305 * "synconcealed(lnum, col)" function
17306 */
17307 static void
17308f_synconcealed(argvars, rettv)
17309 typval_T *argvars UNUSED;
17310 typval_T *rettv;
17311{
17312#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17313 long lnum;
17314 long col;
17315 int syntax_flags = 0;
17316 int cchar;
17317 int matchid = 0;
17318 char_u str[NUMBUFLEN];
17319#endif
17320
17321 rettv->v_type = VAR_LIST;
17322 rettv->vval.v_list = NULL;
17323
17324#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17325 lnum = get_tv_lnum(argvars); /* -1 on type error */
17326 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17327
17328 vim_memset(str, NUL, sizeof(str));
17329
17330 if (rettv_list_alloc(rettv) != FAIL)
17331 {
17332 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17333 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17334 && curwin->w_p_cole > 0)
17335 {
17336 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17337 syntax_flags = get_syntax_info(&matchid);
17338
17339 /* get the conceal character */
17340 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17341 {
17342 cchar = syn_get_sub_char();
17343 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17344 cchar = lcs_conceal;
17345 if (cchar != NUL)
17346 {
17347# ifdef FEAT_MBYTE
17348 if (has_mbyte)
17349 (*mb_char2bytes)(cchar, str);
17350 else
17351# endif
17352 str[0] = cchar;
17353 }
17354 }
17355 }
17356
17357 list_append_number(rettv->vval.v_list,
17358 (syntax_flags & HL_CONCEAL) != 0);
17359 /* -1 to auto-determine strlen */
17360 list_append_string(rettv->vval.v_list, str, -1);
17361 list_append_number(rettv->vval.v_list, matchid);
17362 }
17363#endif
17364}
17365
17366/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017367 * "synstack(lnum, col)" function
17368 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017369 static void
17370f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017371 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017372 typval_T *rettv;
17373{
17374#ifdef FEAT_SYN_HL
17375 long lnum;
17376 long col;
17377 int i;
17378 int id;
17379#endif
17380
17381 rettv->v_type = VAR_LIST;
17382 rettv->vval.v_list = NULL;
17383
17384#ifdef FEAT_SYN_HL
17385 lnum = get_tv_lnum(argvars); /* -1 on type error */
17386 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17387
17388 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017389 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017390 && rettv_list_alloc(rettv) != FAIL)
17391 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017392 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017393 for (i = 0; ; ++i)
17394 {
17395 id = syn_get_stack_item(i);
17396 if (id < 0)
17397 break;
17398 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17399 break;
17400 }
17401 }
17402#endif
17403}
17404
17405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406 * "system()" function
17407 */
17408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017409f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017410 typval_T *argvars;
17411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017412{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017413 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017415 char_u *infile = NULL;
17416 char_u buf[NUMBUFLEN];
17417 int err = FALSE;
17418 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017420 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017421 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017422
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017423 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017424 {
17425 /*
17426 * Write the string to a temp file, to be used for input of the shell
17427 * command.
17428 */
17429 if ((infile = vim_tempname('i')) == NULL)
17430 {
17431 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017432 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017433 }
17434
17435 fd = mch_fopen((char *)infile, WRITEBIN);
17436 if (fd == NULL)
17437 {
17438 EMSG2(_(e_notopen), infile);
17439 goto done;
17440 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017441 p = get_tv_string_buf_chk(&argvars[1], buf);
17442 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017443 {
17444 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017445 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017446 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017447 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17448 err = TRUE;
17449 if (fclose(fd) != 0)
17450 err = TRUE;
17451 if (err)
17452 {
17453 EMSG(_("E677: Error writing temp file"));
17454 goto done;
17455 }
17456 }
17457
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017458 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17459 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017460
Bram Moolenaar071d4272004-06-13 20:20:40 +000017461#ifdef USE_CR
17462 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017463 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017464 {
17465 char_u *s;
17466
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017467 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017468 {
17469 if (*s == CAR)
17470 *s = NL;
17471 }
17472 }
17473#else
17474# ifdef USE_CRNL
17475 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017476 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477 {
17478 char_u *s, *d;
17479
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017480 d = res;
17481 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482 {
17483 if (s[0] == CAR && s[1] == NL)
17484 ++s;
17485 *d++ = *s;
17486 }
17487 *d = NUL;
17488 }
17489# endif
17490#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017491
17492done:
17493 if (infile != NULL)
17494 {
17495 mch_remove(infile);
17496 vim_free(infile);
17497 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017498 rettv->v_type = VAR_STRING;
17499 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500}
17501
17502/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017503 * "tabpagebuflist()" function
17504 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017505 static void
17506f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017507 typval_T *argvars UNUSED;
17508 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017509{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017510#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017511 tabpage_T *tp;
17512 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017513
17514 if (argvars[0].v_type == VAR_UNKNOWN)
17515 wp = firstwin;
17516 else
17517 {
17518 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17519 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017520 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017521 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017522 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017523 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017524 for (; wp != NULL; wp = wp->w_next)
17525 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017526 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017527 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017528 }
17529#endif
17530}
17531
17532
17533/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017534 * "tabpagenr()" function
17535 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017536 static void
17537f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017538 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017539 typval_T *rettv;
17540{
17541 int nr = 1;
17542#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017543 char_u *arg;
17544
17545 if (argvars[0].v_type != VAR_UNKNOWN)
17546 {
17547 arg = get_tv_string_chk(&argvars[0]);
17548 nr = 0;
17549 if (arg != NULL)
17550 {
17551 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017552 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017553 else
17554 EMSG2(_(e_invexpr2), arg);
17555 }
17556 }
17557 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017558 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017559#endif
17560 rettv->vval.v_number = nr;
17561}
17562
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017563
17564#ifdef FEAT_WINDOWS
17565static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17566
17567/*
17568 * Common code for tabpagewinnr() and winnr().
17569 */
17570 static int
17571get_winnr(tp, argvar)
17572 tabpage_T *tp;
17573 typval_T *argvar;
17574{
17575 win_T *twin;
17576 int nr = 1;
17577 win_T *wp;
17578 char_u *arg;
17579
17580 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17581 if (argvar->v_type != VAR_UNKNOWN)
17582 {
17583 arg = get_tv_string_chk(argvar);
17584 if (arg == NULL)
17585 nr = 0; /* type error; errmsg already given */
17586 else if (STRCMP(arg, "$") == 0)
17587 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17588 else if (STRCMP(arg, "#") == 0)
17589 {
17590 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17591 if (twin == NULL)
17592 nr = 0;
17593 }
17594 else
17595 {
17596 EMSG2(_(e_invexpr2), arg);
17597 nr = 0;
17598 }
17599 }
17600
17601 if (nr > 0)
17602 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17603 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017604 {
17605 if (wp == NULL)
17606 {
17607 /* didn't find it in this tabpage */
17608 nr = 0;
17609 break;
17610 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017611 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017612 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017613 return nr;
17614}
17615#endif
17616
17617/*
17618 * "tabpagewinnr()" function
17619 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017620 static void
17621f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017622 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017623 typval_T *rettv;
17624{
17625 int nr = 1;
17626#ifdef FEAT_WINDOWS
17627 tabpage_T *tp;
17628
17629 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17630 if (tp == NULL)
17631 nr = 0;
17632 else
17633 nr = get_winnr(tp, &argvars[1]);
17634#endif
17635 rettv->vval.v_number = nr;
17636}
17637
17638
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017639/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017640 * "tagfiles()" function
17641 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017642 static void
17643f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017644 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017645 typval_T *rettv;
17646{
Bram Moolenaard9462e32011-04-11 21:35:11 +020017647 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017648 tagname_T tn;
17649 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017650
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017651 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017652 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017653 fname = alloc(MAXPATHL);
17654 if (fname == NULL)
17655 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017656
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017657 for (first = TRUE; ; first = FALSE)
17658 if (get_tagfname(&tn, first, fname) == FAIL
17659 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017660 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017661 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020017662 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017663}
17664
17665/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017666 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017667 */
17668 static void
17669f_taglist(argvars, rettv)
17670 typval_T *argvars;
17671 typval_T *rettv;
17672{
17673 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017674
17675 tag_pattern = get_tv_string(&argvars[0]);
17676
17677 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017678 if (*tag_pattern == NUL)
17679 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017680
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017681 if (rettv_list_alloc(rettv) == OK)
17682 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017683}
17684
17685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686 * "tempname()" function
17687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017689f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017690 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017692{
17693 static int x = 'A';
17694
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017695 rettv->v_type = VAR_STRING;
17696 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017697
17698 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17699 * names. Skip 'I' and 'O', they are used for shell redirection. */
17700 do
17701 {
17702 if (x == 'Z')
17703 x = '0';
17704 else if (x == '9')
17705 x = 'A';
17706 else
17707 {
17708#ifdef EBCDIC
17709 if (x == 'I')
17710 x = 'J';
17711 else if (x == 'R')
17712 x = 'S';
17713 else
17714#endif
17715 ++x;
17716 }
17717 } while (x == 'I' || x == 'O');
17718}
17719
17720/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017721 * "test(list)" function: Just checking the walls...
17722 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017723 static void
17724f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017725 typval_T *argvars UNUSED;
17726 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017727{
17728 /* Used for unit testing. Change the code below to your liking. */
17729#if 0
17730 listitem_T *li;
17731 list_T *l;
17732 char_u *bad, *good;
17733
17734 if (argvars[0].v_type != VAR_LIST)
17735 return;
17736 l = argvars[0].vval.v_list;
17737 if (l == NULL)
17738 return;
17739 li = l->lv_first;
17740 if (li == NULL)
17741 return;
17742 bad = get_tv_string(&li->li_tv);
17743 li = li->li_next;
17744 if (li == NULL)
17745 return;
17746 good = get_tv_string(&li->li_tv);
17747 rettv->vval.v_number = test_edit_score(bad, good);
17748#endif
17749}
17750
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017751#ifdef FEAT_FLOAT
17752/*
17753 * "tan()" function
17754 */
17755 static void
17756f_tan(argvars, rettv)
17757 typval_T *argvars;
17758 typval_T *rettv;
17759{
17760 float_T f;
17761
17762 rettv->v_type = VAR_FLOAT;
17763 if (get_float_arg(argvars, &f) == OK)
17764 rettv->vval.v_float = tan(f);
17765 else
17766 rettv->vval.v_float = 0.0;
17767}
17768
17769/*
17770 * "tanh()" function
17771 */
17772 static void
17773f_tanh(argvars, rettv)
17774 typval_T *argvars;
17775 typval_T *rettv;
17776{
17777 float_T f;
17778
17779 rettv->v_type = VAR_FLOAT;
17780 if (get_float_arg(argvars, &f) == OK)
17781 rettv->vval.v_float = tanh(f);
17782 else
17783 rettv->vval.v_float = 0.0;
17784}
17785#endif
17786
Bram Moolenaard52d9742005-08-21 22:20:28 +000017787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788 * "tolower(string)" function
17789 */
17790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017791f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017792 typval_T *argvars;
17793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017794{
17795 char_u *p;
17796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017797 p = vim_strsave(get_tv_string(&argvars[0]));
17798 rettv->v_type = VAR_STRING;
17799 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017800
17801 if (p != NULL)
17802 while (*p != NUL)
17803 {
17804#ifdef FEAT_MBYTE
17805 int l;
17806
17807 if (enc_utf8)
17808 {
17809 int c, lc;
17810
17811 c = utf_ptr2char(p);
17812 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017813 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017814 /* TODO: reallocate string when byte count changes. */
17815 if (utf_char2len(lc) == l)
17816 utf_char2bytes(lc, p);
17817 p += l;
17818 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017819 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017820 p += l; /* skip multi-byte character */
17821 else
17822#endif
17823 {
17824 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17825 ++p;
17826 }
17827 }
17828}
17829
17830/*
17831 * "toupper(string)" function
17832 */
17833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017834f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017835 typval_T *argvars;
17836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017838 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017839 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840}
17841
17842/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017843 * "tr(string, fromstr, tostr)" function
17844 */
17845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017846f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017847 typval_T *argvars;
17848 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017849{
17850 char_u *instr;
17851 char_u *fromstr;
17852 char_u *tostr;
17853 char_u *p;
17854#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017855 int inlen;
17856 int fromlen;
17857 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017858 int idx;
17859 char_u *cpstr;
17860 int cplen;
17861 int first = TRUE;
17862#endif
17863 char_u buf[NUMBUFLEN];
17864 char_u buf2[NUMBUFLEN];
17865 garray_T ga;
17866
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017867 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017868 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17869 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017870
17871 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017872 rettv->v_type = VAR_STRING;
17873 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017874 if (fromstr == NULL || tostr == NULL)
17875 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017876 ga_init2(&ga, (int)sizeof(char), 80);
17877
17878#ifdef FEAT_MBYTE
17879 if (!has_mbyte)
17880#endif
17881 /* not multi-byte: fromstr and tostr must be the same length */
17882 if (STRLEN(fromstr) != STRLEN(tostr))
17883 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017884#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017885error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017886#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017887 EMSG2(_(e_invarg2), fromstr);
17888 ga_clear(&ga);
17889 return;
17890 }
17891
17892 /* fromstr and tostr have to contain the same number of chars */
17893 while (*instr != NUL)
17894 {
17895#ifdef FEAT_MBYTE
17896 if (has_mbyte)
17897 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017898 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017899 cpstr = instr;
17900 cplen = inlen;
17901 idx = 0;
17902 for (p = fromstr; *p != NUL; p += fromlen)
17903 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017904 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017905 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17906 {
17907 for (p = tostr; *p != NUL; p += tolen)
17908 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017909 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017910 if (idx-- == 0)
17911 {
17912 cplen = tolen;
17913 cpstr = p;
17914 break;
17915 }
17916 }
17917 if (*p == NUL) /* tostr is shorter than fromstr */
17918 goto error;
17919 break;
17920 }
17921 ++idx;
17922 }
17923
17924 if (first && cpstr == instr)
17925 {
17926 /* Check that fromstr and tostr have the same number of
17927 * (multi-byte) characters. Done only once when a character
17928 * of instr doesn't appear in fromstr. */
17929 first = FALSE;
17930 for (p = tostr; *p != NUL; p += tolen)
17931 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017932 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017933 --idx;
17934 }
17935 if (idx != 0)
17936 goto error;
17937 }
17938
17939 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017940 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017941 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017942
17943 instr += inlen;
17944 }
17945 else
17946#endif
17947 {
17948 /* When not using multi-byte chars we can do it faster. */
17949 p = vim_strchr(fromstr, *instr);
17950 if (p != NULL)
17951 ga_append(&ga, tostr[p - fromstr]);
17952 else
17953 ga_append(&ga, *instr);
17954 ++instr;
17955 }
17956 }
17957
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017958 /* add a terminating NUL */
17959 ga_grow(&ga, 1);
17960 ga_append(&ga, NUL);
17961
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017962 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017963}
17964
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017965#ifdef FEAT_FLOAT
17966/*
17967 * "trunc({float})" function
17968 */
17969 static void
17970f_trunc(argvars, rettv)
17971 typval_T *argvars;
17972 typval_T *rettv;
17973{
17974 float_T f;
17975
17976 rettv->v_type = VAR_FLOAT;
17977 if (get_float_arg(argvars, &f) == OK)
17978 /* trunc() is not in C90, use floor() or ceil() instead. */
17979 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17980 else
17981 rettv->vval.v_float = 0.0;
17982}
17983#endif
17984
Bram Moolenaar8299df92004-07-10 09:47:34 +000017985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017986 * "type(expr)" function
17987 */
17988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017989f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017990 typval_T *argvars;
17991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017993 int n;
17994
17995 switch (argvars[0].v_type)
17996 {
17997 case VAR_NUMBER: n = 0; break;
17998 case VAR_STRING: n = 1; break;
17999 case VAR_FUNC: n = 2; break;
18000 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018001 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018002#ifdef FEAT_FLOAT
18003 case VAR_FLOAT: n = 5; break;
18004#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018005 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18006 }
18007 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008}
18009
18010/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018011 * "undofile(name)" function
18012 */
18013 static void
18014f_undofile(argvars, rettv)
18015 typval_T *argvars;
18016 typval_T *rettv;
18017{
18018 rettv->v_type = VAR_STRING;
18019#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018020 {
18021 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
18022
18023 if (ffname != NULL)
18024 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18025 vim_free(ffname);
18026 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018027#else
18028 rettv->vval.v_string = NULL;
18029#endif
18030}
18031
18032/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018033 * "undotree()" function
18034 */
18035 static void
18036f_undotree(argvars, rettv)
18037 typval_T *argvars UNUSED;
18038 typval_T *rettv;
18039{
18040 if (rettv_dict_alloc(rettv) == OK)
18041 {
18042 dict_T *dict = rettv->vval.v_dict;
18043 list_T *list;
18044
Bram Moolenaar730cde92010-06-27 05:18:54 +020018045 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018046 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018047 dict_add_nr_str(dict, "save_last",
18048 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018049 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18050 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018051 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018052
18053 list = list_alloc();
18054 if (list != NULL)
18055 {
18056 u_eval_tree(curbuf->b_u_oldhead, list);
18057 dict_add_list(dict, "entries", list);
18058 }
18059 }
18060}
18061
18062/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018063 * "values(dict)" function
18064 */
18065 static void
18066f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018067 typval_T *argvars;
18068 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018069{
18070 dict_list(argvars, rettv, 1);
18071}
18072
18073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 * "virtcol(string)" function
18075 */
18076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018077f_virtcol(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{
18081 colnr_T vcol = 0;
18082 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018083 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018085 fp = var2fpos(&argvars[0], FALSE, &fnum);
18086 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18087 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 {
18089 getvvcol(curwin, fp, NULL, NULL, &vcol);
18090 ++vcol;
18091 }
18092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018093 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094}
18095
18096/*
18097 * "visualmode()" function
18098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018100f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018101 typval_T *argvars UNUSED;
18102 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103{
18104#ifdef FEAT_VISUAL
18105 char_u str[2];
18106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018107 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108 str[0] = curbuf->b_visual_mode_eval;
18109 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018110 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018111
18112 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018113 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018114 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115#endif
18116}
18117
18118/*
18119 * "winbufnr(nr)" function
18120 */
18121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018122f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018123 typval_T *argvars;
18124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125{
18126 win_T *wp;
18127
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018128 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018130 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018132 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018133}
18134
18135/*
18136 * "wincol()" function
18137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018139f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018140 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142{
18143 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018144 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145}
18146
18147/*
18148 * "winheight(nr)" function
18149 */
18150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018151f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018152 typval_T *argvars;
18153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154{
18155 win_T *wp;
18156
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018157 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018159 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018161 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162}
18163
18164/*
18165 * "winline()" function
18166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018168f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018169 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018170 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171{
18172 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018173 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174}
18175
18176/*
18177 * "winnr()" function
18178 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018180f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018181 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018182 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018183{
18184 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018185
Bram Moolenaar071d4272004-06-13 20:20:40 +000018186#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018187 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018188#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018189 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018190}
18191
18192/*
18193 * "winrestcmd()" function
18194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018196f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018197 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199{
18200#ifdef FEAT_WINDOWS
18201 win_T *wp;
18202 int winnr = 1;
18203 garray_T ga;
18204 char_u buf[50];
18205
18206 ga_init2(&ga, (int)sizeof(char), 70);
18207 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18208 {
18209 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18210 ga_concat(&ga, buf);
18211# ifdef FEAT_VERTSPLIT
18212 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18213 ga_concat(&ga, buf);
18214# endif
18215 ++winnr;
18216 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018217 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018219 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018221 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018223 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018224}
18225
18226/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018227 * "winrestview()" function
18228 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018229 static void
18230f_winrestview(argvars, rettv)
18231 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018232 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018233{
18234 dict_T *dict;
18235
18236 if (argvars[0].v_type != VAR_DICT
18237 || (dict = argvars[0].vval.v_dict) == NULL)
18238 EMSG(_(e_invarg));
18239 else
18240 {
18241 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18242 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18243#ifdef FEAT_VIRTUALEDIT
18244 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18245#endif
18246 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018247 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018248
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018249 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018250#ifdef FEAT_DIFF
18251 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18252#endif
18253 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18254 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18255
18256 check_cursor();
18257 changed_cline_bef_curs();
18258 invalidate_botline();
18259 redraw_later(VALID);
18260
18261 if (curwin->w_topline == 0)
18262 curwin->w_topline = 1;
18263 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18264 curwin->w_topline = curbuf->b_ml.ml_line_count;
18265#ifdef FEAT_DIFF
18266 check_topfill(curwin, TRUE);
18267#endif
18268 }
18269}
18270
18271/*
18272 * "winsaveview()" function
18273 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018274 static void
18275f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018276 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018277 typval_T *rettv;
18278{
18279 dict_T *dict;
18280
Bram Moolenaara800b422010-06-27 01:15:55 +020018281 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018282 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018283 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018284
18285 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18286 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18287#ifdef FEAT_VIRTUALEDIT
18288 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18289#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018290 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018291 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18292
18293 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18294#ifdef FEAT_DIFF
18295 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18296#endif
18297 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18298 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18299}
18300
18301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018302 * "winwidth(nr)" function
18303 */
18304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018305f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018306 typval_T *argvars;
18307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308{
18309 win_T *wp;
18310
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018311 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018313 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314 else
18315#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018316 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018318 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319#endif
18320}
18321
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018323 * "writefile()" function
18324 */
18325 static void
18326f_writefile(argvars, rettv)
18327 typval_T *argvars;
18328 typval_T *rettv;
18329{
18330 int binary = FALSE;
18331 char_u *fname;
18332 FILE *fd;
18333 listitem_T *li;
18334 char_u *s;
18335 int ret = 0;
18336 int c;
18337
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018338 if (check_restricted() || check_secure())
18339 return;
18340
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018341 if (argvars[0].v_type != VAR_LIST)
18342 {
18343 EMSG2(_(e_listarg), "writefile()");
18344 return;
18345 }
18346 if (argvars[0].vval.v_list == NULL)
18347 return;
18348
18349 if (argvars[2].v_type != VAR_UNKNOWN
18350 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18351 binary = TRUE;
18352
18353 /* Always open the file in binary mode, library functions have a mind of
18354 * their own about CR-LF conversion. */
18355 fname = get_tv_string(&argvars[1]);
18356 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18357 {
18358 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18359 ret = -1;
18360 }
18361 else
18362 {
18363 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18364 li = li->li_next)
18365 {
18366 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18367 {
18368 if (*s == '\n')
18369 c = putc(NUL, fd);
18370 else
18371 c = putc(*s, fd);
18372 if (c == EOF)
18373 {
18374 ret = -1;
18375 break;
18376 }
18377 }
18378 if (!binary || li->li_next != NULL)
18379 if (putc('\n', fd) == EOF)
18380 {
18381 ret = -1;
18382 break;
18383 }
18384 if (ret < 0)
18385 {
18386 EMSG(_(e_write));
18387 break;
18388 }
18389 }
18390 fclose(fd);
18391 }
18392
18393 rettv->vval.v_number = ret;
18394}
18395
18396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018397 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018398 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399 */
18400 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018401var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018402 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018403 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018404 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018405{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018406 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018407 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018408 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409
Bram Moolenaara5525202006-03-02 22:52:09 +000018410 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018411 if (varp->v_type == VAR_LIST)
18412 {
18413 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018414 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018415 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018416 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018417
18418 l = varp->vval.v_list;
18419 if (l == NULL)
18420 return NULL;
18421
18422 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018423 pos.lnum = list_find_nr(l, 0L, &error);
18424 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018425 return NULL; /* invalid line number */
18426
18427 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018428 pos.col = list_find_nr(l, 1L, &error);
18429 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018430 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018431 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018432
18433 /* We accept "$" for the column number: last column. */
18434 li = list_find(l, 1L);
18435 if (li != NULL && li->li_tv.v_type == VAR_STRING
18436 && li->li_tv.vval.v_string != NULL
18437 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18438 pos.col = len + 1;
18439
Bram Moolenaara5525202006-03-02 22:52:09 +000018440 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018441 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018442 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018443 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018444
Bram Moolenaara5525202006-03-02 22:52:09 +000018445#ifdef FEAT_VIRTUALEDIT
18446 /* Get the virtual offset. Defaults to zero. */
18447 pos.coladd = list_find_nr(l, 2L, &error);
18448 if (error)
18449 pos.coladd = 0;
18450#endif
18451
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018452 return &pos;
18453 }
18454
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018455 name = get_tv_string_chk(varp);
18456 if (name == NULL)
18457 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018458 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018460#ifdef FEAT_VISUAL
18461 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18462 {
18463 if (VIsual_active)
18464 return &VIsual;
18465 return &curwin->w_cursor;
18466 }
18467#endif
18468 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018469 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018470 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018471 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18472 return NULL;
18473 return pp;
18474 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018475
18476#ifdef FEAT_VIRTUALEDIT
18477 pos.coladd = 0;
18478#endif
18479
Bram Moolenaar477933c2007-07-17 14:32:23 +000018480 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018481 {
18482 pos.col = 0;
18483 if (name[1] == '0') /* "w0": first visible line */
18484 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018485 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018486 pos.lnum = curwin->w_topline;
18487 return &pos;
18488 }
18489 else if (name[1] == '$') /* "w$": last visible line */
18490 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018491 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018492 pos.lnum = curwin->w_botline - 1;
18493 return &pos;
18494 }
18495 }
18496 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018497 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018498 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499 {
18500 pos.lnum = curbuf->b_ml.ml_line_count;
18501 pos.col = 0;
18502 }
18503 else
18504 {
18505 pos.lnum = curwin->w_cursor.lnum;
18506 pos.col = (colnr_T)STRLEN(ml_get_curline());
18507 }
18508 return &pos;
18509 }
18510 return NULL;
18511}
18512
18513/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018514 * Convert list in "arg" into a position and optional file number.
18515 * When "fnump" is NULL there is no file number, only 3 items.
18516 * Note that the column is passed on as-is, the caller may want to decrement
18517 * it to use 1 for the first column.
18518 * Return FAIL when conversion is not possible, doesn't check the position for
18519 * validity.
18520 */
18521 static int
18522list2fpos(arg, posp, fnump)
18523 typval_T *arg;
18524 pos_T *posp;
18525 int *fnump;
18526{
18527 list_T *l = arg->vval.v_list;
18528 long i = 0;
18529 long n;
18530
Bram Moolenaarbde35262006-07-23 20:12:24 +000018531 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18532 * when "fnump" isn't NULL and "coladd" is optional. */
18533 if (arg->v_type != VAR_LIST
18534 || l == NULL
18535 || l->lv_len < (fnump == NULL ? 2 : 3)
18536 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018537 return FAIL;
18538
18539 if (fnump != NULL)
18540 {
18541 n = list_find_nr(l, i++, NULL); /* fnum */
18542 if (n < 0)
18543 return FAIL;
18544 if (n == 0)
18545 n = curbuf->b_fnum; /* current buffer */
18546 *fnump = n;
18547 }
18548
18549 n = list_find_nr(l, i++, NULL); /* lnum */
18550 if (n < 0)
18551 return FAIL;
18552 posp->lnum = n;
18553
18554 n = list_find_nr(l, i++, NULL); /* col */
18555 if (n < 0)
18556 return FAIL;
18557 posp->col = n;
18558
18559#ifdef FEAT_VIRTUALEDIT
18560 n = list_find_nr(l, i, NULL);
18561 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018562 posp->coladd = 0;
18563 else
18564 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018565#endif
18566
18567 return OK;
18568}
18569
18570/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571 * Get the length of an environment variable name.
18572 * Advance "arg" to the first character after the name.
18573 * Return 0 for error.
18574 */
18575 static int
18576get_env_len(arg)
18577 char_u **arg;
18578{
18579 char_u *p;
18580 int len;
18581
18582 for (p = *arg; vim_isIDc(*p); ++p)
18583 ;
18584 if (p == *arg) /* no name found */
18585 return 0;
18586
18587 len = (int)(p - *arg);
18588 *arg = p;
18589 return len;
18590}
18591
18592/*
18593 * Get the length of the name of a function or internal variable.
18594 * "arg" is advanced to the first non-white character after the name.
18595 * Return 0 if something is wrong.
18596 */
18597 static int
18598get_id_len(arg)
18599 char_u **arg;
18600{
18601 char_u *p;
18602 int len;
18603
18604 /* Find the end of the name. */
18605 for (p = *arg; eval_isnamec(*p); ++p)
18606 ;
18607 if (p == *arg) /* no name found */
18608 return 0;
18609
18610 len = (int)(p - *arg);
18611 *arg = skipwhite(p);
18612
18613 return len;
18614}
18615
18616/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018617 * Get the length of the name of a variable or function.
18618 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018620 * Return -1 if curly braces expansion failed.
18621 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622 * If the name contains 'magic' {}'s, expand them and return the
18623 * expanded name in an allocated string via 'alias' - caller must free.
18624 */
18625 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018626get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627 char_u **arg;
18628 char_u **alias;
18629 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018630 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631{
18632 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633 char_u *p;
18634 char_u *expr_start;
18635 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636
18637 *alias = NULL; /* default to no alias */
18638
18639 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18640 && (*arg)[2] == (int)KE_SNR)
18641 {
18642 /* hard coded <SNR>, already translated */
18643 *arg += 3;
18644 return get_id_len(arg) + 3;
18645 }
18646 len = eval_fname_script(*arg);
18647 if (len > 0)
18648 {
18649 /* literal "<SID>", "s:" or "<SNR>" */
18650 *arg += len;
18651 }
18652
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018654 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018656 p = find_name_end(*arg, &expr_start, &expr_end,
18657 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 if (expr_start != NULL)
18659 {
18660 char_u *temp_string;
18661
18662 if (!evaluate)
18663 {
18664 len += (int)(p - *arg);
18665 *arg = skipwhite(p);
18666 return len;
18667 }
18668
18669 /*
18670 * Include any <SID> etc in the expanded string:
18671 * Thus the -len here.
18672 */
18673 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18674 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018675 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018676 *alias = temp_string;
18677 *arg = skipwhite(p);
18678 return (int)STRLEN(temp_string);
18679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018680
18681 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018682 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683 EMSG2(_(e_invexpr2), *arg);
18684
18685 return len;
18686}
18687
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018688/*
18689 * Find the end of a variable or function name, taking care of magic braces.
18690 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18691 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018692 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018693 * Return a pointer to just after the name. Equal to "arg" if there is no
18694 * valid name.
18695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018697find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698 char_u *arg;
18699 char_u **expr_start;
18700 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018701 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018703 int mb_nest = 0;
18704 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705 char_u *p;
18706
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018707 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018709 *expr_start = NULL;
18710 *expr_end = NULL;
18711 }
18712
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018713 /* Quick check for valid starting character. */
18714 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18715 return arg;
18716
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018717 for (p = arg; *p != NUL
18718 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018719 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018720 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018721 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018722 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018723 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018724 if (*p == '\'')
18725 {
18726 /* skip over 'string' to avoid counting [ and ] inside it. */
18727 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18728 ;
18729 if (*p == NUL)
18730 break;
18731 }
18732 else if (*p == '"')
18733 {
18734 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18735 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18736 if (*p == '\\' && p[1] != NUL)
18737 ++p;
18738 if (*p == NUL)
18739 break;
18740 }
18741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018742 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018744 if (*p == '[')
18745 ++br_nest;
18746 else if (*p == ']')
18747 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018750 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018752 if (*p == '{')
18753 {
18754 mb_nest++;
18755 if (expr_start != NULL && *expr_start == NULL)
18756 *expr_start = p;
18757 }
18758 else if (*p == '}')
18759 {
18760 mb_nest--;
18761 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18762 *expr_end = p;
18763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 }
18766
18767 return p;
18768}
18769
18770/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018771 * Expands out the 'magic' {}'s in a variable/function name.
18772 * Note that this can call itself recursively, to deal with
18773 * constructs like foo{bar}{baz}{bam}
18774 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18775 * "in_start" ^
18776 * "expr_start" ^
18777 * "expr_end" ^
18778 * "in_end" ^
18779 *
18780 * Returns a new allocated string, which the caller must free.
18781 * Returns NULL for failure.
18782 */
18783 static char_u *
18784make_expanded_name(in_start, expr_start, expr_end, in_end)
18785 char_u *in_start;
18786 char_u *expr_start;
18787 char_u *expr_end;
18788 char_u *in_end;
18789{
18790 char_u c1;
18791 char_u *retval = NULL;
18792 char_u *temp_result;
18793 char_u *nextcmd = NULL;
18794
18795 if (expr_end == NULL || in_end == NULL)
18796 return NULL;
18797 *expr_start = NUL;
18798 *expr_end = NUL;
18799 c1 = *in_end;
18800 *in_end = NUL;
18801
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018802 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018803 if (temp_result != NULL && nextcmd == NULL)
18804 {
18805 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18806 + (in_end - expr_end) + 1));
18807 if (retval != NULL)
18808 {
18809 STRCPY(retval, in_start);
18810 STRCAT(retval, temp_result);
18811 STRCAT(retval, expr_end + 1);
18812 }
18813 }
18814 vim_free(temp_result);
18815
18816 *in_end = c1; /* put char back for error messages */
18817 *expr_start = '{';
18818 *expr_end = '}';
18819
18820 if (retval != NULL)
18821 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018822 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018823 if (expr_start != NULL)
18824 {
18825 /* Further expansion! */
18826 temp_result = make_expanded_name(retval, expr_start,
18827 expr_end, temp_result);
18828 vim_free(retval);
18829 retval = temp_result;
18830 }
18831 }
18832
18833 return retval;
18834}
18835
18836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018838 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839 */
18840 static int
18841eval_isnamec(c)
18842 int c;
18843{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018844 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18845}
18846
18847/*
18848 * Return TRUE if character "c" can be used as the first character in a
18849 * variable or function name (excluding '{' and '}').
18850 */
18851 static int
18852eval_isnamec1(c)
18853 int c;
18854{
18855 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856}
18857
18858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859 * Set number v: variable to "val".
18860 */
18861 void
18862set_vim_var_nr(idx, val)
18863 int idx;
18864 long val;
18865{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018866 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018867}
18868
18869/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018870 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018871 */
18872 long
18873get_vim_var_nr(idx)
18874 int idx;
18875{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018876 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877}
18878
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018879/*
18880 * Get string v: variable value. Uses a static buffer, can only be used once.
18881 */
18882 char_u *
18883get_vim_var_str(idx)
18884 int idx;
18885{
18886 return get_tv_string(&vimvars[idx].vv_tv);
18887}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018888
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018890 * Get List v: variable value. Caller must take care of reference count when
18891 * needed.
18892 */
18893 list_T *
18894get_vim_var_list(idx)
18895 int idx;
18896{
18897 return vimvars[idx].vv_list;
18898}
18899
18900/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018901 * Set v:char to character "c".
18902 */
18903 void
18904set_vim_var_char(c)
18905 int c;
18906{
18907#ifdef FEAT_MBYTE
18908 char_u buf[MB_MAXBYTES];
18909#else
18910 char_u buf[2];
18911#endif
18912
18913#ifdef FEAT_MBYTE
18914 if (has_mbyte)
18915 buf[(*mb_char2bytes)(c, buf)] = NUL;
18916 else
18917#endif
18918 {
18919 buf[0] = c;
18920 buf[1] = NUL;
18921 }
18922 set_vim_var_string(VV_CHAR, buf, -1);
18923}
18924
18925/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018926 * Set v:count to "count" and v:count1 to "count1".
18927 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928 */
18929 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018930set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931 long count;
18932 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018933 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018935 if (set_prevcount)
18936 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018937 vimvars[VV_COUNT].vv_nr = count;
18938 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939}
18940
18941/*
18942 * Set string v: variable to a copy of "val".
18943 */
18944 void
18945set_vim_var_string(idx, val, len)
18946 int idx;
18947 char_u *val;
18948 int len; /* length of "val" to use or -1 (whole string) */
18949{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018950 /* Need to do this (at least) once, since we can't initialize a union.
18951 * Will always be invoked when "v:progname" is set. */
18952 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18953
Bram Moolenaare9a41262005-01-15 22:18:47 +000018954 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018956 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018958 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018960 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961}
18962
18963/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018964 * Set List v: variable to "val".
18965 */
18966 void
18967set_vim_var_list(idx, val)
18968 int idx;
18969 list_T *val;
18970{
18971 list_unref(vimvars[idx].vv_list);
18972 vimvars[idx].vv_list = val;
18973 if (val != NULL)
18974 ++val->lv_refcount;
18975}
18976
18977/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018978 * Set v:register if needed.
18979 */
18980 void
18981set_reg_var(c)
18982 int c;
18983{
18984 char_u regname;
18985
18986 if (c == 0 || c == ' ')
18987 regname = '"';
18988 else
18989 regname = c;
18990 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018991 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992 set_vim_var_string(VV_REG, &regname, 1);
18993}
18994
18995/*
18996 * Get or set v:exception. If "oldval" == NULL, return the current value.
18997 * Otherwise, restore the value to "oldval" and return NULL.
18998 * Must always be called in pairs to save and restore v:exception! Does not
18999 * take care of memory allocations.
19000 */
19001 char_u *
19002v_exception(oldval)
19003 char_u *oldval;
19004{
19005 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019006 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007
Bram Moolenaare9a41262005-01-15 22:18:47 +000019008 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009 return NULL;
19010}
19011
19012/*
19013 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19014 * Otherwise, restore the value to "oldval" and return NULL.
19015 * Must always be called in pairs to save and restore v:throwpoint! Does not
19016 * take care of memory allocations.
19017 */
19018 char_u *
19019v_throwpoint(oldval)
19020 char_u *oldval;
19021{
19022 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019023 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024
Bram Moolenaare9a41262005-01-15 22:18:47 +000019025 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019026 return NULL;
19027}
19028
19029#if defined(FEAT_AUTOCMD) || defined(PROTO)
19030/*
19031 * Set v:cmdarg.
19032 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19033 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19034 * Must always be called in pairs!
19035 */
19036 char_u *
19037set_cmdarg(eap, oldarg)
19038 exarg_T *eap;
19039 char_u *oldarg;
19040{
19041 char_u *oldval;
19042 char_u *newval;
19043 unsigned len;
19044
Bram Moolenaare9a41262005-01-15 22:18:47 +000019045 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019046 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019048 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019049 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019050 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 }
19052
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019053 if (eap->force_bin == FORCE_BIN)
19054 len = 6;
19055 else if (eap->force_bin == FORCE_NOBIN)
19056 len = 8;
19057 else
19058 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019059
19060 if (eap->read_edit)
19061 len += 7;
19062
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019063 if (eap->force_ff != 0)
19064 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19065# ifdef FEAT_MBYTE
19066 if (eap->force_enc != 0)
19067 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019068 if (eap->bad_char != 0)
19069 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019070# endif
19071
19072 newval = alloc(len + 1);
19073 if (newval == NULL)
19074 return NULL;
19075
19076 if (eap->force_bin == FORCE_BIN)
19077 sprintf((char *)newval, " ++bin");
19078 else if (eap->force_bin == FORCE_NOBIN)
19079 sprintf((char *)newval, " ++nobin");
19080 else
19081 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019082
19083 if (eap->read_edit)
19084 STRCAT(newval, " ++edit");
19085
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019086 if (eap->force_ff != 0)
19087 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19088 eap->cmd + eap->force_ff);
19089# ifdef FEAT_MBYTE
19090 if (eap->force_enc != 0)
19091 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19092 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019093 if (eap->bad_char == BAD_KEEP)
19094 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19095 else if (eap->bad_char == BAD_DROP)
19096 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19097 else if (eap->bad_char != 0)
19098 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019099# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019100 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019101 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102}
19103#endif
19104
19105/*
19106 * Get the value of internal variable "name".
19107 * Return OK or FAIL.
19108 */
19109 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019110get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019111 char_u *name;
19112 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019113 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019114 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115{
19116 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019117 typval_T *tv = NULL;
19118 typval_T atv;
19119 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019120 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121
19122 /* truncate the name, so that we can use strcmp() */
19123 cc = name[len];
19124 name[len] = NUL;
19125
19126 /*
19127 * Check for "b:changedtick".
19128 */
19129 if (STRCMP(name, "b:changedtick") == 0)
19130 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019131 atv.v_type = VAR_NUMBER;
19132 atv.vval.v_number = curbuf->b_changedtick;
19133 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134 }
19135
19136 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137 * Check for user-defined variables.
19138 */
19139 else
19140 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019141 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019143 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144 }
19145
Bram Moolenaare9a41262005-01-15 22:18:47 +000019146 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019148 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019149 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150 ret = FAIL;
19151 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019152 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019153 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154
19155 name[len] = cc;
19156
19157 return ret;
19158}
19159
19160/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019161 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19162 * Also handle function call with Funcref variable: func(expr)
19163 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19164 */
19165 static int
19166handle_subscript(arg, rettv, evaluate, verbose)
19167 char_u **arg;
19168 typval_T *rettv;
19169 int evaluate; /* do more than finding the end */
19170 int verbose; /* give error messages */
19171{
19172 int ret = OK;
19173 dict_T *selfdict = NULL;
19174 char_u *s;
19175 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019176 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019177
19178 while (ret == OK
19179 && (**arg == '['
19180 || (**arg == '.' && rettv->v_type == VAR_DICT)
19181 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19182 && !vim_iswhite(*(*arg - 1)))
19183 {
19184 if (**arg == '(')
19185 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019186 /* need to copy the funcref so that we can clear rettv */
19187 functv = *rettv;
19188 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019189
19190 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019191 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019192 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019193 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19194 &len, evaluate, selfdict);
19195
19196 /* Clear the funcref afterwards, so that deleting it while
19197 * evaluating the arguments is possible (see test55). */
19198 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019199
19200 /* Stop the expression evaluation when immediately aborting on
19201 * error, or when an interrupt occurred or an exception was thrown
19202 * but not caught. */
19203 if (aborting())
19204 {
19205 if (ret == OK)
19206 clear_tv(rettv);
19207 ret = FAIL;
19208 }
19209 dict_unref(selfdict);
19210 selfdict = NULL;
19211 }
19212 else /* **arg == '[' || **arg == '.' */
19213 {
19214 dict_unref(selfdict);
19215 if (rettv->v_type == VAR_DICT)
19216 {
19217 selfdict = rettv->vval.v_dict;
19218 if (selfdict != NULL)
19219 ++selfdict->dv_refcount;
19220 }
19221 else
19222 selfdict = NULL;
19223 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19224 {
19225 clear_tv(rettv);
19226 ret = FAIL;
19227 }
19228 }
19229 }
19230 dict_unref(selfdict);
19231 return ret;
19232}
19233
19234/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019235 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019236 * value).
19237 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019238 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019239alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019240{
Bram Moolenaar33570922005-01-25 22:26:29 +000019241 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019242}
19243
19244/*
19245 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246 * The string "s" must have been allocated, it is consumed.
19247 * Return NULL for out of memory, the variable otherwise.
19248 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019249 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019250alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251 char_u *s;
19252{
Bram Moolenaar33570922005-01-25 22:26:29 +000019253 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019255 rettv = alloc_tv();
19256 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019257 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019258 rettv->v_type = VAR_STRING;
19259 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019260 }
19261 else
19262 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019263 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264}
19265
19266/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019267 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019268 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019269 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019270free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019271 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272{
19273 if (varp != NULL)
19274 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019275 switch (varp->v_type)
19276 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019277 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019278 func_unref(varp->vval.v_string);
19279 /*FALLTHROUGH*/
19280 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019281 vim_free(varp->vval.v_string);
19282 break;
19283 case VAR_LIST:
19284 list_unref(varp->vval.v_list);
19285 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019286 case VAR_DICT:
19287 dict_unref(varp->vval.v_dict);
19288 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019289 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019290#ifdef FEAT_FLOAT
19291 case VAR_FLOAT:
19292#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019293 case VAR_UNKNOWN:
19294 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019295 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019296 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019297 break;
19298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299 vim_free(varp);
19300 }
19301}
19302
19303/*
19304 * Free the memory for a variable value and set the value to NULL or 0.
19305 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019306 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019307clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019308 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309{
19310 if (varp != NULL)
19311 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019312 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019314 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019315 func_unref(varp->vval.v_string);
19316 /*FALLTHROUGH*/
19317 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019318 vim_free(varp->vval.v_string);
19319 varp->vval.v_string = NULL;
19320 break;
19321 case VAR_LIST:
19322 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019323 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019324 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019325 case VAR_DICT:
19326 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019327 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019328 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019329 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019330 varp->vval.v_number = 0;
19331 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019332#ifdef FEAT_FLOAT
19333 case VAR_FLOAT:
19334 varp->vval.v_float = 0.0;
19335 break;
19336#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019337 case VAR_UNKNOWN:
19338 break;
19339 default:
19340 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019342 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343 }
19344}
19345
19346/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019347 * Set the value of a variable to NULL without freeing items.
19348 */
19349 static void
19350init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019351 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019352{
19353 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019354 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019355}
19356
19357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358 * Get the number value of a variable.
19359 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019360 * For incompatible types, return 0.
19361 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19362 * caller of incompatible types: it sets *denote to TRUE if "denote"
19363 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364 */
19365 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019366get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019367 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019369 int error = FALSE;
19370
19371 return get_tv_number_chk(varp, &error); /* return 0L on error */
19372}
19373
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019374 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019375get_tv_number_chk(varp, denote)
19376 typval_T *varp;
19377 int *denote;
19378{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019379 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019381 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019383 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019384 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019385#ifdef FEAT_FLOAT
19386 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019387 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019388 break;
19389#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019390 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019391 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019392 break;
19393 case VAR_STRING:
19394 if (varp->vval.v_string != NULL)
19395 vim_str2nr(varp->vval.v_string, NULL, NULL,
19396 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019397 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019398 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019399 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019400 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019401 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019402 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019403 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019404 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019405 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019406 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019408 if (denote == NULL) /* useful for values that must be unsigned */
19409 n = -1;
19410 else
19411 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019412 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413}
19414
19415/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019416 * Get the lnum from the first argument.
19417 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019418 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019419 */
19420 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019421get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019422 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423{
Bram Moolenaar33570922005-01-25 22:26:29 +000019424 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425 linenr_T lnum;
19426
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019427 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019428 if (lnum == 0) /* no valid number, try using line() */
19429 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019430 rettv.v_type = VAR_NUMBER;
19431 f_line(argvars, &rettv);
19432 lnum = rettv.vval.v_number;
19433 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019434 }
19435 return lnum;
19436}
19437
19438/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019439 * Get the lnum from the first argument.
19440 * Also accepts "$", then "buf" is used.
19441 * Returns 0 on error.
19442 */
19443 static linenr_T
19444get_tv_lnum_buf(argvars, buf)
19445 typval_T *argvars;
19446 buf_T *buf;
19447{
19448 if (argvars[0].v_type == VAR_STRING
19449 && argvars[0].vval.v_string != NULL
19450 && argvars[0].vval.v_string[0] == '$'
19451 && buf != NULL)
19452 return buf->b_ml.ml_line_count;
19453 return get_tv_number_chk(&argvars[0], NULL);
19454}
19455
19456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457 * Get the string value of a variable.
19458 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019459 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19460 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461 * If the String variable has never been set, return an empty string.
19462 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019463 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19464 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465 */
19466 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019467get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019468 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469{
19470 static char_u mybuf[NUMBUFLEN];
19471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019472 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019473}
19474
19475 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019476get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019477 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019478 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019479{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019480 char_u *res = get_tv_string_buf_chk(varp, buf);
19481
19482 return res != NULL ? res : (char_u *)"";
19483}
19484
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019485 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019486get_tv_string_chk(varp)
19487 typval_T *varp;
19488{
19489 static char_u mybuf[NUMBUFLEN];
19490
19491 return get_tv_string_buf_chk(varp, mybuf);
19492}
19493
19494 static char_u *
19495get_tv_string_buf_chk(varp, buf)
19496 typval_T *varp;
19497 char_u *buf;
19498{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019499 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019500 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019501 case VAR_NUMBER:
19502 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19503 return buf;
19504 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019505 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019506 break;
19507 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019508 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019509 break;
19510 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019511 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019512 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019513#ifdef FEAT_FLOAT
19514 case VAR_FLOAT:
19515 EMSG(_("E806: using Float as a String"));
19516 break;
19517#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019518 case VAR_STRING:
19519 if (varp->vval.v_string != NULL)
19520 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019521 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019522 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019523 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019524 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019526 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527}
19528
19529/*
19530 * Find variable "name" in the list of variables.
19531 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019532 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019533 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019534 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019535 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019536 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019537find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019538 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019539 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019541 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019542 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543
Bram Moolenaara7043832005-01-21 11:56:39 +000019544 ht = find_var_ht(name, &varname);
19545 if (htp != NULL)
19546 *htp = ht;
19547 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019548 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019549 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550}
19551
19552/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019553 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019554 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019556 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019557find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019558 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019559 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019560 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019561{
Bram Moolenaar33570922005-01-25 22:26:29 +000019562 hashitem_T *hi;
19563
19564 if (*varname == NUL)
19565 {
19566 /* Must be something like "s:", otherwise "ht" would be NULL. */
19567 switch (varname[-2])
19568 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019569 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019570 case 'g': return &globvars_var;
19571 case 'v': return &vimvars_var;
19572 case 'b': return &curbuf->b_bufvar;
19573 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019574#ifdef FEAT_WINDOWS
19575 case 't': return &curtab->tp_winvar;
19576#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019577 case 'l': return current_funccal == NULL
19578 ? NULL : &current_funccal->l_vars_var;
19579 case 'a': return current_funccal == NULL
19580 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019581 }
19582 return NULL;
19583 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019584
19585 hi = hash_find(ht, varname);
19586 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019587 {
19588 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019589 * worked find the variable again. Don't auto-load a script if it was
19590 * loaded already, otherwise it would be loaded every time when
19591 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019592 if (ht == &globvarht && !writing)
19593 {
19594 /* Note: script_autoload() may make "hi" invalid. It must either
19595 * be obtained again or not used. */
19596 if (!script_autoload(varname, FALSE) || aborting())
19597 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019598 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019599 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019600 if (HASHITEM_EMPTY(hi))
19601 return NULL;
19602 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019603 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019604}
19605
19606/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019607 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019608 * Set "varname" to the start of name without ':'.
19609 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019610 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019611find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612 char_u *name;
19613 char_u **varname;
19614{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019615 hashitem_T *hi;
19616
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617 if (name[1] != ':')
19618 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019619 /* The name must not start with a colon or #. */
19620 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 return NULL;
19622 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019623
19624 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019625 hi = hash_find(&compat_hashtab, name);
19626 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019627 return &compat_hashtab;
19628
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019630 return &globvarht; /* global variable */
19631 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019632 }
19633 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019634 if (*name == 'g') /* global variable */
19635 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019636 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19637 */
19638 if (vim_strchr(name + 2, ':') != NULL
19639 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019640 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019642 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019644 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019645#ifdef FEAT_WINDOWS
19646 if (*name == 't') /* tab page variable */
19647 return &curtab->tp_vars.dv_hashtab;
19648#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019649 if (*name == 'v') /* v: variable */
19650 return &vimvarht;
19651 if (*name == 'a' && current_funccal != NULL) /* function argument */
19652 return &current_funccal->l_avars.dv_hashtab;
19653 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19654 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019655 if (*name == 's' /* script variable */
19656 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19657 return &SCRIPT_VARS(current_SID);
19658 return NULL;
19659}
19660
19661/*
19662 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019663 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019664 * Returns NULL when it doesn't exist.
19665 */
19666 char_u *
19667get_var_value(name)
19668 char_u *name;
19669{
Bram Moolenaar33570922005-01-25 22:26:29 +000019670 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671
Bram Moolenaara7043832005-01-21 11:56:39 +000019672 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673 if (v == NULL)
19674 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019675 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676}
19677
19678/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019679 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680 * sourcing this script and when executing functions defined in the script.
19681 */
19682 void
19683new_script_vars(id)
19684 scid_T id;
19685{
Bram Moolenaara7043832005-01-21 11:56:39 +000019686 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019687 hashtab_T *ht;
19688 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019689
Bram Moolenaar071d4272004-06-13 20:20:40 +000019690 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19691 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019692 /* Re-allocating ga_data means that an ht_array pointing to
19693 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019694 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019695 for (i = 1; i <= ga_scripts.ga_len; ++i)
19696 {
19697 ht = &SCRIPT_VARS(i);
19698 if (ht->ht_mask == HT_INIT_SIZE - 1)
19699 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019700 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019701 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019702 }
19703
Bram Moolenaar071d4272004-06-13 20:20:40 +000019704 while (ga_scripts.ga_len < id)
19705 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019706 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019707 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019708 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019710 }
19711 }
19712}
19713
19714/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019715 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19716 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717 */
19718 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019719init_var_dict(dict, dict_var)
19720 dict_T *dict;
19721 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722{
Bram Moolenaar33570922005-01-25 22:26:29 +000019723 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019724 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019725 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019726 dict_var->di_tv.vval.v_dict = dict;
19727 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019728 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019729 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19730 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731}
19732
19733/*
19734 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019735 * Frees all allocated variables and the value they contain.
19736 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737 */
19738 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019739vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019740 hashtab_T *ht;
19741{
19742 vars_clear_ext(ht, TRUE);
19743}
19744
19745/*
19746 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19747 */
19748 static void
19749vars_clear_ext(ht, free_val)
19750 hashtab_T *ht;
19751 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752{
Bram Moolenaara7043832005-01-21 11:56:39 +000019753 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019754 hashitem_T *hi;
19755 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756
Bram Moolenaar33570922005-01-25 22:26:29 +000019757 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019758 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019759 for (hi = ht->ht_array; todo > 0; ++hi)
19760 {
19761 if (!HASHITEM_EMPTY(hi))
19762 {
19763 --todo;
19764
Bram Moolenaar33570922005-01-25 22:26:29 +000019765 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019766 * ht_array might change then. hash_clear() takes care of it
19767 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019768 v = HI2DI(hi);
19769 if (free_val)
19770 clear_tv(&v->di_tv);
19771 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19772 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019773 }
19774 }
19775 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019776 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777}
19778
Bram Moolenaara7043832005-01-21 11:56:39 +000019779/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019780 * Delete a variable from hashtab "ht" at item "hi".
19781 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019784delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019785 hashtab_T *ht;
19786 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019787{
Bram Moolenaar33570922005-01-25 22:26:29 +000019788 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019789
19790 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019791 clear_tv(&di->di_tv);
19792 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793}
19794
19795/*
19796 * List the value of one internal variable.
19797 */
19798 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019799list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019800 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019802 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019803{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019804 char_u *tofree;
19805 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019806 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019807
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019808 current_copyID += COPYID_INC;
19809 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019810 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019811 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019812 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813}
19814
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019816list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 char_u *prefix;
19818 char_u *name;
19819 int type;
19820 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019821 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822{
Bram Moolenaar31859182007-08-14 20:41:13 +000019823 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19824 msg_start();
19825 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826 if (name != NULL) /* "a:" vars don't have a name stored */
19827 msg_puts(name);
19828 msg_putchar(' ');
19829 msg_advance(22);
19830 if (type == VAR_NUMBER)
19831 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019832 else if (type == VAR_FUNC)
19833 msg_putchar('*');
19834 else if (type == VAR_LIST)
19835 {
19836 msg_putchar('[');
19837 if (*string == '[')
19838 ++string;
19839 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019840 else if (type == VAR_DICT)
19841 {
19842 msg_putchar('{');
19843 if (*string == '{')
19844 ++string;
19845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019846 else
19847 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019848
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019850
19851 if (type == VAR_FUNC)
19852 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019853 if (*first)
19854 {
19855 msg_clr_eos();
19856 *first = FALSE;
19857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019858}
19859
19860/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019861 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862 * If the variable already exists, the value is updated.
19863 * Otherwise the variable is created.
19864 */
19865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019866set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019868 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019869 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019870{
Bram Moolenaar33570922005-01-25 22:26:29 +000019871 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019873 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019875 ht = find_var_ht(name, &varname);
19876 if (ht == NULL || *varname == NUL)
19877 {
19878 EMSG2(_(e_illvar), name);
19879 return;
19880 }
19881 v = find_var_in_ht(ht, varname, TRUE);
19882
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019883 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
19884 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019885
Bram Moolenaar33570922005-01-25 22:26:29 +000019886 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019887 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019888 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019889 if (var_check_ro(v->di_flags, name)
19890 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019891 return;
19892 if (v->di_tv.v_type != tv->v_type
19893 && !((v->di_tv.v_type == VAR_STRING
19894 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019895 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019896 || tv->v_type == VAR_NUMBER))
19897#ifdef FEAT_FLOAT
19898 && !((v->di_tv.v_type == VAR_NUMBER
19899 || v->di_tv.v_type == VAR_FLOAT)
19900 && (tv->v_type == VAR_NUMBER
19901 || tv->v_type == VAR_FLOAT))
19902#endif
19903 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019904 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019905 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019906 return;
19907 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019908
19909 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019910 * Handle setting internal v: variables separately: we don't change
19911 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019912 */
19913 if (ht == &vimvarht)
19914 {
19915 if (v->di_tv.v_type == VAR_STRING)
19916 {
19917 vim_free(v->di_tv.vval.v_string);
19918 if (copy || tv->v_type != VAR_STRING)
19919 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19920 else
19921 {
19922 /* Take over the string to avoid an extra alloc/free. */
19923 v->di_tv.vval.v_string = tv->vval.v_string;
19924 tv->vval.v_string = NULL;
19925 }
19926 }
19927 else if (v->di_tv.v_type != VAR_NUMBER)
19928 EMSG2(_(e_intern2), "set_var()");
19929 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019930 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019931 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019932 if (STRCMP(varname, "searchforward") == 0)
19933 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19934 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019935 return;
19936 }
19937
19938 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019939 }
19940 else /* add a new variable */
19941 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019942 /* Can't add "v:" variable. */
19943 if (ht == &vimvarht)
19944 {
19945 EMSG2(_(e_illvar), name);
19946 return;
19947 }
19948
Bram Moolenaar92124a32005-06-17 22:03:40 +000019949 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019950 if (!valid_varname(varname))
19951 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019952
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019953 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19954 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019955 if (v == NULL)
19956 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019957 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019958 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019960 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019961 return;
19962 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019963 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019965
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019966 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019967 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019968 else
19969 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019970 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019971 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019972 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974}
19975
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019976/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019977 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019978 * Also give an error message.
19979 */
19980 static int
19981var_check_ro(flags, name)
19982 int flags;
19983 char_u *name;
19984{
19985 if (flags & DI_FLAGS_RO)
19986 {
19987 EMSG2(_(e_readonlyvar), name);
19988 return TRUE;
19989 }
19990 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19991 {
19992 EMSG2(_(e_readonlysbx), name);
19993 return TRUE;
19994 }
19995 return FALSE;
19996}
19997
19998/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019999 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20000 * Also give an error message.
20001 */
20002 static int
20003var_check_fixed(flags, name)
20004 int flags;
20005 char_u *name;
20006{
20007 if (flags & DI_FLAGS_FIX)
20008 {
20009 EMSG2(_("E795: Cannot delete variable %s"), name);
20010 return TRUE;
20011 }
20012 return FALSE;
20013}
20014
20015/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020016 * Check if a funcref is assigned to a valid variable name.
20017 * Return TRUE and give an error if not.
20018 */
20019 static int
20020var_check_func_name(name, new_var)
20021 char_u *name; /* points to start of variable name */
20022 int new_var; /* TRUE when creating the variable */
20023{
20024 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20025 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20026 ? name[2] : name[0]))
20027 {
20028 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20029 name);
20030 return TRUE;
20031 }
20032 /* Don't allow hiding a function. When "v" is not NULL we might be
20033 * assigning another function to the same var, the type is checked
20034 * below. */
20035 if (new_var && function_exists(name))
20036 {
20037 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20038 name);
20039 return TRUE;
20040 }
20041 return FALSE;
20042}
20043
20044/*
20045 * Check if a variable name is valid.
20046 * Return FALSE and give an error if not.
20047 */
20048 static int
20049valid_varname(varname)
20050 char_u *varname;
20051{
20052 char_u *p;
20053
20054 for (p = varname; *p != NUL; ++p)
20055 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20056 && *p != AUTOLOAD_CHAR)
20057 {
20058 EMSG2(_(e_illvar), varname);
20059 return FALSE;
20060 }
20061 return TRUE;
20062}
20063
20064/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020065 * Return TRUE if typeval "tv" is set to be locked (immutable).
20066 * Also give an error message, using "name".
20067 */
20068 static int
20069tv_check_lock(lock, name)
20070 int lock;
20071 char_u *name;
20072{
20073 if (lock & VAR_LOCKED)
20074 {
20075 EMSG2(_("E741: Value is locked: %s"),
20076 name == NULL ? (char_u *)_("Unknown") : name);
20077 return TRUE;
20078 }
20079 if (lock & VAR_FIXED)
20080 {
20081 EMSG2(_("E742: Cannot change value of %s"),
20082 name == NULL ? (char_u *)_("Unknown") : name);
20083 return TRUE;
20084 }
20085 return FALSE;
20086}
20087
20088/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020089 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020090 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020091 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020092 * It is OK for "from" and "to" to point to the same item. This is used to
20093 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020094 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020095 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020096copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020097 typval_T *from;
20098 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020100 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020101 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020102 switch (from->v_type)
20103 {
20104 case VAR_NUMBER:
20105 to->vval.v_number = from->vval.v_number;
20106 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020107#ifdef FEAT_FLOAT
20108 case VAR_FLOAT:
20109 to->vval.v_float = from->vval.v_float;
20110 break;
20111#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020112 case VAR_STRING:
20113 case VAR_FUNC:
20114 if (from->vval.v_string == NULL)
20115 to->vval.v_string = NULL;
20116 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020117 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020118 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020119 if (from->v_type == VAR_FUNC)
20120 func_ref(to->vval.v_string);
20121 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020122 break;
20123 case VAR_LIST:
20124 if (from->vval.v_list == NULL)
20125 to->vval.v_list = NULL;
20126 else
20127 {
20128 to->vval.v_list = from->vval.v_list;
20129 ++to->vval.v_list->lv_refcount;
20130 }
20131 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020132 case VAR_DICT:
20133 if (from->vval.v_dict == NULL)
20134 to->vval.v_dict = NULL;
20135 else
20136 {
20137 to->vval.v_dict = from->vval.v_dict;
20138 ++to->vval.v_dict->dv_refcount;
20139 }
20140 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020141 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020142 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020143 break;
20144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145}
20146
20147/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020148 * Make a copy of an item.
20149 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020150 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20151 * reference to an already copied list/dict can be used.
20152 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020153 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020154 static int
20155item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020156 typval_T *from;
20157 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020158 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020159 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020160{
20161 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020162 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020163
Bram Moolenaar33570922005-01-25 22:26:29 +000020164 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020165 {
20166 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020167 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020168 }
20169 ++recurse;
20170
20171 switch (from->v_type)
20172 {
20173 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020174#ifdef FEAT_FLOAT
20175 case VAR_FLOAT:
20176#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020177 case VAR_STRING:
20178 case VAR_FUNC:
20179 copy_tv(from, to);
20180 break;
20181 case VAR_LIST:
20182 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020183 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020184 if (from->vval.v_list == NULL)
20185 to->vval.v_list = NULL;
20186 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20187 {
20188 /* use the copy made earlier */
20189 to->vval.v_list = from->vval.v_list->lv_copylist;
20190 ++to->vval.v_list->lv_refcount;
20191 }
20192 else
20193 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20194 if (to->vval.v_list == NULL)
20195 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020196 break;
20197 case VAR_DICT:
20198 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020199 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020200 if (from->vval.v_dict == NULL)
20201 to->vval.v_dict = NULL;
20202 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20203 {
20204 /* use the copy made earlier */
20205 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20206 ++to->vval.v_dict->dv_refcount;
20207 }
20208 else
20209 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20210 if (to->vval.v_dict == NULL)
20211 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020212 break;
20213 default:
20214 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020215 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020216 }
20217 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020218 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020219}
20220
20221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222 * ":echo expr1 ..." print each argument separated with a space, add a
20223 * newline at the end.
20224 * ":echon expr1 ..." print each argument plain.
20225 */
20226 void
20227ex_echo(eap)
20228 exarg_T *eap;
20229{
20230 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020231 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020232 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020233 char_u *p;
20234 int needclr = TRUE;
20235 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020236 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237
20238 if (eap->skip)
20239 ++emsg_skip;
20240 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20241 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020242 /* If eval1() causes an error message the text from the command may
20243 * still need to be cleared. E.g., "echo 22,44". */
20244 need_clr_eos = needclr;
20245
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020247 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248 {
20249 /*
20250 * Report the invalid expression unless the expression evaluation
20251 * has been cancelled due to an aborting error, an interrupt, or an
20252 * exception.
20253 */
20254 if (!aborting())
20255 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020256 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257 break;
20258 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020259 need_clr_eos = FALSE;
20260
Bram Moolenaar071d4272004-06-13 20:20:40 +000020261 if (!eap->skip)
20262 {
20263 if (atstart)
20264 {
20265 atstart = FALSE;
20266 /* Call msg_start() after eval1(), evaluating the expression
20267 * may cause a message to appear. */
20268 if (eap->cmdidx == CMD_echo)
20269 msg_start();
20270 }
20271 else if (eap->cmdidx == CMD_echo)
20272 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020273 current_copyID += COPYID_INC;
20274 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020275 if (p != NULL)
20276 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020278 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020280 if (*p != TAB && needclr)
20281 {
20282 /* remove any text still there from the command */
20283 msg_clr_eos();
20284 needclr = FALSE;
20285 }
20286 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287 }
20288 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020289 {
20290#ifdef FEAT_MBYTE
20291 if (has_mbyte)
20292 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020293 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020294
20295 (void)msg_outtrans_len_attr(p, i, echo_attr);
20296 p += i - 1;
20297 }
20298 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020300 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020303 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020305 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306 arg = skipwhite(arg);
20307 }
20308 eap->nextcmd = check_nextcmd(arg);
20309
20310 if (eap->skip)
20311 --emsg_skip;
20312 else
20313 {
20314 /* remove text that may still be there from the command */
20315 if (needclr)
20316 msg_clr_eos();
20317 if (eap->cmdidx == CMD_echo)
20318 msg_end();
20319 }
20320}
20321
20322/*
20323 * ":echohl {name}".
20324 */
20325 void
20326ex_echohl(eap)
20327 exarg_T *eap;
20328{
20329 int id;
20330
20331 id = syn_name2id(eap->arg);
20332 if (id == 0)
20333 echo_attr = 0;
20334 else
20335 echo_attr = syn_id2attr(id);
20336}
20337
20338/*
20339 * ":execute expr1 ..." execute the result of an expression.
20340 * ":echomsg expr1 ..." Print a message
20341 * ":echoerr expr1 ..." Print an error
20342 * Each gets spaces around each argument and a newline at the end for
20343 * echo commands
20344 */
20345 void
20346ex_execute(eap)
20347 exarg_T *eap;
20348{
20349 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020350 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351 int ret = OK;
20352 char_u *p;
20353 garray_T ga;
20354 int len;
20355 int save_did_emsg;
20356
20357 ga_init2(&ga, 1, 80);
20358
20359 if (eap->skip)
20360 ++emsg_skip;
20361 while (*arg != NUL && *arg != '|' && *arg != '\n')
20362 {
20363 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020364 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365 {
20366 /*
20367 * Report the invalid expression unless the expression evaluation
20368 * has been cancelled due to an aborting error, an interrupt, or an
20369 * exception.
20370 */
20371 if (!aborting())
20372 EMSG2(_(e_invexpr2), p);
20373 ret = FAIL;
20374 break;
20375 }
20376
20377 if (!eap->skip)
20378 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020379 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 len = (int)STRLEN(p);
20381 if (ga_grow(&ga, len + 2) == FAIL)
20382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020383 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384 ret = FAIL;
20385 break;
20386 }
20387 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390 ga.ga_len += len;
20391 }
20392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020393 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394 arg = skipwhite(arg);
20395 }
20396
20397 if (ret != FAIL && ga.ga_data != NULL)
20398 {
20399 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020400 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020401 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020402 out_flush();
20403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020404 else if (eap->cmdidx == CMD_echoerr)
20405 {
20406 /* We don't want to abort following commands, restore did_emsg. */
20407 save_did_emsg = did_emsg;
20408 EMSG((char_u *)ga.ga_data);
20409 if (!force_abort)
20410 did_emsg = save_did_emsg;
20411 }
20412 else if (eap->cmdidx == CMD_execute)
20413 do_cmdline((char_u *)ga.ga_data,
20414 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20415 }
20416
20417 ga_clear(&ga);
20418
20419 if (eap->skip)
20420 --emsg_skip;
20421
20422 eap->nextcmd = check_nextcmd(arg);
20423}
20424
20425/*
20426 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20427 * "arg" points to the "&" or '+' when called, to "option" when returning.
20428 * Returns NULL when no option name found. Otherwise pointer to the char
20429 * after the option name.
20430 */
20431 static char_u *
20432find_option_end(arg, opt_flags)
20433 char_u **arg;
20434 int *opt_flags;
20435{
20436 char_u *p = *arg;
20437
20438 ++p;
20439 if (*p == 'g' && p[1] == ':')
20440 {
20441 *opt_flags = OPT_GLOBAL;
20442 p += 2;
20443 }
20444 else if (*p == 'l' && p[1] == ':')
20445 {
20446 *opt_flags = OPT_LOCAL;
20447 p += 2;
20448 }
20449 else
20450 *opt_flags = 0;
20451
20452 if (!ASCII_ISALPHA(*p))
20453 return NULL;
20454 *arg = p;
20455
20456 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20457 p += 4; /* termcap option */
20458 else
20459 while (ASCII_ISALPHA(*p))
20460 ++p;
20461 return p;
20462}
20463
20464/*
20465 * ":function"
20466 */
20467 void
20468ex_function(eap)
20469 exarg_T *eap;
20470{
20471 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020472 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473 int j;
20474 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476 char_u *name = NULL;
20477 char_u *p;
20478 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020479 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020480 garray_T newargs;
20481 garray_T newlines;
20482 int varargs = FALSE;
20483 int mustend = FALSE;
20484 int flags = 0;
20485 ufunc_T *fp;
20486 int indent;
20487 int nesting;
20488 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020489 dictitem_T *v;
20490 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020491 static int func_nr = 0; /* number for nameless function */
20492 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020493 hashtab_T *ht;
20494 int todo;
20495 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020496 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497
20498 /*
20499 * ":function" without argument: list functions.
20500 */
20501 if (ends_excmd(*eap->arg))
20502 {
20503 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020504 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020505 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020506 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020507 {
20508 if (!HASHITEM_EMPTY(hi))
20509 {
20510 --todo;
20511 fp = HI2UF(hi);
20512 if (!isdigit(*fp->uf_name))
20513 list_func_head(fp, FALSE);
20514 }
20515 }
20516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517 eap->nextcmd = check_nextcmd(eap->arg);
20518 return;
20519 }
20520
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020521 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020522 * ":function /pat": list functions matching pattern.
20523 */
20524 if (*eap->arg == '/')
20525 {
20526 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20527 if (!eap->skip)
20528 {
20529 regmatch_T regmatch;
20530
20531 c = *p;
20532 *p = NUL;
20533 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20534 *p = c;
20535 if (regmatch.regprog != NULL)
20536 {
20537 regmatch.rm_ic = p_ic;
20538
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020539 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020540 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20541 {
20542 if (!HASHITEM_EMPTY(hi))
20543 {
20544 --todo;
20545 fp = HI2UF(hi);
20546 if (!isdigit(*fp->uf_name)
20547 && vim_regexec(&regmatch, fp->uf_name, 0))
20548 list_func_head(fp, FALSE);
20549 }
20550 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020551 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020552 }
20553 }
20554 if (*p == '/')
20555 ++p;
20556 eap->nextcmd = check_nextcmd(p);
20557 return;
20558 }
20559
20560 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020561 * Get the function name. There are these situations:
20562 * func normal function name
20563 * "name" == func, "fudi.fd_dict" == NULL
20564 * dict.func new dictionary entry
20565 * "name" == NULL, "fudi.fd_dict" set,
20566 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20567 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020568 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020569 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20570 * dict.func existing dict entry that's not a Funcref
20571 * "name" == NULL, "fudi.fd_dict" set,
20572 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020574 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020575 name = trans_function_name(&p, eap->skip, 0, &fudi);
20576 paren = (vim_strchr(p, '(') != NULL);
20577 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578 {
20579 /*
20580 * Return on an invalid expression in braces, unless the expression
20581 * evaluation has been cancelled due to an aborting error, an
20582 * interrupt, or an exception.
20583 */
20584 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020585 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020586 if (!eap->skip && fudi.fd_newkey != NULL)
20587 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020588 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020589 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591 else
20592 eap->skip = TRUE;
20593 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020594
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595 /* An error in a function call during evaluation of an expression in magic
20596 * braces should not cause the function not to be defined. */
20597 saved_did_emsg = did_emsg;
20598 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020599
20600 /*
20601 * ":function func" with only function name: list function.
20602 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020603 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020604 {
20605 if (!ends_excmd(*skipwhite(p)))
20606 {
20607 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020608 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609 }
20610 eap->nextcmd = check_nextcmd(p);
20611 if (eap->nextcmd != NULL)
20612 *p = NUL;
20613 if (!eap->skip && !got_int)
20614 {
20615 fp = find_func(name);
20616 if (fp != NULL)
20617 {
20618 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020619 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020620 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020621 if (FUNCLINE(fp, j) == NULL)
20622 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020623 msg_putchar('\n');
20624 msg_outnum((long)(j + 1));
20625 if (j < 9)
20626 msg_putchar(' ');
20627 if (j < 99)
20628 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020629 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020630 out_flush(); /* show a line at a time */
20631 ui_breakcheck();
20632 }
20633 if (!got_int)
20634 {
20635 msg_putchar('\n');
20636 msg_puts((char_u *)" endfunction");
20637 }
20638 }
20639 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020640 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020642 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643 }
20644
20645 /*
20646 * ":function name(arg1, arg2)" Define function.
20647 */
20648 p = skipwhite(p);
20649 if (*p != '(')
20650 {
20651 if (!eap->skip)
20652 {
20653 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020654 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020655 }
20656 /* attempt to continue by skipping some text */
20657 if (vim_strchr(p, '(') != NULL)
20658 p = vim_strchr(p, '(');
20659 }
20660 p = skipwhite(p + 1);
20661
20662 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20663 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20664
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020665 if (!eap->skip)
20666 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020667 /* Check the name of the function. Unless it's a dictionary function
20668 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020669 if (name != NULL)
20670 arg = name;
20671 else
20672 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020673 if (arg != NULL && (fudi.fd_di == NULL
20674 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020675 {
20676 if (*arg == K_SPECIAL)
20677 j = 3;
20678 else
20679 j = 0;
20680 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20681 : eval_isnamec(arg[j])))
20682 ++j;
20683 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020684 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020685 }
20686 }
20687
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688 /*
20689 * Isolate the arguments: "arg1, arg2, ...)"
20690 */
20691 while (*p != ')')
20692 {
20693 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20694 {
20695 varargs = TRUE;
20696 p += 3;
20697 mustend = TRUE;
20698 }
20699 else
20700 {
20701 arg = p;
20702 while (ASCII_ISALNUM(*p) || *p == '_')
20703 ++p;
20704 if (arg == p || isdigit(*arg)
20705 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20706 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20707 {
20708 if (!eap->skip)
20709 EMSG2(_("E125: Illegal argument: %s"), arg);
20710 break;
20711 }
20712 if (ga_grow(&newargs, 1) == FAIL)
20713 goto erret;
20714 c = *p;
20715 *p = NUL;
20716 arg = vim_strsave(arg);
20717 if (arg == NULL)
20718 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020719
20720 /* Check for duplicate argument name. */
20721 for (i = 0; i < newargs.ga_len; ++i)
20722 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
20723 {
20724 EMSG2(_("E853: Duplicate argument name: %s"), arg);
20725 goto erret;
20726 }
20727
Bram Moolenaar071d4272004-06-13 20:20:40 +000020728 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20729 *p = c;
20730 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020731 if (*p == ',')
20732 ++p;
20733 else
20734 mustend = TRUE;
20735 }
20736 p = skipwhite(p);
20737 if (mustend && *p != ')')
20738 {
20739 if (!eap->skip)
20740 EMSG2(_(e_invarg2), eap->arg);
20741 break;
20742 }
20743 }
20744 ++p; /* skip the ')' */
20745
Bram Moolenaare9a41262005-01-15 22:18:47 +000020746 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020747 for (;;)
20748 {
20749 p = skipwhite(p);
20750 if (STRNCMP(p, "range", 5) == 0)
20751 {
20752 flags |= FC_RANGE;
20753 p += 5;
20754 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020755 else if (STRNCMP(p, "dict", 4) == 0)
20756 {
20757 flags |= FC_DICT;
20758 p += 4;
20759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020760 else if (STRNCMP(p, "abort", 5) == 0)
20761 {
20762 flags |= FC_ABORT;
20763 p += 5;
20764 }
20765 else
20766 break;
20767 }
20768
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020769 /* When there is a line break use what follows for the function body.
20770 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20771 if (*p == '\n')
20772 line_arg = p + 1;
20773 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020774 EMSG(_(e_trailing));
20775
20776 /*
20777 * Read the body of the function, until ":endfunction" is found.
20778 */
20779 if (KeyTyped)
20780 {
20781 /* Check if the function already exists, don't let the user type the
20782 * whole function before telling him it doesn't work! For a script we
20783 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020784 if (!eap->skip && !eap->forceit)
20785 {
20786 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20787 EMSG(_(e_funcdict));
20788 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020789 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020791
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020792 if (!eap->skip && did_emsg)
20793 goto erret;
20794
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795 msg_putchar('\n'); /* don't overwrite the function name */
20796 cmdline_row = msg_row;
20797 }
20798
20799 indent = 2;
20800 nesting = 0;
20801 for (;;)
20802 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020020803 if (KeyTyped)
20804 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020805 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020806 sourcing_lnum_off = sourcing_lnum;
20807
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020808 if (line_arg != NULL)
20809 {
20810 /* Use eap->arg, split up in parts by line breaks. */
20811 theline = line_arg;
20812 p = vim_strchr(theline, '\n');
20813 if (p == NULL)
20814 line_arg += STRLEN(line_arg);
20815 else
20816 {
20817 *p = NUL;
20818 line_arg = p + 1;
20819 }
20820 }
20821 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020822 theline = getcmdline(':', 0L, indent);
20823 else
20824 theline = eap->getline(':', eap->cookie, indent);
20825 if (KeyTyped)
20826 lines_left = Rows - 1;
20827 if (theline == NULL)
20828 {
20829 EMSG(_("E126: Missing :endfunction"));
20830 goto erret;
20831 }
20832
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020833 /* Detect line continuation: sourcing_lnum increased more than one. */
20834 if (sourcing_lnum > sourcing_lnum_off + 1)
20835 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20836 else
20837 sourcing_lnum_off = 0;
20838
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839 if (skip_until != NULL)
20840 {
20841 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20842 * don't check for ":endfunc". */
20843 if (STRCMP(theline, skip_until) == 0)
20844 {
20845 vim_free(skip_until);
20846 skip_until = NULL;
20847 }
20848 }
20849 else
20850 {
20851 /* skip ':' and blanks*/
20852 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20853 ;
20854
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020855 /* Check for "endfunction". */
20856 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020858 if (line_arg == NULL)
20859 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860 break;
20861 }
20862
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020863 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864 * at "end". */
20865 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20866 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020867 else if (STRNCMP(p, "if", 2) == 0
20868 || STRNCMP(p, "wh", 2) == 0
20869 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870 || STRNCMP(p, "try", 3) == 0)
20871 indent += 2;
20872
20873 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020874 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020875 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020876 if (*p == '!')
20877 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020878 p += eval_fname_script(p);
20879 if (ASCII_ISALPHA(*p))
20880 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020881 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020882 if (*skipwhite(p) == '(')
20883 {
20884 ++nesting;
20885 indent += 2;
20886 }
20887 }
20888 }
20889
20890 /* Check for ":append" or ":insert". */
20891 p = skip_range(p, NULL);
20892 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20893 || (p[0] == 'i'
20894 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20895 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20896 skip_until = vim_strsave((char_u *)".");
20897
20898 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20899 arg = skipwhite(skiptowhite(p));
20900 if (arg[0] == '<' && arg[1] =='<'
20901 && ((p[0] == 'p' && p[1] == 'y'
20902 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20903 || (p[0] == 'p' && p[1] == 'e'
20904 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20905 || (p[0] == 't' && p[1] == 'c'
20906 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020020907 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
20908 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20910 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020911 || (p[0] == 'm' && p[1] == 'z'
20912 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913 ))
20914 {
20915 /* ":python <<" continues until a dot, like ":append" */
20916 p = skipwhite(arg + 2);
20917 if (*p == NUL)
20918 skip_until = vim_strsave((char_u *)".");
20919 else
20920 skip_until = vim_strsave(p);
20921 }
20922 }
20923
20924 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020925 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020926 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020927 if (line_arg == NULL)
20928 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020929 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020930 }
20931
20932 /* Copy the line to newly allocated memory. get_one_sourceline()
20933 * allocates 250 bytes per line, this saves 80% on average. The cost
20934 * is an extra alloc/free. */
20935 p = vim_strsave(theline);
20936 if (p != NULL)
20937 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020938 if (line_arg == NULL)
20939 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020940 theline = p;
20941 }
20942
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020943 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20944
20945 /* Add NULL lines for continuation lines, so that the line count is
20946 * equal to the index in the growarray. */
20947 while (sourcing_lnum_off-- > 0)
20948 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020949
20950 /* Check for end of eap->arg. */
20951 if (line_arg != NULL && *line_arg == NUL)
20952 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953 }
20954
20955 /* Don't define the function when skipping commands or when an error was
20956 * detected. */
20957 if (eap->skip || did_emsg)
20958 goto erret;
20959
20960 /*
20961 * If there are no errors, add the function
20962 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020963 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020964 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020965 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020966 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020967 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020968 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020969 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020970 goto erret;
20971 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020972
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020973 fp = find_func(name);
20974 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020975 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020976 if (!eap->forceit)
20977 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020978 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020979 goto erret;
20980 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020981 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020982 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020983 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020984 name);
20985 goto erret;
20986 }
20987 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020988 ga_clear_strings(&(fp->uf_args));
20989 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020990 vim_free(name);
20991 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993 }
20994 else
20995 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020996 char numbuf[20];
20997
20998 fp = NULL;
20999 if (fudi.fd_newkey == NULL && !eap->forceit)
21000 {
21001 EMSG(_(e_funcdict));
21002 goto erret;
21003 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021004 if (fudi.fd_di == NULL)
21005 {
21006 /* Can't add a function to a locked dictionary */
21007 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21008 goto erret;
21009 }
21010 /* Can't change an existing function if it is locked */
21011 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21012 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021013
21014 /* Give the function a sequential number. Can only be used with a
21015 * Funcref! */
21016 vim_free(name);
21017 sprintf(numbuf, "%d", ++func_nr);
21018 name = vim_strsave((char_u *)numbuf);
21019 if (name == NULL)
21020 goto erret;
21021 }
21022
21023 if (fp == NULL)
21024 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021025 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021026 {
21027 int slen, plen;
21028 char_u *scriptname;
21029
21030 /* Check that the autoload name matches the script name. */
21031 j = FAIL;
21032 if (sourcing_name != NULL)
21033 {
21034 scriptname = autoload_name(name);
21035 if (scriptname != NULL)
21036 {
21037 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021038 plen = (int)STRLEN(p);
21039 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021040 if (slen > plen && fnamecmp(p,
21041 sourcing_name + slen - plen) == 0)
21042 j = OK;
21043 vim_free(scriptname);
21044 }
21045 }
21046 if (j == FAIL)
21047 {
21048 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21049 goto erret;
21050 }
21051 }
21052
21053 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021054 if (fp == NULL)
21055 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021056
21057 if (fudi.fd_dict != NULL)
21058 {
21059 if (fudi.fd_di == NULL)
21060 {
21061 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021062 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021063 if (fudi.fd_di == NULL)
21064 {
21065 vim_free(fp);
21066 goto erret;
21067 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021068 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21069 {
21070 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021071 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021072 goto erret;
21073 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021074 }
21075 else
21076 /* overwrite existing dict entry */
21077 clear_tv(&fudi.fd_di->di_tv);
21078 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021079 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021080 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021081 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021082
21083 /* behave like "dict" was used */
21084 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021085 }
21086
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021088 STRCPY(fp->uf_name, name);
21089 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021091 fp->uf_args = newargs;
21092 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021093#ifdef FEAT_PROFILE
21094 fp->uf_tml_count = NULL;
21095 fp->uf_tml_total = NULL;
21096 fp->uf_tml_self = NULL;
21097 fp->uf_profiling = FALSE;
21098 if (prof_def_func())
21099 func_do_profile(fp);
21100#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021101 fp->uf_varargs = varargs;
21102 fp->uf_flags = flags;
21103 fp->uf_calls = 0;
21104 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021105 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106
21107erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021108 ga_clear_strings(&newargs);
21109 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021110ret_free:
21111 vim_free(skip_until);
21112 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021113 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115}
21116
21117/*
21118 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021119 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021121 * flags:
21122 * TFN_INT: internal function name OK
21123 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021124 * Advances "pp" to just after the function name (if no error).
21125 */
21126 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021127trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128 char_u **pp;
21129 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021130 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021131 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021133 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134 char_u *start;
21135 char_u *end;
21136 int lead;
21137 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021139 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021140
21141 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021142 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021144
21145 /* Check for hard coded <SNR>: already translated function ID (from a user
21146 * command). */
21147 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21148 && (*pp)[2] == (int)KE_SNR)
21149 {
21150 *pp += 3;
21151 len = get_id_len(pp) + 3;
21152 return vim_strnsave(start, len);
21153 }
21154
21155 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21156 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021157 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021158 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021160
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021161 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21162 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021163 if (end == start)
21164 {
21165 if (!skip)
21166 EMSG(_("E129: Function name required"));
21167 goto theend;
21168 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021169 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021170 {
21171 /*
21172 * Report an invalid expression in braces, unless the expression
21173 * evaluation has been cancelled due to an aborting error, an
21174 * interrupt, or an exception.
21175 */
21176 if (!aborting())
21177 {
21178 if (end != NULL)
21179 EMSG2(_(e_invarg2), start);
21180 }
21181 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021182 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021183 goto theend;
21184 }
21185
21186 if (lv.ll_tv != NULL)
21187 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021188 if (fdp != NULL)
21189 {
21190 fdp->fd_dict = lv.ll_dict;
21191 fdp->fd_newkey = lv.ll_newkey;
21192 lv.ll_newkey = NULL;
21193 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021194 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021195 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21196 {
21197 name = vim_strsave(lv.ll_tv->vval.v_string);
21198 *pp = end;
21199 }
21200 else
21201 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021202 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21203 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021204 EMSG(_(e_funcref));
21205 else
21206 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021207 name = NULL;
21208 }
21209 goto theend;
21210 }
21211
21212 if (lv.ll_name == NULL)
21213 {
21214 /* Error found, but continue after the function name. */
21215 *pp = end;
21216 goto theend;
21217 }
21218
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021219 /* Check if the name is a Funcref. If so, use the value. */
21220 if (lv.ll_exp_name != NULL)
21221 {
21222 len = (int)STRLEN(lv.ll_exp_name);
21223 name = deref_func_name(lv.ll_exp_name, &len);
21224 if (name == lv.ll_exp_name)
21225 name = NULL;
21226 }
21227 else
21228 {
21229 len = (int)(end - *pp);
21230 name = deref_func_name(*pp, &len);
21231 if (name == *pp)
21232 name = NULL;
21233 }
21234 if (name != NULL)
21235 {
21236 name = vim_strsave(name);
21237 *pp = end;
21238 goto theend;
21239 }
21240
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021241 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021242 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021243 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021244 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21245 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21246 {
21247 /* When there was "s:" already or the name expanded to get a
21248 * leading "s:" then remove it. */
21249 lv.ll_name += 2;
21250 len -= 2;
21251 lead = 2;
21252 }
21253 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021254 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021255 {
21256 if (lead == 2) /* skip over "s:" */
21257 lv.ll_name += 2;
21258 len = (int)(end - lv.ll_name);
21259 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021260
21261 /*
21262 * Copy the function name to allocated memory.
21263 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21264 * Accept <SNR>123_name() outside a script.
21265 */
21266 if (skip)
21267 lead = 0; /* do nothing */
21268 else if (lead > 0)
21269 {
21270 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021271 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21272 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021273 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021274 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021275 if (current_SID <= 0)
21276 {
21277 EMSG(_(e_usingsid));
21278 goto theend;
21279 }
21280 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21281 lead += (int)STRLEN(sid_buf);
21282 }
21283 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021284 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021285 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021286 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021287 goto theend;
21288 }
21289 name = alloc((unsigned)(len + lead + 1));
21290 if (name != NULL)
21291 {
21292 if (lead > 0)
21293 {
21294 name[0] = K_SPECIAL;
21295 name[1] = KS_EXTRA;
21296 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021297 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021298 STRCPY(name + 3, sid_buf);
21299 }
21300 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21301 name[len + lead] = NUL;
21302 }
21303 *pp = end;
21304
21305theend:
21306 clear_lval(&lv);
21307 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308}
21309
21310/*
21311 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21312 * Return 2 if "p" starts with "s:".
21313 * Return 0 otherwise.
21314 */
21315 static int
21316eval_fname_script(p)
21317 char_u *p;
21318{
21319 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21320 || STRNICMP(p + 1, "SNR>", 4) == 0))
21321 return 5;
21322 if (p[0] == 's' && p[1] == ':')
21323 return 2;
21324 return 0;
21325}
21326
21327/*
21328 * Return TRUE if "p" starts with "<SID>" or "s:".
21329 * Only works if eval_fname_script() returned non-zero for "p"!
21330 */
21331 static int
21332eval_fname_sid(p)
21333 char_u *p;
21334{
21335 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21336}
21337
21338/*
21339 * List the head of the function: "name(arg1, arg2)".
21340 */
21341 static void
21342list_func_head(fp, indent)
21343 ufunc_T *fp;
21344 int indent;
21345{
21346 int j;
21347
21348 msg_start();
21349 if (indent)
21350 MSG_PUTS(" ");
21351 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021352 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353 {
21354 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021355 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356 }
21357 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021358 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021360 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361 {
21362 if (j)
21363 MSG_PUTS(", ");
21364 msg_puts(FUNCARG(fp, j));
21365 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021366 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367 {
21368 if (j)
21369 MSG_PUTS(", ");
21370 MSG_PUTS("...");
21371 }
21372 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021373 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021374 if (p_verbose > 0)
21375 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376}
21377
21378/*
21379 * Find a function by name, return pointer to it in ufuncs.
21380 * Return NULL for unknown function.
21381 */
21382 static ufunc_T *
21383find_func(name)
21384 char_u *name;
21385{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021386 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021388 hi = hash_find(&func_hashtab, name);
21389 if (!HASHITEM_EMPTY(hi))
21390 return HI2UF(hi);
21391 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392}
21393
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021394#if defined(EXITFREE) || defined(PROTO)
21395 void
21396free_all_functions()
21397{
21398 hashitem_T *hi;
21399
21400 /* Need to start all over every time, because func_free() may change the
21401 * hash table. */
21402 while (func_hashtab.ht_used > 0)
21403 for (hi = func_hashtab.ht_array; ; ++hi)
21404 if (!HASHITEM_EMPTY(hi))
21405 {
21406 func_free(HI2UF(hi));
21407 break;
21408 }
21409}
21410#endif
21411
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021412/*
21413 * Return TRUE if a function "name" exists.
21414 */
21415 static int
21416function_exists(name)
21417 char_u *name;
21418{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021419 char_u *nm = name;
21420 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021421 int n = FALSE;
21422
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021423 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021424 nm = skipwhite(nm);
21425
21426 /* Only accept "funcname", "funcname ", "funcname (..." and
21427 * "funcname(...", not "funcname!...". */
21428 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021429 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021430 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021431 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021432 else
21433 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021434 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021435 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021436 return n;
21437}
21438
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021439/*
21440 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021441 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021442 */
21443 static int
21444builtin_function(name)
21445 char_u *name;
21446{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021447 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21448 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021449}
21450
Bram Moolenaar05159a02005-02-26 23:04:13 +000021451#if defined(FEAT_PROFILE) || defined(PROTO)
21452/*
21453 * Start profiling function "fp".
21454 */
21455 static void
21456func_do_profile(fp)
21457 ufunc_T *fp;
21458{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021459 int len = fp->uf_lines.ga_len;
21460
21461 if (len == 0)
21462 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021463 fp->uf_tm_count = 0;
21464 profile_zero(&fp->uf_tm_self);
21465 profile_zero(&fp->uf_tm_total);
21466 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021467 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021468 if (fp->uf_tml_total == NULL)
21469 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021470 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021471 if (fp->uf_tml_self == NULL)
21472 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021473 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021474 fp->uf_tml_idx = -1;
21475 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21476 || fp->uf_tml_self == NULL)
21477 return; /* out of memory */
21478
21479 fp->uf_profiling = TRUE;
21480}
21481
21482/*
21483 * Dump the profiling results for all functions in file "fd".
21484 */
21485 void
21486func_dump_profile(fd)
21487 FILE *fd;
21488{
21489 hashitem_T *hi;
21490 int todo;
21491 ufunc_T *fp;
21492 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021493 ufunc_T **sorttab;
21494 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021495
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021496 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021497 if (todo == 0)
21498 return; /* nothing to dump */
21499
Bram Moolenaar73830342005-02-28 22:48:19 +000021500 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21501
Bram Moolenaar05159a02005-02-26 23:04:13 +000021502 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21503 {
21504 if (!HASHITEM_EMPTY(hi))
21505 {
21506 --todo;
21507 fp = HI2UF(hi);
21508 if (fp->uf_profiling)
21509 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021510 if (sorttab != NULL)
21511 sorttab[st_len++] = fp;
21512
Bram Moolenaar05159a02005-02-26 23:04:13 +000021513 if (fp->uf_name[0] == K_SPECIAL)
21514 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21515 else
21516 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21517 if (fp->uf_tm_count == 1)
21518 fprintf(fd, "Called 1 time\n");
21519 else
21520 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21521 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21522 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21523 fprintf(fd, "\n");
21524 fprintf(fd, "count total (s) self (s)\n");
21525
21526 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21527 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021528 if (FUNCLINE(fp, i) == NULL)
21529 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021530 prof_func_line(fd, fp->uf_tml_count[i],
21531 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021532 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21533 }
21534 fprintf(fd, "\n");
21535 }
21536 }
21537 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021538
21539 if (sorttab != NULL && st_len > 0)
21540 {
21541 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21542 prof_total_cmp);
21543 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21544 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21545 prof_self_cmp);
21546 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21547 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021548
21549 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021550}
Bram Moolenaar73830342005-02-28 22:48:19 +000021551
21552 static void
21553prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21554 FILE *fd;
21555 ufunc_T **sorttab;
21556 int st_len;
21557 char *title;
21558 int prefer_self; /* when equal print only self time */
21559{
21560 int i;
21561 ufunc_T *fp;
21562
21563 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21564 fprintf(fd, "count total (s) self (s) function\n");
21565 for (i = 0; i < 20 && i < st_len; ++i)
21566 {
21567 fp = sorttab[i];
21568 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21569 prefer_self);
21570 if (fp->uf_name[0] == K_SPECIAL)
21571 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21572 else
21573 fprintf(fd, " %s()\n", fp->uf_name);
21574 }
21575 fprintf(fd, "\n");
21576}
21577
21578/*
21579 * Print the count and times for one function or function line.
21580 */
21581 static void
21582prof_func_line(fd, count, total, self, prefer_self)
21583 FILE *fd;
21584 int count;
21585 proftime_T *total;
21586 proftime_T *self;
21587 int prefer_self; /* when equal print only self time */
21588{
21589 if (count > 0)
21590 {
21591 fprintf(fd, "%5d ", count);
21592 if (prefer_self && profile_equal(total, self))
21593 fprintf(fd, " ");
21594 else
21595 fprintf(fd, "%s ", profile_msg(total));
21596 if (!prefer_self && profile_equal(total, self))
21597 fprintf(fd, " ");
21598 else
21599 fprintf(fd, "%s ", profile_msg(self));
21600 }
21601 else
21602 fprintf(fd, " ");
21603}
21604
21605/*
21606 * Compare function for total time sorting.
21607 */
21608 static int
21609#ifdef __BORLANDC__
21610_RTLENTRYF
21611#endif
21612prof_total_cmp(s1, s2)
21613 const void *s1;
21614 const void *s2;
21615{
21616 ufunc_T *p1, *p2;
21617
21618 p1 = *(ufunc_T **)s1;
21619 p2 = *(ufunc_T **)s2;
21620 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21621}
21622
21623/*
21624 * Compare function for self time sorting.
21625 */
21626 static int
21627#ifdef __BORLANDC__
21628_RTLENTRYF
21629#endif
21630prof_self_cmp(s1, s2)
21631 const void *s1;
21632 const void *s2;
21633{
21634 ufunc_T *p1, *p2;
21635
21636 p1 = *(ufunc_T **)s1;
21637 p2 = *(ufunc_T **)s2;
21638 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21639}
21640
Bram Moolenaar05159a02005-02-26 23:04:13 +000021641#endif
21642
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021643/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021644 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021645 * Return TRUE if a package was loaded.
21646 */
21647 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021648script_autoload(name, reload)
21649 char_u *name;
21650 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021651{
21652 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021653 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021654 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021655 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021656
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021657 /* Return quickly when autoload disabled. */
21658 if (no_autoload)
21659 return FALSE;
21660
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021661 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021662 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021663 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021664 return FALSE;
21665
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021666 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021667
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021668 /* Find the name in the list of previously loaded package names. Skip
21669 * "autoload/", it's always the same. */
21670 for (i = 0; i < ga_loaded.ga_len; ++i)
21671 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21672 break;
21673 if (!reload && i < ga_loaded.ga_len)
21674 ret = FALSE; /* was loaded already */
21675 else
21676 {
21677 /* Remember the name if it wasn't loaded already. */
21678 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21679 {
21680 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21681 tofree = NULL;
21682 }
21683
21684 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021685 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021686 ret = TRUE;
21687 }
21688
21689 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021690 return ret;
21691}
21692
21693/*
21694 * Return the autoload script name for a function or variable name.
21695 * Returns NULL when out of memory.
21696 */
21697 static char_u *
21698autoload_name(name)
21699 char_u *name;
21700{
21701 char_u *p;
21702 char_u *scriptname;
21703
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021704 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021705 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21706 if (scriptname == NULL)
21707 return FALSE;
21708 STRCPY(scriptname, "autoload/");
21709 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021710 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021711 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021712 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021713 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021714 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021715}
21716
Bram Moolenaar071d4272004-06-13 20:20:40 +000021717#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21718
21719/*
21720 * Function given to ExpandGeneric() to obtain the list of user defined
21721 * function names.
21722 */
21723 char_u *
21724get_user_func_name(xp, idx)
21725 expand_T *xp;
21726 int idx;
21727{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021728 static long_u done;
21729 static hashitem_T *hi;
21730 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021731
21732 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021733 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021734 done = 0;
21735 hi = func_hashtab.ht_array;
21736 }
21737 if (done < func_hashtab.ht_used)
21738 {
21739 if (done++ > 0)
21740 ++hi;
21741 while (HASHITEM_EMPTY(hi))
21742 ++hi;
21743 fp = HI2UF(hi);
21744
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010021745 if (fp->uf_flags & FC_DICT)
21746 return NULL; /* don't show dict functions */
21747
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021748 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21749 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021750
21751 cat_func_name(IObuff, fp);
21752 if (xp->xp_context != EXPAND_USER_FUNC)
21753 {
21754 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021755 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756 STRCAT(IObuff, ")");
21757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021758 return IObuff;
21759 }
21760 return NULL;
21761}
21762
21763#endif /* FEAT_CMDL_COMPL */
21764
21765/*
21766 * Copy the function name of "fp" to buffer "buf".
21767 * "buf" must be able to hold the function name plus three bytes.
21768 * Takes care of script-local function names.
21769 */
21770 static void
21771cat_func_name(buf, fp)
21772 char_u *buf;
21773 ufunc_T *fp;
21774{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021775 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776 {
21777 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021778 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021779 }
21780 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021781 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782}
21783
21784/*
21785 * ":delfunction {name}"
21786 */
21787 void
21788ex_delfunction(eap)
21789 exarg_T *eap;
21790{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021791 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021792 char_u *p;
21793 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021794 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795
21796 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021797 name = trans_function_name(&p, eap->skip, 0, &fudi);
21798 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021799 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021800 {
21801 if (fudi.fd_dict != NULL && !eap->skip)
21802 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021805 if (!ends_excmd(*skipwhite(p)))
21806 {
21807 vim_free(name);
21808 EMSG(_(e_trailing));
21809 return;
21810 }
21811 eap->nextcmd = check_nextcmd(p);
21812 if (eap->nextcmd != NULL)
21813 *p = NUL;
21814
21815 if (!eap->skip)
21816 fp = find_func(name);
21817 vim_free(name);
21818
21819 if (!eap->skip)
21820 {
21821 if (fp == NULL)
21822 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021823 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824 return;
21825 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021826 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827 {
21828 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21829 return;
21830 }
21831
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021832 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021833 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021834 /* Delete the dict item that refers to the function, it will
21835 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021836 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021837 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021838 else
21839 func_free(fp);
21840 }
21841}
21842
21843/*
21844 * Free a function and remove it from the list of functions.
21845 */
21846 static void
21847func_free(fp)
21848 ufunc_T *fp;
21849{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021850 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021851
21852 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021853 ga_clear_strings(&(fp->uf_args));
21854 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021855#ifdef FEAT_PROFILE
21856 vim_free(fp->uf_tml_count);
21857 vim_free(fp->uf_tml_total);
21858 vim_free(fp->uf_tml_self);
21859#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021860
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021861 /* remove the function from the function hashtable */
21862 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21863 if (HASHITEM_EMPTY(hi))
21864 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021865 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021866 hash_remove(&func_hashtab, hi);
21867
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021868 vim_free(fp);
21869}
21870
21871/*
21872 * Unreference a Function: decrement the reference count and free it when it
21873 * becomes zero. Only for numbered functions.
21874 */
21875 static void
21876func_unref(name)
21877 char_u *name;
21878{
21879 ufunc_T *fp;
21880
21881 if (name != NULL && isdigit(*name))
21882 {
21883 fp = find_func(name);
21884 if (fp == NULL)
21885 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021886 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021887 {
21888 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021889 * when "uf_calls" becomes zero. */
21890 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021891 func_free(fp);
21892 }
21893 }
21894}
21895
21896/*
21897 * Count a reference to a Function.
21898 */
21899 static void
21900func_ref(name)
21901 char_u *name;
21902{
21903 ufunc_T *fp;
21904
21905 if (name != NULL && isdigit(*name))
21906 {
21907 fp = find_func(name);
21908 if (fp == NULL)
21909 EMSG2(_(e_intern2), "func_ref()");
21910 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021911 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021912 }
21913}
21914
21915/*
21916 * Call a user function.
21917 */
21918 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021919call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920 ufunc_T *fp; /* pointer to function */
21921 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021922 typval_T *argvars; /* arguments */
21923 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924 linenr_T firstline; /* first line of range */
21925 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021926 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021927{
Bram Moolenaar33570922005-01-25 22:26:29 +000021928 char_u *save_sourcing_name;
21929 linenr_T save_sourcing_lnum;
21930 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021931 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021932 int save_did_emsg;
21933 static int depth = 0;
21934 dictitem_T *v;
21935 int fixvar_idx = 0; /* index in fixvar[] */
21936 int i;
21937 int ai;
21938 char_u numbuf[NUMBUFLEN];
21939 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021940#ifdef FEAT_PROFILE
21941 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021942 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021944
21945 /* If depth of calling is getting too high, don't execute the function */
21946 if (depth >= p_mfd)
21947 {
21948 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021949 rettv->v_type = VAR_NUMBER;
21950 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951 return;
21952 }
21953 ++depth;
21954
21955 line_breakcheck(); /* check for CTRL-C hit */
21956
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021957 fc = (funccall_T *)alloc(sizeof(funccall_T));
21958 fc->caller = current_funccal;
21959 current_funccal = fc;
21960 fc->func = fp;
21961 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021962 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021963 fc->linenr = 0;
21964 fc->returned = FALSE;
21965 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021966 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021967 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21968 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021969
Bram Moolenaar33570922005-01-25 22:26:29 +000021970 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021971 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021972 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21973 * each argument variable and saves a lot of time.
21974 */
21975 /*
21976 * Init l: variables.
21977 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021978 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021979 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021980 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021981 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21982 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021983 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021984 name = v->di_key;
21985 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021986 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021987 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021988 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021989 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021990 v->di_tv.vval.v_dict = selfdict;
21991 ++selfdict->dv_refcount;
21992 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021993
Bram Moolenaar33570922005-01-25 22:26:29 +000021994 /*
21995 * Init a: variables.
21996 * Set a:0 to "argcount".
21997 * Set a:000 to a list with room for the "..." arguments.
21998 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021999 init_var_dict(&fc->l_avars, &fc->l_avars_var);
22000 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022001 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022002 /* Use "name" to avoid a warning from some compiler that checks the
22003 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022004 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022005 name = v->di_key;
22006 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022007 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022008 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022009 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022010 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022011 v->di_tv.vval.v_list = &fc->l_varlist;
22012 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22013 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22014 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022015
22016 /*
22017 * Set a:firstline to "firstline" and a:lastline to "lastline".
22018 * Set a:name to named arguments.
22019 * Set a:N to the "..." arguments.
22020 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022021 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022022 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022023 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022024 (varnumber_T)lastline);
22025 for (i = 0; i < argcount; ++i)
22026 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022027 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022028 if (ai < 0)
22029 /* named argument a:name */
22030 name = FUNCARG(fp, i);
22031 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022032 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022033 /* "..." argument a:1, a:2, etc. */
22034 sprintf((char *)numbuf, "%d", ai + 1);
22035 name = numbuf;
22036 }
22037 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22038 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022039 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022040 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22041 }
22042 else
22043 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022044 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22045 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022046 if (v == NULL)
22047 break;
22048 v->di_flags = DI_FLAGS_RO;
22049 }
22050 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022051 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022052
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022053 /* Note: the values are copied directly to avoid alloc/free.
22054 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022055 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022056 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022057
22058 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22059 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022060 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22061 fc->l_listitems[ai].li_tv = argvars[i];
22062 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022063 }
22064 }
22065
Bram Moolenaar071d4272004-06-13 20:20:40 +000022066 /* Don't redraw while executing the function. */
22067 ++RedrawingDisabled;
22068 save_sourcing_name = sourcing_name;
22069 save_sourcing_lnum = sourcing_lnum;
22070 sourcing_lnum = 1;
22071 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022072 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022073 if (sourcing_name != NULL)
22074 {
22075 if (save_sourcing_name != NULL
22076 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22077 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22078 else
22079 STRCPY(sourcing_name, "function ");
22080 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22081
22082 if (p_verbose >= 12)
22083 {
22084 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022085 verbose_enter_scroll();
22086
Bram Moolenaar555b2802005-05-19 21:08:39 +000022087 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088 if (p_verbose >= 14)
22089 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022090 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022091 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022092 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022093 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094
22095 msg_puts((char_u *)"(");
22096 for (i = 0; i < argcount; ++i)
22097 {
22098 if (i > 0)
22099 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022100 if (argvars[i].v_type == VAR_NUMBER)
22101 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022102 else
22103 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022104 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22105 if (s != NULL)
22106 {
22107 trunc_string(s, buf, MSG_BUF_CLEN);
22108 msg_puts(buf);
22109 vim_free(tofree);
22110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111 }
22112 }
22113 msg_puts((char_u *)")");
22114 }
22115 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022116
22117 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118 --no_wait_return;
22119 }
22120 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022121#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022122 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022123 {
22124 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22125 func_do_profile(fp);
22126 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022127 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022128 {
22129 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022130 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022131 profile_zero(&fp->uf_tm_children);
22132 }
22133 script_prof_save(&wait_start);
22134 }
22135#endif
22136
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022138 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139 save_did_emsg = did_emsg;
22140 did_emsg = FALSE;
22141
22142 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022143 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22145
22146 --RedrawingDisabled;
22147
22148 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022149 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022151 clear_tv(rettv);
22152 rettv->v_type = VAR_NUMBER;
22153 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154 }
22155
Bram Moolenaar05159a02005-02-26 23:04:13 +000022156#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022157 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022158 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022159 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022160 profile_end(&call_start);
22161 profile_sub_wait(&wait_start, &call_start);
22162 profile_add(&fp->uf_tm_total, &call_start);
22163 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022164 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022165 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022166 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22167 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022168 }
22169 }
22170#endif
22171
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172 /* when being verbose, mention the return value */
22173 if (p_verbose >= 12)
22174 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022176 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022177
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022179 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022180 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022181 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022182 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022183 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022185 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022186 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022187 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022188 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022189
Bram Moolenaar555b2802005-05-19 21:08:39 +000022190 /* The value may be very long. Skip the middle part, so that we
22191 * have some idea how it starts and ends. smsg() would always
22192 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022193 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022194 if (s != NULL)
22195 {
22196 trunc_string(s, buf, MSG_BUF_CLEN);
22197 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
22198 vim_free(tofree);
22199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200 }
22201 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022202
22203 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022204 --no_wait_return;
22205 }
22206
22207 vim_free(sourcing_name);
22208 sourcing_name = save_sourcing_name;
22209 sourcing_lnum = save_sourcing_lnum;
22210 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022211#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022212 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022213 script_prof_restore(&wait_start);
22214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022215
22216 if (p_verbose >= 12 && sourcing_name != NULL)
22217 {
22218 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022219 verbose_enter_scroll();
22220
Bram Moolenaar555b2802005-05-19 21:08:39 +000022221 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022222 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022223
22224 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022225 --no_wait_return;
22226 }
22227
22228 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022229 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022230 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022231
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022232 /* If the a:000 list and the l: and a: dicts are not referenced we can
22233 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022234 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22235 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22236 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22237 {
22238 free_funccal(fc, FALSE);
22239 }
22240 else
22241 {
22242 hashitem_T *hi;
22243 listitem_T *li;
22244 int todo;
22245
22246 /* "fc" is still in use. This can happen when returning "a:000" or
22247 * assigning "l:" to a global variable.
22248 * Link "fc" in the list for garbage collection later. */
22249 fc->caller = previous_funccal;
22250 previous_funccal = fc;
22251
22252 /* Make a copy of the a: variables, since we didn't do that above. */
22253 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22254 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22255 {
22256 if (!HASHITEM_EMPTY(hi))
22257 {
22258 --todo;
22259 v = HI2DI(hi);
22260 copy_tv(&v->di_tv, &v->di_tv);
22261 }
22262 }
22263
22264 /* Make a copy of the a:000 items, since we didn't do that above. */
22265 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22266 copy_tv(&li->li_tv, &li->li_tv);
22267 }
22268}
22269
22270/*
22271 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022272 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022273 */
22274 static int
22275can_free_funccal(fc, copyID)
22276 funccall_T *fc;
22277 int copyID;
22278{
22279 return (fc->l_varlist.lv_copyID != copyID
22280 && fc->l_vars.dv_copyID != copyID
22281 && fc->l_avars.dv_copyID != copyID);
22282}
22283
22284/*
22285 * Free "fc" and what it contains.
22286 */
22287 static void
22288free_funccal(fc, free_val)
22289 funccall_T *fc;
22290 int free_val; /* a: vars were allocated */
22291{
22292 listitem_T *li;
22293
22294 /* The a: variables typevals may not have been allocated, only free the
22295 * allocated variables. */
22296 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22297
22298 /* free all l: variables */
22299 vars_clear(&fc->l_vars.dv_hashtab);
22300
22301 /* Free the a:000 variables if they were allocated. */
22302 if (free_val)
22303 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22304 clear_tv(&li->li_tv);
22305
22306 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307}
22308
22309/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022310 * Add a number variable "name" to dict "dp" with value "nr".
22311 */
22312 static void
22313add_nr_var(dp, v, name, nr)
22314 dict_T *dp;
22315 dictitem_T *v;
22316 char *name;
22317 varnumber_T nr;
22318{
22319 STRCPY(v->di_key, name);
22320 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22321 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22322 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022323 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022324 v->di_tv.vval.v_number = nr;
22325}
22326
22327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022328 * ":return [expr]"
22329 */
22330 void
22331ex_return(eap)
22332 exarg_T *eap;
22333{
22334 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022335 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022336 int returning = FALSE;
22337
22338 if (current_funccal == NULL)
22339 {
22340 EMSG(_("E133: :return not inside a function"));
22341 return;
22342 }
22343
22344 if (eap->skip)
22345 ++emsg_skip;
22346
22347 eap->nextcmd = NULL;
22348 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022349 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350 {
22351 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022352 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022353 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022354 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355 }
22356 /* It's safer to return also on error. */
22357 else if (!eap->skip)
22358 {
22359 /*
22360 * Return unless the expression evaluation has been cancelled due to an
22361 * aborting error, an interrupt, or an exception.
22362 */
22363 if (!aborting())
22364 returning = do_return(eap, FALSE, TRUE, NULL);
22365 }
22366
22367 /* When skipping or the return gets pending, advance to the next command
22368 * in this line (!returning). Otherwise, ignore the rest of the line.
22369 * Following lines will be ignored by get_func_line(). */
22370 if (returning)
22371 eap->nextcmd = NULL;
22372 else if (eap->nextcmd == NULL) /* no argument */
22373 eap->nextcmd = check_nextcmd(arg);
22374
22375 if (eap->skip)
22376 --emsg_skip;
22377}
22378
22379/*
22380 * Return from a function. Possibly makes the return pending. Also called
22381 * for a pending return at the ":endtry" or after returning from an extra
22382 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022383 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022384 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385 * FALSE when the return gets pending.
22386 */
22387 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022388do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022389 exarg_T *eap;
22390 int reanimate;
22391 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022392 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393{
22394 int idx;
22395 struct condstack *cstack = eap->cstack;
22396
22397 if (reanimate)
22398 /* Undo the return. */
22399 current_funccal->returned = FALSE;
22400
22401 /*
22402 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22403 * not in its finally clause (which then is to be executed next) is found.
22404 * In this case, make the ":return" pending for execution at the ":endtry".
22405 * Otherwise, return normally.
22406 */
22407 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22408 if (idx >= 0)
22409 {
22410 cstack->cs_pending[idx] = CSTP_RETURN;
22411
22412 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022413 /* A pending return again gets pending. "rettv" points to an
22414 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022415 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022416 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022417 else
22418 {
22419 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022420 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022421 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022422 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022424 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425 {
22426 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022427 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022428 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429 else
22430 EMSG(_(e_outofmem));
22431 }
22432 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022433 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022434
22435 if (reanimate)
22436 {
22437 /* The pending return value could be overwritten by a ":return"
22438 * without argument in a finally clause; reset the default
22439 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022440 current_funccal->rettv->v_type = VAR_NUMBER;
22441 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442 }
22443 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022444 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445 }
22446 else
22447 {
22448 current_funccal->returned = TRUE;
22449
22450 /* If the return is carried out now, store the return value. For
22451 * a return immediately after reanimation, the value is already
22452 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022453 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022455 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022456 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022458 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022459 }
22460 }
22461
22462 return idx < 0;
22463}
22464
22465/*
22466 * Free the variable with a pending return value.
22467 */
22468 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022469discard_pending_return(rettv)
22470 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022471{
Bram Moolenaar33570922005-01-25 22:26:29 +000022472 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473}
22474
22475/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022476 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022477 * is an allocated string. Used by report_pending() for verbose messages.
22478 */
22479 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022480get_return_cmd(rettv)
22481 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022482{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022483 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022484 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022485 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022486
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022487 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022488 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022489 if (s == NULL)
22490 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022491
22492 STRCPY(IObuff, ":return ");
22493 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22494 if (STRLEN(s) + 8 >= IOSIZE)
22495 STRCPY(IObuff + IOSIZE - 4, "...");
22496 vim_free(tofree);
22497 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498}
22499
22500/*
22501 * Get next function line.
22502 * Called by do_cmdline() to get the next line.
22503 * Returns allocated string, or NULL for end of function.
22504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022505 char_u *
22506get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022507 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022508 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022509 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510{
Bram Moolenaar33570922005-01-25 22:26:29 +000022511 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022512 ufunc_T *fp = fcp->func;
22513 char_u *retval;
22514 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022515
22516 /* If breakpoints have been added/deleted need to check for it. */
22517 if (fcp->dbg_tick != debug_tick)
22518 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022519 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022520 sourcing_lnum);
22521 fcp->dbg_tick = debug_tick;
22522 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022523#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022524 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022525 func_line_end(cookie);
22526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022527
Bram Moolenaar05159a02005-02-26 23:04:13 +000022528 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022529 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22530 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531 retval = NULL;
22532 else
22533 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022534 /* Skip NULL lines (continuation lines). */
22535 while (fcp->linenr < gap->ga_len
22536 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22537 ++fcp->linenr;
22538 if (fcp->linenr >= gap->ga_len)
22539 retval = NULL;
22540 else
22541 {
22542 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22543 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022544#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022545 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022546 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022547#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022549 }
22550
22551 /* Did we encounter a breakpoint? */
22552 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22553 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022554 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022555 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022556 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557 sourcing_lnum);
22558 fcp->dbg_tick = debug_tick;
22559 }
22560
22561 return retval;
22562}
22563
Bram Moolenaar05159a02005-02-26 23:04:13 +000022564#if defined(FEAT_PROFILE) || defined(PROTO)
22565/*
22566 * Called when starting to read a function line.
22567 * "sourcing_lnum" must be correct!
22568 * When skipping lines it may not actually be executed, but we won't find out
22569 * until later and we need to store the time now.
22570 */
22571 void
22572func_line_start(cookie)
22573 void *cookie;
22574{
22575 funccall_T *fcp = (funccall_T *)cookie;
22576 ufunc_T *fp = fcp->func;
22577
22578 if (fp->uf_profiling && sourcing_lnum >= 1
22579 && sourcing_lnum <= fp->uf_lines.ga_len)
22580 {
22581 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022582 /* Skip continuation lines. */
22583 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22584 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022585 fp->uf_tml_execed = FALSE;
22586 profile_start(&fp->uf_tml_start);
22587 profile_zero(&fp->uf_tml_children);
22588 profile_get_wait(&fp->uf_tml_wait);
22589 }
22590}
22591
22592/*
22593 * Called when actually executing a function line.
22594 */
22595 void
22596func_line_exec(cookie)
22597 void *cookie;
22598{
22599 funccall_T *fcp = (funccall_T *)cookie;
22600 ufunc_T *fp = fcp->func;
22601
22602 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22603 fp->uf_tml_execed = TRUE;
22604}
22605
22606/*
22607 * Called when done with a function line.
22608 */
22609 void
22610func_line_end(cookie)
22611 void *cookie;
22612{
22613 funccall_T *fcp = (funccall_T *)cookie;
22614 ufunc_T *fp = fcp->func;
22615
22616 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22617 {
22618 if (fp->uf_tml_execed)
22619 {
22620 ++fp->uf_tml_count[fp->uf_tml_idx];
22621 profile_end(&fp->uf_tml_start);
22622 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022623 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022624 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22625 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022626 }
22627 fp->uf_tml_idx = -1;
22628 }
22629}
22630#endif
22631
Bram Moolenaar071d4272004-06-13 20:20:40 +000022632/*
22633 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022634 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022635 */
22636 int
22637func_has_ended(cookie)
22638 void *cookie;
22639{
Bram Moolenaar33570922005-01-25 22:26:29 +000022640 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022641
22642 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22643 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022644 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645 || fcp->returned);
22646}
22647
22648/*
22649 * return TRUE if cookie indicates a function which "abort"s on errors.
22650 */
22651 int
22652func_has_abort(cookie)
22653 void *cookie;
22654{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022655 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656}
22657
22658#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22659typedef enum
22660{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022661 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22662 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22663 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664} var_flavour_T;
22665
22666static var_flavour_T var_flavour __ARGS((char_u *varname));
22667
22668 static var_flavour_T
22669var_flavour(varname)
22670 char_u *varname;
22671{
22672 char_u *p = varname;
22673
22674 if (ASCII_ISUPPER(*p))
22675 {
22676 while (*(++p))
22677 if (ASCII_ISLOWER(*p))
22678 return VAR_FLAVOUR_SESSION;
22679 return VAR_FLAVOUR_VIMINFO;
22680 }
22681 else
22682 return VAR_FLAVOUR_DEFAULT;
22683}
22684#endif
22685
22686#if defined(FEAT_VIMINFO) || defined(PROTO)
22687/*
22688 * Restore global vars that start with a capital from the viminfo file
22689 */
22690 int
22691read_viminfo_varlist(virp, writing)
22692 vir_T *virp;
22693 int writing;
22694{
22695 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022696 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022697 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022698
22699 if (!writing && (find_viminfo_parameter('!') != NULL))
22700 {
22701 tab = vim_strchr(virp->vir_line + 1, '\t');
22702 if (tab != NULL)
22703 {
22704 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022705 switch (*tab)
22706 {
22707 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022708#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022709 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022710#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022711 case 'D': type = VAR_DICT; break;
22712 case 'L': type = VAR_LIST; break;
22713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714
22715 tab = vim_strchr(tab, '\t');
22716 if (tab != NULL)
22717 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022718 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022719 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022720 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022722#ifdef FEAT_FLOAT
22723 else if (type == VAR_FLOAT)
22724 (void)string2float(tab + 1, &tv.vval.v_float);
22725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022727 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022728 if (type == VAR_DICT || type == VAR_LIST)
22729 {
22730 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
22731
22732 if (etv == NULL)
22733 /* Failed to parse back the dict or list, use it as a
22734 * string. */
22735 tv.v_type = VAR_STRING;
22736 else
22737 {
22738 vim_free(tv.vval.v_string);
22739 tv = *etv;
22740 }
22741 }
22742
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022743 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022744
22745 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022746 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022747 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
22748 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749 }
22750 }
22751 }
22752
22753 return viminfo_readline(virp);
22754}
22755
22756/*
22757 * Write global vars that start with a capital to the viminfo file
22758 */
22759 void
22760write_viminfo_varlist(fp)
22761 FILE *fp;
22762{
Bram Moolenaar33570922005-01-25 22:26:29 +000022763 hashitem_T *hi;
22764 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022765 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022766 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022767 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022768 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022769 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022770
22771 if (find_viminfo_parameter('!') == NULL)
22772 return;
22773
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022774 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022775
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022776 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022777 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022778 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022779 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022780 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022781 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022782 this_var = HI2DI(hi);
22783 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022784 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022785 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022786 {
22787 case VAR_STRING: s = "STR"; break;
22788 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022789#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022790 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022791#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022792 case VAR_DICT: s = "DIC"; break;
22793 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000022794 default: continue;
22795 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022796 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022797 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022798 if (p != NULL)
22799 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022800 vim_free(tofree);
22801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022802 }
22803 }
22804}
22805#endif
22806
22807#if defined(FEAT_SESSION) || defined(PROTO)
22808 int
22809store_session_globals(fd)
22810 FILE *fd;
22811{
Bram Moolenaar33570922005-01-25 22:26:29 +000022812 hashitem_T *hi;
22813 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022814 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022815 char_u *p, *t;
22816
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022817 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022818 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022819 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022820 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022821 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022822 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022823 this_var = HI2DI(hi);
22824 if ((this_var->di_tv.v_type == VAR_NUMBER
22825 || this_var->di_tv.v_type == VAR_STRING)
22826 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022827 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022828 /* Escape special characters with a backslash. Turn a LF and
22829 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022830 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022831 (char_u *)"\\\"\n\r");
22832 if (p == NULL) /* out of memory */
22833 break;
22834 for (t = p; *t != NUL; ++t)
22835 if (*t == '\n')
22836 *t = 'n';
22837 else if (*t == '\r')
22838 *t = 'r';
22839 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022840 this_var->di_key,
22841 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22842 : ' ',
22843 p,
22844 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22845 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022846 || put_eol(fd) == FAIL)
22847 {
22848 vim_free(p);
22849 return FAIL;
22850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851 vim_free(p);
22852 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022853#ifdef FEAT_FLOAT
22854 else if (this_var->di_tv.v_type == VAR_FLOAT
22855 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22856 {
22857 float_T f = this_var->di_tv.vval.v_float;
22858 int sign = ' ';
22859
22860 if (f < 0)
22861 {
22862 f = -f;
22863 sign = '-';
22864 }
22865 if ((fprintf(fd, "let %s = %c&%f",
22866 this_var->di_key, sign, f) < 0)
22867 || put_eol(fd) == FAIL)
22868 return FAIL;
22869 }
22870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871 }
22872 }
22873 return OK;
22874}
22875#endif
22876
Bram Moolenaar661b1822005-07-28 22:36:45 +000022877/*
22878 * Display script name where an item was last set.
22879 * Should only be invoked when 'verbose' is non-zero.
22880 */
22881 void
22882last_set_msg(scriptID)
22883 scid_T scriptID;
22884{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022885 char_u *p;
22886
Bram Moolenaar661b1822005-07-28 22:36:45 +000022887 if (scriptID != 0)
22888 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022889 p = home_replace_save(NULL, get_scriptname(scriptID));
22890 if (p != NULL)
22891 {
22892 verbose_enter();
22893 MSG_PUTS(_("\n\tLast set from "));
22894 MSG_PUTS(p);
22895 vim_free(p);
22896 verbose_leave();
22897 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022898 }
22899}
22900
Bram Moolenaard812df62008-11-09 12:46:09 +000022901/*
22902 * List v:oldfiles in a nice way.
22903 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022904 void
22905ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022906 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022907{
22908 list_T *l = vimvars[VV_OLDFILES].vv_list;
22909 listitem_T *li;
22910 int nr = 0;
22911
22912 if (l == NULL)
22913 msg((char_u *)_("No old files"));
22914 else
22915 {
22916 msg_start();
22917 msg_scroll = TRUE;
22918 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22919 {
22920 msg_outnum((long)++nr);
22921 MSG_PUTS(": ");
22922 msg_outtrans(get_tv_string(&li->li_tv));
22923 msg_putchar('\n');
22924 out_flush(); /* output one line at a time */
22925 ui_breakcheck();
22926 }
22927 /* Assume "got_int" was set to truncate the listing. */
22928 got_int = FALSE;
22929
22930#ifdef FEAT_BROWSE_CMD
22931 if (cmdmod.browse)
22932 {
22933 quit_more = FALSE;
22934 nr = prompt_for_number(FALSE);
22935 msg_starthere();
22936 if (nr > 0)
22937 {
22938 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22939 (long)nr);
22940
22941 if (p != NULL)
22942 {
22943 p = expand_env_save(p);
22944 eap->arg = p;
22945 eap->cmdidx = CMD_edit;
22946 cmdmod.browse = FALSE;
22947 do_exedit(eap, NULL);
22948 vim_free(p);
22949 }
22950 }
22951 }
22952#endif
22953 }
22954}
22955
Bram Moolenaar071d4272004-06-13 20:20:40 +000022956#endif /* FEAT_EVAL */
22957
Bram Moolenaar071d4272004-06-13 20:20:40 +000022958
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022959#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022960
22961#ifdef WIN3264
22962/*
22963 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22964 */
22965static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22966static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22967static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22968
22969/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022970 * Get the short path (8.3) for the filename in "fnamep".
22971 * Only works for a valid file name.
22972 * When the path gets longer "fnamep" is changed and the allocated buffer
22973 * is put in "bufp".
22974 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22975 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976 */
22977 static int
22978get_short_pathname(fnamep, bufp, fnamelen)
22979 char_u **fnamep;
22980 char_u **bufp;
22981 int *fnamelen;
22982{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022983 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984 char_u *newbuf;
22985
22986 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 l = GetShortPathName(*fnamep, *fnamep, len);
22988 if (l > len - 1)
22989 {
22990 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022991 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022992 newbuf = vim_strnsave(*fnamep, l);
22993 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022994 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022995
22996 vim_free(*bufp);
22997 *fnamep = *bufp = newbuf;
22998
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022999 /* Really should always succeed, as the buffer is big enough. */
23000 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023001 }
23002
23003 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023004 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005}
23006
23007/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023008 * Get the short path (8.3) for the filename in "fname". The converted
23009 * path is returned in "bufp".
23010 *
23011 * Some of the directories specified in "fname" may not exist. This function
23012 * will shorten the existing directories at the beginning of the path and then
23013 * append the remaining non-existing path.
23014 *
23015 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023016 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023017 * bufp - Pointer to an allocated buffer for the filename.
23018 * fnamelen - Length of the filename pointed to by fname
23019 *
23020 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023021 */
23022 static int
23023shortpath_for_invalid_fname(fname, bufp, fnamelen)
23024 char_u **fname;
23025 char_u **bufp;
23026 int *fnamelen;
23027{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023028 char_u *short_fname, *save_fname, *pbuf_unused;
23029 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023031 int old_len, len;
23032 int new_len, sfx_len;
23033 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034
23035 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023036 old_len = *fnamelen;
23037 save_fname = vim_strnsave(*fname, old_len);
23038 pbuf_unused = NULL;
23039 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023040
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023041 endp = save_fname + old_len - 1; /* Find the end of the copy */
23042 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023043
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023044 /*
23045 * Try shortening the supplied path till it succeeds by removing one
23046 * directory at a time from the tail of the path.
23047 */
23048 len = 0;
23049 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023051 /* go back one path-separator */
23052 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23053 --endp;
23054 if (endp <= save_fname)
23055 break; /* processed the complete path */
23056
23057 /*
23058 * Replace the path separator with a NUL and try to shorten the
23059 * resulting path.
23060 */
23061 ch = *endp;
23062 *endp = 0;
23063 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023064 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023065 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23066 {
23067 retval = FAIL;
23068 goto theend;
23069 }
23070 *endp = ch; /* preserve the string */
23071
23072 if (len > 0)
23073 break; /* successfully shortened the path */
23074
23075 /* failed to shorten the path. Skip the path separator */
23076 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023077 }
23078
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023079 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023080 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023081 /*
23082 * Succeeded in shortening the path. Now concatenate the shortened
23083 * path with the remaining path at the tail.
23084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023085
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023086 /* Compute the length of the new path. */
23087 sfx_len = (int)(save_endp - endp) + 1;
23088 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023090 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023091 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023092 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023093 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023094 /* There is not enough space in the currently allocated string,
23095 * copy it to a buffer big enough. */
23096 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023097 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023098 {
23099 retval = FAIL;
23100 goto theend;
23101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023102 }
23103 else
23104 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023105 /* Transfer short_fname to the main buffer (it's big enough),
23106 * unless get_short_pathname() did its work in-place. */
23107 *fname = *bufp = save_fname;
23108 if (short_fname != save_fname)
23109 vim_strncpy(save_fname, short_fname, len);
23110 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023111 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023112
23113 /* concat the not-shortened part of the path */
23114 vim_strncpy(*fname + len, endp, sfx_len);
23115 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023116 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023117
23118theend:
23119 vim_free(pbuf_unused);
23120 vim_free(save_fname);
23121
23122 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023123}
23124
23125/*
23126 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023127 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128 */
23129 static int
23130shortpath_for_partial(fnamep, bufp, fnamelen)
23131 char_u **fnamep;
23132 char_u **bufp;
23133 int *fnamelen;
23134{
23135 int sepcount, len, tflen;
23136 char_u *p;
23137 char_u *pbuf, *tfname;
23138 int hasTilde;
23139
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023140 /* Count up the path separators from the RHS.. so we know which part
23141 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023142 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023143 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023144 if (vim_ispathsep(*p))
23145 ++sepcount;
23146
23147 /* Need full path first (use expand_env() to remove a "~/") */
23148 hasTilde = (**fnamep == '~');
23149 if (hasTilde)
23150 pbuf = tfname = expand_env_save(*fnamep);
23151 else
23152 pbuf = tfname = FullName_save(*fnamep, FALSE);
23153
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023154 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023156 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23157 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023158
23159 if (len == 0)
23160 {
23161 /* Don't have a valid filename, so shorten the rest of the
23162 * path if we can. This CAN give us invalid 8.3 filenames, but
23163 * there's not a lot of point in guessing what it might be.
23164 */
23165 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023166 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23167 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168 }
23169
23170 /* Count the paths backward to find the beginning of the desired string. */
23171 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023172 {
23173#ifdef FEAT_MBYTE
23174 if (has_mbyte)
23175 p -= mb_head_off(tfname, p);
23176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023177 if (vim_ispathsep(*p))
23178 {
23179 if (sepcount == 0 || (hasTilde && sepcount == 1))
23180 break;
23181 else
23182 sepcount --;
23183 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185 if (hasTilde)
23186 {
23187 --p;
23188 if (p >= tfname)
23189 *p = '~';
23190 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023191 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023192 }
23193 else
23194 ++p;
23195
23196 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23197 vim_free(*bufp);
23198 *fnamelen = (int)STRLEN(p);
23199 *bufp = pbuf;
23200 *fnamep = p;
23201
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023202 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023203}
23204#endif /* WIN3264 */
23205
23206/*
23207 * Adjust a filename, according to a string of modifiers.
23208 * *fnamep must be NUL terminated when called. When returning, the length is
23209 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023210 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023211 * When there is an error, *fnamep is set to NULL.
23212 */
23213 int
23214modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23215 char_u *src; /* string with modifiers */
23216 int *usedlen; /* characters after src that are used */
23217 char_u **fnamep; /* file name so far */
23218 char_u **bufp; /* buffer for allocated file name or NULL */
23219 int *fnamelen; /* length of fnamep */
23220{
23221 int valid = 0;
23222 char_u *tail;
23223 char_u *s, *p, *pbuf;
23224 char_u dirname[MAXPATHL];
23225 int c;
23226 int has_fullname = 0;
23227#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023228 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023229 int has_shortname = 0;
23230#endif
23231
23232repeat:
23233 /* ":p" - full path/file_name */
23234 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23235 {
23236 has_fullname = 1;
23237
23238 valid |= VALID_PATH;
23239 *usedlen += 2;
23240
23241 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23242 if ((*fnamep)[0] == '~'
23243#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23244 && ((*fnamep)[1] == '/'
23245# ifdef BACKSLASH_IN_FILENAME
23246 || (*fnamep)[1] == '\\'
23247# endif
23248 || (*fnamep)[1] == NUL)
23249
23250#endif
23251 )
23252 {
23253 *fnamep = expand_env_save(*fnamep);
23254 vim_free(*bufp); /* free any allocated file name */
23255 *bufp = *fnamep;
23256 if (*fnamep == NULL)
23257 return -1;
23258 }
23259
23260 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023261 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023262 {
23263 if (vim_ispathsep(*p)
23264 && p[1] == '.'
23265 && (p[2] == NUL
23266 || vim_ispathsep(p[2])
23267 || (p[2] == '.'
23268 && (p[3] == NUL || vim_ispathsep(p[3])))))
23269 break;
23270 }
23271
23272 /* FullName_save() is slow, don't use it when not needed. */
23273 if (*p != NUL || !vim_isAbsName(*fnamep))
23274 {
23275 *fnamep = FullName_save(*fnamep, *p != NUL);
23276 vim_free(*bufp); /* free any allocated file name */
23277 *bufp = *fnamep;
23278 if (*fnamep == NULL)
23279 return -1;
23280 }
23281
23282 /* Append a path separator to a directory. */
23283 if (mch_isdir(*fnamep))
23284 {
23285 /* Make room for one or two extra characters. */
23286 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23287 vim_free(*bufp); /* free any allocated file name */
23288 *bufp = *fnamep;
23289 if (*fnamep == NULL)
23290 return -1;
23291 add_pathsep(*fnamep);
23292 }
23293 }
23294
23295 /* ":." - path relative to the current directory */
23296 /* ":~" - path relative to the home directory */
23297 /* ":8" - shortname path - postponed till after */
23298 while (src[*usedlen] == ':'
23299 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23300 {
23301 *usedlen += 2;
23302 if (c == '8')
23303 {
23304#ifdef WIN3264
23305 has_shortname = 1; /* Postpone this. */
23306#endif
23307 continue;
23308 }
23309 pbuf = NULL;
23310 /* Need full path first (use expand_env() to remove a "~/") */
23311 if (!has_fullname)
23312 {
23313 if (c == '.' && **fnamep == '~')
23314 p = pbuf = expand_env_save(*fnamep);
23315 else
23316 p = pbuf = FullName_save(*fnamep, FALSE);
23317 }
23318 else
23319 p = *fnamep;
23320
23321 has_fullname = 0;
23322
23323 if (p != NULL)
23324 {
23325 if (c == '.')
23326 {
23327 mch_dirname(dirname, MAXPATHL);
23328 s = shorten_fname(p, dirname);
23329 if (s != NULL)
23330 {
23331 *fnamep = s;
23332 if (pbuf != NULL)
23333 {
23334 vim_free(*bufp); /* free any allocated file name */
23335 *bufp = pbuf;
23336 pbuf = NULL;
23337 }
23338 }
23339 }
23340 else
23341 {
23342 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23343 /* Only replace it when it starts with '~' */
23344 if (*dirname == '~')
23345 {
23346 s = vim_strsave(dirname);
23347 if (s != NULL)
23348 {
23349 *fnamep = s;
23350 vim_free(*bufp);
23351 *bufp = s;
23352 }
23353 }
23354 }
23355 vim_free(pbuf);
23356 }
23357 }
23358
23359 tail = gettail(*fnamep);
23360 *fnamelen = (int)STRLEN(*fnamep);
23361
23362 /* ":h" - head, remove "/file_name", can be repeated */
23363 /* Don't remove the first "/" or "c:\" */
23364 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23365 {
23366 valid |= VALID_HEAD;
23367 *usedlen += 2;
23368 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023369 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023370 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023371 *fnamelen = (int)(tail - *fnamep);
23372#ifdef VMS
23373 if (*fnamelen > 0)
23374 *fnamelen += 1; /* the path separator is part of the path */
23375#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023376 if (*fnamelen == 0)
23377 {
23378 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23379 p = vim_strsave((char_u *)".");
23380 if (p == NULL)
23381 return -1;
23382 vim_free(*bufp);
23383 *bufp = *fnamep = tail = p;
23384 *fnamelen = 1;
23385 }
23386 else
23387 {
23388 while (tail > s && !after_pathsep(s, tail))
23389 mb_ptr_back(*fnamep, tail);
23390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023391 }
23392
23393 /* ":8" - shortname */
23394 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23395 {
23396 *usedlen += 2;
23397#ifdef WIN3264
23398 has_shortname = 1;
23399#endif
23400 }
23401
23402#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023403 /*
23404 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023405 */
23406 if (has_shortname)
23407 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023408 /* Copy the string if it is shortened by :h and when it wasn't copied
23409 * yet, because we are going to change it in place. Avoids changing
23410 * the buffer name for "%:8". */
23411 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023412 {
23413 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023414 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023415 return -1;
23416 vim_free(*bufp);
23417 *bufp = *fnamep = p;
23418 }
23419
23420 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023421 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023422 if (!has_fullname && !vim_isAbsName(*fnamep))
23423 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023424 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023425 return -1;
23426 }
23427 else
23428 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023429 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023430
Bram Moolenaardc935552011-08-17 15:23:23 +020023431 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023432 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023433 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023434 return -1;
23435
23436 if (l == 0)
23437 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023438 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023439 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023440 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023441 return -1;
23442 }
23443 *fnamelen = l;
23444 }
23445 }
23446#endif /* WIN3264 */
23447
23448 /* ":t" - tail, just the basename */
23449 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23450 {
23451 *usedlen += 2;
23452 *fnamelen -= (int)(tail - *fnamep);
23453 *fnamep = tail;
23454 }
23455
23456 /* ":e" - extension, can be repeated */
23457 /* ":r" - root, without extension, can be repeated */
23458 while (src[*usedlen] == ':'
23459 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23460 {
23461 /* find a '.' in the tail:
23462 * - for second :e: before the current fname
23463 * - otherwise: The last '.'
23464 */
23465 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23466 s = *fnamep - 2;
23467 else
23468 s = *fnamep + *fnamelen - 1;
23469 for ( ; s > tail; --s)
23470 if (s[0] == '.')
23471 break;
23472 if (src[*usedlen + 1] == 'e') /* :e */
23473 {
23474 if (s > tail)
23475 {
23476 *fnamelen += (int)(*fnamep - (s + 1));
23477 *fnamep = s + 1;
23478#ifdef VMS
23479 /* cut version from the extension */
23480 s = *fnamep + *fnamelen - 1;
23481 for ( ; s > *fnamep; --s)
23482 if (s[0] == ';')
23483 break;
23484 if (s > *fnamep)
23485 *fnamelen = s - *fnamep;
23486#endif
23487 }
23488 else if (*fnamep <= tail)
23489 *fnamelen = 0;
23490 }
23491 else /* :r */
23492 {
23493 if (s > tail) /* remove one extension */
23494 *fnamelen = (int)(s - *fnamep);
23495 }
23496 *usedlen += 2;
23497 }
23498
23499 /* ":s?pat?foo?" - substitute */
23500 /* ":gs?pat?foo?" - global substitute */
23501 if (src[*usedlen] == ':'
23502 && (src[*usedlen + 1] == 's'
23503 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23504 {
23505 char_u *str;
23506 char_u *pat;
23507 char_u *sub;
23508 int sep;
23509 char_u *flags;
23510 int didit = FALSE;
23511
23512 flags = (char_u *)"";
23513 s = src + *usedlen + 2;
23514 if (src[*usedlen + 1] == 'g')
23515 {
23516 flags = (char_u *)"g";
23517 ++s;
23518 }
23519
23520 sep = *s++;
23521 if (sep)
23522 {
23523 /* find end of pattern */
23524 p = vim_strchr(s, sep);
23525 if (p != NULL)
23526 {
23527 pat = vim_strnsave(s, (int)(p - s));
23528 if (pat != NULL)
23529 {
23530 s = p + 1;
23531 /* find end of substitution */
23532 p = vim_strchr(s, sep);
23533 if (p != NULL)
23534 {
23535 sub = vim_strnsave(s, (int)(p - s));
23536 str = vim_strnsave(*fnamep, *fnamelen);
23537 if (sub != NULL && str != NULL)
23538 {
23539 *usedlen = (int)(p + 1 - src);
23540 s = do_string_sub(str, pat, sub, flags);
23541 if (s != NULL)
23542 {
23543 *fnamep = s;
23544 *fnamelen = (int)STRLEN(s);
23545 vim_free(*bufp);
23546 *bufp = s;
23547 didit = TRUE;
23548 }
23549 }
23550 vim_free(sub);
23551 vim_free(str);
23552 }
23553 vim_free(pat);
23554 }
23555 }
23556 /* after using ":s", repeat all the modifiers */
23557 if (didit)
23558 goto repeat;
23559 }
23560 }
23561
23562 return valid;
23563}
23564
23565/*
23566 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23567 * "flags" can be "g" to do a global substitute.
23568 * Returns an allocated string, NULL for error.
23569 */
23570 char_u *
23571do_string_sub(str, pat, sub, flags)
23572 char_u *str;
23573 char_u *pat;
23574 char_u *sub;
23575 char_u *flags;
23576{
23577 int sublen;
23578 regmatch_T regmatch;
23579 int i;
23580 int do_all;
23581 char_u *tail;
23582 garray_T ga;
23583 char_u *ret;
23584 char_u *save_cpo;
23585
23586 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23587 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023588 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023589
23590 ga_init2(&ga, 1, 200);
23591
23592 do_all = (flags[0] == 'g');
23593
23594 regmatch.rm_ic = p_ic;
23595 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23596 if (regmatch.regprog != NULL)
23597 {
23598 tail = str;
23599 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23600 {
23601 /*
23602 * Get some space for a temporary buffer to do the substitution
23603 * into. It will contain:
23604 * - The text up to where the match is.
23605 * - The substituted text.
23606 * - The text after the match.
23607 */
23608 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23609 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23610 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23611 {
23612 ga_clear(&ga);
23613 break;
23614 }
23615
23616 /* copy the text up to where the match is */
23617 i = (int)(regmatch.startp[0] - tail);
23618 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23619 /* add the substituted text */
23620 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23621 + ga.ga_len + i, TRUE, TRUE, FALSE);
23622 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023623 /* avoid getting stuck on a match with an empty string */
23624 if (tail == regmatch.endp[0])
23625 {
23626 if (*tail == NUL)
23627 break;
23628 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23629 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023630 }
23631 else
23632 {
23633 tail = regmatch.endp[0];
23634 if (*tail == NUL)
23635 break;
23636 }
23637 if (!do_all)
23638 break;
23639 }
23640
23641 if (ga.ga_data != NULL)
23642 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23643
23644 vim_free(regmatch.regprog);
23645 }
23646
23647 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23648 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023649 if (p_cpo == empty_option)
23650 p_cpo = save_cpo;
23651 else
23652 /* Darn, evaluating {sub} expression changed the value. */
23653 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654
23655 return ret;
23656}
23657
23658#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */