blob: 8a021941e96515cc008e7758c050c4c3071f0753 [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 Moolenaarf193fff2006-04-27 00:02:13 +0000355 {VV_NAME("char", VAR_STRING), VV_RO},
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));
383#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
384static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
385#endif
386static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
387static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
388static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000389static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
390static void list_glob_vars __ARGS((int *first));
391static void list_buf_vars __ARGS((int *first));
392static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000393#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000394static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000395#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000396static void list_vim_vars __ARGS((int *first));
397static void list_script_vars __ARGS((int *first));
398static void list_func_vars __ARGS((int *first));
399static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000400static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
401static int check_changedtick __ARGS((char_u *arg));
402static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
403static void clear_lval __ARGS((lval_T *lp));
404static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
405static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
406static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
407static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
408static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
409static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
410static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
411static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
412static void item_lock __ARGS((typval_T *tv, int deep, int lock));
413static int tv_islocked __ARGS((typval_T *tv));
414
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
416static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000421static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
422static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000423
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000424static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
427static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
428static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000429static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static listitem_T *listitem_alloc __ARGS((void));
431static void listitem_free __ARGS((listitem_T *item));
432static void listitem_remove __ARGS((list_T *l, listitem_T *item));
433static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100434static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
435static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
436static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000438static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000441static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
443static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
444static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000445static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000446static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000447static char_u *list2string __ARGS((typval_T *tv, int copyID));
448static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000449static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000450static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
451static void set_ref_in_list __ARGS((list_T *l, int copyID));
452static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200453static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000454static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000455static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
457static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000458static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000460static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000461static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000462static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
463static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000464static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#ifdef FEAT_FLOAT
466static int string2float __ARGS((char_u *text, float_T *value));
467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
469static int find_internal_func __ARGS((char_u *name));
470static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
471static 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 +0200472static 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 +0000473static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000474static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000475
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#ifdef FEAT_FLOAT
477static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200478static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200486static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000487static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200488static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000489#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000490static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000501#ifdef FEAT_FLOAT
502static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
503#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000504static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000507static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000508static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000509#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000510static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000511static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
513#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000514static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000516#ifdef FEAT_FLOAT
517static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200518static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000519#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
523static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200534#ifdef FEAT_FLOAT
535static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
536#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000539static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000545#ifdef FEAT_FLOAT
546static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000549#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000550static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000559static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000561static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000567static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000575static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000576static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000577static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000578static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200581static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000582static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000590static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000604static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000610static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000622#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200623static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000624static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
625#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000630static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000631static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000632static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000634static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000638#ifdef vim_mkdir
639static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100642#ifdef FEAT_MZSCHEME
643static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000647static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000648#ifdef FEAT_FLOAT
649static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000652static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000653static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000655static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000656static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
669static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
670#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000672static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000674static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000681static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000682static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000683static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000684static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200686static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000687static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000689static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#ifdef FEAT_FLOAT
692static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200693static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000696static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000697static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000700#ifdef FEAT_FLOAT
701static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
703#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000704static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200705static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706#ifdef HAVE_STRFTIME
707static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
708#endif
709static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200715static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200716static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000722static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200723static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000725static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000726static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000727static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000728static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000729static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000731static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200732#ifdef FEAT_FLOAT
733static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
735#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000739#ifdef FEAT_FLOAT
740static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
741#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200743static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200744static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000754static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000756static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000757static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000759static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000760static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000761static int get_env_len __ARGS((char_u **arg));
762static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000763static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000764static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
765#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
766#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
767 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000768static 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 +0000769static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000770static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
772static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static typval_T *alloc_tv __ARGS((void));
774static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static void init_tv __ARGS((typval_T *varp));
776static long get_tv_number __ARGS((typval_T *varp));
777static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000778static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000779static char_u *get_tv_string __ARGS((typval_T *varp));
780static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000781static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000782static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000783static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
785static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
786static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000787static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
788static 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 +0000789static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
790static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000791static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200792static int var_check_func_name __ARGS((char_u *name, int new_var));
793static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000794static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000795static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000796static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
797static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
798static int eval_fname_script __ARGS((char_u *p));
799static int eval_fname_sid __ARGS((char_u *p));
800static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000801static ufunc_T *find_func __ARGS((char_u *name));
802static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000803static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000804#ifdef FEAT_PROFILE
805static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000806static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
807static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
808static int
809# ifdef __BORLANDC__
810 _RTLENTRYF
811# endif
812 prof_total_cmp __ARGS((const void *s1, const void *s2));
813static int
814# ifdef __BORLANDC__
815 _RTLENTRYF
816# endif
817 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000819static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000820static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000821static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822static void func_free __ARGS((ufunc_T *fp));
823static void func_unref __ARGS((char_u *name));
824static void func_ref __ARGS((char_u *name));
825static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000826static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
827static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000828static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000829static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
830static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000831static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000832static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000833static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200835
836#ifdef EBCDIC
837static int compare_func_name __ARGS((const void *s1, const void *s2));
838static void sortFunctions __ARGS(());
839#endif
840
841
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000842/* Character used as separated in autoload function/variable names. */
843#define AUTOLOAD_CHAR '#'
844
Bram Moolenaar33570922005-01-25 22:26:29 +0000845/*
846 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000847 */
848 void
849eval_init()
850{
Bram Moolenaar33570922005-01-25 22:26:29 +0000851 int i;
852 struct vimvar *p;
853
854 init_var_dict(&globvardict, &globvars_var);
855 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200856 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000857 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000858 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000859
860 for (i = 0; i < VV_LEN; ++i)
861 {
862 p = &vimvars[i];
863 STRCPY(p->vv_di.di_key, p->vv_name);
864 if (p->vv_flags & VV_RO)
865 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
866 else if (p->vv_flags & VV_RO_SBX)
867 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
868 else
869 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000870
871 /* add to v: scope dict, unless the value is not always available */
872 if (p->vv_type != VAR_UNKNOWN)
873 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000874 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000875 /* add to compat scope dict */
876 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000877 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000878 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200879
880#ifdef EBCDIC
881 /*
882 * Sort the function table, to enable binary sort.
883 */
884 sortFunctions();
885#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000886}
887
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000888#if defined(EXITFREE) || defined(PROTO)
889 void
890eval_clear()
891{
892 int i;
893 struct vimvar *p;
894
895 for (i = 0; i < VV_LEN; ++i)
896 {
897 p = &vimvars[i];
898 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000899 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000900 vim_free(p->vv_str);
901 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000902 }
903 else if (p->vv_di.di_tv.v_type == VAR_LIST)
904 {
905 list_unref(p->vv_list);
906 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908 }
909 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000910 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911 hash_clear(&compat_hashtab);
912
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000914
915 /* global variables */
916 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000917
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000918 /* autoloaded script names */
919 ga_clear_strings(&ga_loaded);
920
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200921 /* script-local variables */
922 for (i = 1; i <= ga_scripts.ga_len; ++i)
923 {
924 vars_clear(&SCRIPT_VARS(i));
925 vim_free(SCRIPT_SV(i));
926 }
927 ga_clear(&ga_scripts);
928
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000929 /* unreferenced lists and dicts */
930 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000931
932 /* functions */
933 free_all_functions();
934 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000935}
936#endif
937
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 * Return the name of the executed function.
940 */
941 char_u *
942func_name(cookie)
943 void *cookie;
944{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000945 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946}
947
948/*
949 * Return the address holding the next breakpoint line for a funccall cookie.
950 */
951 linenr_T *
952func_breakpoint(cookie)
953 void *cookie;
954{
Bram Moolenaar33570922005-01-25 22:26:29 +0000955 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
958/*
959 * Return the address holding the debug tick for a funccall cookie.
960 */
961 int *
962func_dbg_tick(cookie)
963 void *cookie;
964{
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967
968/*
969 * Return the nesting level for a funccall cookie.
970 */
971 int
972func_level(cookie)
973 void *cookie;
974{
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000979funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000981/* pointer to list of previously used funccal, still around because some
982 * item in it is still being used. */
983funccall_T *previous_funccal = NULL;
984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985/*
986 * Return TRUE when a function was ended by a ":return" command.
987 */
988 int
989current_func_returned()
990{
991 return current_funccal->returned;
992}
993
994
995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 * Set an internal variable to a string value. Creates the variable if it does
997 * not already exist.
998 */
999 void
1000set_internal_string_var(name, value)
1001 char_u *name;
1002 char_u *value;
1003{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001004 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001005 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
1007 val = vim_strsave(value);
1008 if (val != NULL)
1009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001010 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001011 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001013 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001014 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 }
1016 }
1017}
1018
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001019static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001020static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001021static char_u *redir_endp = NULL;
1022static char_u *redir_varname = NULL;
1023
1024/*
1025 * Start recording command output to a variable
1026 * Returns OK if successfully completed the setup. FAIL otherwise.
1027 */
1028 int
1029var_redir_start(name, append)
1030 char_u *name;
1031 int append; /* append to an existing variable */
1032{
1033 int save_emsg;
1034 int err;
1035 typval_T tv;
1036
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001037 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001038 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001039 {
1040 EMSG(_(e_invarg));
1041 return FAIL;
1042 }
1043
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001044 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001045 redir_varname = vim_strsave(name);
1046 if (redir_varname == NULL)
1047 return FAIL;
1048
1049 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1050 if (redir_lval == NULL)
1051 {
1052 var_redir_stop();
1053 return FAIL;
1054 }
1055
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001056 /* The output is stored in growarray "redir_ga" until redirection ends. */
1057 ga_init2(&redir_ga, (int)sizeof(char), 500);
1058
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001060 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1061 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1063 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001064 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001065 if (redir_endp != NULL && *redir_endp != NUL)
1066 /* Trailing characters are present after the variable name */
1067 EMSG(_(e_trailing));
1068 else
1069 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001070 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001071 var_redir_stop();
1072 return FAIL;
1073 }
1074
1075 /* check if we can write to the variable: set it to or append an empty
1076 * string */
1077 save_emsg = did_emsg;
1078 did_emsg = FALSE;
1079 tv.v_type = VAR_STRING;
1080 tv.vval.v_string = (char_u *)"";
1081 if (append)
1082 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1083 else
1084 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001085 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001087 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 if (err)
1089 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001090 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091 var_redir_stop();
1092 return FAIL;
1093 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094
1095 return OK;
1096}
1097
1098/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001099 * Append "value[value_len]" to the variable set by var_redir_start().
1100 * The actual appending is postponed until redirection ends, because the value
1101 * appended may in fact be the string we write to, changing it may cause freed
1102 * memory to be used:
1103 * :redir => foo
1104 * :let foo
1105 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 */
1107 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001108var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001110 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001112 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113
1114 if (redir_lval == NULL)
1115 return;
1116
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001117 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001118 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001123 {
1124 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001125 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001126 }
1127 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129}
1130
1131/*
1132 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001133 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 */
1135 void
1136var_redir_stop()
1137{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001138 typval_T tv;
1139
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 if (redir_lval != NULL)
1141 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001142 /* If there was no error: assign the text to the variable. */
1143 if (redir_endp != NULL)
1144 {
1145 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1146 tv.v_type = VAR_STRING;
1147 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001148 /* Call get_lval() again, if it's inside a Dict or List it may
1149 * have changed. */
1150 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1151 FALSE, FALSE, FALSE, FNE_CHECK_START);
1152 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1153 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1154 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001155 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001156
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001157 /* free the collected output */
1158 vim_free(redir_ga.ga_data);
1159 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001160
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 vim_free(redir_lval);
1162 redir_lval = NULL;
1163 }
1164 vim_free(redir_varname);
1165 redir_varname = NULL;
1166}
1167
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168# if defined(FEAT_MBYTE) || defined(PROTO)
1169 int
1170eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1171 char_u *enc_from;
1172 char_u *enc_to;
1173 char_u *fname_from;
1174 char_u *fname_to;
1175{
1176 int err = FALSE;
1177
1178 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1179 set_vim_var_string(VV_CC_TO, enc_to, -1);
1180 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1181 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1182 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1183 err = TRUE;
1184 set_vim_var_string(VV_CC_FROM, NULL, -1);
1185 set_vim_var_string(VV_CC_TO, NULL, -1);
1186 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1187 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1188
1189 if (err)
1190 return FAIL;
1191 return OK;
1192}
1193# endif
1194
1195# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1196 int
1197eval_printexpr(fname, args)
1198 char_u *fname;
1199 char_u *args;
1200{
1201 int err = FALSE;
1202
1203 set_vim_var_string(VV_FNAME_IN, fname, -1);
1204 set_vim_var_string(VV_CMDARG, args, -1);
1205 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1206 err = TRUE;
1207 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1208 set_vim_var_string(VV_CMDARG, NULL, -1);
1209
1210 if (err)
1211 {
1212 mch_remove(fname);
1213 return FAIL;
1214 }
1215 return OK;
1216}
1217# endif
1218
1219# if defined(FEAT_DIFF) || defined(PROTO)
1220 void
1221eval_diff(origfile, newfile, outfile)
1222 char_u *origfile;
1223 char_u *newfile;
1224 char_u *outfile;
1225{
1226 int err = FALSE;
1227
1228 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1229 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1230 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1231 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1232 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1233 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1234 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1235}
1236
1237 void
1238eval_patch(origfile, difffile, outfile)
1239 char_u *origfile;
1240 char_u *difffile;
1241 char_u *outfile;
1242{
1243 int err;
1244
1245 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1246 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1247 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1248 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1249 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1250 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1251 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1252}
1253# endif
1254
1255/*
1256 * Top level evaluation function, returning a boolean.
1257 * Sets "error" to TRUE if there was an error.
1258 * Return TRUE or FALSE.
1259 */
1260 int
1261eval_to_bool(arg, error, nextcmd, skip)
1262 char_u *arg;
1263 int *error;
1264 char_u **nextcmd;
1265 int skip; /* only parse, don't execute */
1266{
Bram Moolenaar33570922005-01-25 22:26:29 +00001267 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 int retval = FALSE;
1269
1270 if (skip)
1271 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001272 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 else
1275 {
1276 *error = FALSE;
1277 if (!skip)
1278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001279 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001280 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 }
1282 }
1283 if (skip)
1284 --emsg_skip;
1285
1286 return retval;
1287}
1288
1289/*
1290 * Top level evaluation function, returning a string. If "skip" is TRUE,
1291 * only parsing to "nextcmd" is done, without reporting errors. Return
1292 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1293 */
1294 char_u *
1295eval_to_string_skip(arg, nextcmd, skip)
1296 char_u *arg;
1297 char_u **nextcmd;
1298 int skip; /* only parse, don't execute */
1299{
Bram Moolenaar33570922005-01-25 22:26:29 +00001300 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 char_u *retval;
1302
1303 if (skip)
1304 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 retval = NULL;
1307 else
1308 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001309 retval = vim_strsave(get_tv_string(&tv));
1310 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312 if (skip)
1313 --emsg_skip;
1314
1315 return retval;
1316}
1317
1318/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001319 * Skip over an expression at "*pp".
1320 * Return FAIL for an error, OK otherwise.
1321 */
1322 int
1323skip_expr(pp)
1324 char_u **pp;
1325{
Bram Moolenaar33570922005-01-25 22:26:29 +00001326 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001327
1328 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001330}
1331
1332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001334 * When "convert" is TRUE convert a List into a sequence of lines and convert
1335 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 * Return pointer to allocated memory, or NULL for failure.
1337 */
1338 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001339eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 char_u *arg;
1341 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001342 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
Bram Moolenaar33570922005-01-25 22:26:29 +00001344 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001346 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001347#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001348 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001351 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 retval = NULL;
1353 else
1354 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001355 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 {
1357 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001358 if (tv.vval.v_list != NULL)
1359 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001360 ga_append(&ga, NUL);
1361 retval = (char_u *)ga.ga_data;
1362 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001363#ifdef FEAT_FLOAT
1364 else if (convert && tv.v_type == VAR_FLOAT)
1365 {
1366 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1367 retval = vim_strsave(numbuf);
1368 }
1369#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001370 else
1371 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001372 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 }
1374
1375 return retval;
1376}
1377
1378/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001379 * Call eval_to_string() without using current local variables and using
1380 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 */
1382 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001383eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 char_u *arg;
1385 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001386 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387{
1388 char_u *retval;
1389 void *save_funccalp;
1390
1391 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001392 if (use_sandbox)
1393 ++sandbox;
1394 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001395 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001396 if (use_sandbox)
1397 --sandbox;
1398 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 restore_funccal(save_funccalp);
1400 return retval;
1401}
1402
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403/*
1404 * Top level evaluation function, returning a number.
1405 * Evaluates "expr" silently.
1406 * Returns -1 for an error.
1407 */
1408 int
1409eval_to_number(expr)
1410 char_u *expr;
1411{
Bram Moolenaar33570922005-01-25 22:26:29 +00001412 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001414 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
1416 ++emsg_off;
1417
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001418 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 retval = -1;
1420 else
1421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001422 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001423 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 }
1425 --emsg_off;
1426
1427 return retval;
1428}
1429
Bram Moolenaara40058a2005-07-11 22:42:07 +00001430/*
1431 * Prepare v: variable "idx" to be used.
1432 * Save the current typeval in "save_tv".
1433 * When not used yet add the variable to the v: hashtable.
1434 */
1435 static void
1436prepare_vimvar(idx, save_tv)
1437 int idx;
1438 typval_T *save_tv;
1439{
1440 *save_tv = vimvars[idx].vv_tv;
1441 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1442 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1443}
1444
1445/*
1446 * Restore v: variable "idx" to typeval "save_tv".
1447 * When no longer defined, remove the variable from the v: hashtable.
1448 */
1449 static void
1450restore_vimvar(idx, save_tv)
1451 int idx;
1452 typval_T *save_tv;
1453{
1454 hashitem_T *hi;
1455
Bram Moolenaara40058a2005-07-11 22:42:07 +00001456 vimvars[idx].vv_tv = *save_tv;
1457 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1458 {
1459 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1460 if (HASHITEM_EMPTY(hi))
1461 EMSG2(_(e_intern2), "restore_vimvar()");
1462 else
1463 hash_remove(&vimvarht, hi);
1464 }
1465}
1466
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001467#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001468/*
1469 * Evaluate an expression to a list with suggestions.
1470 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001471 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001472 */
1473 list_T *
1474eval_spell_expr(badword, expr)
1475 char_u *badword;
1476 char_u *expr;
1477{
1478 typval_T save_val;
1479 typval_T rettv;
1480 list_T *list = NULL;
1481 char_u *p = skipwhite(expr);
1482
1483 /* Set "v:val" to the bad word. */
1484 prepare_vimvar(VV_VAL, &save_val);
1485 vimvars[VV_VAL].vv_type = VAR_STRING;
1486 vimvars[VV_VAL].vv_str = badword;
1487 if (p_verbose == 0)
1488 ++emsg_off;
1489
1490 if (eval1(&p, &rettv, TRUE) == OK)
1491 {
1492 if (rettv.v_type != VAR_LIST)
1493 clear_tv(&rettv);
1494 else
1495 list = rettv.vval.v_list;
1496 }
1497
1498 if (p_verbose == 0)
1499 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001500 restore_vimvar(VV_VAL, &save_val);
1501
1502 return list;
1503}
1504
1505/*
1506 * "list" is supposed to contain two items: a word and a number. Return the
1507 * word in "pp" and the number as the return value.
1508 * Return -1 if anything isn't right.
1509 * Used to get the good word and score from the eval_spell_expr() result.
1510 */
1511 int
1512get_spellword(list, pp)
1513 list_T *list;
1514 char_u **pp;
1515{
1516 listitem_T *li;
1517
1518 li = list->lv_first;
1519 if (li == NULL)
1520 return -1;
1521 *pp = get_tv_string(&li->li_tv);
1522
1523 li = li->li_next;
1524 if (li == NULL)
1525 return -1;
1526 return get_tv_number(&li->li_tv);
1527}
1528#endif
1529
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001530/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001531 * Top level evaluation function.
1532 * Returns an allocated typval_T with the result.
1533 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001534 */
1535 typval_T *
1536eval_expr(arg, nextcmd)
1537 char_u *arg;
1538 char_u **nextcmd;
1539{
1540 typval_T *tv;
1541
1542 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001543 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001544 {
1545 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001546 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001547 }
1548
1549 return tv;
1550}
1551
1552
Bram Moolenaar4f688582007-07-24 12:34:30 +00001553#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1554 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001556 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001557 * Uses argv[argc] for the function arguments. Only Number and String
1558 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001559 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001561 static int
1562call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 char_u *func;
1564 int argc;
1565 char_u **argv;
1566 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
Bram Moolenaar33570922005-01-25 22:26:29 +00001569 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 long n;
1571 int len;
1572 int i;
1573 int doesrange;
1574 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001575 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001577 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001579 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
1581 for (i = 0; i < argc; i++)
1582 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001583 /* Pass a NULL or empty argument as an empty string */
1584 if (argv[i] == NULL || *argv[i] == NUL)
1585 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001586 argvars[i].v_type = VAR_STRING;
1587 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001588 continue;
1589 }
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 /* Recognize a number argument, the others must be strings. */
1592 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1593 if (len != 0 && len == (int)STRLEN(argv[i]))
1594 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001595 argvars[i].v_type = VAR_NUMBER;
1596 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
1598 else
1599 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001600 argvars[i].v_type = VAR_STRING;
1601 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 }
1603 }
1604
1605 if (safe)
1606 {
1607 save_funccalp = save_funccal();
1608 ++sandbox;
1609 }
1610
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1612 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (safe)
1616 {
1617 --sandbox;
1618 restore_funccal(save_funccalp);
1619 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 vim_free(argvars);
1621
1622 if (ret == FAIL)
1623 clear_tv(rettv);
1624
1625 return ret;
1626}
1627
Bram Moolenaar4f688582007-07-24 12:34:30 +00001628# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001629/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001630 * Call vimL function "func" and return the result as a string.
1631 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001632 * Uses argv[argc] for the function arguments.
1633 */
1634 void *
1635call_func_retstr(func, argc, argv, safe)
1636 char_u *func;
1637 int argc;
1638 char_u **argv;
1639 int safe; /* use the sandbox */
1640{
1641 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001642 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643
1644 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1645 return NULL;
1646
1647 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001648 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 return retval;
1650}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001651# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652
Bram Moolenaar4f688582007-07-24 12:34:30 +00001653# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001654/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001655 * Call vimL function "func" and return the result as a number.
1656 * Returns -1 when calling the function fails.
1657 * Uses argv[argc] for the function arguments.
1658 */
1659 long
1660call_func_retnr(func, argc, argv, safe)
1661 char_u *func;
1662 int argc;
1663 char_u **argv;
1664 int safe; /* use the sandbox */
1665{
1666 typval_T rettv;
1667 long retval;
1668
1669 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1670 return -1;
1671
1672 retval = get_tv_number_chk(&rettv, NULL);
1673 clear_tv(&rettv);
1674 return retval;
1675}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001676# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001677
1678/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001679 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001681 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 */
1683 void *
1684call_func_retlist(func, argc, argv, safe)
1685 char_u *func;
1686 int argc;
1687 char_u **argv;
1688 int safe; /* use the sandbox */
1689{
1690 typval_T rettv;
1691
1692 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1693 return NULL;
1694
1695 if (rettv.v_type != VAR_LIST)
1696 {
1697 clear_tv(&rettv);
1698 return NULL;
1699 }
1700
1701 return rettv.vval.v_list;
1702}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703#endif
1704
Bram Moolenaar4f688582007-07-24 12:34:30 +00001705
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706/*
1707 * Save the current function call pointer, and set it to NULL.
1708 * Used when executing autocommands and for ":source".
1709 */
1710 void *
1711save_funccal()
1712{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001713 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 current_funccal = NULL;
1716 return (void *)fc;
1717}
1718
1719 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001720restore_funccal(vfc)
1721 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001723 funccall_T *fc = (funccall_T *)vfc;
1724
1725 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726}
1727
Bram Moolenaar05159a02005-02-26 23:04:13 +00001728#if defined(FEAT_PROFILE) || defined(PROTO)
1729/*
1730 * Prepare profiling for entering a child or something else that is not
1731 * counted for the script/function itself.
1732 * Should always be called in pair with prof_child_exit().
1733 */
1734 void
1735prof_child_enter(tm)
1736 proftime_T *tm; /* place to store waittime */
1737{
1738 funccall_T *fc = current_funccal;
1739
1740 if (fc != NULL && fc->func->uf_profiling)
1741 profile_start(&fc->prof_child);
1742 script_prof_save(tm);
1743}
1744
1745/*
1746 * Take care of time spent in a child.
1747 * Should always be called after prof_child_enter().
1748 */
1749 void
1750prof_child_exit(tm)
1751 proftime_T *tm; /* where waittime was stored */
1752{
1753 funccall_T *fc = current_funccal;
1754
1755 if (fc != NULL && fc->func->uf_profiling)
1756 {
1757 profile_end(&fc->prof_child);
1758 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1759 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1760 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1761 }
1762 script_prof_restore(tm);
1763}
1764#endif
1765
1766
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767#ifdef FEAT_FOLDING
1768/*
1769 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1770 * it in "*cp". Doesn't give error messages.
1771 */
1772 int
1773eval_foldexpr(arg, cp)
1774 char_u *arg;
1775 int *cp;
1776{
Bram Moolenaar33570922005-01-25 22:26:29 +00001777 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 int retval;
1779 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001780 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1781 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001784 if (use_sandbox)
1785 ++sandbox;
1786 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 retval = 0;
1790 else
1791 {
1792 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 if (tv.v_type == VAR_NUMBER)
1794 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001795 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 retval = 0;
1797 else
1798 {
1799 /* If the result is a string, check if there is a non-digit before
1800 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (!VIM_ISDIGIT(*s) && *s != '-')
1803 *cp = *s++;
1804 retval = atol((char *)s);
1805 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 }
1808 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001809 if (use_sandbox)
1810 --sandbox;
1811 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
1813 return retval;
1814}
1815#endif
1816
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818 * ":let" list all variable values
1819 * ":let var1 var2" list variable values
1820 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001821 * ":let var += expr" assignment command.
1822 * ":let var -= expr" assignment command.
1823 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 */
1826 void
1827ex_let(eap)
1828 exarg_T *eap;
1829{
1830 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001832 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834 int var_count = 0;
1835 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001836 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001837 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001838 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
Bram Moolenaardb552d602006-03-23 22:59:57 +00001840 argend = skip_var_list(arg, &var_count, &semicolon);
1841 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001842 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001843 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1844 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001845 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001848 /*
1849 * ":let" without "=": list variables
1850 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 if (*arg == '[')
1852 EMSG(_(e_invarg));
1853 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001854 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001855 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001857 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001859 list_glob_vars(&first);
1860 list_buf_vars(&first);
1861 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001862#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001863 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001864#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001865 list_script_vars(&first);
1866 list_func_vars(&first);
1867 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 eap->nextcmd = check_nextcmd(arg);
1870 }
1871 else
1872 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001873 op[0] = '=';
1874 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001875 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001876 {
1877 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1878 op[0] = expr[-1]; /* +=, -= or .= */
1879 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001880 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (eap->skip)
1883 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (eap->skip)
1886 {
1887 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 --emsg_skip;
1890 }
1891 else if (i != FAIL)
1892 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001894 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
1897 }
1898}
1899
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900/*
1901 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1902 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 * When "nextchars" is not NULL it points to a string with characters that
1904 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1905 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 * Returns OK or FAIL;
1907 */
1908 static int
1909ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1910 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001911 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 int copy; /* copy values from "tv", don't move */
1913 int semicolon; /* from skip_var_list() */
1914 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916{
1917 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001918 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001920 listitem_T *item;
1921 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922
1923 if (*arg != '[')
1924 {
1925 /*
1926 * ":let var = expr" or ":for var in list"
1927 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001928 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 return FAIL;
1930 return OK;
1931 }
1932
1933 /*
1934 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1935 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001936 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937 {
1938 EMSG(_(e_listreq));
1939 return FAIL;
1940 }
1941
1942 i = list_len(l);
1943 if (semicolon == 0 && var_count < i)
1944 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001945 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 return FAIL;
1947 }
1948 if (var_count - semicolon > i)
1949 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001950 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 return FAIL;
1952 }
1953
1954 item = l->lv_first;
1955 while (*arg != ']')
1956 {
1957 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001958 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 item = item->li_next;
1960 if (arg == NULL)
1961 return FAIL;
1962
1963 arg = skipwhite(arg);
1964 if (*arg == ';')
1965 {
1966 /* Put the rest of the list (may be empty) in the var after ';'.
1967 * Create a new list for this. */
1968 l = list_alloc();
1969 if (l == NULL)
1970 return FAIL;
1971 while (item != NULL)
1972 {
1973 list_append_tv(l, &item->li_tv);
1974 item = item->li_next;
1975 }
1976
1977 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001978 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 ltv.vval.v_list = l;
1980 l->lv_refcount = 1;
1981
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001982 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1983 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984 clear_tv(&ltv);
1985 if (arg == NULL)
1986 return FAIL;
1987 break;
1988 }
1989 else if (*arg != ',' && *arg != ']')
1990 {
1991 EMSG2(_(e_intern2), "ex_let_vars()");
1992 return FAIL;
1993 }
1994 }
1995
1996 return OK;
1997}
1998
1999/*
2000 * Skip over assignable variable "var" or list of variables "[var, var]".
2001 * Used for ":let varvar = expr" and ":for varvar in expr".
2002 * For "[var, var]" increment "*var_count" for each variable.
2003 * for "[var, var; var]" set "semicolon".
2004 * Return NULL for an error.
2005 */
2006 static char_u *
2007skip_var_list(arg, var_count, semicolon)
2008 char_u *arg;
2009 int *var_count;
2010 int *semicolon;
2011{
2012 char_u *p, *s;
2013
2014 if (*arg == '[')
2015 {
2016 /* "[var, var]": find the matching ']'. */
2017 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002018 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 {
2020 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2021 s = skip_var_one(p);
2022 if (s == p)
2023 {
2024 EMSG2(_(e_invarg2), p);
2025 return NULL;
2026 }
2027 ++*var_count;
2028
2029 p = skipwhite(s);
2030 if (*p == ']')
2031 break;
2032 else if (*p == ';')
2033 {
2034 if (*semicolon == 1)
2035 {
2036 EMSG(_("Double ; in list of variables"));
2037 return NULL;
2038 }
2039 *semicolon = 1;
2040 }
2041 else if (*p != ',')
2042 {
2043 EMSG2(_(e_invarg2), p);
2044 return NULL;
2045 }
2046 }
2047 return p + 1;
2048 }
2049 else
2050 return skip_var_one(arg);
2051}
2052
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002053/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002054 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002055 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002056 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002057 static char_u *
2058skip_var_one(arg)
2059 char_u *arg;
2060{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002061 if (*arg == '@' && arg[1] != NUL)
2062 return arg + 2;
2063 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2064 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002065}
2066
Bram Moolenaara7043832005-01-21 11:56:39 +00002067/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002068 * List variables for hashtab "ht" with prefix "prefix".
2069 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002070 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002071 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002072list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002073 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002074 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002075 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002076 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002077{
Bram Moolenaar33570922005-01-25 22:26:29 +00002078 hashitem_T *hi;
2079 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002080 int todo;
2081
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002082 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002083 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2084 {
2085 if (!HASHITEM_EMPTY(hi))
2086 {
2087 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002088 di = HI2DI(hi);
2089 if (empty || di->di_tv.v_type != VAR_STRING
2090 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002091 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002092 }
2093 }
2094}
2095
2096/*
2097 * List global variables.
2098 */
2099 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002100list_glob_vars(first)
2101 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002103 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002104}
2105
2106/*
2107 * List buffer variables.
2108 */
2109 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110list_buf_vars(first)
2111 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002112{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002113 char_u numbuf[NUMBUFLEN];
2114
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002115 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2116 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002117
2118 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2120 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002121}
2122
2123/*
2124 * List window variables.
2125 */
2126 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002127list_win_vars(first)
2128 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002129{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002130 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2131 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002132}
2133
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002134#ifdef FEAT_WINDOWS
2135/*
2136 * List tab page variables.
2137 */
2138 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139list_tab_vars(first)
2140 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002141{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002142 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2143 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002144}
2145#endif
2146
Bram Moolenaara7043832005-01-21 11:56:39 +00002147/*
2148 * List Vim variables.
2149 */
2150 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151list_vim_vars(first)
2152 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002153{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155}
2156
2157/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002158 * List script-local variables, if there is a script.
2159 */
2160 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161list_script_vars(first)
2162 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002163{
2164 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002165 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2166 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002167}
2168
2169/*
2170 * List function variables, if there is a function.
2171 */
2172 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173list_func_vars(first)
2174 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002175{
2176 if (current_funccal != NULL)
2177 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002179}
2180
2181/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182 * List variables in "arg".
2183 */
2184 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002185list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 exarg_T *eap;
2187 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189{
2190 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002191 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002193 char_u *name_start;
2194 char_u *arg_subsc;
2195 char_u *tofree;
2196 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197
2198 while (!ends_excmd(*arg) && !got_int)
2199 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002200 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002202 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002203 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2204 {
2205 emsg_severe = TRUE;
2206 EMSG(_(e_trailing));
2207 break;
2208 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002210 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 /* get_name_len() takes care of expanding curly braces */
2213 name_start = name = arg;
2214 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2215 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002217 /* This is mainly to keep test 49 working: when expanding
2218 * curly braces fails overrule the exception error message. */
2219 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221 emsg_severe = TRUE;
2222 EMSG2(_(e_invarg2), arg);
2223 break;
2224 }
2225 error = TRUE;
2226 }
2227 else
2228 {
2229 if (tofree != NULL)
2230 name = tofree;
2231 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 else
2234 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 /* handle d.key, l[idx], f(expr) */
2236 arg_subsc = arg;
2237 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002238 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002242 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002243 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002244 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002245 case 'g': list_glob_vars(first); break;
2246 case 'b': list_buf_vars(first); break;
2247 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002248#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002249 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002250#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002251 case 'v': list_vim_vars(first); break;
2252 case 's': list_script_vars(first); break;
2253 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 default:
2255 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002256 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 }
2258 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 {
2260 char_u numbuf[NUMBUFLEN];
2261 char_u *tf;
2262 int c;
2263 char_u *s;
2264
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002265 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 c = *arg;
2267 *arg = NUL;
2268 list_one_var_a((char_u *)"",
2269 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002270 tv.v_type,
2271 s == NULL ? (char_u *)"" : s,
2272 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 *arg = c;
2274 vim_free(tf);
2275 }
2276 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002277 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 }
2279 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280
2281 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283
2284 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 }
2286
2287 return arg;
2288}
2289
2290/*
2291 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2292 * Returns a pointer to the char just after the var name.
2293 * Returns NULL if there is an error.
2294 */
2295 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002296ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002298 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002300 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002301 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302{
2303 int c1;
2304 char_u *name;
2305 char_u *p;
2306 char_u *arg_end = NULL;
2307 int len;
2308 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002309 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310
2311 /*
2312 * ":let $VAR = expr": Set environment variable.
2313 */
2314 if (*arg == '$')
2315 {
2316 /* Find the end of the name. */
2317 ++arg;
2318 name = arg;
2319 len = get_env_len(&arg);
2320 if (len == 0)
2321 EMSG2(_(e_invarg2), name - 1);
2322 else
2323 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002324 if (op != NULL && (*op == '+' || *op == '-'))
2325 EMSG2(_(e_letwrong), op);
2326 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002327 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002329 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 {
2331 c1 = name[len];
2332 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002333 p = get_tv_string_chk(tv);
2334 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002335 {
2336 int mustfree = FALSE;
2337 char_u *s = vim_getenv(name, &mustfree);
2338
2339 if (s != NULL)
2340 {
2341 p = tofree = concat_str(s, p);
2342 if (mustfree)
2343 vim_free(s);
2344 }
2345 }
2346 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002347 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002348 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002349 if (STRICMP(name, "HOME") == 0)
2350 init_homedir();
2351 else if (didset_vim && STRICMP(name, "VIM") == 0)
2352 didset_vim = FALSE;
2353 else if (didset_vimruntime
2354 && STRICMP(name, "VIMRUNTIME") == 0)
2355 didset_vimruntime = FALSE;
2356 arg_end = arg;
2357 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002359 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002360 }
2361 }
2362 }
2363
2364 /*
2365 * ":let &option = expr": Set option value.
2366 * ":let &l:option = expr": Set local option value.
2367 * ":let &g:option = expr": Set global option value.
2368 */
2369 else if (*arg == '&')
2370 {
2371 /* Find the end of the name. */
2372 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002373 if (p == NULL || (endchars != NULL
2374 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375 EMSG(_(e_letunexp));
2376 else
2377 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 long n;
2379 int opt_type;
2380 long numval;
2381 char_u *stringval = NULL;
2382 char_u *s;
2383
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 c1 = *p;
2385 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386
2387 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002388 s = get_tv_string_chk(tv); /* != NULL if number or string */
2389 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 {
2391 opt_type = get_option_value(arg, &numval,
2392 &stringval, opt_flags);
2393 if ((opt_type == 1 && *op == '.')
2394 || (opt_type == 0 && *op != '.'))
2395 EMSG2(_(e_letwrong), op);
2396 else
2397 {
2398 if (opt_type == 1) /* number */
2399 {
2400 if (*op == '+')
2401 n = numval + n;
2402 else
2403 n = numval - n;
2404 }
2405 else if (opt_type == 0 && stringval != NULL) /* string */
2406 {
2407 s = concat_str(stringval, s);
2408 vim_free(stringval);
2409 stringval = s;
2410 }
2411 }
2412 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002413 if (s != NULL)
2414 {
2415 set_option_value(arg, n, s, opt_flags);
2416 arg_end = p;
2417 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002418 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 }
2421 }
2422
2423 /*
2424 * ":let @r = expr": Set register contents.
2425 */
2426 else if (*arg == '@')
2427 {
2428 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429 if (op != NULL && (*op == '+' || *op == '-'))
2430 EMSG2(_(e_letwrong), op);
2431 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002432 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 EMSG(_(e_letunexp));
2434 else
2435 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002436 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 char_u *s;
2438
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002439 p = get_tv_string_chk(tv);
2440 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002442 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 if (s != NULL)
2444 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002445 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 vim_free(s);
2447 }
2448 }
2449 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002450 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002452 arg_end = arg + 1;
2453 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002454 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 }
2456 }
2457
2458 /*
2459 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002462 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002464 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002466 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2470 EMSG(_(e_letunexp));
2471 else
2472 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 arg_end = p;
2475 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478 }
2479
2480 else
2481 EMSG2(_(e_invarg2), arg);
2482
2483 return arg_end;
2484}
2485
2486/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2488 */
2489 static int
2490check_changedtick(arg)
2491 char_u *arg;
2492{
2493 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2494 {
2495 EMSG2(_(e_readonlyvar), arg);
2496 return TRUE;
2497 }
2498 return FALSE;
2499}
2500
2501/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 * Get an lval: variable, Dict item or List item that can be assigned a value
2503 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2504 * "name.key", "name.key[expr]" etc.
2505 * Indexing only works if "name" is an existing List or Dictionary.
2506 * "name" points to the start of the name.
2507 * If "rettv" is not NULL it points to the value to be assigned.
2508 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2509 * wrong; must end in space or cmd separator.
2510 *
2511 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002512 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 */
2515 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002516get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002518 typval_T *rettv;
2519 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 int unlet;
2521 int skip;
2522 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002523 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 char_u *expr_start, *expr_end;
2527 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002528 dictitem_T *v;
2529 typval_T var1;
2530 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002532 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002538 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539
2540 if (skip)
2541 {
2542 /* When skipping just find the end of the name. */
2543 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002544 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 }
2546
2547 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002548 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 if (expr_start != NULL)
2550 {
2551 /* Don't expand the name when we already know there is an error. */
2552 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2553 && *p != '[' && *p != '.')
2554 {
2555 EMSG(_(e_trailing));
2556 return NULL;
2557 }
2558
2559 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2560 if (lp->ll_exp_name == NULL)
2561 {
2562 /* Report an invalid expression in braces, unless the
2563 * expression evaluation has been cancelled due to an
2564 * aborting error, an interrupt, or an exception. */
2565 if (!aborting() && !quiet)
2566 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002567 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 EMSG2(_(e_invarg2), name);
2569 return NULL;
2570 }
2571 }
2572 lp->ll_name = lp->ll_exp_name;
2573 }
2574 else
2575 lp->ll_name = name;
2576
2577 /* Without [idx] or .key we are done. */
2578 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2579 return p;
2580
2581 cc = *p;
2582 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002583 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 if (v == NULL && !quiet)
2585 EMSG2(_(e_undefvar), lp->ll_name);
2586 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002587 if (v == NULL)
2588 return NULL;
2589
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 /*
2591 * Loop until no more [idx] or .key is following.
2592 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002593 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002595 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2597 && !(lp->ll_tv->v_type == VAR_DICT
2598 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 if (!quiet)
2601 EMSG(_("E689: Can only index a List or Dictionary"));
2602 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002603 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 if (!quiet)
2607 EMSG(_("E708: [:] must come last"));
2608 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002610
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 len = -1;
2612 if (*p == '.')
2613 {
2614 key = p + 1;
2615 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2616 ;
2617 if (len == 0)
2618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 if (!quiet)
2620 EMSG(_(e_emptykey));
2621 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002622 }
2623 p = key + len;
2624 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002625 else
2626 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 if (*p == ':')
2630 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002631 else
2632 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 empty1 = FALSE;
2634 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002636 if (get_tv_string_chk(&var1) == NULL)
2637 {
2638 /* not a number or string */
2639 clear_tv(&var1);
2640 return NULL;
2641 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 }
2643
2644 /* Optionally get the second index [ :expr]. */
2645 if (*p == ':')
2646 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002650 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002651 if (!empty1)
2652 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002654 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (rettv != NULL && (rettv->v_type != VAR_LIST
2656 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 if (!quiet)
2659 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 if (!empty1)
2661 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 }
2664 p = skipwhite(p + 1);
2665 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 else
2668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2671 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 if (!empty1)
2673 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002676 if (get_tv_string_chk(&var2) == NULL)
2677 {
2678 /* not a number or string */
2679 if (!empty1)
2680 clear_tv(&var1);
2681 clear_tv(&var2);
2682 return NULL;
2683 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002689
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!quiet)
2693 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 if (!empty1)
2695 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 }
2700
2701 /* Skip to past ']'. */
2702 ++p;
2703 }
2704
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 {
2707 if (len == -1)
2708 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002710 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 if (*key == NUL)
2712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (!quiet)
2714 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002719 lp->ll_list = NULL;
2720 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002721 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002722
2723 /* When assigning to g: check that a function and variable name is
2724 * valid. */
2725 if (rettv != NULL && lp->ll_dict == &globvardict)
2726 {
2727 if (rettv->v_type == VAR_FUNC
2728 && var_check_func_name(key, lp->ll_di == NULL))
2729 return NULL;
2730 if (!valid_varname(key))
2731 return NULL;
2732 }
2733
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002736 /* Can't add "v:" variable. */
2737 if (lp->ll_dict == &vimvardict)
2738 {
2739 EMSG2(_(e_illvar), name);
2740 return NULL;
2741 }
2742
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002743 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002747 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 if (len == -1)
2749 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 }
2752 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 if (len == -1)
2757 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 p = NULL;
2760 break;
2761 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002762 /* existing variable, need to check if it can be changed */
2763 else if (var_check_ro(lp->ll_di->di_flags, name))
2764 return NULL;
2765
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 if (len == -1)
2767 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 }
2770 else
2771 {
2772 /*
2773 * Get the number and item for the only or first index of the List.
2774 */
2775 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 else
2778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002779 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 clear_tv(&var1);
2781 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002782 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 lp->ll_list = lp->ll_tv->vval.v_list;
2784 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2785 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002787 if (lp->ll_n1 < 0)
2788 {
2789 lp->ll_n1 = 0;
2790 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2791 }
2792 }
2793 if (lp->ll_li == NULL)
2794 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002797 if (!quiet)
2798 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 }
2801
2802 /*
2803 * May need to find the item or absolute index for the second
2804 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 * When no index given: "lp->ll_empty2" is TRUE.
2806 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002810 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002816 {
2817 if (!quiet)
2818 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002820 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 }
2823
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2825 if (lp->ll_n1 < 0)
2826 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2827 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002828 {
2829 if (!quiet)
2830 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002832 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002833 }
2834
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002836 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002837 }
2838
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 return p;
2840}
2841
2842/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002843 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 */
2845 static void
2846clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002847 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848{
2849 vim_free(lp->ll_exp_name);
2850 vim_free(lp->ll_newkey);
2851}
2852
2853/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002854 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002856 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 */
2858 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002859set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002860 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002862 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002864 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865{
2866 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002867 listitem_T *ri;
2868 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869
2870 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002871 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002873 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 cc = *endp;
2875 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002876 if (op != NULL && *op != '=')
2877 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002878 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002879
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002880 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002881 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002882 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002883 {
2884 if (tv_op(&tv, rettv, op) == OK)
2885 set_var(lp->ll_name, &tv, FALSE);
2886 clear_tv(&tv);
2887 }
2888 }
2889 else
2890 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002892 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002894 else if (tv_check_lock(lp->ll_newkey == NULL
2895 ? lp->ll_tv->v_lock
2896 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2897 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 else if (lp->ll_range)
2899 {
2900 /*
2901 * Assign the List values to the list items.
2902 */
2903 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002904 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002905 if (op != NULL && *op != '=')
2906 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2907 else
2908 {
2909 clear_tv(&lp->ll_li->li_tv);
2910 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2911 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 ri = ri->li_next;
2913 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2914 break;
2915 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002916 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002918 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 ri = NULL;
2921 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002922 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002923 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 lp->ll_li = lp->ll_li->li_next;
2925 ++lp->ll_n1;
2926 }
2927 if (ri != NULL)
2928 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002929 else if (lp->ll_empty2
2930 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 : lp->ll_n1 != lp->ll_n2)
2932 EMSG(_("E711: List value has not enough items"));
2933 }
2934 else
2935 {
2936 /*
2937 * Assign to a List or Dictionary item.
2938 */
2939 if (lp->ll_newkey != NULL)
2940 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002941 if (op != NULL && *op != '=')
2942 {
2943 EMSG2(_(e_letwrong), op);
2944 return;
2945 }
2946
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002948 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 if (di == NULL)
2950 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002951 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2952 {
2953 vim_free(di);
2954 return;
2955 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002957 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002958 else if (op != NULL && *op != '=')
2959 {
2960 tv_op(lp->ll_tv, rettv, op);
2961 return;
2962 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002963 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002965
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 /*
2967 * Assign the value to the variable or list item.
2968 */
2969 if (copy)
2970 copy_tv(rettv, lp->ll_tv);
2971 else
2972 {
2973 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002974 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002976 }
2977 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002978}
2979
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002980/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002981 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2982 * Returns OK or FAIL.
2983 */
2984 static int
2985tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002986 typval_T *tv1;
2987 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002988 char_u *op;
2989{
2990 long n;
2991 char_u numbuf[NUMBUFLEN];
2992 char_u *s;
2993
2994 /* Can't do anything with a Funcref or a Dict on the right. */
2995 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2996 {
2997 switch (tv1->v_type)
2998 {
2999 case VAR_DICT:
3000 case VAR_FUNC:
3001 break;
3002
3003 case VAR_LIST:
3004 if (*op != '+' || tv2->v_type != VAR_LIST)
3005 break;
3006 /* List += List */
3007 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3008 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3009 return OK;
3010
3011 case VAR_NUMBER:
3012 case VAR_STRING:
3013 if (tv2->v_type == VAR_LIST)
3014 break;
3015 if (*op == '+' || *op == '-')
3016 {
3017 /* nr += nr or nr -= nr*/
3018 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003019#ifdef FEAT_FLOAT
3020 if (tv2->v_type == VAR_FLOAT)
3021 {
3022 float_T f = n;
3023
3024 if (*op == '+')
3025 f += tv2->vval.v_float;
3026 else
3027 f -= tv2->vval.v_float;
3028 clear_tv(tv1);
3029 tv1->v_type = VAR_FLOAT;
3030 tv1->vval.v_float = f;
3031 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003032 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003033#endif
3034 {
3035 if (*op == '+')
3036 n += get_tv_number(tv2);
3037 else
3038 n -= get_tv_number(tv2);
3039 clear_tv(tv1);
3040 tv1->v_type = VAR_NUMBER;
3041 tv1->vval.v_number = n;
3042 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003043 }
3044 else
3045 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003046 if (tv2->v_type == VAR_FLOAT)
3047 break;
3048
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003049 /* str .= str */
3050 s = get_tv_string(tv1);
3051 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3052 clear_tv(tv1);
3053 tv1->v_type = VAR_STRING;
3054 tv1->vval.v_string = s;
3055 }
3056 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003057
3058#ifdef FEAT_FLOAT
3059 case VAR_FLOAT:
3060 {
3061 float_T f;
3062
3063 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3064 && tv2->v_type != VAR_NUMBER
3065 && tv2->v_type != VAR_STRING))
3066 break;
3067 if (tv2->v_type == VAR_FLOAT)
3068 f = tv2->vval.v_float;
3069 else
3070 f = get_tv_number(tv2);
3071 if (*op == '+')
3072 tv1->vval.v_float += f;
3073 else
3074 tv1->vval.v_float -= f;
3075 }
3076 return OK;
3077#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003078 }
3079 }
3080
3081 EMSG2(_(e_letwrong), op);
3082 return FAIL;
3083}
3084
3085/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003086 * Add a watcher to a list.
3087 */
3088 static void
3089list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003090 list_T *l;
3091 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003092{
3093 lw->lw_next = l->lv_watch;
3094 l->lv_watch = lw;
3095}
3096
3097/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003098 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003099 * No warning when it isn't found...
3100 */
3101 static void
3102list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003103 list_T *l;
3104 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003105{
Bram Moolenaar33570922005-01-25 22:26:29 +00003106 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003107
3108 lwp = &l->lv_watch;
3109 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3110 {
3111 if (lw == lwrem)
3112 {
3113 *lwp = lw->lw_next;
3114 break;
3115 }
3116 lwp = &lw->lw_next;
3117 }
3118}
3119
3120/*
3121 * Just before removing an item from a list: advance watchers to the next
3122 * item.
3123 */
3124 static void
3125list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003126 list_T *l;
3127 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003128{
Bram Moolenaar33570922005-01-25 22:26:29 +00003129 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130
3131 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3132 if (lw->lw_item == item)
3133 lw->lw_item = item->li_next;
3134}
3135
3136/*
3137 * Evaluate the expression used in a ":for var in expr" command.
3138 * "arg" points to "var".
3139 * Set "*errp" to TRUE for an error, FALSE otherwise;
3140 * Return a pointer that holds the info. Null when there is an error.
3141 */
3142 void *
3143eval_for_line(arg, errp, nextcmdp, skip)
3144 char_u *arg;
3145 int *errp;
3146 char_u **nextcmdp;
3147 int skip;
3148{
Bram Moolenaar33570922005-01-25 22:26:29 +00003149 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003150 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003151 typval_T tv;
3152 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003153
3154 *errp = TRUE; /* default: there is an error */
3155
Bram Moolenaar33570922005-01-25 22:26:29 +00003156 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003157 if (fi == NULL)
3158 return NULL;
3159
3160 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3161 if (expr == NULL)
3162 return fi;
3163
3164 expr = skipwhite(expr);
3165 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3166 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003167 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168 return fi;
3169 }
3170
3171 if (skip)
3172 ++emsg_skip;
3173 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3174 {
3175 *errp = FALSE;
3176 if (!skip)
3177 {
3178 l = tv.vval.v_list;
3179 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003180 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003181 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003182 clear_tv(&tv);
3183 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184 else
3185 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003186 /* No need to increment the refcount, it's already set for the
3187 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188 fi->fi_list = l;
3189 list_add_watch(l, &fi->fi_lw);
3190 fi->fi_lw.lw_item = l->lv_first;
3191 }
3192 }
3193 }
3194 if (skip)
3195 --emsg_skip;
3196
3197 return fi;
3198}
3199
3200/*
3201 * Use the first item in a ":for" list. Advance to the next.
3202 * Assign the values to the variable (list). "arg" points to the first one.
3203 * Return TRUE when a valid item was found, FALSE when at end of list or
3204 * something wrong.
3205 */
3206 int
3207next_for_item(fi_void, arg)
3208 void *fi_void;
3209 char_u *arg;
3210{
Bram Moolenaar33570922005-01-25 22:26:29 +00003211 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003213 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214
3215 item = fi->fi_lw.lw_item;
3216 if (item == NULL)
3217 result = FALSE;
3218 else
3219 {
3220 fi->fi_lw.lw_item = item->li_next;
3221 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3222 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3223 }
3224 return result;
3225}
3226
3227/*
3228 * Free the structure used to store info used by ":for".
3229 */
3230 void
3231free_for_info(fi_void)
3232 void *fi_void;
3233{
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003236 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003237 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003238 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003239 list_unref(fi->fi_list);
3240 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241 vim_free(fi);
3242}
3243
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3245
3246 void
3247set_context_for_expression(xp, arg, cmdidx)
3248 expand_T *xp;
3249 char_u *arg;
3250 cmdidx_T cmdidx;
3251{
3252 int got_eq = FALSE;
3253 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256 if (cmdidx == CMD_let)
3257 {
3258 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003259 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260 {
3261 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003262 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003263 {
3264 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003265 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 if (vim_iswhite(*p))
3267 break;
3268 }
3269 return;
3270 }
3271 }
3272 else
3273 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3274 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 while ((xp->xp_pattern = vim_strpbrk(arg,
3276 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3277 {
3278 c = *xp->xp_pattern;
3279 if (c == '&')
3280 {
3281 c = xp->xp_pattern[1];
3282 if (c == '&')
3283 {
3284 ++xp->xp_pattern;
3285 xp->xp_context = cmdidx != CMD_let || got_eq
3286 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3287 }
3288 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003291 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3292 xp->xp_pattern += 2;
3293
3294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 }
3296 else if (c == '$')
3297 {
3298 /* environment variable */
3299 xp->xp_context = EXPAND_ENV_VARS;
3300 }
3301 else if (c == '=')
3302 {
3303 got_eq = TRUE;
3304 xp->xp_context = EXPAND_EXPRESSION;
3305 }
3306 else if (c == '<'
3307 && xp->xp_context == EXPAND_FUNCTIONS
3308 && vim_strchr(xp->xp_pattern, '(') == NULL)
3309 {
3310 /* Function name can start with "<SNR>" */
3311 break;
3312 }
3313 else if (cmdidx != CMD_let || got_eq)
3314 {
3315 if (c == '"') /* string */
3316 {
3317 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3318 if (c == '\\' && xp->xp_pattern[1] != NUL)
3319 ++xp->xp_pattern;
3320 xp->xp_context = EXPAND_NOTHING;
3321 }
3322 else if (c == '\'') /* literal string */
3323 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003324 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3326 /* skip */ ;
3327 xp->xp_context = EXPAND_NOTHING;
3328 }
3329 else if (c == '|')
3330 {
3331 if (xp->xp_pattern[1] == '|')
3332 {
3333 ++xp->xp_pattern;
3334 xp->xp_context = EXPAND_EXPRESSION;
3335 }
3336 else
3337 xp->xp_context = EXPAND_COMMANDS;
3338 }
3339 else
3340 xp->xp_context = EXPAND_EXPRESSION;
3341 }
3342 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343 /* Doesn't look like something valid, expand as an expression
3344 * anyway. */
3345 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 arg = xp->xp_pattern;
3347 if (*arg != NUL)
3348 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3349 /* skip */ ;
3350 }
3351 xp->xp_pattern = arg;
3352}
3353
3354#endif /* FEAT_CMDL_COMPL */
3355
3356/*
3357 * ":1,25call func(arg1, arg2)" function call.
3358 */
3359 void
3360ex_call(eap)
3361 exarg_T *eap;
3362{
3363 char_u *arg = eap->arg;
3364 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003366 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003368 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 linenr_T lnum;
3370 int doesrange;
3371 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003372 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003374 if (eap->skip)
3375 {
3376 /* trans_function_name() doesn't work well when skipping, use eval0()
3377 * instead to skip to any following command, e.g. for:
3378 * :if 0 | call dict.foo().bar() | endif */
3379 eval0(eap->arg, &rettv, &eap->nextcmd, FALSE);
3380 return;
3381 }
3382
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003383 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003384 if (fudi.fd_newkey != NULL)
3385 {
3386 /* Still need to give an error message for missing key. */
3387 EMSG2(_(e_dictkey), fudi.fd_newkey);
3388 vim_free(fudi.fd_newkey);
3389 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003390 if (tofree == NULL)
3391 return;
3392
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003393 /* Increase refcount on dictionary, it could get deleted when evaluating
3394 * the arguments. */
3395 if (fudi.fd_dict != NULL)
3396 ++fudi.fd_dict->dv_refcount;
3397
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003398 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003399 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003400 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
Bram Moolenaar532c7802005-01-27 14:44:31 +00003402 /* Skip white space to allow ":call func ()". Not good, but required for
3403 * backward compatibility. */
3404 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003405 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406
3407 if (*startarg != '(')
3408 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003409 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 goto end;
3411 }
3412
3413 /*
3414 * When skipping, evaluate the function once, to find the end of the
3415 * arguments.
3416 * When the function takes a range, this is discovered after the first
3417 * call, and the loop is broken.
3418 */
3419 if (eap->skip)
3420 {
3421 ++emsg_skip;
3422 lnum = eap->line2; /* do it once, also with an invalid range */
3423 }
3424 else
3425 lnum = eap->line1;
3426 for ( ; lnum <= eap->line2; ++lnum)
3427 {
3428 if (!eap->skip && eap->addr_count > 0)
3429 {
3430 curwin->w_cursor.lnum = lnum;
3431 curwin->w_cursor.col = 0;
3432 }
3433 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003434 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003435 eap->line1, eap->line2, &doesrange,
3436 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 {
3438 failed = TRUE;
3439 break;
3440 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003441
3442 /* Handle a function returning a Funcref, Dictionary or List. */
3443 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3444 {
3445 failed = TRUE;
3446 break;
3447 }
3448
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003449 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 if (doesrange || eap->skip)
3451 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003452
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003454 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003455 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003456 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 if (aborting())
3458 break;
3459 }
3460 if (eap->skip)
3461 --emsg_skip;
3462
3463 if (!failed)
3464 {
3465 /* Check for trailing illegal characters and a following command. */
3466 if (!ends_excmd(*arg))
3467 {
3468 emsg_severe = TRUE;
3469 EMSG(_(e_trailing));
3470 }
3471 else
3472 eap->nextcmd = check_nextcmd(arg);
3473 }
3474
3475end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003476 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003477 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478}
3479
3480/*
3481 * ":unlet[!] var1 ... " command.
3482 */
3483 void
3484ex_unlet(eap)
3485 exarg_T *eap;
3486{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003487 ex_unletlock(eap, eap->arg, 0);
3488}
3489
3490/*
3491 * ":lockvar" and ":unlockvar" commands
3492 */
3493 void
3494ex_lockvar(eap)
3495 exarg_T *eap;
3496{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003498 int deep = 2;
3499
3500 if (eap->forceit)
3501 deep = -1;
3502 else if (vim_isdigit(*arg))
3503 {
3504 deep = getdigits(&arg);
3505 arg = skipwhite(arg);
3506 }
3507
3508 ex_unletlock(eap, arg, deep);
3509}
3510
3511/*
3512 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3513 */
3514 static void
3515ex_unletlock(eap, argstart, deep)
3516 exarg_T *eap;
3517 char_u *argstart;
3518 int deep;
3519{
3520 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003523 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524
3525 do
3526 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003527 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003528 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3529 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003530 if (lv.ll_name == NULL)
3531 error = TRUE; /* error but continue parsing */
3532 if (name_end == NULL || (!vim_iswhite(*name_end)
3533 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003535 if (name_end != NULL)
3536 {
3537 emsg_severe = TRUE;
3538 EMSG(_(e_trailing));
3539 }
3540 if (!(eap->skip || error))
3541 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 break;
3543 }
3544
3545 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003546 {
3547 if (eap->cmdidx == CMD_unlet)
3548 {
3549 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3550 error = TRUE;
3551 }
3552 else
3553 {
3554 if (do_lock_var(&lv, name_end, deep,
3555 eap->cmdidx == CMD_lockvar) == FAIL)
3556 error = TRUE;
3557 }
3558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003560 if (!eap->skip)
3561 clear_lval(&lv);
3562
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 arg = skipwhite(name_end);
3564 } while (!ends_excmd(*arg));
3565
3566 eap->nextcmd = check_nextcmd(arg);
3567}
3568
Bram Moolenaar8c711452005-01-14 21:53:12 +00003569 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003570do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003571 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003572 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003573 int forceit;
3574{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003575 int ret = OK;
3576 int cc;
3577
3578 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003579 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003580 cc = *name_end;
3581 *name_end = NUL;
3582
3583 /* Normal name or expanded name. */
3584 if (check_changedtick(lp->ll_name))
3585 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003586 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003587 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003588 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003589 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003590 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3591 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003592 else if (lp->ll_range)
3593 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003594 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003595
3596 /* Delete a range of List items. */
3597 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3598 {
3599 li = lp->ll_li->li_next;
3600 listitem_remove(lp->ll_list, lp->ll_li);
3601 lp->ll_li = li;
3602 ++lp->ll_n1;
3603 }
3604 }
3605 else
3606 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003607 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003608 /* unlet a List item. */
3609 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003610 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003612 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613 }
3614
3615 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616}
3617
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618/*
3619 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003620 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 */
3622 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003623do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003625 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626{
Bram Moolenaar33570922005-01-25 22:26:29 +00003627 hashtab_T *ht;
3628 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003629 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003630 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631
Bram Moolenaar33570922005-01-25 22:26:29 +00003632 ht = find_var_ht(name, &varname);
3633 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003635 hi = hash_find(ht, varname);
3636 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003637 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003638 di = HI2DI(hi);
3639 if (var_check_fixed(di->di_flags, name)
3640 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003641 return FAIL;
3642 delete_var(ht, hi);
3643 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003646 if (forceit)
3647 return OK;
3648 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 return FAIL;
3650}
3651
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003652/*
3653 * Lock or unlock variable indicated by "lp".
3654 * "deep" is the levels to go (-1 for unlimited);
3655 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3656 */
3657 static int
3658do_lock_var(lp, name_end, deep, lock)
3659 lval_T *lp;
3660 char_u *name_end;
3661 int deep;
3662 int lock;
3663{
3664 int ret = OK;
3665 int cc;
3666 dictitem_T *di;
3667
3668 if (deep == 0) /* nothing to do */
3669 return OK;
3670
3671 if (lp->ll_tv == NULL)
3672 {
3673 cc = *name_end;
3674 *name_end = NUL;
3675
3676 /* Normal name or expanded name. */
3677 if (check_changedtick(lp->ll_name))
3678 ret = FAIL;
3679 else
3680 {
3681 di = find_var(lp->ll_name, NULL);
3682 if (di == NULL)
3683 ret = FAIL;
3684 else
3685 {
3686 if (lock)
3687 di->di_flags |= DI_FLAGS_LOCK;
3688 else
3689 di->di_flags &= ~DI_FLAGS_LOCK;
3690 item_lock(&di->di_tv, deep, lock);
3691 }
3692 }
3693 *name_end = cc;
3694 }
3695 else if (lp->ll_range)
3696 {
3697 listitem_T *li = lp->ll_li;
3698
3699 /* (un)lock a range of List items. */
3700 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3701 {
3702 item_lock(&li->li_tv, deep, lock);
3703 li = li->li_next;
3704 ++lp->ll_n1;
3705 }
3706 }
3707 else if (lp->ll_list != NULL)
3708 /* (un)lock a List item. */
3709 item_lock(&lp->ll_li->li_tv, deep, lock);
3710 else
3711 /* un(lock) a Dictionary item. */
3712 item_lock(&lp->ll_di->di_tv, deep, lock);
3713
3714 return ret;
3715}
3716
3717/*
3718 * Lock or unlock an item. "deep" is nr of levels to go.
3719 */
3720 static void
3721item_lock(tv, deep, lock)
3722 typval_T *tv;
3723 int deep;
3724 int lock;
3725{
3726 static int recurse = 0;
3727 list_T *l;
3728 listitem_T *li;
3729 dict_T *d;
3730 hashitem_T *hi;
3731 int todo;
3732
3733 if (recurse >= DICT_MAXNEST)
3734 {
3735 EMSG(_("E743: variable nested too deep for (un)lock"));
3736 return;
3737 }
3738 if (deep == 0)
3739 return;
3740 ++recurse;
3741
3742 /* lock/unlock the item itself */
3743 if (lock)
3744 tv->v_lock |= VAR_LOCKED;
3745 else
3746 tv->v_lock &= ~VAR_LOCKED;
3747
3748 switch (tv->v_type)
3749 {
3750 case VAR_LIST:
3751 if ((l = tv->vval.v_list) != NULL)
3752 {
3753 if (lock)
3754 l->lv_lock |= VAR_LOCKED;
3755 else
3756 l->lv_lock &= ~VAR_LOCKED;
3757 if (deep < 0 || deep > 1)
3758 /* recursive: lock/unlock the items the List contains */
3759 for (li = l->lv_first; li != NULL; li = li->li_next)
3760 item_lock(&li->li_tv, deep - 1, lock);
3761 }
3762 break;
3763 case VAR_DICT:
3764 if ((d = tv->vval.v_dict) != NULL)
3765 {
3766 if (lock)
3767 d->dv_lock |= VAR_LOCKED;
3768 else
3769 d->dv_lock &= ~VAR_LOCKED;
3770 if (deep < 0 || deep > 1)
3771 {
3772 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003773 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003774 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3775 {
3776 if (!HASHITEM_EMPTY(hi))
3777 {
3778 --todo;
3779 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3780 }
3781 }
3782 }
3783 }
3784 }
3785 --recurse;
3786}
3787
Bram Moolenaara40058a2005-07-11 22:42:07 +00003788/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003789 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3790 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003791 */
3792 static int
3793tv_islocked(tv)
3794 typval_T *tv;
3795{
3796 return (tv->v_lock & VAR_LOCKED)
3797 || (tv->v_type == VAR_LIST
3798 && tv->vval.v_list != NULL
3799 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3800 || (tv->v_type == VAR_DICT
3801 && tv->vval.v_dict != NULL
3802 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3803}
3804
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3806/*
3807 * Delete all "menutrans_" variables.
3808 */
3809 void
3810del_menutrans_vars()
3811{
Bram Moolenaar33570922005-01-25 22:26:29 +00003812 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003813 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814
Bram Moolenaar33570922005-01-25 22:26:29 +00003815 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003816 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003817 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003818 {
3819 if (!HASHITEM_EMPTY(hi))
3820 {
3821 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003822 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3823 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003824 }
3825 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003826 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827}
3828#endif
3829
3830#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3831
3832/*
3833 * Local string buffer for the next two functions to store a variable name
3834 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3835 * get_user_var_name().
3836 */
3837
3838static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3839
3840static char_u *varnamebuf = NULL;
3841static int varnamebuflen = 0;
3842
3843/*
3844 * Function to concatenate a prefix and a variable name.
3845 */
3846 static char_u *
3847cat_prefix_varname(prefix, name)
3848 int prefix;
3849 char_u *name;
3850{
3851 int len;
3852
3853 len = (int)STRLEN(name) + 3;
3854 if (len > varnamebuflen)
3855 {
3856 vim_free(varnamebuf);
3857 len += 10; /* some additional space */
3858 varnamebuf = alloc(len);
3859 if (varnamebuf == NULL)
3860 {
3861 varnamebuflen = 0;
3862 return NULL;
3863 }
3864 varnamebuflen = len;
3865 }
3866 *varnamebuf = prefix;
3867 varnamebuf[1] = ':';
3868 STRCPY(varnamebuf + 2, name);
3869 return varnamebuf;
3870}
3871
3872/*
3873 * Function given to ExpandGeneric() to obtain the list of user defined
3874 * (global/buffer/window/built-in) variable names.
3875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 char_u *
3877get_user_var_name(xp, idx)
3878 expand_T *xp;
3879 int idx;
3880{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003881 static long_u gdone;
3882 static long_u bdone;
3883 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003884#ifdef FEAT_WINDOWS
3885 static long_u tdone;
3886#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003887 static int vidx;
3888 static hashitem_T *hi;
3889 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890
3891 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003892 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003893 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003894#ifdef FEAT_WINDOWS
3895 tdone = 0;
3896#endif
3897 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003898
3899 /* Global variables */
3900 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003902 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003903 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003904 else
3905 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003906 while (HASHITEM_EMPTY(hi))
3907 ++hi;
3908 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3909 return cat_prefix_varname('g', hi->hi_key);
3910 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003912
3913 /* b: variables */
3914 ht = &curbuf->b_vars.dv_hashtab;
3915 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003917 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003918 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003919 else
3920 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003921 while (HASHITEM_EMPTY(hi))
3922 ++hi;
3923 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003925 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003927 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 return (char_u *)"b:changedtick";
3929 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003930
3931 /* w: variables */
3932 ht = &curwin->w_vars.dv_hashtab;
3933 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003935 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003937 else
3938 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003939 while (HASHITEM_EMPTY(hi))
3940 ++hi;
3941 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003943
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003944#ifdef FEAT_WINDOWS
3945 /* t: variables */
3946 ht = &curtab->tp_vars.dv_hashtab;
3947 if (tdone < ht->ht_used)
3948 {
3949 if (tdone++ == 0)
3950 hi = ht->ht_array;
3951 else
3952 ++hi;
3953 while (HASHITEM_EMPTY(hi))
3954 ++hi;
3955 return cat_prefix_varname('t', hi->hi_key);
3956 }
3957#endif
3958
Bram Moolenaar33570922005-01-25 22:26:29 +00003959 /* v: variables */
3960 if (vidx < VV_LEN)
3961 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962
3963 vim_free(varnamebuf);
3964 varnamebuf = NULL;
3965 varnamebuflen = 0;
3966 return NULL;
3967}
3968
3969#endif /* FEAT_CMDL_COMPL */
3970
3971/*
3972 * types for expressions.
3973 */
3974typedef enum
3975{
3976 TYPE_UNKNOWN = 0
3977 , TYPE_EQUAL /* == */
3978 , TYPE_NEQUAL /* != */
3979 , TYPE_GREATER /* > */
3980 , TYPE_GEQUAL /* >= */
3981 , TYPE_SMALLER /* < */
3982 , TYPE_SEQUAL /* <= */
3983 , TYPE_MATCH /* =~ */
3984 , TYPE_NOMATCH /* !~ */
3985} exptype_T;
3986
3987/*
3988 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003989 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3991 */
3992
3993/*
3994 * Handle zero level expression.
3995 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003996 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003997 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 * Return OK or FAIL.
3999 */
4000 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004001eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 char_u **nextcmd;
4005 int evaluate;
4006{
4007 int ret;
4008 char_u *p;
4009
4010 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004011 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 if (ret == FAIL || !ends_excmd(*p))
4013 {
4014 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004015 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 /*
4017 * Report the invalid expression unless the expression evaluation has
4018 * been cancelled due to an aborting error, an interrupt, or an
4019 * exception.
4020 */
4021 if (!aborting())
4022 EMSG2(_(e_invexpr2), arg);
4023 ret = FAIL;
4024 }
4025 if (nextcmd != NULL)
4026 *nextcmd = check_nextcmd(p);
4027
4028 return ret;
4029}
4030
4031/*
4032 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004033 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 *
4035 * "arg" must point to the first non-white of the expression.
4036 * "arg" is advanced to the next non-white after the recognized expression.
4037 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004038 * Note: "rettv.v_lock" is not set.
4039 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 * Return OK or FAIL.
4041 */
4042 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 int evaluate;
4047{
4048 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004049 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050
4051 /*
4052 * Get the first variable.
4053 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004054 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 return FAIL;
4056
4057 if ((*arg)[0] == '?')
4058 {
4059 result = FALSE;
4060 if (evaluate)
4061 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004062 int error = FALSE;
4063
4064 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004066 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004067 if (error)
4068 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
4070
4071 /*
4072 * Get the second variable.
4073 */
4074 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004075 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 return FAIL;
4077
4078 /*
4079 * Check for the ":".
4080 */
4081 if ((*arg)[0] != ':')
4082 {
4083 EMSG(_("E109: Missing ':' after '?'"));
4084 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004085 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 return FAIL;
4087 }
4088
4089 /*
4090 * Get the third variable.
4091 */
4092 *arg = skipwhite(*arg + 1);
4093 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4094 {
4095 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004096 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 return FAIL;
4098 }
4099 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004100 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
4102
4103 return OK;
4104}
4105
4106/*
4107 * Handle first level expression:
4108 * expr2 || expr2 || expr2 logical OR
4109 *
4110 * "arg" must point to the first non-white of the expression.
4111 * "arg" is advanced to the next non-white after the recognized expression.
4112 *
4113 * Return OK or FAIL.
4114 */
4115 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 int evaluate;
4120{
Bram Moolenaar33570922005-01-25 22:26:29 +00004121 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 long result;
4123 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004124 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125
4126 /*
4127 * Get the first variable.
4128 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 return FAIL;
4131
4132 /*
4133 * Repeat until there is no following "||".
4134 */
4135 first = TRUE;
4136 result = FALSE;
4137 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4138 {
4139 if (evaluate && first)
4140 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004141 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004144 if (error)
4145 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 first = FALSE;
4147 }
4148
4149 /*
4150 * Get the second variable.
4151 */
4152 *arg = skipwhite(*arg + 2);
4153 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4154 return FAIL;
4155
4156 /*
4157 * Compute the result.
4158 */
4159 if (evaluate && !result)
4160 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004161 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004163 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004164 if (error)
4165 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 }
4167 if (evaluate)
4168 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169 rettv->v_type = VAR_NUMBER;
4170 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 }
4172 }
4173
4174 return OK;
4175}
4176
4177/*
4178 * Handle second level expression:
4179 * expr3 && expr3 && expr3 logical AND
4180 *
4181 * "arg" must point to the first non-white of the expression.
4182 * "arg" is advanced to the next non-white after the recognized expression.
4183 *
4184 * Return OK or FAIL.
4185 */
4186 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 int evaluate;
4191{
Bram Moolenaar33570922005-01-25 22:26:29 +00004192 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 long result;
4194 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004195 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196
4197 /*
4198 * Get the first variable.
4199 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 return FAIL;
4202
4203 /*
4204 * Repeat until there is no following "&&".
4205 */
4206 first = TRUE;
4207 result = TRUE;
4208 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4209 {
4210 if (evaluate && first)
4211 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004212 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004214 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004215 if (error)
4216 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 first = FALSE;
4218 }
4219
4220 /*
4221 * Get the second variable.
4222 */
4223 *arg = skipwhite(*arg + 2);
4224 if (eval4(arg, &var2, evaluate && result) == FAIL)
4225 return FAIL;
4226
4227 /*
4228 * Compute the result.
4229 */
4230 if (evaluate && result)
4231 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004232 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004235 if (error)
4236 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 }
4238 if (evaluate)
4239 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004240 rettv->v_type = VAR_NUMBER;
4241 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243 }
4244
4245 return OK;
4246}
4247
4248/*
4249 * Handle third level expression:
4250 * var1 == var2
4251 * var1 =~ var2
4252 * var1 != var2
4253 * var1 !~ var2
4254 * var1 > var2
4255 * var1 >= var2
4256 * var1 < var2
4257 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004258 * var1 is var2
4259 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 *
4261 * "arg" must point to the first non-white of the expression.
4262 * "arg" is advanced to the next non-white after the recognized expression.
4263 *
4264 * Return OK or FAIL.
4265 */
4266 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004267eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 int evaluate;
4271{
Bram Moolenaar33570922005-01-25 22:26:29 +00004272 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 char_u *p;
4274 int i;
4275 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004276 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 int len = 2;
4278 long n1, n2;
4279 char_u *s1, *s2;
4280 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4281 regmatch_T regmatch;
4282 int ic;
4283 char_u *save_cpo;
4284
4285 /*
4286 * Get the first variable.
4287 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 return FAIL;
4290
4291 p = *arg;
4292 switch (p[0])
4293 {
4294 case '=': if (p[1] == '=')
4295 type = TYPE_EQUAL;
4296 else if (p[1] == '~')
4297 type = TYPE_MATCH;
4298 break;
4299 case '!': if (p[1] == '=')
4300 type = TYPE_NEQUAL;
4301 else if (p[1] == '~')
4302 type = TYPE_NOMATCH;
4303 break;
4304 case '>': if (p[1] != '=')
4305 {
4306 type = TYPE_GREATER;
4307 len = 1;
4308 }
4309 else
4310 type = TYPE_GEQUAL;
4311 break;
4312 case '<': if (p[1] != '=')
4313 {
4314 type = TYPE_SMALLER;
4315 len = 1;
4316 }
4317 else
4318 type = TYPE_SEQUAL;
4319 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004320 case 'i': if (p[1] == 's')
4321 {
4322 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4323 len = 5;
4324 if (!vim_isIDc(p[len]))
4325 {
4326 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4327 type_is = TRUE;
4328 }
4329 }
4330 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 }
4332
4333 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004334 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 */
4336 if (type != TYPE_UNKNOWN)
4337 {
4338 /* extra question mark appended: ignore case */
4339 if (p[len] == '?')
4340 {
4341 ic = TRUE;
4342 ++len;
4343 }
4344 /* extra '#' appended: match case */
4345 else if (p[len] == '#')
4346 {
4347 ic = FALSE;
4348 ++len;
4349 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004350 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 else
4352 ic = p_ic;
4353
4354 /*
4355 * Get the second variable.
4356 */
4357 *arg = skipwhite(p + len);
4358 if (eval5(arg, &var2, evaluate) == FAIL)
4359 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004360 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 return FAIL;
4362 }
4363
4364 if (evaluate)
4365 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004366 if (type_is && rettv->v_type != var2.v_type)
4367 {
4368 /* For "is" a different type always means FALSE, for "notis"
4369 * it means TRUE. */
4370 n1 = (type == TYPE_NEQUAL);
4371 }
4372 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4373 {
4374 if (type_is)
4375 {
4376 n1 = (rettv->v_type == var2.v_type
4377 && rettv->vval.v_list == var2.vval.v_list);
4378 if (type == TYPE_NEQUAL)
4379 n1 = !n1;
4380 }
4381 else if (rettv->v_type != var2.v_type
4382 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4383 {
4384 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004385 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004386 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004387 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004388 clear_tv(rettv);
4389 clear_tv(&var2);
4390 return FAIL;
4391 }
4392 else
4393 {
4394 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004395 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4396 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004397 if (type == TYPE_NEQUAL)
4398 n1 = !n1;
4399 }
4400 }
4401
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004402 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4403 {
4404 if (type_is)
4405 {
4406 n1 = (rettv->v_type == var2.v_type
4407 && rettv->vval.v_dict == var2.vval.v_dict);
4408 if (type == TYPE_NEQUAL)
4409 n1 = !n1;
4410 }
4411 else if (rettv->v_type != var2.v_type
4412 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4413 {
4414 if (rettv->v_type != var2.v_type)
4415 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4416 else
4417 EMSG(_("E736: Invalid operation for Dictionary"));
4418 clear_tv(rettv);
4419 clear_tv(&var2);
4420 return FAIL;
4421 }
4422 else
4423 {
4424 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004425 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4426 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004427 if (type == TYPE_NEQUAL)
4428 n1 = !n1;
4429 }
4430 }
4431
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004432 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4433 {
4434 if (rettv->v_type != var2.v_type
4435 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4436 {
4437 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004438 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004439 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004440 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004441 clear_tv(rettv);
4442 clear_tv(&var2);
4443 return FAIL;
4444 }
4445 else
4446 {
4447 /* Compare two Funcrefs for being equal or unequal. */
4448 if (rettv->vval.v_string == NULL
4449 || var2.vval.v_string == NULL)
4450 n1 = FALSE;
4451 else
4452 n1 = STRCMP(rettv->vval.v_string,
4453 var2.vval.v_string) == 0;
4454 if (type == TYPE_NEQUAL)
4455 n1 = !n1;
4456 }
4457 }
4458
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004459#ifdef FEAT_FLOAT
4460 /*
4461 * If one of the two variables is a float, compare as a float.
4462 * When using "=~" or "!~", always compare as string.
4463 */
4464 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4465 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4466 {
4467 float_T f1, f2;
4468
4469 if (rettv->v_type == VAR_FLOAT)
4470 f1 = rettv->vval.v_float;
4471 else
4472 f1 = get_tv_number(rettv);
4473 if (var2.v_type == VAR_FLOAT)
4474 f2 = var2.vval.v_float;
4475 else
4476 f2 = get_tv_number(&var2);
4477 n1 = FALSE;
4478 switch (type)
4479 {
4480 case TYPE_EQUAL: n1 = (f1 == f2); break;
4481 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4482 case TYPE_GREATER: n1 = (f1 > f2); break;
4483 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4484 case TYPE_SMALLER: n1 = (f1 < f2); break;
4485 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4486 case TYPE_UNKNOWN:
4487 case TYPE_MATCH:
4488 case TYPE_NOMATCH: break; /* avoid gcc warning */
4489 }
4490 }
4491#endif
4492
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 /*
4494 * If one of the two variables is a number, compare as a number.
4495 * When using "=~" or "!~", always compare as string.
4496 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004497 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4499 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004500 n1 = get_tv_number(rettv);
4501 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 switch (type)
4503 {
4504 case TYPE_EQUAL: n1 = (n1 == n2); break;
4505 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4506 case TYPE_GREATER: n1 = (n1 > n2); break;
4507 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4508 case TYPE_SMALLER: n1 = (n1 < n2); break;
4509 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4510 case TYPE_UNKNOWN:
4511 case TYPE_MATCH:
4512 case TYPE_NOMATCH: break; /* avoid gcc warning */
4513 }
4514 }
4515 else
4516 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004517 s1 = get_tv_string_buf(rettv, buf1);
4518 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4520 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4521 else
4522 i = 0;
4523 n1 = FALSE;
4524 switch (type)
4525 {
4526 case TYPE_EQUAL: n1 = (i == 0); break;
4527 case TYPE_NEQUAL: n1 = (i != 0); break;
4528 case TYPE_GREATER: n1 = (i > 0); break;
4529 case TYPE_GEQUAL: n1 = (i >= 0); break;
4530 case TYPE_SMALLER: n1 = (i < 0); break;
4531 case TYPE_SEQUAL: n1 = (i <= 0); break;
4532
4533 case TYPE_MATCH:
4534 case TYPE_NOMATCH:
4535 /* avoid 'l' flag in 'cpoptions' */
4536 save_cpo = p_cpo;
4537 p_cpo = (char_u *)"";
4538 regmatch.regprog = vim_regcomp(s2,
4539 RE_MAGIC + RE_STRING);
4540 regmatch.rm_ic = ic;
4541 if (regmatch.regprog != NULL)
4542 {
4543 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4544 vim_free(regmatch.regprog);
4545 if (type == TYPE_NOMATCH)
4546 n1 = !n1;
4547 }
4548 p_cpo = save_cpo;
4549 break;
4550
4551 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4552 }
4553 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004554 clear_tv(rettv);
4555 clear_tv(&var2);
4556 rettv->v_type = VAR_NUMBER;
4557 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 }
4559 }
4560
4561 return OK;
4562}
4563
4564/*
4565 * Handle fourth level expression:
4566 * + number addition
4567 * - number subtraction
4568 * . string concatenation
4569 *
4570 * "arg" must point to the first non-white of the expression.
4571 * "arg" is advanced to the next non-white after the recognized expression.
4572 *
4573 * Return OK or FAIL.
4574 */
4575 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004576eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 int evaluate;
4580{
Bram Moolenaar33570922005-01-25 22:26:29 +00004581 typval_T var2;
4582 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 int op;
4584 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004585#ifdef FEAT_FLOAT
4586 float_T f1 = 0, f2 = 0;
4587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 char_u *s1, *s2;
4589 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4590 char_u *p;
4591
4592 /*
4593 * Get the first variable.
4594 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004595 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 return FAIL;
4597
4598 /*
4599 * Repeat computing, until no '+', '-' or '.' is following.
4600 */
4601 for (;;)
4602 {
4603 op = **arg;
4604 if (op != '+' && op != '-' && op != '.')
4605 break;
4606
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004607 if ((op != '+' || rettv->v_type != VAR_LIST)
4608#ifdef FEAT_FLOAT
4609 && (op == '.' || rettv->v_type != VAR_FLOAT)
4610#endif
4611 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004612 {
4613 /* For "list + ...", an illegal use of the first operand as
4614 * a number cannot be determined before evaluating the 2nd
4615 * operand: if this is also a list, all is ok.
4616 * For "something . ...", "something - ..." or "non-list + ...",
4617 * we know that the first operand needs to be a string or number
4618 * without evaluating the 2nd operand. So check before to avoid
4619 * side effects after an error. */
4620 if (evaluate && get_tv_string_chk(rettv) == NULL)
4621 {
4622 clear_tv(rettv);
4623 return FAIL;
4624 }
4625 }
4626
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 /*
4628 * Get the second variable.
4629 */
4630 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004631 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004633 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 return FAIL;
4635 }
4636
4637 if (evaluate)
4638 {
4639 /*
4640 * Compute the result.
4641 */
4642 if (op == '.')
4643 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004644 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4645 s2 = get_tv_string_buf_chk(&var2, buf2);
4646 if (s2 == NULL) /* type error ? */
4647 {
4648 clear_tv(rettv);
4649 clear_tv(&var2);
4650 return FAIL;
4651 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004652 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004653 clear_tv(rettv);
4654 rettv->v_type = VAR_STRING;
4655 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004657 else if (op == '+' && rettv->v_type == VAR_LIST
4658 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004659 {
4660 /* concatenate Lists */
4661 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4662 &var3) == FAIL)
4663 {
4664 clear_tv(rettv);
4665 clear_tv(&var2);
4666 return FAIL;
4667 }
4668 clear_tv(rettv);
4669 *rettv = var3;
4670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 else
4672 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004673 int error = FALSE;
4674
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004675#ifdef FEAT_FLOAT
4676 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004677 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004678 f1 = rettv->vval.v_float;
4679 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004680 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004681 else
4682#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004683 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004684 n1 = get_tv_number_chk(rettv, &error);
4685 if (error)
4686 {
4687 /* This can only happen for "list + non-list". For
4688 * "non-list + ..." or "something - ...", we returned
4689 * before evaluating the 2nd operand. */
4690 clear_tv(rettv);
4691 return FAIL;
4692 }
4693#ifdef FEAT_FLOAT
4694 if (var2.v_type == VAR_FLOAT)
4695 f1 = n1;
4696#endif
4697 }
4698#ifdef FEAT_FLOAT
4699 if (var2.v_type == VAR_FLOAT)
4700 {
4701 f2 = var2.vval.v_float;
4702 n2 = 0;
4703 }
4704 else
4705#endif
4706 {
4707 n2 = get_tv_number_chk(&var2, &error);
4708 if (error)
4709 {
4710 clear_tv(rettv);
4711 clear_tv(&var2);
4712 return FAIL;
4713 }
4714#ifdef FEAT_FLOAT
4715 if (rettv->v_type == VAR_FLOAT)
4716 f2 = n2;
4717#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004718 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004719 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004720
4721#ifdef FEAT_FLOAT
4722 /* If there is a float on either side the result is a float. */
4723 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4724 {
4725 if (op == '+')
4726 f1 = f1 + f2;
4727 else
4728 f1 = f1 - f2;
4729 rettv->v_type = VAR_FLOAT;
4730 rettv->vval.v_float = f1;
4731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004733#endif
4734 {
4735 if (op == '+')
4736 n1 = n1 + n2;
4737 else
4738 n1 = n1 - n2;
4739 rettv->v_type = VAR_NUMBER;
4740 rettv->vval.v_number = n1;
4741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004743 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 }
4745 }
4746 return OK;
4747}
4748
4749/*
4750 * Handle fifth level expression:
4751 * * number multiplication
4752 * / number division
4753 * % number modulo
4754 *
4755 * "arg" must point to the first non-white of the expression.
4756 * "arg" is advanced to the next non-white after the recognized expression.
4757 *
4758 * Return OK or FAIL.
4759 */
4760 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004761eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004765 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766{
Bram Moolenaar33570922005-01-25 22:26:29 +00004767 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 int op;
4769 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004770#ifdef FEAT_FLOAT
4771 int use_float = FALSE;
4772 float_T f1 = 0, f2;
4773#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004774 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775
4776 /*
4777 * Get the first variable.
4778 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004779 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 return FAIL;
4781
4782 /*
4783 * Repeat computing, until no '*', '/' or '%' is following.
4784 */
4785 for (;;)
4786 {
4787 op = **arg;
4788 if (op != '*' && op != '/' && op != '%')
4789 break;
4790
4791 if (evaluate)
4792 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004793#ifdef FEAT_FLOAT
4794 if (rettv->v_type == VAR_FLOAT)
4795 {
4796 f1 = rettv->vval.v_float;
4797 use_float = TRUE;
4798 n1 = 0;
4799 }
4800 else
4801#endif
4802 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004803 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004804 if (error)
4805 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 }
4807 else
4808 n1 = 0;
4809
4810 /*
4811 * Get the second variable.
4812 */
4813 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004814 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 return FAIL;
4816
4817 if (evaluate)
4818 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004819#ifdef FEAT_FLOAT
4820 if (var2.v_type == VAR_FLOAT)
4821 {
4822 if (!use_float)
4823 {
4824 f1 = n1;
4825 use_float = TRUE;
4826 }
4827 f2 = var2.vval.v_float;
4828 n2 = 0;
4829 }
4830 else
4831#endif
4832 {
4833 n2 = get_tv_number_chk(&var2, &error);
4834 clear_tv(&var2);
4835 if (error)
4836 return FAIL;
4837#ifdef FEAT_FLOAT
4838 if (use_float)
4839 f2 = n2;
4840#endif
4841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842
4843 /*
4844 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004845 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004847#ifdef FEAT_FLOAT
4848 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004850 if (op == '*')
4851 f1 = f1 * f2;
4852 else if (op == '/')
4853 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004854# ifdef VMS
4855 /* VMS crashes on divide by zero, work around it */
4856 if (f2 == 0.0)
4857 {
4858 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004859 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004860 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004861 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004862 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004863 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004864 }
4865 else
4866 f1 = f1 / f2;
4867# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004868 /* We rely on the floating point library to handle divide
4869 * by zero to result in "inf" and not a crash. */
4870 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004871# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004874 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004875 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004876 return FAIL;
4877 }
4878 rettv->v_type = VAR_FLOAT;
4879 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 }
4881 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004884 if (op == '*')
4885 n1 = n1 * n2;
4886 else if (op == '/')
4887 {
4888 if (n2 == 0) /* give an error message? */
4889 {
4890 if (n1 == 0)
4891 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4892 else if (n1 < 0)
4893 n1 = -0x7fffffffL;
4894 else
4895 n1 = 0x7fffffffL;
4896 }
4897 else
4898 n1 = n1 / n2;
4899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901 {
4902 if (n2 == 0) /* give an error message? */
4903 n1 = 0;
4904 else
4905 n1 = n1 % n2;
4906 }
4907 rettv->v_type = VAR_NUMBER;
4908 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 }
4911 }
4912
4913 return OK;
4914}
4915
4916/*
4917 * Handle sixth level expression:
4918 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004919 * "string" string constant
4920 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 * &option-name option value
4922 * @r register contents
4923 * identifier variable value
4924 * function() function call
4925 * $VAR environment variable
4926 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004927 * [expr, expr] List
4928 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 *
4930 * Also handle:
4931 * ! in front logical NOT
4932 * - in front unary minus
4933 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004934 * trailing [] subscript in String or List
4935 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 *
4937 * "arg" must point to the first non-white of the expression.
4938 * "arg" is advanced to the next non-white after the recognized expression.
4939 *
4940 * Return OK or FAIL.
4941 */
4942 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004943eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004947 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 long n;
4950 int len;
4951 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 char_u *start_leader, *end_leader;
4953 int ret = OK;
4954 char_u *alias;
4955
4956 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004957 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004958 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004960 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961
4962 /*
4963 * Skip '!' and '-' characters. They are handled later.
4964 */
4965 start_leader = *arg;
4966 while (**arg == '!' || **arg == '-' || **arg == '+')
4967 *arg = skipwhite(*arg + 1);
4968 end_leader = *arg;
4969
4970 switch (**arg)
4971 {
4972 /*
4973 * Number constant.
4974 */
4975 case '0':
4976 case '1':
4977 case '2':
4978 case '3':
4979 case '4':
4980 case '5':
4981 case '6':
4982 case '7':
4983 case '8':
4984 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004985 {
4986#ifdef FEAT_FLOAT
4987 char_u *p = skipdigits(*arg + 1);
4988 int get_float = FALSE;
4989
4990 /* We accept a float when the format matches
4991 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004992 * strict to avoid backwards compatibility problems.
4993 * Don't look for a float after the "." operator, so that
4994 * ":let vers = 1.2.3" doesn't fail. */
4995 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004997 get_float = TRUE;
4998 p = skipdigits(p + 2);
4999 if (*p == 'e' || *p == 'E')
5000 {
5001 ++p;
5002 if (*p == '-' || *p == '+')
5003 ++p;
5004 if (!vim_isdigit(*p))
5005 get_float = FALSE;
5006 else
5007 p = skipdigits(p + 1);
5008 }
5009 if (ASCII_ISALPHA(*p) || *p == '.')
5010 get_float = FALSE;
5011 }
5012 if (get_float)
5013 {
5014 float_T f;
5015
5016 *arg += string2float(*arg, &f);
5017 if (evaluate)
5018 {
5019 rettv->v_type = VAR_FLOAT;
5020 rettv->vval.v_float = f;
5021 }
5022 }
5023 else
5024#endif
5025 {
5026 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5027 *arg += len;
5028 if (evaluate)
5029 {
5030 rettv->v_type = VAR_NUMBER;
5031 rettv->vval.v_number = n;
5032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 }
5034 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036
5037 /*
5038 * String constant: "string".
5039 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005040 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 break;
5042
5043 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005046 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005047 break;
5048
5049 /*
5050 * List: [expr, expr]
5051 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005052 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 break;
5054
5055 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056 * Dictionary: {key: val, key: val}
5057 */
5058 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5059 break;
5060
5061 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005062 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005064 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 break;
5066
5067 /*
5068 * Environment variable: $VAR.
5069 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005070 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 break;
5072
5073 /*
5074 * Register contents: @r.
5075 */
5076 case '@': ++*arg;
5077 if (evaluate)
5078 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005079 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005080 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 }
5082 if (**arg != NUL)
5083 ++*arg;
5084 break;
5085
5086 /*
5087 * nested expression: (expression).
5088 */
5089 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 if (**arg == ')')
5092 ++*arg;
5093 else if (ret == OK)
5094 {
5095 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005096 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 ret = FAIL;
5098 }
5099 break;
5100
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 break;
5103 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005104
5105 if (ret == NOTDONE)
5106 {
5107 /*
5108 * Must be a variable or function name.
5109 * Can also be a curly-braces kind of name: {expr}.
5110 */
5111 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005112 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005113 if (alias != NULL)
5114 s = alias;
5115
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005116 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005117 ret = FAIL;
5118 else
5119 {
5120 if (**arg == '(') /* recursive! */
5121 {
5122 /* If "s" is the name of a variable of type VAR_FUNC
5123 * use its contents. */
5124 s = deref_func_name(s, &len);
5125
5126 /* Invoke the function. */
5127 ret = get_func_tv(s, len, rettv, arg,
5128 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005129 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005130 /* Stop the expression evaluation when immediately
5131 * aborting on error, or when an interrupt occurred or
5132 * an exception was thrown but not caught. */
5133 if (aborting())
5134 {
5135 if (ret == OK)
5136 clear_tv(rettv);
5137 ret = FAIL;
5138 }
5139 }
5140 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005141 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005142 else
5143 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005144 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005145 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 }
5147
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 *arg = skipwhite(*arg);
5149
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005150 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5151 * expr(expr). */
5152 if (ret == OK)
5153 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154
5155 /*
5156 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5157 */
5158 if (ret == OK && evaluate && end_leader > start_leader)
5159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005160 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005161 int val = 0;
5162#ifdef FEAT_FLOAT
5163 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005164
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005165 if (rettv->v_type == VAR_FLOAT)
5166 f = rettv->vval.v_float;
5167 else
5168#endif
5169 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005170 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005172 clear_tv(rettv);
5173 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005175 else
5176 {
5177 while (end_leader > start_leader)
5178 {
5179 --end_leader;
5180 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005181 {
5182#ifdef FEAT_FLOAT
5183 if (rettv->v_type == VAR_FLOAT)
5184 f = !f;
5185 else
5186#endif
5187 val = !val;
5188 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005189 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005190 {
5191#ifdef FEAT_FLOAT
5192 if (rettv->v_type == VAR_FLOAT)
5193 f = -f;
5194 else
5195#endif
5196 val = -val;
5197 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005198 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005199#ifdef FEAT_FLOAT
5200 if (rettv->v_type == VAR_FLOAT)
5201 {
5202 clear_tv(rettv);
5203 rettv->vval.v_float = f;
5204 }
5205 else
5206#endif
5207 {
5208 clear_tv(rettv);
5209 rettv->v_type = VAR_NUMBER;
5210 rettv->vval.v_number = val;
5211 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 }
5214
5215 return ret;
5216}
5217
5218/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005219 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5220 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005221 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5222 */
5223 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005224eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005225 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005226 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005227 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005228 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005229{
5230 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005231 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005232 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005233 long len = -1;
5234 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005235 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005237
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005238 if (rettv->v_type == VAR_FUNC
5239#ifdef FEAT_FLOAT
5240 || rettv->v_type == VAR_FLOAT
5241#endif
5242 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005244 if (verbose)
5245 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246 return FAIL;
5247 }
5248
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005251 /*
5252 * dict.name
5253 */
5254 key = *arg + 1;
5255 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5256 ;
5257 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260 }
5261 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 /*
5264 * something[idx]
5265 *
5266 * Get the (first) variable from inside the [].
5267 */
5268 *arg = skipwhite(*arg + 1);
5269 if (**arg == ':')
5270 empty1 = TRUE;
5271 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5272 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005273 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5274 {
5275 /* not a number or string */
5276 clear_tv(&var1);
5277 return FAIL;
5278 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279
5280 /*
5281 * Get the second variable from inside the [:].
5282 */
5283 if (**arg == ':')
5284 {
5285 range = TRUE;
5286 *arg = skipwhite(*arg + 1);
5287 if (**arg == ']')
5288 empty2 = TRUE;
5289 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291 if (!empty1)
5292 clear_tv(&var1);
5293 return FAIL;
5294 }
5295 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5296 {
5297 /* not a number or string */
5298 if (!empty1)
5299 clear_tv(&var1);
5300 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005301 return FAIL;
5302 }
5303 }
5304
5305 /* Check for the ']'. */
5306 if (**arg != ']')
5307 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005308 if (verbose)
5309 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005310 clear_tv(&var1);
5311 if (range)
5312 clear_tv(&var2);
5313 return FAIL;
5314 }
5315 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005316 }
5317
5318 if (evaluate)
5319 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005320 n1 = 0;
5321 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005323 n1 = get_tv_number(&var1);
5324 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325 }
5326 if (range)
5327 {
5328 if (empty2)
5329 n2 = -1;
5330 else
5331 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005332 n2 = get_tv_number(&var2);
5333 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005334 }
5335 }
5336
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005337 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005338 {
5339 case VAR_NUMBER:
5340 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005341 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 len = (long)STRLEN(s);
5343 if (range)
5344 {
5345 /* The resulting variable is a substring. If the indexes
5346 * are out of range the result is empty. */
5347 if (n1 < 0)
5348 {
5349 n1 = len + n1;
5350 if (n1 < 0)
5351 n1 = 0;
5352 }
5353 if (n2 < 0)
5354 n2 = len + n2;
5355 else if (n2 >= len)
5356 n2 = len;
5357 if (n1 >= len || n2 < 0 || n1 > n2)
5358 s = NULL;
5359 else
5360 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5361 }
5362 else
5363 {
5364 /* The resulting variable is a string of a single
5365 * character. If the index is too big or negative the
5366 * result is empty. */
5367 if (n1 >= len || n1 < 0)
5368 s = NULL;
5369 else
5370 s = vim_strnsave(s + n1, 1);
5371 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005372 clear_tv(rettv);
5373 rettv->v_type = VAR_STRING;
5374 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 break;
5376
5377 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005378 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 if (n1 < 0)
5380 n1 = len + n1;
5381 if (!empty1 && (n1 < 0 || n1 >= len))
5382 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005383 /* For a range we allow invalid values and return an empty
5384 * list. A list index out of range is an error. */
5385 if (!range)
5386 {
5387 if (verbose)
5388 EMSGN(_(e_listidx), n1);
5389 return FAIL;
5390 }
5391 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392 }
5393 if (range)
5394 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005395 list_T *l;
5396 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397
5398 if (n2 < 0)
5399 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005400 else if (n2 >= len)
5401 n2 = len - 1;
5402 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005403 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 l = list_alloc();
5405 if (l == NULL)
5406 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005407 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 n1 <= n2; ++n1)
5409 {
5410 if (list_append_tv(l, &item->li_tv) == FAIL)
5411 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005412 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413 return FAIL;
5414 }
5415 item = item->li_next;
5416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005417 clear_tv(rettv);
5418 rettv->v_type = VAR_LIST;
5419 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005420 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421 }
5422 else
5423 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005424 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005425 clear_tv(rettv);
5426 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427 }
5428 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005429
5430 case VAR_DICT:
5431 if (range)
5432 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005433 if (verbose)
5434 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005435 if (len == -1)
5436 clear_tv(&var1);
5437 return FAIL;
5438 }
5439 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005440 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005441
5442 if (len == -1)
5443 {
5444 key = get_tv_string(&var1);
5445 if (*key == NUL)
5446 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005447 if (verbose)
5448 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005449 clear_tv(&var1);
5450 return FAIL;
5451 }
5452 }
5453
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005454 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005455
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005456 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005457 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005458 if (len == -1)
5459 clear_tv(&var1);
5460 if (item == NULL)
5461 return FAIL;
5462
5463 copy_tv(&item->di_tv, &var1);
5464 clear_tv(rettv);
5465 *rettv = var1;
5466 }
5467 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 }
5469 }
5470
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 return OK;
5472}
5473
5474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 * Get an option value.
5476 * "arg" points to the '&' or '+' before the option name.
5477 * "arg" is advanced to character after the option name.
5478 * Return OK or FAIL.
5479 */
5480 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005481get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005483 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 int evaluate;
5485{
5486 char_u *option_end;
5487 long numval;
5488 char_u *stringval;
5489 int opt_type;
5490 int c;
5491 int working = (**arg == '+'); /* has("+option") */
5492 int ret = OK;
5493 int opt_flags;
5494
5495 /*
5496 * Isolate the option name and find its value.
5497 */
5498 option_end = find_option_end(arg, &opt_flags);
5499 if (option_end == NULL)
5500 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005501 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 EMSG2(_("E112: Option name missing: %s"), *arg);
5503 return FAIL;
5504 }
5505
5506 if (!evaluate)
5507 {
5508 *arg = option_end;
5509 return OK;
5510 }
5511
5512 c = *option_end;
5513 *option_end = NUL;
5514 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005515 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516
5517 if (opt_type == -3) /* invalid name */
5518 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 EMSG2(_("E113: Unknown option: %s"), *arg);
5521 ret = FAIL;
5522 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005523 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 {
5525 if (opt_type == -2) /* hidden string option */
5526 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005527 rettv->v_type = VAR_STRING;
5528 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 }
5530 else if (opt_type == -1) /* hidden number option */
5531 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005532 rettv->v_type = VAR_NUMBER;
5533 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 }
5535 else if (opt_type == 1) /* number option */
5536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005537 rettv->v_type = VAR_NUMBER;
5538 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 }
5540 else /* string option */
5541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005542 rettv->v_type = VAR_STRING;
5543 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 }
5545 }
5546 else if (working && (opt_type == -2 || opt_type == -1))
5547 ret = FAIL;
5548
5549 *option_end = c; /* put back for error messages */
5550 *arg = option_end;
5551
5552 return ret;
5553}
5554
5555/*
5556 * Allocate a variable for a string constant.
5557 * Return OK or FAIL.
5558 */
5559 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 int evaluate;
5564{
5565 char_u *p;
5566 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 int extra = 0;
5568
5569 /*
5570 * Find the end of the string, skipping backslashed characters.
5571 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005572 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 {
5574 if (*p == '\\' && p[1] != NUL)
5575 {
5576 ++p;
5577 /* A "\<x>" form occupies at least 4 characters, and produces up
5578 * to 6 characters: reserve space for 2 extra */
5579 if (*p == '<')
5580 extra += 2;
5581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
5583
5584 if (*p != '"')
5585 {
5586 EMSG2(_("E114: Missing quote: %s"), *arg);
5587 return FAIL;
5588 }
5589
5590 /* If only parsing, set *arg and return here */
5591 if (!evaluate)
5592 {
5593 *arg = p + 1;
5594 return OK;
5595 }
5596
5597 /*
5598 * Copy the string into allocated memory, handling backslashed
5599 * characters.
5600 */
5601 name = alloc((unsigned)(p - *arg + extra));
5602 if (name == NULL)
5603 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005604 rettv->v_type = VAR_STRING;
5605 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 {
5609 if (*p == '\\')
5610 {
5611 switch (*++p)
5612 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005613 case 'b': *name++ = BS; ++p; break;
5614 case 'e': *name++ = ESC; ++p; break;
5615 case 'f': *name++ = FF; ++p; break;
5616 case 'n': *name++ = NL; ++p; break;
5617 case 'r': *name++ = CAR; ++p; break;
5618 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619
5620 case 'X': /* hex: "\x1", "\x12" */
5621 case 'x':
5622 case 'u': /* Unicode: "\u0023" */
5623 case 'U':
5624 if (vim_isxdigit(p[1]))
5625 {
5626 int n, nr;
5627 int c = toupper(*p);
5628
5629 if (c == 'X')
5630 n = 2;
5631 else
5632 n = 4;
5633 nr = 0;
5634 while (--n >= 0 && vim_isxdigit(p[1]))
5635 {
5636 ++p;
5637 nr = (nr << 4) + hex2nr(*p);
5638 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005639 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640#ifdef FEAT_MBYTE
5641 /* For "\u" store the number according to
5642 * 'encoding'. */
5643 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 else
5646#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005647 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 break;
5650
5651 /* octal: "\1", "\12", "\123" */
5652 case '0':
5653 case '1':
5654 case '2':
5655 case '3':
5656 case '4':
5657 case '5':
5658 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659 case '7': *name = *p++ - '0';
5660 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005662 *name = (*name << 3) + *p++ - '0';
5663 if (*p >= '0' && *p <= '7')
5664 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005666 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 break;
5668
5669 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 if (extra != 0)
5672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005673 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 break;
5675 }
5676 /* FALLTHROUGH */
5677
Bram Moolenaar8c711452005-01-14 21:53:12 +00005678 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 break;
5680 }
5681 }
5682 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005683 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005686 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 *arg = p + 1;
5688
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 return OK;
5690}
5691
5692/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005693 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 * Return OK or FAIL.
5695 */
5696 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005697get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 int evaluate;
5701{
5702 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005703 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005704 int reduce = 0;
5705
5706 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005707 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005708 */
5709 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5710 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005712 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005713 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005714 break;
5715 ++reduce;
5716 ++p;
5717 }
5718 }
5719
Bram Moolenaar8c711452005-01-14 21:53:12 +00005720 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005721 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005722 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005723 return FAIL;
5724 }
5725
Bram Moolenaar8c711452005-01-14 21:53:12 +00005726 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005727 if (!evaluate)
5728 {
5729 *arg = p + 1;
5730 return OK;
5731 }
5732
5733 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005734 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005735 */
5736 str = alloc((unsigned)((p - *arg) - reduce));
5737 if (str == NULL)
5738 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005739 rettv->v_type = VAR_STRING;
5740 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005741
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005743 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005747 break;
5748 ++p;
5749 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005751 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005752 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005753 *arg = p + 1;
5754
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 return OK;
5756}
5757
5758/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759 * Allocate a variable for a List and fill it from "*arg".
5760 * Return OK or FAIL.
5761 */
5762 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005763get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005764 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005765 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005766 int evaluate;
5767{
Bram Moolenaar33570922005-01-25 22:26:29 +00005768 list_T *l = NULL;
5769 typval_T tv;
5770 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005771
5772 if (evaluate)
5773 {
5774 l = list_alloc();
5775 if (l == NULL)
5776 return FAIL;
5777 }
5778
5779 *arg = skipwhite(*arg + 1);
5780 while (**arg != ']' && **arg != NUL)
5781 {
5782 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5783 goto failret;
5784 if (evaluate)
5785 {
5786 item = listitem_alloc();
5787 if (item != NULL)
5788 {
5789 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005790 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005791 list_append(l, item);
5792 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005793 else
5794 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005795 }
5796
5797 if (**arg == ']')
5798 break;
5799 if (**arg != ',')
5800 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802 goto failret;
5803 }
5804 *arg = skipwhite(*arg + 1);
5805 }
5806
5807 if (**arg != ']')
5808 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005810failret:
5811 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005812 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813 return FAIL;
5814 }
5815
5816 *arg = skipwhite(*arg + 1);
5817 if (evaluate)
5818 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005819 rettv->v_type = VAR_LIST;
5820 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821 ++l->lv_refcount;
5822 }
5823
5824 return OK;
5825}
5826
5827/*
5828 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005829 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005830 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005831 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832list_alloc()
5833{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005834 list_T *l;
5835
5836 l = (list_T *)alloc_clear(sizeof(list_T));
5837 if (l != NULL)
5838 {
5839 /* Prepend the list to the list of lists for garbage collection. */
5840 if (first_list != NULL)
5841 first_list->lv_used_prev = l;
5842 l->lv_used_prev = NULL;
5843 l->lv_used_next = first_list;
5844 first_list = l;
5845 }
5846 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847}
5848
5849/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005850 * Allocate an empty list for a return value.
5851 * Returns OK or FAIL.
5852 */
5853 static int
5854rettv_list_alloc(rettv)
5855 typval_T *rettv;
5856{
5857 list_T *l = list_alloc();
5858
5859 if (l == NULL)
5860 return FAIL;
5861
5862 rettv->vval.v_list = l;
5863 rettv->v_type = VAR_LIST;
5864 ++l->lv_refcount;
5865 return OK;
5866}
5867
5868/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005869 * Unreference a list: decrement the reference count and free it when it
5870 * becomes zero.
5871 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005872 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005874 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005875{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005876 if (l != NULL && --l->lv_refcount <= 0)
5877 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878}
5879
5880/*
5881 * Free a list, including all items it points to.
5882 * Ignores the reference count.
5883 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005884 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005885list_free(l, recurse)
5886 list_T *l;
5887 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888{
Bram Moolenaar33570922005-01-25 22:26:29 +00005889 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005891 /* Remove the list from the list of lists for garbage collection. */
5892 if (l->lv_used_prev == NULL)
5893 first_list = l->lv_used_next;
5894 else
5895 l->lv_used_prev->lv_used_next = l->lv_used_next;
5896 if (l->lv_used_next != NULL)
5897 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5898
Bram Moolenaard9fba312005-06-26 22:34:35 +00005899 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005900 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005901 /* Remove the item before deleting it. */
5902 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005903 if (recurse || (item->li_tv.v_type != VAR_LIST
5904 && item->li_tv.v_type != VAR_DICT))
5905 clear_tv(&item->li_tv);
5906 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907 }
5908 vim_free(l);
5909}
5910
5911/*
5912 * Allocate a list item.
5913 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005914 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915listitem_alloc()
5916{
Bram Moolenaar33570922005-01-25 22:26:29 +00005917 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918}
5919
5920/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005921 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922 */
5923 static void
5924listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005925 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005927 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005928 vim_free(item);
5929}
5930
5931/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005932 * Remove a list item from a List and free it. Also clears the value.
5933 */
5934 static void
5935listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005936 list_T *l;
5937 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005938{
5939 list_remove(l, item, item);
5940 listitem_free(item);
5941}
5942
5943/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944 * Get the number of items in a list.
5945 */
5946 static long
5947list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005948 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950 if (l == NULL)
5951 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005952 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953}
5954
5955/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005956 * Return TRUE when two lists have exactly the same values.
5957 */
5958 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005959list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005960 list_T *l1;
5961 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005962 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005963 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005964{
Bram Moolenaar33570922005-01-25 22:26:29 +00005965 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005966
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005967 if (l1 == NULL || l2 == NULL)
5968 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005969 if (l1 == l2)
5970 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005971 if (list_len(l1) != list_len(l2))
5972 return FALSE;
5973
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005974 for (item1 = l1->lv_first, item2 = l2->lv_first;
5975 item1 != NULL && item2 != NULL;
5976 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005977 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005978 return FALSE;
5979 return item1 == NULL && item2 == NULL;
5980}
5981
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005982#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5983 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005984/*
5985 * Return the dictitem that an entry in a hashtable points to.
5986 */
5987 dictitem_T *
5988dict_lookup(hi)
5989 hashitem_T *hi;
5990{
5991 return HI2DI(hi);
5992}
5993#endif
5994
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005995/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005996 * Return TRUE when two dictionaries have exactly the same key/values.
5997 */
5998 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005999dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006000 dict_T *d1;
6001 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006002 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006003 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006004{
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 hashitem_T *hi;
6006 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006007 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006008
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006009 if (d1 == NULL || d2 == NULL)
6010 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006011 if (d1 == d2)
6012 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006013 if (dict_len(d1) != dict_len(d2))
6014 return FALSE;
6015
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006016 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006017 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006018 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006019 if (!HASHITEM_EMPTY(hi))
6020 {
6021 item2 = dict_find(d2, hi->hi_key, -1);
6022 if (item2 == NULL)
6023 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006024 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006025 return FALSE;
6026 --todo;
6027 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006028 }
6029 return TRUE;
6030}
6031
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006032static int tv_equal_recurse_limit;
6033
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006034/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006035 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006036 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006037 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006038 */
6039 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006040tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006041 typval_T *tv1;
6042 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006043 int ic; /* ignore case */
6044 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006045{
6046 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006047 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006048 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006049 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006050
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006051 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006052 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006053
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006054 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006055 * recursiveness to a limit. We guess they are equal then.
6056 * A fixed limit has the problem of still taking an awful long time.
6057 * Reduce the limit every time running into it. That should work fine for
6058 * deeply linked structures that are not recursively linked and catch
6059 * recursiveness quickly. */
6060 if (!recursive)
6061 tv_equal_recurse_limit = 1000;
6062 if (recursive_cnt >= tv_equal_recurse_limit)
6063 {
6064 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006065 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006066 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006067
6068 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006069 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006070 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006071 ++recursive_cnt;
6072 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6073 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006074 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006075
6076 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006077 ++recursive_cnt;
6078 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6079 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006080 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006081
6082 case VAR_FUNC:
6083 return (tv1->vval.v_string != NULL
6084 && tv2->vval.v_string != NULL
6085 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6086
6087 case VAR_NUMBER:
6088 return tv1->vval.v_number == tv2->vval.v_number;
6089
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006090#ifdef FEAT_FLOAT
6091 case VAR_FLOAT:
6092 return tv1->vval.v_float == tv2->vval.v_float;
6093#endif
6094
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006095 case VAR_STRING:
6096 s1 = get_tv_string_buf(tv1, buf1);
6097 s2 = get_tv_string_buf(tv2, buf2);
6098 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006099 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006100
6101 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102 return TRUE;
6103}
6104
6105/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006106 * Locate item with index "n" in list "l" and return it.
6107 * A negative index is counted from the end; -1 is the last item.
6108 * Returns NULL when "n" is out of range.
6109 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006110 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006111list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006112 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006113 long n;
6114{
Bram Moolenaar33570922005-01-25 22:26:29 +00006115 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006116 long idx;
6117
6118 if (l == NULL)
6119 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006120
6121 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006122 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006123 n = l->lv_len + n;
6124
6125 /* Check for index out of range. */
6126 if (n < 0 || n >= l->lv_len)
6127 return NULL;
6128
6129 /* When there is a cached index may start search from there. */
6130 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006131 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006132 if (n < l->lv_idx / 2)
6133 {
6134 /* closest to the start of the list */
6135 item = l->lv_first;
6136 idx = 0;
6137 }
6138 else if (n > (l->lv_idx + l->lv_len) / 2)
6139 {
6140 /* closest to the end of the list */
6141 item = l->lv_last;
6142 idx = l->lv_len - 1;
6143 }
6144 else
6145 {
6146 /* closest to the cached index */
6147 item = l->lv_idx_item;
6148 idx = l->lv_idx;
6149 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006150 }
6151 else
6152 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006153 if (n < l->lv_len / 2)
6154 {
6155 /* closest to the start of the list */
6156 item = l->lv_first;
6157 idx = 0;
6158 }
6159 else
6160 {
6161 /* closest to the end of the list */
6162 item = l->lv_last;
6163 idx = l->lv_len - 1;
6164 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006165 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006166
6167 while (n > idx)
6168 {
6169 /* search forward */
6170 item = item->li_next;
6171 ++idx;
6172 }
6173 while (n < idx)
6174 {
6175 /* search backward */
6176 item = item->li_prev;
6177 --idx;
6178 }
6179
6180 /* cache the used index */
6181 l->lv_idx = idx;
6182 l->lv_idx_item = item;
6183
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006184 return item;
6185}
6186
6187/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006188 * Get list item "l[idx]" as a number.
6189 */
6190 static long
6191list_find_nr(l, idx, errorp)
6192 list_T *l;
6193 long idx;
6194 int *errorp; /* set to TRUE when something wrong */
6195{
6196 listitem_T *li;
6197
6198 li = list_find(l, idx);
6199 if (li == NULL)
6200 {
6201 if (errorp != NULL)
6202 *errorp = TRUE;
6203 return -1L;
6204 }
6205 return get_tv_number_chk(&li->li_tv, errorp);
6206}
6207
6208/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006209 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6210 */
6211 char_u *
6212list_find_str(l, idx)
6213 list_T *l;
6214 long idx;
6215{
6216 listitem_T *li;
6217
6218 li = list_find(l, idx - 1);
6219 if (li == NULL)
6220 {
6221 EMSGN(_(e_listidx), idx);
6222 return NULL;
6223 }
6224 return get_tv_string(&li->li_tv);
6225}
6226
6227/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006228 * Locate "item" list "l" and return its index.
6229 * Returns -1 when "item" is not in the list.
6230 */
6231 static long
6232list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006233 list_T *l;
6234 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006235{
6236 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006237 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006238
6239 if (l == NULL)
6240 return -1;
6241 idx = 0;
6242 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6243 ++idx;
6244 if (li == NULL)
6245 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006246 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006247}
6248
6249/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006250 * Append item "item" to the end of list "l".
6251 */
6252 static void
6253list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006254 list_T *l;
6255 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006256{
6257 if (l->lv_last == NULL)
6258 {
6259 /* empty list */
6260 l->lv_first = item;
6261 l->lv_last = item;
6262 item->li_prev = NULL;
6263 }
6264 else
6265 {
6266 l->lv_last->li_next = item;
6267 item->li_prev = l->lv_last;
6268 l->lv_last = item;
6269 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006270 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006271 item->li_next = NULL;
6272}
6273
6274/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006275 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006276 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006277 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006278 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006279list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006280 list_T *l;
6281 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006282{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006283 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006284
Bram Moolenaar05159a02005-02-26 23:04:13 +00006285 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006287 copy_tv(tv, &li->li_tv);
6288 list_append(l, li);
6289 return OK;
6290}
6291
6292/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006293 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006294 * Return FAIL when out of memory.
6295 */
6296 int
6297list_append_dict(list, dict)
6298 list_T *list;
6299 dict_T *dict;
6300{
6301 listitem_T *li = listitem_alloc();
6302
6303 if (li == NULL)
6304 return FAIL;
6305 li->li_tv.v_type = VAR_DICT;
6306 li->li_tv.v_lock = 0;
6307 li->li_tv.vval.v_dict = dict;
6308 list_append(list, li);
6309 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006310 return OK;
6311}
6312
6313/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006314 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006315 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006316 * Returns FAIL when out of memory.
6317 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006318 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006319list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006320 list_T *l;
6321 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006322 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006323{
6324 listitem_T *li = listitem_alloc();
6325
6326 if (li == NULL)
6327 return FAIL;
6328 list_append(l, li);
6329 li->li_tv.v_type = VAR_STRING;
6330 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006331 if (str == NULL)
6332 li->li_tv.vval.v_string = NULL;
6333 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006334 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006335 return FAIL;
6336 return OK;
6337}
6338
6339/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006340 * Append "n" to list "l".
6341 * Returns FAIL when out of memory.
6342 */
6343 static int
6344list_append_number(l, n)
6345 list_T *l;
6346 varnumber_T n;
6347{
6348 listitem_T *li;
6349
6350 li = listitem_alloc();
6351 if (li == NULL)
6352 return FAIL;
6353 li->li_tv.v_type = VAR_NUMBER;
6354 li->li_tv.v_lock = 0;
6355 li->li_tv.vval.v_number = n;
6356 list_append(l, li);
6357 return OK;
6358}
6359
6360/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006361 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006362 * If "item" is NULL append at the end.
6363 * Return FAIL when out of memory.
6364 */
6365 static int
6366list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006367 list_T *l;
6368 typval_T *tv;
6369 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006370{
Bram Moolenaar33570922005-01-25 22:26:29 +00006371 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006372
6373 if (ni == NULL)
6374 return FAIL;
6375 copy_tv(tv, &ni->li_tv);
6376 if (item == NULL)
6377 /* Append new item at end of list. */
6378 list_append(l, ni);
6379 else
6380 {
6381 /* Insert new item before existing item. */
6382 ni->li_prev = item->li_prev;
6383 ni->li_next = item;
6384 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006385 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006386 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006387 ++l->lv_idx;
6388 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006389 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006390 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006391 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006392 l->lv_idx_item = NULL;
6393 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006394 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006395 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006396 }
6397 return OK;
6398}
6399
6400/*
6401 * Extend "l1" with "l2".
6402 * If "bef" is NULL append at the end, otherwise insert before this item.
6403 * Returns FAIL when out of memory.
6404 */
6405 static int
6406list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 list_T *l1;
6408 list_T *l2;
6409 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006410{
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006412 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006413
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006414 /* We also quit the loop when we have inserted the original item count of
6415 * the list, avoid a hang when we extend a list with itself. */
6416 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006417 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6418 return FAIL;
6419 return OK;
6420}
6421
6422/*
6423 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6424 * Return FAIL when out of memory.
6425 */
6426 static int
6427list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006428 list_T *l1;
6429 list_T *l2;
6430 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006431{
Bram Moolenaar33570922005-01-25 22:26:29 +00006432 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006433
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006434 if (l1 == NULL || l2 == NULL)
6435 return FAIL;
6436
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006437 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006438 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006439 if (l == NULL)
6440 return FAIL;
6441 tv->v_type = VAR_LIST;
6442 tv->vval.v_list = l;
6443
6444 /* append all items from the second list */
6445 return list_extend(l, l2, NULL);
6446}
6447
6448/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006449 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006450 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006451 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006452 * Returns NULL when out of memory.
6453 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006454 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006455list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006456 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006457 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006458 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006459{
Bram Moolenaar33570922005-01-25 22:26:29 +00006460 list_T *copy;
6461 listitem_T *item;
6462 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006463
6464 if (orig == NULL)
6465 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006466
6467 copy = list_alloc();
6468 if (copy != NULL)
6469 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006470 if (copyID != 0)
6471 {
6472 /* Do this before adding the items, because one of the items may
6473 * refer back to this list. */
6474 orig->lv_copyID = copyID;
6475 orig->lv_copylist = copy;
6476 }
6477 for (item = orig->lv_first; item != NULL && !got_int;
6478 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006479 {
6480 ni = listitem_alloc();
6481 if (ni == NULL)
6482 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006483 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006484 {
6485 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6486 {
6487 vim_free(ni);
6488 break;
6489 }
6490 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006491 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006492 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006493 list_append(copy, ni);
6494 }
6495 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006496 if (item != NULL)
6497 {
6498 list_unref(copy);
6499 copy = NULL;
6500 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006501 }
6502
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006503 return copy;
6504}
6505
6506/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006508 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006509 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006510 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006511list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006512 list_T *l;
6513 listitem_T *item;
6514 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006515{
Bram Moolenaar33570922005-01-25 22:26:29 +00006516 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006517
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006518 /* notify watchers */
6519 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006520 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006521 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 list_fix_watch(l, ip);
6523 if (ip == item2)
6524 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006525 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526
6527 if (item2->li_next == NULL)
6528 l->lv_last = item->li_prev;
6529 else
6530 item2->li_next->li_prev = item->li_prev;
6531 if (item->li_prev == NULL)
6532 l->lv_first = item2->li_next;
6533 else
6534 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006535 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006536}
6537
6538/*
6539 * Return an allocated string with the string representation of a list.
6540 * May return NULL.
6541 */
6542 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006543list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006544 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006545 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006546{
6547 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006548
6549 if (tv->vval.v_list == NULL)
6550 return NULL;
6551 ga_init2(&ga, (int)sizeof(char), 80);
6552 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006553 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006554 {
6555 vim_free(ga.ga_data);
6556 return NULL;
6557 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006558 ga_append(&ga, ']');
6559 ga_append(&ga, NUL);
6560 return (char_u *)ga.ga_data;
6561}
6562
6563/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006564 * Join list "l" into a string in "*gap", using separator "sep".
6565 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006566 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006567 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006568 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006569list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006570 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006571 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006572 char_u *sep;
6573 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006574 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006575{
6576 int first = TRUE;
6577 char_u *tofree;
6578 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006579 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006580 char_u *s;
6581
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006582 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006583 {
6584 if (first)
6585 first = FALSE;
6586 else
6587 ga_concat(gap, sep);
6588
6589 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006590 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006591 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006592 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006593 if (s != NULL)
6594 ga_concat(gap, s);
6595 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006596 if (s == NULL)
6597 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006598 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006599 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006600 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006601}
6602
6603/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006604 * Garbage collection for lists and dictionaries.
6605 *
6606 * We use reference counts to be able to free most items right away when they
6607 * are no longer used. But for composite items it's possible that it becomes
6608 * unused while the reference count is > 0: When there is a recursive
6609 * reference. Example:
6610 * :let l = [1, 2, 3]
6611 * :let d = {9: l}
6612 * :let l[1] = d
6613 *
6614 * Since this is quite unusual we handle this with garbage collection: every
6615 * once in a while find out which lists and dicts are not referenced from any
6616 * variable.
6617 *
6618 * Here is a good reference text about garbage collection (refers to Python
6619 * but it applies to all reference-counting mechanisms):
6620 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006621 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006622
6623/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006624 * Do garbage collection for lists and dicts.
6625 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006626 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006627 int
6628garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006629{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006630 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006631 buf_T *buf;
6632 win_T *wp;
6633 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006634 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006635 int did_free;
6636 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006637#ifdef FEAT_WINDOWS
6638 tabpage_T *tp;
6639#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006640
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006641 /* Only do this once. */
6642 want_garbage_collect = FALSE;
6643 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006644 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006645
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006646 /* We advance by two because we add one for items referenced through
6647 * previous_funccal. */
6648 current_copyID += COPYID_INC;
6649 copyID = current_copyID;
6650
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006651 /*
6652 * 1. Go through all accessible variables and mark all lists and dicts
6653 * with copyID.
6654 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006655
6656 /* Don't free variables in the previous_funccal list unless they are only
6657 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006658 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006659 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6660 {
6661 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6662 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6663 }
6664
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006665 /* script-local variables */
6666 for (i = 1; i <= ga_scripts.ga_len; ++i)
6667 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6668
6669 /* buffer-local variables */
6670 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6671 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6672
6673 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006674 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006675 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6676
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006677#ifdef FEAT_WINDOWS
6678 /* tabpage-local variables */
6679 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6680 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6681#endif
6682
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006683 /* global variables */
6684 set_ref_in_ht(&globvarht, copyID);
6685
6686 /* function-local variables */
6687 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6688 {
6689 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6690 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6691 }
6692
Bram Moolenaard812df62008-11-09 12:46:09 +00006693 /* v: vars */
6694 set_ref_in_ht(&vimvarht, copyID);
6695
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006696 /*
6697 * 2. Free lists and dictionaries that are not referenced.
6698 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006699 did_free = free_unref_items(copyID);
6700
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006701 /*
6702 * 3. Check if any funccal can be freed now.
6703 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006704 for (pfc = &previous_funccal; *pfc != NULL; )
6705 {
6706 if (can_free_funccal(*pfc, copyID))
6707 {
6708 fc = *pfc;
6709 *pfc = fc->caller;
6710 free_funccal(fc, TRUE);
6711 did_free = TRUE;
6712 did_free_funccal = TRUE;
6713 }
6714 else
6715 pfc = &(*pfc)->caller;
6716 }
6717 if (did_free_funccal)
6718 /* When a funccal was freed some more items might be garbage
6719 * collected, so run again. */
6720 (void)garbage_collect();
6721
6722 return did_free;
6723}
6724
6725/*
6726 * Free lists and dictionaries that are no longer referenced.
6727 */
6728 static int
6729free_unref_items(copyID)
6730 int copyID;
6731{
6732 dict_T *dd;
6733 list_T *ll;
6734 int did_free = FALSE;
6735
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006736 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006737 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006738 */
6739 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006740 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006741 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006742 /* Free the Dictionary and ordinary items it contains, but don't
6743 * recurse into Lists and Dictionaries, they will be in the list
6744 * of dicts or list of lists. */
6745 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006746 did_free = TRUE;
6747
6748 /* restart, next dict may also have been freed */
6749 dd = first_dict;
6750 }
6751 else
6752 dd = dd->dv_used_next;
6753
6754 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006755 * Go through the list of lists and free items without the copyID.
6756 * But don't free a list that has a watcher (used in a for loop), these
6757 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006758 */
6759 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006760 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6761 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006762 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006763 /* Free the List and ordinary items it contains, but don't recurse
6764 * into Lists and Dictionaries, they will be in the list of dicts
6765 * or list of lists. */
6766 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006767 did_free = TRUE;
6768
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006769 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 ll = first_list;
6771 }
6772 else
6773 ll = ll->lv_used_next;
6774
6775 return did_free;
6776}
6777
6778/*
6779 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6780 */
6781 static void
6782set_ref_in_ht(ht, copyID)
6783 hashtab_T *ht;
6784 int copyID;
6785{
6786 int todo;
6787 hashitem_T *hi;
6788
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006789 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006790 for (hi = ht->ht_array; todo > 0; ++hi)
6791 if (!HASHITEM_EMPTY(hi))
6792 {
6793 --todo;
6794 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6795 }
6796}
6797
6798/*
6799 * Mark all lists and dicts referenced through list "l" with "copyID".
6800 */
6801 static void
6802set_ref_in_list(l, copyID)
6803 list_T *l;
6804 int copyID;
6805{
6806 listitem_T *li;
6807
6808 for (li = l->lv_first; li != NULL; li = li->li_next)
6809 set_ref_in_item(&li->li_tv, copyID);
6810}
6811
6812/*
6813 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6814 */
6815 static void
6816set_ref_in_item(tv, copyID)
6817 typval_T *tv;
6818 int copyID;
6819{
6820 dict_T *dd;
6821 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006822
6823 switch (tv->v_type)
6824 {
6825 case VAR_DICT:
6826 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006827 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006828 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006829 /* Didn't see this dict yet. */
6830 dd->dv_copyID = copyID;
6831 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006832 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006833 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006834
6835 case VAR_LIST:
6836 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006837 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006838 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006839 /* Didn't see this list yet. */
6840 ll->lv_copyID = copyID;
6841 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006842 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006843 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006844 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006845 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006846}
6847
6848/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006849 * Allocate an empty header for a dictionary.
6850 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006851 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006852dict_alloc()
6853{
Bram Moolenaar33570922005-01-25 22:26:29 +00006854 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006855
Bram Moolenaar33570922005-01-25 22:26:29 +00006856 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006857 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006858 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006859 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006860 if (first_dict != NULL)
6861 first_dict->dv_used_prev = d;
6862 d->dv_used_next = first_dict;
6863 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006864 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006865
Bram Moolenaar33570922005-01-25 22:26:29 +00006866 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006867 d->dv_lock = 0;
6868 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006869 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006870 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006871 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006872}
6873
6874/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006875 * Allocate an empty dict for a return value.
6876 * Returns OK or FAIL.
6877 */
6878 static int
6879rettv_dict_alloc(rettv)
6880 typval_T *rettv;
6881{
6882 dict_T *d = dict_alloc();
6883
6884 if (d == NULL)
6885 return FAIL;
6886
6887 rettv->vval.v_dict = d;
6888 rettv->v_type = VAR_DICT;
6889 ++d->dv_refcount;
6890 return OK;
6891}
6892
6893
6894/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006895 * Unreference a Dictionary: decrement the reference count and free it when it
6896 * becomes zero.
6897 */
6898 static void
6899dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006900 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006901{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006902 if (d != NULL && --d->dv_refcount <= 0)
6903 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006904}
6905
6906/*
6907 * Free a Dictionary, including all items it contains.
6908 * Ignores the reference count.
6909 */
6910 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006911dict_free(d, recurse)
6912 dict_T *d;
6913 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006914{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006915 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006916 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006917 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006918
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919 /* Remove the dict from the list of dicts for garbage collection. */
6920 if (d->dv_used_prev == NULL)
6921 first_dict = d->dv_used_next;
6922 else
6923 d->dv_used_prev->dv_used_next = d->dv_used_next;
6924 if (d->dv_used_next != NULL)
6925 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6926
6927 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006928 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006929 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006930 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006931 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006932 if (!HASHITEM_EMPTY(hi))
6933 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006934 /* Remove the item before deleting it, just in case there is
6935 * something recursive causing trouble. */
6936 di = HI2DI(hi);
6937 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006938 if (recurse || (di->di_tv.v_type != VAR_LIST
6939 && di->di_tv.v_type != VAR_DICT))
6940 clear_tv(&di->di_tv);
6941 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006942 --todo;
6943 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006944 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006945 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006946 vim_free(d);
6947}
6948
6949/*
6950 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006951 * The "key" is copied to the new item.
6952 * Note that the value of the item "di_tv" still needs to be initialized!
6953 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006954 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006955 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006956dictitem_alloc(key)
6957 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006958{
Bram Moolenaar33570922005-01-25 22:26:29 +00006959 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006960
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006961 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006962 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006963 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006964 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006965 di->di_flags = 0;
6966 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006967 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006968}
6969
6970/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006971 * Make a copy of a Dictionary item.
6972 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006973 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006974dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006975 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006976{
Bram Moolenaar33570922005-01-25 22:26:29 +00006977 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006978
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006979 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6980 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006981 if (di != NULL)
6982 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006983 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006984 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006985 copy_tv(&org->di_tv, &di->di_tv);
6986 }
6987 return di;
6988}
6989
6990/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006991 * Remove item "item" from Dictionary "dict" and free it.
6992 */
6993 static void
6994dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006995 dict_T *dict;
6996 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006997{
Bram Moolenaar33570922005-01-25 22:26:29 +00006998 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006999
Bram Moolenaar33570922005-01-25 22:26:29 +00007000 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007001 if (HASHITEM_EMPTY(hi))
7002 EMSG2(_(e_intern2), "dictitem_remove()");
7003 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007004 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007005 dictitem_free(item);
7006}
7007
7008/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007009 * Free a dict item. Also clears the value.
7010 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007011 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007012dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007013 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007014{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007015 clear_tv(&item->di_tv);
7016 vim_free(item);
7017}
7018
7019/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007020 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7021 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007022 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007023 * Returns NULL when out of memory.
7024 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007025 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007026dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007027 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007028 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007029 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007030{
Bram Moolenaar33570922005-01-25 22:26:29 +00007031 dict_T *copy;
7032 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007033 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007034 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007035
7036 if (orig == NULL)
7037 return NULL;
7038
7039 copy = dict_alloc();
7040 if (copy != NULL)
7041 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007042 if (copyID != 0)
7043 {
7044 orig->dv_copyID = copyID;
7045 orig->dv_copydict = copy;
7046 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007047 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007048 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007049 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007050 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007051 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007052 --todo;
7053
7054 di = dictitem_alloc(hi->hi_key);
7055 if (di == NULL)
7056 break;
7057 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007058 {
7059 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7060 copyID) == FAIL)
7061 {
7062 vim_free(di);
7063 break;
7064 }
7065 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007066 else
7067 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7068 if (dict_add(copy, di) == FAIL)
7069 {
7070 dictitem_free(di);
7071 break;
7072 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007073 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007074 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007075
Bram Moolenaare9a41262005-01-15 22:18:47 +00007076 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007077 if (todo > 0)
7078 {
7079 dict_unref(copy);
7080 copy = NULL;
7081 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007082 }
7083
7084 return copy;
7085}
7086
7087/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007088 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007089 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007090 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007091 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007092dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007093 dict_T *d;
7094 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007095{
Bram Moolenaar33570922005-01-25 22:26:29 +00007096 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007097}
7098
Bram Moolenaar8c711452005-01-14 21:53:12 +00007099/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007100 * Add a number or string entry to dictionary "d".
7101 * When "str" is NULL use number "nr", otherwise use "str".
7102 * Returns FAIL when out of memory and when key already exists.
7103 */
7104 int
7105dict_add_nr_str(d, key, nr, str)
7106 dict_T *d;
7107 char *key;
7108 long nr;
7109 char_u *str;
7110{
7111 dictitem_T *item;
7112
7113 item = dictitem_alloc((char_u *)key);
7114 if (item == NULL)
7115 return FAIL;
7116 item->di_tv.v_lock = 0;
7117 if (str == NULL)
7118 {
7119 item->di_tv.v_type = VAR_NUMBER;
7120 item->di_tv.vval.v_number = nr;
7121 }
7122 else
7123 {
7124 item->di_tv.v_type = VAR_STRING;
7125 item->di_tv.vval.v_string = vim_strsave(str);
7126 }
7127 if (dict_add(d, item) == FAIL)
7128 {
7129 dictitem_free(item);
7130 return FAIL;
7131 }
7132 return OK;
7133}
7134
7135/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007136 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007137 * Returns FAIL when out of memory and when key already exists.
7138 */
7139 int
7140dict_add_list(d, key, list)
7141 dict_T *d;
7142 char *key;
7143 list_T *list;
7144{
7145 dictitem_T *item;
7146
7147 item = dictitem_alloc((char_u *)key);
7148 if (item == NULL)
7149 return FAIL;
7150 item->di_tv.v_lock = 0;
7151 item->di_tv.v_type = VAR_LIST;
7152 item->di_tv.vval.v_list = list;
7153 if (dict_add(d, item) == FAIL)
7154 {
7155 dictitem_free(item);
7156 return FAIL;
7157 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007158 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007159 return OK;
7160}
7161
7162/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007163 * Get the number of items in a Dictionary.
7164 */
7165 static long
7166dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007167 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007168{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007169 if (d == NULL)
7170 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007171 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007172}
7173
7174/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175 * Find item "key[len]" in Dictionary "d".
7176 * If "len" is negative use strlen(key).
7177 * Returns NULL when not found.
7178 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007179 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007180dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007182 char_u *key;
7183 int len;
7184{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007185#define AKEYLEN 200
7186 char_u buf[AKEYLEN];
7187 char_u *akey;
7188 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007190
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007191 if (len < 0)
7192 akey = key;
7193 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007194 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007195 tofree = akey = vim_strnsave(key, len);
7196 if (akey == NULL)
7197 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007198 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007199 else
7200 {
7201 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007202 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007203 akey = buf;
7204 }
7205
Bram Moolenaar33570922005-01-25 22:26:29 +00007206 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007207 vim_free(tofree);
7208 if (HASHITEM_EMPTY(hi))
7209 return NULL;
7210 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007211}
7212
7213/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007214 * Get a string item from a dictionary.
7215 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007216 * Returns NULL if the entry doesn't exist or out of memory.
7217 */
7218 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007219get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007220 dict_T *d;
7221 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007222 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007223{
7224 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007225 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007226
7227 di = dict_find(d, key, -1);
7228 if (di == NULL)
7229 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007230 s = get_tv_string(&di->di_tv);
7231 if (save && s != NULL)
7232 s = vim_strsave(s);
7233 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007234}
7235
7236/*
7237 * Get a number item from a dictionary.
7238 * Returns 0 if the entry doesn't exist or out of memory.
7239 */
7240 long
7241get_dict_number(d, key)
7242 dict_T *d;
7243 char_u *key;
7244{
7245 dictitem_T *di;
7246
7247 di = dict_find(d, key, -1);
7248 if (di == NULL)
7249 return 0;
7250 return get_tv_number(&di->di_tv);
7251}
7252
7253/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007254 * Return an allocated string with the string representation of a Dictionary.
7255 * May return NULL.
7256 */
7257 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007258dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007259 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007260 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261{
7262 garray_T ga;
7263 int first = TRUE;
7264 char_u *tofree;
7265 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007266 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007268 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007269 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007271 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272 return NULL;
7273 ga_init2(&ga, (int)sizeof(char), 80);
7274 ga_append(&ga, '{');
7275
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007276 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007277 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007278 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007279 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007280 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007281 --todo;
7282
7283 if (first)
7284 first = FALSE;
7285 else
7286 ga_concat(&ga, (char_u *)", ");
7287
7288 tofree = string_quote(hi->hi_key, FALSE);
7289 if (tofree != NULL)
7290 {
7291 ga_concat(&ga, tofree);
7292 vim_free(tofree);
7293 }
7294 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007295 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007296 if (s != NULL)
7297 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007298 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007299 if (s == NULL)
7300 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007301 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007303 if (todo > 0)
7304 {
7305 vim_free(ga.ga_data);
7306 return NULL;
7307 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007308
7309 ga_append(&ga, '}');
7310 ga_append(&ga, NUL);
7311 return (char_u *)ga.ga_data;
7312}
7313
7314/*
7315 * Allocate a variable for a Dictionary and fill it from "*arg".
7316 * Return OK or FAIL. Returns NOTDONE for {expr}.
7317 */
7318 static int
7319get_dict_tv(arg, rettv, evaluate)
7320 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007322 int evaluate;
7323{
Bram Moolenaar33570922005-01-25 22:26:29 +00007324 dict_T *d = NULL;
7325 typval_T tvkey;
7326 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007327 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007329 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331
7332 /*
7333 * First check if it's not a curly-braces thing: {expr}.
7334 * Must do this without evaluating, otherwise a function may be called
7335 * twice. Unfortunately this means we need to call eval1() twice for the
7336 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007337 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339 if (*start != '}')
7340 {
7341 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7342 return FAIL;
7343 if (*start == '}')
7344 return NOTDONE;
7345 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007346
7347 if (evaluate)
7348 {
7349 d = dict_alloc();
7350 if (d == NULL)
7351 return FAIL;
7352 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 tvkey.v_type = VAR_UNKNOWN;
7354 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007355
7356 *arg = skipwhite(*arg + 1);
7357 while (**arg != '}' && **arg != NUL)
7358 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007359 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007360 goto failret;
7361 if (**arg != ':')
7362 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007363 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007364 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007365 goto failret;
7366 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007367 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007368 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007369 key = get_tv_string_buf_chk(&tvkey, buf);
7370 if (key == NULL || *key == NUL)
7371 {
7372 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7373 if (key != NULL)
7374 EMSG(_(e_emptykey));
7375 clear_tv(&tvkey);
7376 goto failret;
7377 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007378 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007379
7380 *arg = skipwhite(*arg + 1);
7381 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7382 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007383 if (evaluate)
7384 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007385 goto failret;
7386 }
7387 if (evaluate)
7388 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007389 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390 if (item != NULL)
7391 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007392 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007393 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394 clear_tv(&tv);
7395 goto failret;
7396 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007397 item = dictitem_alloc(key);
7398 clear_tv(&tvkey);
7399 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007401 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007402 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007403 if (dict_add(d, item) == FAIL)
7404 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007405 }
7406 }
7407
7408 if (**arg == '}')
7409 break;
7410 if (**arg != ',')
7411 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007412 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413 goto failret;
7414 }
7415 *arg = skipwhite(*arg + 1);
7416 }
7417
7418 if (**arg != '}')
7419 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007420 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007421failret:
7422 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007423 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424 return FAIL;
7425 }
7426
7427 *arg = skipwhite(*arg + 1);
7428 if (evaluate)
7429 {
7430 rettv->v_type = VAR_DICT;
7431 rettv->vval.v_dict = d;
7432 ++d->dv_refcount;
7433 }
7434
7435 return OK;
7436}
7437
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007439 * Return a string with the string representation of a variable.
7440 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007441 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007442 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007443 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007444 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007445 */
7446 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007447echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007448 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007449 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007450 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007451 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007452{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007453 static int recurse = 0;
7454 char_u *r = NULL;
7455
Bram Moolenaar33570922005-01-25 22:26:29 +00007456 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007457 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007458 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007459 *tofree = NULL;
7460 return NULL;
7461 }
7462 ++recurse;
7463
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007464 switch (tv->v_type)
7465 {
7466 case VAR_FUNC:
7467 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007468 r = tv->vval.v_string;
7469 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007470
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007471 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007472 if (tv->vval.v_list == NULL)
7473 {
7474 *tofree = NULL;
7475 r = NULL;
7476 }
7477 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7478 {
7479 *tofree = NULL;
7480 r = (char_u *)"[...]";
7481 }
7482 else
7483 {
7484 tv->vval.v_list->lv_copyID = copyID;
7485 *tofree = list2string(tv, copyID);
7486 r = *tofree;
7487 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007488 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007489
Bram Moolenaar8c711452005-01-14 21:53:12 +00007490 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007491 if (tv->vval.v_dict == NULL)
7492 {
7493 *tofree = NULL;
7494 r = NULL;
7495 }
7496 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7497 {
7498 *tofree = NULL;
7499 r = (char_u *)"{...}";
7500 }
7501 else
7502 {
7503 tv->vval.v_dict->dv_copyID = copyID;
7504 *tofree = dict2string(tv, copyID);
7505 r = *tofree;
7506 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007507 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007508
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007509 case VAR_STRING:
7510 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007511 *tofree = NULL;
7512 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007513 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007514
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007515#ifdef FEAT_FLOAT
7516 case VAR_FLOAT:
7517 *tofree = NULL;
7518 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7519 r = numbuf;
7520 break;
7521#endif
7522
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007523 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007524 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007525 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007526 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007527
7528 --recurse;
7529 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007530}
7531
7532/*
7533 * Return a string with the string representation of a variable.
7534 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7535 * "numbuf" is used for a number.
7536 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007537 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007538 */
7539 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007540tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007541 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007542 char_u **tofree;
7543 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007544 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007545{
7546 switch (tv->v_type)
7547 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007548 case VAR_FUNC:
7549 *tofree = string_quote(tv->vval.v_string, TRUE);
7550 return *tofree;
7551 case VAR_STRING:
7552 *tofree = string_quote(tv->vval.v_string, FALSE);
7553 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007554#ifdef FEAT_FLOAT
7555 case VAR_FLOAT:
7556 *tofree = NULL;
7557 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7558 return numbuf;
7559#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007560 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007561 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007563 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007564 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007565 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007566 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007567 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007568}
7569
7570/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007571 * Return string "str" in ' quotes, doubling ' characters.
7572 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007574 */
7575 static char_u *
7576string_quote(str, function)
7577 char_u *str;
7578 int function;
7579{
Bram Moolenaar33570922005-01-25 22:26:29 +00007580 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007581 char_u *p, *r, *s;
7582
Bram Moolenaar33570922005-01-25 22:26:29 +00007583 len = (function ? 13 : 3);
7584 if (str != NULL)
7585 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007586 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 for (p = str; *p != NUL; mb_ptr_adv(p))
7588 if (*p == '\'')
7589 ++len;
7590 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007591 s = r = alloc(len);
7592 if (r != NULL)
7593 {
7594 if (function)
7595 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007596 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007597 r += 10;
7598 }
7599 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007601 if (str != NULL)
7602 for (p = str; *p != NUL; )
7603 {
7604 if (*p == '\'')
7605 *r++ = '\'';
7606 MB_COPY_CHAR(p, r);
7607 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007609 if (function)
7610 *r++ = ')';
7611 *r++ = NUL;
7612 }
7613 return s;
7614}
7615
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007616#ifdef FEAT_FLOAT
7617/*
7618 * Convert the string "text" to a floating point number.
7619 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7620 * this always uses a decimal point.
7621 * Returns the length of the text that was consumed.
7622 */
7623 static int
7624string2float(text, value)
7625 char_u *text;
7626 float_T *value; /* result stored here */
7627{
7628 char *s = (char *)text;
7629 float_T f;
7630
7631 f = strtod(s, &s);
7632 *value = f;
7633 return (int)((char_u *)s - text);
7634}
7635#endif
7636
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 * Get the value of an environment variable.
7639 * "arg" is pointing to the '$'. It is advanced to after the name.
7640 * If the environment variable was not set, silently assume it is empty.
7641 * Always return OK.
7642 */
7643 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007644get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 int evaluate;
7648{
7649 char_u *string = NULL;
7650 int len;
7651 int cc;
7652 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007653 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654
7655 ++*arg;
7656 name = *arg;
7657 len = get_env_len(arg);
7658 if (evaluate)
7659 {
7660 if (len != 0)
7661 {
7662 cc = name[len];
7663 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007664 /* first try vim_getenv(), fast for normal environment vars */
7665 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007667 {
7668 if (!mustfree)
7669 string = vim_strsave(string);
7670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 else
7672 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007673 if (mustfree)
7674 vim_free(string);
7675
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 /* next try expanding things like $VIM and ${HOME} */
7677 string = expand_env_save(name - 1);
7678 if (string != NULL && *string == '$')
7679 {
7680 vim_free(string);
7681 string = NULL;
7682 }
7683 }
7684 name[len] = cc;
7685 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007686 rettv->v_type = VAR_STRING;
7687 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 }
7689
7690 return OK;
7691}
7692
7693/*
7694 * Array with names and number of arguments of all internal functions
7695 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7696 */
7697static struct fst
7698{
7699 char *f_name; /* function name */
7700 char f_min_argc; /* minimal number of arguments */
7701 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007702 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007703 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704} functions[] =
7705{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007706#ifdef FEAT_FLOAT
7707 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007708 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007709#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007710 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 {"append", 2, 2, f_append},
7712 {"argc", 0, 0, f_argc},
7713 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007714 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007715#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007716 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007717 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007718 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007721 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 {"bufexists", 1, 1, f_bufexists},
7723 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7724 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7725 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7726 {"buflisted", 1, 1, f_buflisted},
7727 {"bufloaded", 1, 1, f_bufloaded},
7728 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007729 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 {"bufwinnr", 1, 1, f_bufwinnr},
7731 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007732 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007733 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007734#ifdef FEAT_FLOAT
7735 {"ceil", 1, 1, f_ceil},
7736#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007737 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 {"char2nr", 1, 1, f_char2nr},
7739 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007740 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007742#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007743 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007744 {"complete_add", 1, 1, f_complete_add},
7745 {"complete_check", 0, 0, f_complete_check},
7746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007748 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007749#ifdef FEAT_FLOAT
7750 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007751 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007752#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007753 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007755 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007756 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 {"delete", 1, 1, f_delete},
7758 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007759 {"diff_filler", 1, 1, f_diff_filler},
7760 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007761 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007763 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 {"eventhandler", 0, 0, f_eventhandler},
7765 {"executable", 1, 1, f_executable},
7766 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007767#ifdef FEAT_FLOAT
7768 {"exp", 1, 1, f_exp},
7769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007771 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007772 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7774 {"filereadable", 1, 1, f_filereadable},
7775 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007776 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007777 {"finddir", 1, 3, f_finddir},
7778 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007779#ifdef FEAT_FLOAT
7780 {"float2nr", 1, 1, f_float2nr},
7781 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007782 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007783#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007784 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 {"fnamemodify", 2, 2, f_fnamemodify},
7786 {"foldclosed", 1, 1, f_foldclosed},
7787 {"foldclosedend", 1, 1, f_foldclosedend},
7788 {"foldlevel", 1, 1, f_foldlevel},
7789 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007790 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007792 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007793 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007794 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007795 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 {"getbufvar", 2, 2, f_getbufvar},
7797 {"getchar", 0, 1, f_getchar},
7798 {"getcharmod", 0, 0, f_getcharmod},
7799 {"getcmdline", 0, 0, f_getcmdline},
7800 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007801 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007803 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007804 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 {"getfsize", 1, 1, f_getfsize},
7806 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007807 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007808 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007809 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007810 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007811 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007812 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007813 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007814 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007816 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007817 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 {"getwinposx", 0, 0, f_getwinposx},
7819 {"getwinposy", 0, 0, f_getwinposy},
7820 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007821 {"glob", 1, 2, f_glob},
7822 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007824 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007825 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007826 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7828 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7829 {"histadd", 2, 2, f_histadd},
7830 {"histdel", 1, 2, f_histdel},
7831 {"histget", 1, 2, f_histget},
7832 {"histnr", 1, 1, f_histnr},
7833 {"hlID", 1, 1, f_hlID},
7834 {"hlexists", 1, 1, f_hlexists},
7835 {"hostname", 0, 0, f_hostname},
7836 {"iconv", 3, 3, f_iconv},
7837 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007838 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007839 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007841 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {"inputrestore", 0, 0, f_inputrestore},
7843 {"inputsave", 0, 0, f_inputsave},
7844 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007845 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007847 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007848 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007849 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007850 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007852 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {"libcall", 3, 3, f_libcall},
7854 {"libcallnr", 3, 3, f_libcallnr},
7855 {"line", 1, 1, f_line},
7856 {"line2byte", 1, 1, f_line2byte},
7857 {"lispindent", 1, 1, f_lispindent},
7858 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007859#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007860 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007861 {"log10", 1, 1, f_log10},
7862#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007863 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007864 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007865 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007866 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007867 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007868 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007869 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007870 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007871 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007872 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007873 {"max", 1, 1, f_max},
7874 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007875#ifdef vim_mkdir
7876 {"mkdir", 1, 3, f_mkdir},
7877#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007878 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007879#ifdef FEAT_MZSCHEME
7880 {"mzeval", 1, 1, f_mzeval},
7881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 {"nextnonblank", 1, 1, f_nextnonblank},
7883 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007884 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007885#ifdef FEAT_FLOAT
7886 {"pow", 2, 2, f_pow},
7887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007889 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007890 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007891 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007892 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007893 {"reltime", 0, 2, f_reltime},
7894 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {"remote_expr", 2, 3, f_remote_expr},
7896 {"remote_foreground", 1, 1, f_remote_foreground},
7897 {"remote_peek", 1, 2, f_remote_peek},
7898 {"remote_read", 1, 1, f_remote_read},
7899 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007900 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007902 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007904 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007905#ifdef FEAT_FLOAT
7906 {"round", 1, 1, f_round},
7907#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007908 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007909 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007910 {"searchpair", 3, 7, f_searchpair},
7911 {"searchpairpos", 3, 7, f_searchpairpos},
7912 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {"server2client", 2, 2, f_server2client},
7914 {"serverlist", 0, 0, f_serverlist},
7915 {"setbufvar", 3, 3, f_setbufvar},
7916 {"setcmdpos", 1, 1, f_setcmdpos},
7917 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007918 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007919 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007920 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007921 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007923 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007924 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007926 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007928#ifdef FEAT_FLOAT
7929 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007930 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007931#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007932 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007933 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007934 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007935 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007936 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007937#ifdef FEAT_FLOAT
7938 {"sqrt", 1, 1, f_sqrt},
7939 {"str2float", 1, 1, f_str2float},
7940#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007941 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007942 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007943 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944#ifdef HAVE_STRFTIME
7945 {"strftime", 1, 2, f_strftime},
7946#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007947 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007948 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {"strlen", 1, 1, f_strlen},
7950 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007951 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007953 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {"submatch", 1, 1, f_submatch},
7955 {"substitute", 4, 4, f_substitute},
7956 {"synID", 3, 3, f_synID},
7957 {"synIDattr", 2, 3, f_synIDattr},
7958 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007959 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007960 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007961 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007962 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007963 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007964 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007965 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007966 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007967#ifdef FEAT_FLOAT
7968 {"tan", 1, 1, f_tan},
7969 {"tanh", 1, 1, f_tanh},
7970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007972 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {"tolower", 1, 1, f_tolower},
7974 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007975 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007976#ifdef FEAT_FLOAT
7977 {"trunc", 1, 1, f_trunc},
7978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007980 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007981 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007982 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {"virtcol", 1, 1, f_virtcol},
7984 {"visualmode", 0, 1, f_visualmode},
7985 {"winbufnr", 1, 1, f_winbufnr},
7986 {"wincol", 0, 0, f_wincol},
7987 {"winheight", 1, 1, f_winheight},
7988 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007989 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007991 {"winrestview", 1, 1, f_winrestview},
7992 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007994 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995};
7996
7997#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7998
7999/*
8000 * Function given to ExpandGeneric() to obtain the list of internal
8001 * or user defined function names.
8002 */
8003 char_u *
8004get_function_name(xp, idx)
8005 expand_T *xp;
8006 int idx;
8007{
8008 static int intidx = -1;
8009 char_u *name;
8010
8011 if (idx == 0)
8012 intidx = -1;
8013 if (intidx < 0)
8014 {
8015 name = get_user_func_name(xp, idx);
8016 if (name != NULL)
8017 return name;
8018 }
8019 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8020 {
8021 STRCPY(IObuff, functions[intidx].f_name);
8022 STRCAT(IObuff, "(");
8023 if (functions[intidx].f_max_argc == 0)
8024 STRCAT(IObuff, ")");
8025 return IObuff;
8026 }
8027
8028 return NULL;
8029}
8030
8031/*
8032 * Function given to ExpandGeneric() to obtain the list of internal or
8033 * user defined variable or function names.
8034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 char_u *
8036get_expr_name(xp, idx)
8037 expand_T *xp;
8038 int idx;
8039{
8040 static int intidx = -1;
8041 char_u *name;
8042
8043 if (idx == 0)
8044 intidx = -1;
8045 if (intidx < 0)
8046 {
8047 name = get_function_name(xp, idx);
8048 if (name != NULL)
8049 return name;
8050 }
8051 return get_user_var_name(xp, ++intidx);
8052}
8053
8054#endif /* FEAT_CMDL_COMPL */
8055
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008056#if defined(EBCDIC) || defined(PROTO)
8057/*
8058 * Compare struct fst by function name.
8059 */
8060 static int
8061compare_func_name(s1, s2)
8062 const void *s1;
8063 const void *s2;
8064{
8065 struct fst *p1 = (struct fst *)s1;
8066 struct fst *p2 = (struct fst *)s2;
8067
8068 return STRCMP(p1->f_name, p2->f_name);
8069}
8070
8071/*
8072 * Sort the function table by function name.
8073 * The sorting of the table above is ASCII dependant.
8074 * On machines using EBCDIC we have to sort it.
8075 */
8076 static void
8077sortFunctions()
8078{
8079 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8080
8081 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8082}
8083#endif
8084
8085
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086/*
8087 * Find internal function in table above.
8088 * Return index, or -1 if not found
8089 */
8090 static int
8091find_internal_func(name)
8092 char_u *name; /* name of the function */
8093{
8094 int first = 0;
8095 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8096 int cmp;
8097 int x;
8098
8099 /*
8100 * Find the function name in the table. Binary search.
8101 */
8102 while (first <= last)
8103 {
8104 x = first + ((unsigned)(last - first) >> 1);
8105 cmp = STRCMP(name, functions[x].f_name);
8106 if (cmp < 0)
8107 last = x - 1;
8108 else if (cmp > 0)
8109 first = x + 1;
8110 else
8111 return x;
8112 }
8113 return -1;
8114}
8115
8116/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008117 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8118 * name it contains, otherwise return "name".
8119 */
8120 static char_u *
8121deref_func_name(name, lenp)
8122 char_u *name;
8123 int *lenp;
8124{
Bram Moolenaar33570922005-01-25 22:26:29 +00008125 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008126 int cc;
8127
8128 cc = name[*lenp];
8129 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008130 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008131 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008132 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008133 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008134 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008135 {
8136 *lenp = 0;
8137 return (char_u *)""; /* just in case */
8138 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008139 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008140 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008141 }
8142
8143 return name;
8144}
8145
8146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 * Allocate a variable for the result of a function.
8148 * Return OK or FAIL.
8149 */
8150 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008151get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8152 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 char_u *name; /* name of the function */
8154 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 char_u **arg; /* argument, pointing to the '(' */
8157 linenr_T firstline; /* first line of range */
8158 linenr_T lastline; /* last line of range */
8159 int *doesrange; /* return: function handled range */
8160 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008161 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162{
8163 char_u *argp;
8164 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008165 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 int argcount = 0; /* number of arguments found */
8167
8168 /*
8169 * Get the arguments.
8170 */
8171 argp = *arg;
8172 while (argcount < MAX_FUNC_ARGS)
8173 {
8174 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8175 if (*argp == ')' || *argp == ',' || *argp == NUL)
8176 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8178 {
8179 ret = FAIL;
8180 break;
8181 }
8182 ++argcount;
8183 if (*argp != ',')
8184 break;
8185 }
8186 if (*argp == ')')
8187 ++argp;
8188 else
8189 ret = FAIL;
8190
8191 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008192 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008193 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008195 {
8196 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008197 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008198 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008199 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201
8202 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008203 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204
8205 *arg = skipwhite(argp);
8206 return ret;
8207}
8208
8209
8210/*
8211 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008212 * Return OK when the function can't be called, FAIL otherwise.
8213 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 */
8215 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008216call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008217 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008218 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008220 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008222 typval_T *argvars; /* vars for arguments, must have "argcount"
8223 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 linenr_T firstline; /* first line of range */
8225 linenr_T lastline; /* last line of range */
8226 int *doesrange; /* return: function handled range */
8227 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008228 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229{
8230 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231#define ERROR_UNKNOWN 0
8232#define ERROR_TOOMANY 1
8233#define ERROR_TOOFEW 2
8234#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008235#define ERROR_DICT 4
8236#define ERROR_NONE 5
8237#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 int error = ERROR_NONE;
8239 int i;
8240 int llen;
8241 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242#define FLEN_FIXED 40
8243 char_u fname_buf[FLEN_FIXED + 1];
8244 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008245 char_u *name;
8246
8247 /* Make a copy of the name, if it comes from a funcref variable it could
8248 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008249 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008250 if (name == NULL)
8251 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252
8253 /*
8254 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8255 * Change <SNR>123_name() to K_SNR 123_name().
8256 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 llen = eval_fname_script(name);
8259 if (llen > 0)
8260 {
8261 fname_buf[0] = K_SPECIAL;
8262 fname_buf[1] = KS_EXTRA;
8263 fname_buf[2] = (int)KE_SNR;
8264 i = 3;
8265 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8266 {
8267 if (current_SID <= 0)
8268 error = ERROR_SCRIPT;
8269 else
8270 {
8271 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8272 i = (int)STRLEN(fname_buf);
8273 }
8274 }
8275 if (i + STRLEN(name + llen) < FLEN_FIXED)
8276 {
8277 STRCPY(fname_buf + i, name + llen);
8278 fname = fname_buf;
8279 }
8280 else
8281 {
8282 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8283 if (fname == NULL)
8284 error = ERROR_OTHER;
8285 else
8286 {
8287 mch_memmove(fname, fname_buf, (size_t)i);
8288 STRCPY(fname + i, name + llen);
8289 }
8290 }
8291 }
8292 else
8293 fname = name;
8294
8295 *doesrange = FALSE;
8296
8297
8298 /* execute the function if no errors detected and executing */
8299 if (evaluate && error == ERROR_NONE)
8300 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008301 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8302 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 error = ERROR_UNKNOWN;
8304
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008305 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {
8307 /*
8308 * User defined function.
8309 */
8310 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008311
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008313 /* Trigger FuncUndefined event, may load the function. */
8314 if (fp == NULL
8315 && apply_autocmds(EVENT_FUNCUNDEFINED,
8316 fname, fname, TRUE, NULL)
8317 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008319 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 fp = find_func(fname);
8321 }
8322#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008323 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008324 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008325 {
8326 /* loaded a package, search for the function again */
8327 fp = find_func(fname);
8328 }
8329
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 if (fp != NULL)
8331 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008332 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008334 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008336 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008338 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008339 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 else
8341 {
8342 /*
8343 * Call the user function.
8344 * Save and restore search patterns, script variables and
8345 * redo buffer.
8346 */
8347 save_search_patterns();
8348 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008349 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008350 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008351 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008352 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8353 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8354 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008355 /* Function was unreferenced while being used, free it
8356 * now. */
8357 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 restoreRedobuff();
8359 restore_search_patterns();
8360 error = ERROR_NONE;
8361 }
8362 }
8363 }
8364 else
8365 {
8366 /*
8367 * Find the function name in the table, call its implementation.
8368 */
8369 i = find_internal_func(fname);
8370 if (i >= 0)
8371 {
8372 if (argcount < functions[i].f_min_argc)
8373 error = ERROR_TOOFEW;
8374 else if (argcount > functions[i].f_max_argc)
8375 error = ERROR_TOOMANY;
8376 else
8377 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008378 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008379 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 error = ERROR_NONE;
8381 }
8382 }
8383 }
8384 /*
8385 * The function call (or "FuncUndefined" autocommand sequence) might
8386 * have been aborted by an error, an interrupt, or an explicitly thrown
8387 * exception that has not been caught so far. This situation can be
8388 * tested for by calling aborting(). For an error in an internal
8389 * function or for the "E132" error in call_user_func(), however, the
8390 * throw point at which the "force_abort" flag (temporarily reset by
8391 * emsg()) is normally updated has not been reached yet. We need to
8392 * update that flag first to make aborting() reliable.
8393 */
8394 update_force_abort();
8395 }
8396 if (error == ERROR_NONE)
8397 ret = OK;
8398
8399 /*
8400 * Report an error unless the argument evaluation or function call has been
8401 * cancelled due to an aborting error, an interrupt, or an exception.
8402 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008403 if (!aborting())
8404 {
8405 switch (error)
8406 {
8407 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008408 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008409 break;
8410 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008411 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008412 break;
8413 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008414 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008415 name);
8416 break;
8417 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008418 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008419 name);
8420 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008421 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008422 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008423 name);
8424 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008425 }
8426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 if (fname != name && fname != fname_buf)
8429 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008430 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431
8432 return ret;
8433}
8434
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008435/*
8436 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008437 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008438 */
8439 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008440emsg_funcname(ermsg, name)
8441 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008442 char_u *name;
8443{
8444 char_u *p;
8445
8446 if (*name == K_SPECIAL)
8447 p = concat_str((char_u *)"<SNR>", name + 3);
8448 else
8449 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008450 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008451 if (p != name)
8452 vim_free(p);
8453}
8454
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008455/*
8456 * Return TRUE for a non-zero Number and a non-empty String.
8457 */
8458 static int
8459non_zero_arg(argvars)
8460 typval_T *argvars;
8461{
8462 return ((argvars[0].v_type == VAR_NUMBER
8463 && argvars[0].vval.v_number != 0)
8464 || (argvars[0].v_type == VAR_STRING
8465 && argvars[0].vval.v_string != NULL
8466 && *argvars[0].vval.v_string != NUL));
8467}
8468
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469/*********************************************
8470 * Implementation of the built-in functions
8471 */
8472
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008473#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008474static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8475
8476/*
8477 * Get the float value of "argvars[0]" into "f".
8478 * Returns FAIL when the argument is not a Number or Float.
8479 */
8480 static int
8481get_float_arg(argvars, f)
8482 typval_T *argvars;
8483 float_T *f;
8484{
8485 if (argvars[0].v_type == VAR_FLOAT)
8486 {
8487 *f = argvars[0].vval.v_float;
8488 return OK;
8489 }
8490 if (argvars[0].v_type == VAR_NUMBER)
8491 {
8492 *f = (float_T)argvars[0].vval.v_number;
8493 return OK;
8494 }
8495 EMSG(_("E808: Number or Float required"));
8496 return FAIL;
8497}
8498
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008499/*
8500 * "abs(expr)" function
8501 */
8502 static void
8503f_abs(argvars, rettv)
8504 typval_T *argvars;
8505 typval_T *rettv;
8506{
8507 if (argvars[0].v_type == VAR_FLOAT)
8508 {
8509 rettv->v_type = VAR_FLOAT;
8510 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8511 }
8512 else
8513 {
8514 varnumber_T n;
8515 int error = FALSE;
8516
8517 n = get_tv_number_chk(&argvars[0], &error);
8518 if (error)
8519 rettv->vval.v_number = -1;
8520 else if (n > 0)
8521 rettv->vval.v_number = n;
8522 else
8523 rettv->vval.v_number = -n;
8524 }
8525}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008526
8527/*
8528 * "acos()" function
8529 */
8530 static void
8531f_acos(argvars, rettv)
8532 typval_T *argvars;
8533 typval_T *rettv;
8534{
8535 float_T f;
8536
8537 rettv->v_type = VAR_FLOAT;
8538 if (get_float_arg(argvars, &f) == OK)
8539 rettv->vval.v_float = acos(f);
8540 else
8541 rettv->vval.v_float = 0.0;
8542}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008543#endif
8544
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008546 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 */
8548 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008549f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008550 typval_T *argvars;
8551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552{
Bram Moolenaar33570922005-01-25 22:26:29 +00008553 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008555 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008556 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008558 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008559 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008560 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008561 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008562 }
8563 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008564 EMSG(_(e_listreq));
8565}
8566
8567/*
8568 * "append(lnum, string/list)" function
8569 */
8570 static void
8571f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008572 typval_T *argvars;
8573 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008574{
8575 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008576 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008577 list_T *l = NULL;
8578 listitem_T *li = NULL;
8579 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008580 long added = 0;
8581
Bram Moolenaar0d660222005-01-07 21:51:51 +00008582 lnum = get_tv_lnum(argvars);
8583 if (lnum >= 0
8584 && lnum <= curbuf->b_ml.ml_line_count
8585 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008586 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008587 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008588 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008589 l = argvars[1].vval.v_list;
8590 if (l == NULL)
8591 return;
8592 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008593 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008594 for (;;)
8595 {
8596 if (l == NULL)
8597 tv = &argvars[1]; /* append a string */
8598 else if (li == NULL)
8599 break; /* end of list */
8600 else
8601 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008602 line = get_tv_string_chk(tv);
8603 if (line == NULL) /* type error */
8604 {
8605 rettv->vval.v_number = 1; /* Failed */
8606 break;
8607 }
8608 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008609 ++added;
8610 if (l == NULL)
8611 break;
8612 li = li->li_next;
8613 }
8614
8615 appended_lines_mark(lnum, added);
8616 if (curwin->w_cursor.lnum > lnum)
8617 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008619 else
8620 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621}
8622
8623/*
8624 * "argc()" function
8625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008627f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008628 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008631 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632}
8633
8634/*
8635 * "argidx()" function
8636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008638f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008639 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008642 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643}
8644
8645/*
8646 * "argv(nr)" function
8647 */
8648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008649f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008650 typval_T *argvars;
8651 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652{
8653 int idx;
8654
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008655 if (argvars[0].v_type != VAR_UNKNOWN)
8656 {
8657 idx = get_tv_number_chk(&argvars[0], NULL);
8658 if (idx >= 0 && idx < ARGCOUNT)
8659 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8660 else
8661 rettv->vval.v_string = NULL;
8662 rettv->v_type = VAR_STRING;
8663 }
8664 else if (rettv_list_alloc(rettv) == OK)
8665 for (idx = 0; idx < ARGCOUNT; ++idx)
8666 list_append_string(rettv->vval.v_list,
8667 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668}
8669
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008670#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008671/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008672 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008673 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008674 static void
8675f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008676 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008677 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008678{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008679 float_T f;
8680
8681 rettv->v_type = VAR_FLOAT;
8682 if (get_float_arg(argvars, &f) == OK)
8683 rettv->vval.v_float = asin(f);
8684 else
8685 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008686}
8687
8688/*
8689 * "atan()" function
8690 */
8691 static void
8692f_atan(argvars, rettv)
8693 typval_T *argvars;
8694 typval_T *rettv;
8695{
8696 float_T f;
8697
8698 rettv->v_type = VAR_FLOAT;
8699 if (get_float_arg(argvars, &f) == OK)
8700 rettv->vval.v_float = atan(f);
8701 else
8702 rettv->vval.v_float = 0.0;
8703}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008704
8705/*
8706 * "atan2()" function
8707 */
8708 static void
8709f_atan2(argvars, rettv)
8710 typval_T *argvars;
8711 typval_T *rettv;
8712{
8713 float_T fx, fy;
8714
8715 rettv->v_type = VAR_FLOAT;
8716 if (get_float_arg(argvars, &fx) == OK
8717 && get_float_arg(&argvars[1], &fy) == OK)
8718 rettv->vval.v_float = atan2(fx, fy);
8719 else
8720 rettv->vval.v_float = 0.0;
8721}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008722#endif
8723
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724/*
8725 * "browse(save, title, initdir, default)" function
8726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008728f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008729 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731{
8732#ifdef FEAT_BROWSE
8733 int save;
8734 char_u *title;
8735 char_u *initdir;
8736 char_u *defname;
8737 char_u buf[NUMBUFLEN];
8738 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008739 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008741 save = get_tv_number_chk(&argvars[0], &error);
8742 title = get_tv_string_chk(&argvars[1]);
8743 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8744 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008746 if (error || title == NULL || initdir == NULL || defname == NULL)
8747 rettv->vval.v_string = NULL;
8748 else
8749 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008750 do_browse(save ? BROWSE_SAVE : 0,
8751 title, defname, NULL, initdir, NULL, curbuf);
8752#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008753 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008754#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008755 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008756}
8757
8758/*
8759 * "browsedir(title, initdir)" function
8760 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008762f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008763 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008764 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008765{
8766#ifdef FEAT_BROWSE
8767 char_u *title;
8768 char_u *initdir;
8769 char_u buf[NUMBUFLEN];
8770
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008771 title = get_tv_string_chk(&argvars[0]);
8772 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008773
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008774 if (title == NULL || initdir == NULL)
8775 rettv->vval.v_string = NULL;
8776 else
8777 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008778 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008780 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008782 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783}
8784
Bram Moolenaar33570922005-01-25 22:26:29 +00008785static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008786
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787/*
8788 * Find a buffer by number or exact name.
8789 */
8790 static buf_T *
8791find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008792 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793{
8794 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008796 if (avar->v_type == VAR_NUMBER)
8797 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008798 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008800 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008801 if (buf == NULL)
8802 {
8803 /* No full path name match, try a match with a URL or a "nofile"
8804 * buffer, these don't use the full path. */
8805 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8806 if (buf->b_fname != NULL
8807 && (path_with_url(buf->b_fname)
8808#ifdef FEAT_QUICKFIX
8809 || bt_nofile(buf)
8810#endif
8811 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008812 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008813 break;
8814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 }
8816 return buf;
8817}
8818
8819/*
8820 * "bufexists(expr)" function
8821 */
8822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008823f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008824 typval_T *argvars;
8825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008827 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828}
8829
8830/*
8831 * "buflisted(expr)" function
8832 */
8833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008834f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008835 typval_T *argvars;
8836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837{
8838 buf_T *buf;
8839
8840 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008841 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842}
8843
8844/*
8845 * "bufloaded(expr)" function
8846 */
8847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008848f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008849 typval_T *argvars;
8850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851{
8852 buf_T *buf;
8853
8854 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008855 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856}
8857
Bram Moolenaar33570922005-01-25 22:26:29 +00008858static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008859
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860/*
8861 * Get buffer by number or pattern.
8862 */
8863 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008864get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008865 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008867 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 int save_magic;
8869 char_u *save_cpo;
8870 buf_T *buf;
8871
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008872 if (tv->v_type == VAR_NUMBER)
8873 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008874 if (tv->v_type != VAR_STRING)
8875 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 if (name == NULL || *name == NUL)
8877 return curbuf;
8878 if (name[0] == '$' && name[1] == NUL)
8879 return lastbuf;
8880
8881 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8882 save_magic = p_magic;
8883 p_magic = TRUE;
8884 save_cpo = p_cpo;
8885 p_cpo = (char_u *)"";
8886
8887 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8888 TRUE, FALSE));
8889
8890 p_magic = save_magic;
8891 p_cpo = save_cpo;
8892
8893 /* If not found, try expanding the name, like done for bufexists(). */
8894 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008895 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896
8897 return buf;
8898}
8899
8900/*
8901 * "bufname(expr)" function
8902 */
8903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008904f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008905 typval_T *argvars;
8906 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907{
8908 buf_T *buf;
8909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008910 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008912 buf = get_buf_tv(&argvars[0]);
8913 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008915 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008917 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 --emsg_off;
8919}
8920
8921/*
8922 * "bufnr(expr)" function
8923 */
8924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008925f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008926 typval_T *argvars;
8927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928{
8929 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008930 int error = FALSE;
8931 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008933 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008935 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008936 --emsg_off;
8937
8938 /* If the buffer isn't found and the second argument is not zero create a
8939 * new buffer. */
8940 if (buf == NULL
8941 && argvars[1].v_type != VAR_UNKNOWN
8942 && get_tv_number_chk(&argvars[1], &error) != 0
8943 && !error
8944 && (name = get_tv_string_chk(&argvars[0])) != NULL
8945 && !error)
8946 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8947
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008949 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008951 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952}
8953
8954/*
8955 * "bufwinnr(nr)" function
8956 */
8957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008959 typval_T *argvars;
8960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961{
8962#ifdef FEAT_WINDOWS
8963 win_T *wp;
8964 int winnr = 0;
8965#endif
8966 buf_T *buf;
8967
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008968 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008970 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971#ifdef FEAT_WINDOWS
8972 for (wp = firstwin; wp; wp = wp->w_next)
8973 {
8974 ++winnr;
8975 if (wp->w_buffer == buf)
8976 break;
8977 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008978 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008980 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981#endif
8982 --emsg_off;
8983}
8984
8985/*
8986 * "byte2line(byte)" function
8987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008989f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008990 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992{
8993#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008994 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995#else
8996 long boff = 0;
8997
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008998 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009000 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009002 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 (linenr_T)0, &boff);
9004#endif
9005}
9006
9007/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009008 * "byteidx()" function
9009 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009011f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009012 typval_T *argvars;
9013 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009014{
9015#ifdef FEAT_MBYTE
9016 char_u *t;
9017#endif
9018 char_u *str;
9019 long idx;
9020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009021 str = get_tv_string_chk(&argvars[0]);
9022 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009023 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009024 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009025 return;
9026
9027#ifdef FEAT_MBYTE
9028 t = str;
9029 for ( ; idx > 0; idx--)
9030 {
9031 if (*t == NUL) /* EOL reached */
9032 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009033 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009034 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009035 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009036#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009037 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009038 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009039#endif
9040}
9041
9042/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009043 * "call(func, arglist)" function
9044 */
9045 static void
9046f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009047 typval_T *argvars;
9048 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009049{
9050 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009051 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009052 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00009053 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009054 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00009055 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009056
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009057 if (argvars[1].v_type != VAR_LIST)
9058 {
9059 EMSG(_(e_listreq));
9060 return;
9061 }
9062 if (argvars[1].vval.v_list == NULL)
9063 return;
9064
9065 if (argvars[0].v_type == VAR_FUNC)
9066 func = argvars[0].vval.v_string;
9067 else
9068 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009069 if (*func == NUL)
9070 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009071
Bram Moolenaare9a41262005-01-15 22:18:47 +00009072 if (argvars[2].v_type != VAR_UNKNOWN)
9073 {
9074 if (argvars[2].v_type != VAR_DICT)
9075 {
9076 EMSG(_(e_dictreq));
9077 return;
9078 }
9079 selfdict = argvars[2].vval.v_dict;
9080 }
9081
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009082 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9083 item = item->li_next)
9084 {
9085 if (argc == MAX_FUNC_ARGS)
9086 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009087 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009088 break;
9089 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009090 /* Make a copy of each argument. This is needed to be able to set
9091 * v_lock to VAR_FIXED in the copy without changing the original list.
9092 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009093 copy_tv(&item->li_tv, &argv[argc++]);
9094 }
9095
9096 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009097 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009098 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9099 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009100
9101 /* Free the arguments. */
9102 while (argc > 0)
9103 clear_tv(&argv[--argc]);
9104}
9105
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009106#ifdef FEAT_FLOAT
9107/*
9108 * "ceil({float})" function
9109 */
9110 static void
9111f_ceil(argvars, rettv)
9112 typval_T *argvars;
9113 typval_T *rettv;
9114{
9115 float_T f;
9116
9117 rettv->v_type = VAR_FLOAT;
9118 if (get_float_arg(argvars, &f) == OK)
9119 rettv->vval.v_float = ceil(f);
9120 else
9121 rettv->vval.v_float = 0.0;
9122}
9123#endif
9124
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009125/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009126 * "changenr()" function
9127 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009128 static void
9129f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009130 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009131 typval_T *rettv;
9132{
9133 rettv->vval.v_number = curbuf->b_u_seq_cur;
9134}
9135
9136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 * "char2nr(string)" function
9138 */
9139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009140f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009141 typval_T *argvars;
9142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143{
9144#ifdef FEAT_MBYTE
9145 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009146 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 else
9148#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009149 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150}
9151
9152/*
9153 * "cindent(lnum)" function
9154 */
9155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009156f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009157 typval_T *argvars;
9158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159{
9160#ifdef FEAT_CINDENT
9161 pos_T pos;
9162 linenr_T lnum;
9163
9164 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009165 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9167 {
9168 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009169 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 curwin->w_cursor = pos;
9171 }
9172 else
9173#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009174 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175}
9176
9177/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009178 * "clearmatches()" function
9179 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009180 static void
9181f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009182 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009183 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009184{
9185#ifdef FEAT_SEARCH_EXTRA
9186 clear_matches(curwin);
9187#endif
9188}
9189
9190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 * "col(string)" function
9192 */
9193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009194f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009195 typval_T *argvars;
9196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197{
9198 colnr_T col = 0;
9199 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009200 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009202 fp = var2fpos(&argvars[0], FALSE, &fnum);
9203 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 {
9205 if (fp->col == MAXCOL)
9206 {
9207 /* '> can be MAXCOL, get the length of the line then */
9208 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009209 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 else
9211 col = MAXCOL;
9212 }
9213 else
9214 {
9215 col = fp->col + 1;
9216#ifdef FEAT_VIRTUALEDIT
9217 /* col(".") when the cursor is on the NUL at the end of the line
9218 * because of "coladd" can be seen as an extra column. */
9219 if (virtual_active() && fp == &curwin->w_cursor)
9220 {
9221 char_u *p = ml_get_cursor();
9222
9223 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9224 curwin->w_virtcol - curwin->w_cursor.coladd))
9225 {
9226# ifdef FEAT_MBYTE
9227 int l;
9228
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009229 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 col += l;
9231# else
9232 if (*p != NUL && p[1] == NUL)
9233 ++col;
9234# endif
9235 }
9236 }
9237#endif
9238 }
9239 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009240 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241}
9242
Bram Moolenaar572cb562005-08-05 21:35:02 +00009243#if defined(FEAT_INS_EXPAND)
9244/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009245 * "complete()" function
9246 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009247 static void
9248f_complete(argvars, rettv)
9249 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009250 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009251{
9252 int startcol;
9253
9254 if ((State & INSERT) == 0)
9255 {
9256 EMSG(_("E785: complete() can only be used in Insert mode"));
9257 return;
9258 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009259
9260 /* Check for undo allowed here, because if something was already inserted
9261 * the line was already saved for undo and this check isn't done. */
9262 if (!undo_allowed())
9263 return;
9264
Bram Moolenaarade00832006-03-10 21:46:58 +00009265 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9266 {
9267 EMSG(_(e_invarg));
9268 return;
9269 }
9270
9271 startcol = get_tv_number_chk(&argvars[0], NULL);
9272 if (startcol <= 0)
9273 return;
9274
9275 set_completion(startcol - 1, argvars[1].vval.v_list);
9276}
9277
9278/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009279 * "complete_add()" function
9280 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009281 static void
9282f_complete_add(argvars, rettv)
9283 typval_T *argvars;
9284 typval_T *rettv;
9285{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009286 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009287}
9288
9289/*
9290 * "complete_check()" function
9291 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009292 static void
9293f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009294 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009295 typval_T *rettv;
9296{
9297 int saved = RedrawingDisabled;
9298
9299 RedrawingDisabled = 0;
9300 ins_compl_check_keys(0);
9301 rettv->vval.v_number = compl_interrupted;
9302 RedrawingDisabled = saved;
9303}
9304#endif
9305
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306/*
9307 * "confirm(message, buttons[, default [, type]])" function
9308 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009310f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009311 typval_T *argvars UNUSED;
9312 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313{
9314#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9315 char_u *message;
9316 char_u *buttons = NULL;
9317 char_u buf[NUMBUFLEN];
9318 char_u buf2[NUMBUFLEN];
9319 int def = 1;
9320 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009321 char_u *typestr;
9322 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009324 message = get_tv_string_chk(&argvars[0]);
9325 if (message == NULL)
9326 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009327 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009329 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9330 if (buttons == NULL)
9331 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009332 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009334 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009335 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009337 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9338 if (typestr == NULL)
9339 error = TRUE;
9340 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009342 switch (TOUPPER_ASC(*typestr))
9343 {
9344 case 'E': type = VIM_ERROR; break;
9345 case 'Q': type = VIM_QUESTION; break;
9346 case 'I': type = VIM_INFO; break;
9347 case 'W': type = VIM_WARNING; break;
9348 case 'G': type = VIM_GENERIC; break;
9349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 }
9351 }
9352 }
9353 }
9354
9355 if (buttons == NULL || *buttons == NUL)
9356 buttons = (char_u *)_("&Ok");
9357
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009358 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009359 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009360 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361#endif
9362}
9363
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009364/*
9365 * "copy()" function
9366 */
9367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009369 typval_T *argvars;
9370 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009371{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009372 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009373}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009375#ifdef FEAT_FLOAT
9376/*
9377 * "cos()" function
9378 */
9379 static void
9380f_cos(argvars, rettv)
9381 typval_T *argvars;
9382 typval_T *rettv;
9383{
9384 float_T f;
9385
9386 rettv->v_type = VAR_FLOAT;
9387 if (get_float_arg(argvars, &f) == OK)
9388 rettv->vval.v_float = cos(f);
9389 else
9390 rettv->vval.v_float = 0.0;
9391}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009392
9393/*
9394 * "cosh()" function
9395 */
9396 static void
9397f_cosh(argvars, rettv)
9398 typval_T *argvars;
9399 typval_T *rettv;
9400{
9401 float_T f;
9402
9403 rettv->v_type = VAR_FLOAT;
9404 if (get_float_arg(argvars, &f) == OK)
9405 rettv->vval.v_float = cosh(f);
9406 else
9407 rettv->vval.v_float = 0.0;
9408}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009409#endif
9410
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009412 * "count()" function
9413 */
9414 static void
9415f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009416 typval_T *argvars;
9417 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009418{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009419 long n = 0;
9420 int ic = FALSE;
9421
Bram Moolenaare9a41262005-01-15 22:18:47 +00009422 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009423 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009424 listitem_T *li;
9425 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009426 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009427
Bram Moolenaare9a41262005-01-15 22:18:47 +00009428 if ((l = argvars[0].vval.v_list) != NULL)
9429 {
9430 li = l->lv_first;
9431 if (argvars[2].v_type != VAR_UNKNOWN)
9432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009433 int error = FALSE;
9434
9435 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009436 if (argvars[3].v_type != VAR_UNKNOWN)
9437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009438 idx = get_tv_number_chk(&argvars[3], &error);
9439 if (!error)
9440 {
9441 li = list_find(l, idx);
9442 if (li == NULL)
9443 EMSGN(_(e_listidx), idx);
9444 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009445 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009446 if (error)
9447 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009448 }
9449
9450 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009451 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009452 ++n;
9453 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009454 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009455 else if (argvars[0].v_type == VAR_DICT)
9456 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009457 int todo;
9458 dict_T *d;
9459 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009460
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009461 if ((d = argvars[0].vval.v_dict) != NULL)
9462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009463 int error = FALSE;
9464
Bram Moolenaare9a41262005-01-15 22:18:47 +00009465 if (argvars[2].v_type != VAR_UNKNOWN)
9466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009467 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009468 if (argvars[3].v_type != VAR_UNKNOWN)
9469 EMSG(_(e_invarg));
9470 }
9471
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009472 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009473 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009474 {
9475 if (!HASHITEM_EMPTY(hi))
9476 {
9477 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009478 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009479 ++n;
9480 }
9481 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009482 }
9483 }
9484 else
9485 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009486 rettv->vval.v_number = n;
9487}
9488
9489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9491 *
9492 * Checks the existence of a cscope connection.
9493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009495f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009496 typval_T *argvars UNUSED;
9497 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498{
9499#ifdef FEAT_CSCOPE
9500 int num = 0;
9501 char_u *dbpath = NULL;
9502 char_u *prepend = NULL;
9503 char_u buf[NUMBUFLEN];
9504
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009505 if (argvars[0].v_type != VAR_UNKNOWN
9506 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009508 num = (int)get_tv_number(&argvars[0]);
9509 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009510 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009511 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 }
9513
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009514 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515#endif
9516}
9517
9518/*
9519 * "cursor(lnum, col)" function
9520 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009521 * Moves the cursor to the specified line and column.
9522 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009525f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009526 typval_T *argvars;
9527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528{
9529 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009530#ifdef FEAT_VIRTUALEDIT
9531 long coladd = 0;
9532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009534 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009535 if (argvars[1].v_type == VAR_UNKNOWN)
9536 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009537 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009538
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009539 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009540 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009541 line = pos.lnum;
9542 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009543#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009544 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009545#endif
9546 }
9547 else
9548 {
9549 line = get_tv_lnum(argvars);
9550 col = get_tv_number_chk(&argvars[1], NULL);
9551#ifdef FEAT_VIRTUALEDIT
9552 if (argvars[2].v_type != VAR_UNKNOWN)
9553 coladd = get_tv_number_chk(&argvars[2], NULL);
9554#endif
9555 }
9556 if (line < 0 || col < 0
9557#ifdef FEAT_VIRTUALEDIT
9558 || coladd < 0
9559#endif
9560 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009561 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 if (line > 0)
9563 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 if (col > 0)
9565 curwin->w_cursor.col = col - 1;
9566#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009567 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568#endif
9569
9570 /* Make sure the cursor is in a valid position. */
9571 check_cursor();
9572#ifdef FEAT_MBYTE
9573 /* Correct cursor for multi-byte character. */
9574 if (has_mbyte)
9575 mb_adjust_cursor();
9576#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009577
9578 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009579 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580}
9581
9582/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009583 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 */
9585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009587 typval_T *argvars;
9588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009590 int noref = 0;
9591
9592 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009593 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009594 if (noref < 0 || noref > 1)
9595 EMSG(_(e_invarg));
9596 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009597 {
9598 current_copyID += COPYID_INC;
9599 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601}
9602
9603/*
9604 * "delete()" function
9605 */
9606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009607f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009608 typval_T *argvars;
9609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610{
9611 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009612 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009614 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615}
9616
9617/*
9618 * "did_filetype()" function
9619 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009621f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009622 typval_T *argvars UNUSED;
9623 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624{
9625#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627#endif
9628}
9629
9630/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009631 * "diff_filler()" function
9632 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009634f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009635 typval_T *argvars UNUSED;
9636 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009637{
9638#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009639 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009640#endif
9641}
9642
9643/*
9644 * "diff_hlID()" function
9645 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009647f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009648 typval_T *argvars UNUSED;
9649 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009650{
9651#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009652 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009653 static linenr_T prev_lnum = 0;
9654 static int changedtick = 0;
9655 static int fnum = 0;
9656 static int change_start = 0;
9657 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009658 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009659 int filler_lines;
9660 int col;
9661
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009662 if (lnum < 0) /* ignore type error in {lnum} arg */
9663 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009664 if (lnum != prev_lnum
9665 || changedtick != curbuf->b_changedtick
9666 || fnum != curbuf->b_fnum)
9667 {
9668 /* New line, buffer, change: need to get the values. */
9669 filler_lines = diff_check(curwin, lnum);
9670 if (filler_lines < 0)
9671 {
9672 if (filler_lines == -1)
9673 {
9674 change_start = MAXCOL;
9675 change_end = -1;
9676 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9677 hlID = HLF_ADD; /* added line */
9678 else
9679 hlID = HLF_CHD; /* changed line */
9680 }
9681 else
9682 hlID = HLF_ADD; /* added line */
9683 }
9684 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009685 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009686 prev_lnum = lnum;
9687 changedtick = curbuf->b_changedtick;
9688 fnum = curbuf->b_fnum;
9689 }
9690
9691 if (hlID == HLF_CHD || hlID == HLF_TXD)
9692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009693 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009694 if (col >= change_start && col <= change_end)
9695 hlID = HLF_TXD; /* changed text */
9696 else
9697 hlID = HLF_CHD; /* changed line */
9698 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009699 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009700#endif
9701}
9702
9703/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009704 * "empty({expr})" function
9705 */
9706 static void
9707f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009708 typval_T *argvars;
9709 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009710{
9711 int n;
9712
9713 switch (argvars[0].v_type)
9714 {
9715 case VAR_STRING:
9716 case VAR_FUNC:
9717 n = argvars[0].vval.v_string == NULL
9718 || *argvars[0].vval.v_string == NUL;
9719 break;
9720 case VAR_NUMBER:
9721 n = argvars[0].vval.v_number == 0;
9722 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009723#ifdef FEAT_FLOAT
9724 case VAR_FLOAT:
9725 n = argvars[0].vval.v_float == 0.0;
9726 break;
9727#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009728 case VAR_LIST:
9729 n = argvars[0].vval.v_list == NULL
9730 || argvars[0].vval.v_list->lv_first == NULL;
9731 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009732 case VAR_DICT:
9733 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009734 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009735 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009736 default:
9737 EMSG2(_(e_intern2), "f_empty()");
9738 n = 0;
9739 }
9740
9741 rettv->vval.v_number = n;
9742}
9743
9744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 * "escape({string}, {chars})" function
9746 */
9747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009748f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009749 typval_T *argvars;
9750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751{
9752 char_u buf[NUMBUFLEN];
9753
Bram Moolenaar758711c2005-02-02 23:11:38 +00009754 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9755 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757}
9758
9759/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009760 * "eval()" function
9761 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009762 static void
9763f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009764 typval_T *argvars;
9765 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009766{
9767 char_u *s;
9768
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009769 s = get_tv_string_chk(&argvars[0]);
9770 if (s != NULL)
9771 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009773 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9774 {
9775 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009776 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009777 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009778 else if (*s != NUL)
9779 EMSG(_(e_trailing));
9780}
9781
9782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 * "eventhandler()" function
9784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009786f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009787 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009790 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791}
9792
9793/*
9794 * "executable()" function
9795 */
9796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009797f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009798 typval_T *argvars;
9799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009801 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802}
9803
9804/*
9805 * "exists()" function
9806 */
9807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009808f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009809 typval_T *argvars;
9810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811{
9812 char_u *p;
9813 char_u *name;
9814 int n = FALSE;
9815 int len = 0;
9816
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009817 no_autoload = TRUE;
9818
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009819 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 if (*p == '$') /* environment variable */
9821 {
9822 /* first try "normal" environment variables (fast) */
9823 if (mch_getenv(p + 1) != NULL)
9824 n = TRUE;
9825 else
9826 {
9827 /* try expanding things like $VIM and ${HOME} */
9828 p = expand_env_save(p);
9829 if (p != NULL && *p != '$')
9830 n = TRUE;
9831 vim_free(p);
9832 }
9833 }
9834 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009836 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009837 if (*skipwhite(p) != NUL)
9838 n = FALSE; /* trailing garbage */
9839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 else if (*p == '*') /* internal or user defined function */
9841 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009842 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 }
9844 else if (*p == ':')
9845 {
9846 n = cmd_exists(p + 1);
9847 }
9848 else if (*p == '#')
9849 {
9850#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009851 if (p[1] == '#')
9852 n = autocmd_supported(p + 2);
9853 else
9854 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855#endif
9856 }
9857 else /* internal variable */
9858 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009859 char_u *tofree;
9860 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009862 /* get_name_len() takes care of expanding curly braces */
9863 name = p;
9864 len = get_name_len(&p, &tofree, TRUE, FALSE);
9865 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009867 if (tofree != NULL)
9868 name = tofree;
9869 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9870 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009872 /* handle d.key, l[idx], f(expr) */
9873 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9874 if (n)
9875 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 }
9877 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009878 if (*p != NUL)
9879 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009881 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 }
9883
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009884 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009885
9886 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887}
9888
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009889#ifdef FEAT_FLOAT
9890/*
9891 * "exp()" function
9892 */
9893 static void
9894f_exp(argvars, rettv)
9895 typval_T *argvars;
9896 typval_T *rettv;
9897{
9898 float_T f;
9899
9900 rettv->v_type = VAR_FLOAT;
9901 if (get_float_arg(argvars, &f) == OK)
9902 rettv->vval.v_float = exp(f);
9903 else
9904 rettv->vval.v_float = 0.0;
9905}
9906#endif
9907
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908/*
9909 * "expand()" function
9910 */
9911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009912f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009913 typval_T *argvars;
9914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915{
9916 char_u *s;
9917 int len;
9918 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009919 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009921 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009923 rettv->v_type = VAR_STRING;
9924 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 if (*s == '%' || *s == '#' || *s == '<')
9926 {
9927 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009928 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 --emsg_off;
9930 }
9931 else
9932 {
9933 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009934 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009935 if (argvars[1].v_type != VAR_UNKNOWN
9936 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009937 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009938 if (!error)
9939 {
9940 ExpandInit(&xpc);
9941 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009942 if (p_wic)
9943 options += WILD_ICASE;
9944 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, options, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009945 }
9946 else
9947 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 }
9949}
9950
9951/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009952 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009953 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009954 */
9955 static void
9956f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009957 typval_T *argvars;
9958 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009959{
Bram Moolenaar32f649e2011-04-11 13:46:13 +02009960 char *arg_errmsg = N_("extend() argument");
9961
Bram Moolenaare9a41262005-01-15 22:18:47 +00009962 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009963 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009964 list_T *l1, *l2;
9965 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009966 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009967 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009968
Bram Moolenaare9a41262005-01-15 22:18:47 +00009969 l1 = argvars[0].vval.v_list;
9970 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +02009971 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009972 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009973 {
9974 if (argvars[2].v_type != VAR_UNKNOWN)
9975 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009976 before = get_tv_number_chk(&argvars[2], &error);
9977 if (error)
9978 return; /* type error; errmsg already given */
9979
Bram Moolenaar758711c2005-02-02 23:11:38 +00009980 if (before == l1->lv_len)
9981 item = NULL;
9982 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009983 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009984 item = list_find(l1, before);
9985 if (item == NULL)
9986 {
9987 EMSGN(_(e_listidx), before);
9988 return;
9989 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009990 }
9991 }
9992 else
9993 item = NULL;
9994 list_extend(l1, l2, item);
9995
Bram Moolenaare9a41262005-01-15 22:18:47 +00009996 copy_tv(&argvars[0], rettv);
9997 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009998 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009999 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10000 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010001 dict_T *d1, *d2;
10002 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010003 char_u *action;
10004 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010005 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010006 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010007
10008 d1 = argvars[0].vval.v_dict;
10009 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010010 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010011 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010012 {
10013 /* Check the third argument. */
10014 if (argvars[2].v_type != VAR_UNKNOWN)
10015 {
10016 static char *(av[]) = {"keep", "force", "error"};
10017
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010018 action = get_tv_string_chk(&argvars[2]);
10019 if (action == NULL)
10020 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010021 for (i = 0; i < 3; ++i)
10022 if (STRCMP(action, av[i]) == 0)
10023 break;
10024 if (i == 3)
10025 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010026 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010027 return;
10028 }
10029 }
10030 else
10031 action = (char_u *)"force";
10032
10033 /* Go over all entries in the second dict and add them to the
10034 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010035 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010036 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010037 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010038 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010039 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010040 --todo;
10041 di1 = dict_find(d1, hi2->hi_key, -1);
10042 if (di1 == NULL)
10043 {
10044 di1 = dictitem_copy(HI2DI(hi2));
10045 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10046 dictitem_free(di1);
10047 }
10048 else if (*action == 'e')
10049 {
10050 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10051 break;
10052 }
10053 else if (*action == 'f')
10054 {
10055 clear_tv(&di1->di_tv);
10056 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10057 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010058 }
10059 }
10060
Bram Moolenaare9a41262005-01-15 22:18:47 +000010061 copy_tv(&argvars[0], rettv);
10062 }
10063 }
10064 else
10065 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010066}
10067
10068/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010069 * "feedkeys()" function
10070 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010071 static void
10072f_feedkeys(argvars, rettv)
10073 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010074 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010075{
10076 int remap = TRUE;
10077 char_u *keys, *flags;
10078 char_u nbuf[NUMBUFLEN];
10079 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010080 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010081
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010082 /* This is not allowed in the sandbox. If the commands would still be
10083 * executed in the sandbox it would be OK, but it probably happens later,
10084 * when "sandbox" is no longer set. */
10085 if (check_secure())
10086 return;
10087
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010088 keys = get_tv_string(&argvars[0]);
10089 if (*keys != NUL)
10090 {
10091 if (argvars[1].v_type != VAR_UNKNOWN)
10092 {
10093 flags = get_tv_string_buf(&argvars[1], nbuf);
10094 for ( ; *flags != NUL; ++flags)
10095 {
10096 switch (*flags)
10097 {
10098 case 'n': remap = FALSE; break;
10099 case 'm': remap = TRUE; break;
10100 case 't': typed = TRUE; break;
10101 }
10102 }
10103 }
10104
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010105 /* Need to escape K_SPECIAL and CSI before putting the string in the
10106 * typeahead buffer. */
10107 keys_esc = vim_strsave_escape_csi(keys);
10108 if (keys_esc != NULL)
10109 {
10110 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010111 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010112 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010113 if (vgetc_busy)
10114 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010115 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010116 }
10117}
10118
10119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 * "filereadable()" function
10121 */
10122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010123f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010124 typval_T *argvars;
10125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010127 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 char_u *p;
10129 int n;
10130
Bram Moolenaarc236c162008-07-13 17:41:49 +000010131#ifndef O_NONBLOCK
10132# define O_NONBLOCK 0
10133#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010134 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010135 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10136 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 {
10138 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010139 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 }
10141 else
10142 n = FALSE;
10143
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010144 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145}
10146
10147/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010148 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 * rights to write into.
10150 */
10151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010152f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010153 typval_T *argvars;
10154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010156 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157}
10158
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010159static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010160
10161 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010162findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010163 typval_T *argvars;
10164 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010165 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010166{
10167#ifdef FEAT_SEARCHPATH
10168 char_u *fname;
10169 char_u *fresult = NULL;
10170 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10171 char_u *p;
10172 char_u pathbuf[NUMBUFLEN];
10173 int count = 1;
10174 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010175 int error = FALSE;
10176#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010177
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010178 rettv->vval.v_string = NULL;
10179 rettv->v_type = VAR_STRING;
10180
10181#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010182 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010183
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010184 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010185 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010186 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10187 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010188 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010189 else
10190 {
10191 if (*p != NUL)
10192 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010194 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010195 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010196 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010197 }
10198
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010199 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10200 error = TRUE;
10201
10202 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010203 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010204 do
10205 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010206 if (rettv->v_type == VAR_STRING)
10207 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010208 fresult = find_file_in_path_option(first ? fname : NULL,
10209 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010210 0, first, path,
10211 find_what,
10212 curbuf->b_ffname,
10213 find_what == FINDFILE_DIR
10214 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010216
10217 if (fresult != NULL && rettv->v_type == VAR_LIST)
10218 list_append_string(rettv->vval.v_list, fresult, -1);
10219
10220 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010221 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010222
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010223 if (rettv->v_type == VAR_STRING)
10224 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010225#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010226}
10227
Bram Moolenaar33570922005-01-25 22:26:29 +000010228static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10229static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010230
10231/*
10232 * Implementation of map() and filter().
10233 */
10234 static void
10235filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010236 typval_T *argvars;
10237 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010238 int map;
10239{
10240 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010241 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010242 listitem_T *li, *nli;
10243 list_T *l = NULL;
10244 dictitem_T *di;
10245 hashtab_T *ht;
10246 hashitem_T *hi;
10247 dict_T *d = NULL;
10248 typval_T save_val;
10249 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010250 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010251 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010252 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10253 char *arg_errmsg = (map ? N_("map() argument")
10254 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010255 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010256 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010257
Bram Moolenaare9a41262005-01-15 22:18:47 +000010258 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010259 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010260 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010261 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010262 return;
10263 }
10264 else if (argvars[0].v_type == VAR_DICT)
10265 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010266 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010267 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010268 return;
10269 }
10270 else
10271 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010272 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010273 return;
10274 }
10275
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010276 expr = get_tv_string_buf_chk(&argvars[1], buf);
10277 /* On type errors, the preceding call has already displayed an error
10278 * message. Avoid a misleading error message for an empty string that
10279 * was not passed as argument. */
10280 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010282 prepare_vimvar(VV_VAL, &save_val);
10283 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010284
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010285 /* We reset "did_emsg" to be able to detect whether an error
10286 * occurred during evaluation of the expression. */
10287 save_did_emsg = did_emsg;
10288 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010289
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010290 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010291 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010293 vimvars[VV_KEY].vv_type = VAR_STRING;
10294
10295 ht = &d->dv_hashtab;
10296 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010297 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010298 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010300 if (!HASHITEM_EMPTY(hi))
10301 {
10302 --todo;
10303 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010304 if (tv_check_lock(di->di_tv.v_lock,
10305 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010306 break;
10307 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010308 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010309 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010310 break;
10311 if (!map && rem)
10312 dictitem_remove(d, di);
10313 clear_tv(&vimvars[VV_KEY].vv_tv);
10314 }
10315 }
10316 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010317 }
10318 else
10319 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010320 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010322 for (li = l->lv_first; li != NULL; li = nli)
10323 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010324 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010325 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010326 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010327 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010328 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010329 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010330 break;
10331 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010332 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010333 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010334 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010335 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010336
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010337 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010338 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010339
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010340 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010341 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010342
10343 copy_tv(&argvars[0], rettv);
10344}
10345
10346 static int
10347filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010348 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010349 char_u *expr;
10350 int map;
10351 int *remp;
10352{
Bram Moolenaar33570922005-01-25 22:26:29 +000010353 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010354 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010355 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010356
Bram Moolenaar33570922005-01-25 22:26:29 +000010357 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010358 s = expr;
10359 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010360 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010361 if (*s != NUL) /* check for trailing chars after expr */
10362 {
10363 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010364 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010365 }
10366 if (map)
10367 {
10368 /* map(): replace the list item value */
10369 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010370 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010371 *tv = rettv;
10372 }
10373 else
10374 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010375 int error = FALSE;
10376
Bram Moolenaare9a41262005-01-15 22:18:47 +000010377 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010378 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010379 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010380 /* On type error, nothing has been removed; return FAIL to stop the
10381 * loop. The error message was given by get_tv_number_chk(). */
10382 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010383 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010384 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010385 retval = OK;
10386theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010387 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010388 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010389}
10390
10391/*
10392 * "filter()" function
10393 */
10394 static void
10395f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010396 typval_T *argvars;
10397 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010398{
10399 filter_map(argvars, rettv, FALSE);
10400}
10401
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010402/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010403 * "finddir({fname}[, {path}[, {count}]])" function
10404 */
10405 static void
10406f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010407 typval_T *argvars;
10408 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010409{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010410 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010411}
10412
10413/*
10414 * "findfile({fname}[, {path}[, {count}]])" function
10415 */
10416 static void
10417f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010418 typval_T *argvars;
10419 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010420{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010421 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010422}
10423
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010424#ifdef FEAT_FLOAT
10425/*
10426 * "float2nr({float})" function
10427 */
10428 static void
10429f_float2nr(argvars, rettv)
10430 typval_T *argvars;
10431 typval_T *rettv;
10432{
10433 float_T f;
10434
10435 if (get_float_arg(argvars, &f) == OK)
10436 {
10437 if (f < -0x7fffffff)
10438 rettv->vval.v_number = -0x7fffffff;
10439 else if (f > 0x7fffffff)
10440 rettv->vval.v_number = 0x7fffffff;
10441 else
10442 rettv->vval.v_number = (varnumber_T)f;
10443 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010444}
10445
10446/*
10447 * "floor({float})" function
10448 */
10449 static void
10450f_floor(argvars, rettv)
10451 typval_T *argvars;
10452 typval_T *rettv;
10453{
10454 float_T f;
10455
10456 rettv->v_type = VAR_FLOAT;
10457 if (get_float_arg(argvars, &f) == OK)
10458 rettv->vval.v_float = floor(f);
10459 else
10460 rettv->vval.v_float = 0.0;
10461}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010462
10463/*
10464 * "fmod()" function
10465 */
10466 static void
10467f_fmod(argvars, rettv)
10468 typval_T *argvars;
10469 typval_T *rettv;
10470{
10471 float_T fx, fy;
10472
10473 rettv->v_type = VAR_FLOAT;
10474 if (get_float_arg(argvars, &fx) == OK
10475 && get_float_arg(&argvars[1], &fy) == OK)
10476 rettv->vval.v_float = fmod(fx, fy);
10477 else
10478 rettv->vval.v_float = 0.0;
10479}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010480#endif
10481
Bram Moolenaar0d660222005-01-07 21:51:51 +000010482/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010483 * "fnameescape({string})" function
10484 */
10485 static void
10486f_fnameescape(argvars, rettv)
10487 typval_T *argvars;
10488 typval_T *rettv;
10489{
10490 rettv->vval.v_string = vim_strsave_fnameescape(
10491 get_tv_string(&argvars[0]), FALSE);
10492 rettv->v_type = VAR_STRING;
10493}
10494
10495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 * "fnamemodify({fname}, {mods})" function
10497 */
10498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010499f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010500 typval_T *argvars;
10501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502{
10503 char_u *fname;
10504 char_u *mods;
10505 int usedlen = 0;
10506 int len;
10507 char_u *fbuf = NULL;
10508 char_u buf[NUMBUFLEN];
10509
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010510 fname = get_tv_string_chk(&argvars[0]);
10511 mods = get_tv_string_buf_chk(&argvars[1], buf);
10512 if (fname == NULL || mods == NULL)
10513 fname = NULL;
10514 else
10515 {
10516 len = (int)STRLEN(fname);
10517 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010520 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010522 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010524 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 vim_free(fbuf);
10526}
10527
Bram Moolenaar33570922005-01-25 22:26:29 +000010528static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529
10530/*
10531 * "foldclosed()" function
10532 */
10533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010534foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010535 typval_T *argvars;
10536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 int end;
10538{
10539#ifdef FEAT_FOLDING
10540 linenr_T lnum;
10541 linenr_T first, last;
10542
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010543 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10545 {
10546 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10547 {
10548 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010549 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010551 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 return;
10553 }
10554 }
10555#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010556 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557}
10558
10559/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010560 * "foldclosed()" function
10561 */
10562 static void
10563f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010564 typval_T *argvars;
10565 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010566{
10567 foldclosed_both(argvars, rettv, FALSE);
10568}
10569
10570/*
10571 * "foldclosedend()" function
10572 */
10573 static void
10574f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010575 typval_T *argvars;
10576 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010577{
10578 foldclosed_both(argvars, rettv, TRUE);
10579}
10580
10581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582 * "foldlevel()" function
10583 */
10584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010585f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010586 typval_T *argvars;
10587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588{
10589#ifdef FEAT_FOLDING
10590 linenr_T lnum;
10591
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010592 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010594 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596}
10597
10598/*
10599 * "foldtext()" function
10600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010602f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010603 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605{
10606#ifdef FEAT_FOLDING
10607 linenr_T lnum;
10608 char_u *s;
10609 char_u *r;
10610 int len;
10611 char *txt;
10612#endif
10613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010614 rettv->v_type = VAR_STRING;
10615 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010617 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10618 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10619 <= curbuf->b_ml.ml_line_count
10620 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 {
10622 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010623 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10624 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 {
10626 if (!linewhite(lnum))
10627 break;
10628 ++lnum;
10629 }
10630
10631 /* Find interesting text in this line. */
10632 s = skipwhite(ml_get(lnum));
10633 /* skip C comment-start */
10634 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010635 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010637 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010638 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010639 {
10640 s = skipwhite(ml_get(lnum + 1));
10641 if (*s == '*')
10642 s = skipwhite(s + 1);
10643 }
10644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 txt = _("+-%s%3ld lines: ");
10646 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010647 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 + 20 /* for %3ld */
10649 + STRLEN(s))); /* concatenated */
10650 if (r != NULL)
10651 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010652 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10653 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10654 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 len = (int)STRLEN(r);
10656 STRCAT(r, s);
10657 /* remove 'foldmarker' and 'commentstring' */
10658 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010659 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 }
10661 }
10662#endif
10663}
10664
10665/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010666 * "foldtextresult(lnum)" function
10667 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010669f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010670 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010671 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010672{
10673#ifdef FEAT_FOLDING
10674 linenr_T lnum;
10675 char_u *text;
10676 char_u buf[51];
10677 foldinfo_T foldinfo;
10678 int fold_count;
10679#endif
10680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010681 rettv->v_type = VAR_STRING;
10682 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010683#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010684 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010685 /* treat illegal types and illegal string values for {lnum} the same */
10686 if (lnum < 0)
10687 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010688 fold_count = foldedCount(curwin, lnum, &foldinfo);
10689 if (fold_count > 0)
10690 {
10691 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10692 &foldinfo, buf);
10693 if (text == buf)
10694 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010696 }
10697#endif
10698}
10699
10700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 * "foreground()" function
10702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010704f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010705 typval_T *argvars UNUSED;
10706 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708#ifdef FEAT_GUI
10709 if (gui.in_use)
10710 gui_mch_set_foreground();
10711#else
10712# ifdef WIN32
10713 win32_set_foreground();
10714# endif
10715#endif
10716}
10717
10718/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010719 * "function()" function
10720 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010722f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010723 typval_T *argvars;
10724 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010725{
10726 char_u *s;
10727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010728 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010729 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010730 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010731 /* Don't check an autoload name for existence here. */
10732 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010733 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010734 else
10735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010736 rettv->vval.v_string = vim_strsave(s);
10737 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010738 }
10739}
10740
10741/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010742 * "garbagecollect()" function
10743 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010744 static void
10745f_garbagecollect(argvars, rettv)
10746 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010747 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010748{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010749 /* This is postponed until we are back at the toplevel, because we may be
10750 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10751 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010752
10753 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10754 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010755}
10756
10757/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010758 * "get()" function
10759 */
10760 static void
10761f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010762 typval_T *argvars;
10763 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010764{
Bram Moolenaar33570922005-01-25 22:26:29 +000010765 listitem_T *li;
10766 list_T *l;
10767 dictitem_T *di;
10768 dict_T *d;
10769 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010770
Bram Moolenaare9a41262005-01-15 22:18:47 +000010771 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010772 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010773 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010774 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010775 int error = FALSE;
10776
10777 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10778 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010779 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010780 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010781 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010782 else if (argvars[0].v_type == VAR_DICT)
10783 {
10784 if ((d = argvars[0].vval.v_dict) != NULL)
10785 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010786 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010787 if (di != NULL)
10788 tv = &di->di_tv;
10789 }
10790 }
10791 else
10792 EMSG2(_(e_listdictarg), "get()");
10793
10794 if (tv == NULL)
10795 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010796 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010797 copy_tv(&argvars[2], rettv);
10798 }
10799 else
10800 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010801}
10802
Bram Moolenaar342337a2005-07-21 21:11:17 +000010803static 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 +000010804
10805/*
10806 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010807 * Return a range (from start to end) of lines in rettv from the specified
10808 * buffer.
10809 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010810 */
10811 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010812get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010813 buf_T *buf;
10814 linenr_T start;
10815 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010816 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010817 typval_T *rettv;
10818{
10819 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010820
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010821 if (retlist && rettv_list_alloc(rettv) == FAIL)
10822 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010823
10824 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10825 return;
10826
10827 if (!retlist)
10828 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010829 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10830 p = ml_get_buf(buf, start, FALSE);
10831 else
10832 p = (char_u *)"";
10833
10834 rettv->v_type = VAR_STRING;
10835 rettv->vval.v_string = vim_strsave(p);
10836 }
10837 else
10838 {
10839 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010840 return;
10841
10842 if (start < 1)
10843 start = 1;
10844 if (end > buf->b_ml.ml_line_count)
10845 end = buf->b_ml.ml_line_count;
10846 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010847 if (list_append_string(rettv->vval.v_list,
10848 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010849 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010850 }
10851}
10852
10853/*
10854 * "getbufline()" function
10855 */
10856 static void
10857f_getbufline(argvars, rettv)
10858 typval_T *argvars;
10859 typval_T *rettv;
10860{
10861 linenr_T lnum;
10862 linenr_T end;
10863 buf_T *buf;
10864
10865 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10866 ++emsg_off;
10867 buf = get_buf_tv(&argvars[0]);
10868 --emsg_off;
10869
Bram Moolenaar661b1822005-07-28 22:36:45 +000010870 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010871 if (argvars[2].v_type == VAR_UNKNOWN)
10872 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010873 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010874 end = get_tv_lnum_buf(&argvars[2], buf);
10875
Bram Moolenaar342337a2005-07-21 21:11:17 +000010876 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010877}
10878
Bram Moolenaar0d660222005-01-07 21:51:51 +000010879/*
10880 * "getbufvar()" function
10881 */
10882 static void
10883f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010884 typval_T *argvars;
10885 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010886{
10887 buf_T *buf;
10888 buf_T *save_curbuf;
10889 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010890 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010892 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10893 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010894 ++emsg_off;
10895 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010896
10897 rettv->v_type = VAR_STRING;
10898 rettv->vval.v_string = NULL;
10899
10900 if (buf != NULL && varname != NULL)
10901 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010902 /* set curbuf to be our buf, temporarily */
10903 save_curbuf = curbuf;
10904 curbuf = buf;
10905
Bram Moolenaar0d660222005-01-07 21:51:51 +000010906 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010907 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010010908 else if (STRCMP(varname, "changedtick") == 0)
10909 {
10910 rettv->v_type = VAR_NUMBER;
10911 rettv->vval.v_number = curbuf->b_changedtick;
10912 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010913 else
10914 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010915 if (*varname == NUL)
10916 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10917 * scope prefix before the NUL byte is required by
10918 * find_var_in_ht(). */
10919 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010920 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010921 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010922 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010923 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010924 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010925
10926 /* restore previous notion of curbuf */
10927 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010928 }
10929
10930 --emsg_off;
10931}
10932
10933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 * "getchar()" function
10935 */
10936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010937f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010938 typval_T *argvars;
10939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940{
10941 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010942 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010944 /* Position the cursor. Needed after a message that ends in a space. */
10945 windgoto(msg_row, msg_col);
10946
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 ++no_mapping;
10948 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010949 for (;;)
10950 {
10951 if (argvars[0].v_type == VAR_UNKNOWN)
10952 /* getchar(): blocking wait. */
10953 n = safe_vgetc();
10954 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10955 /* getchar(1): only check if char avail */
10956 n = vpeekc();
10957 else if (error || vpeekc() == NUL)
10958 /* illegal argument or getchar(0) and no char avail: return zero */
10959 n = 0;
10960 else
10961 /* getchar(0) and char avail: return char */
10962 n = safe_vgetc();
10963 if (n == K_IGNORE)
10964 continue;
10965 break;
10966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 --no_mapping;
10968 --allow_keys;
10969
Bram Moolenaar219b8702006-11-01 14:32:36 +000010970 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10971 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10972 vimvars[VV_MOUSE_COL].vv_nr = 0;
10973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010974 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 if (IS_SPECIAL(n) || mod_mask != 0)
10976 {
10977 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10978 int i = 0;
10979
10980 /* Turn a special key into three bytes, plus modifier. */
10981 if (mod_mask != 0)
10982 {
10983 temp[i++] = K_SPECIAL;
10984 temp[i++] = KS_MODIFIER;
10985 temp[i++] = mod_mask;
10986 }
10987 if (IS_SPECIAL(n))
10988 {
10989 temp[i++] = K_SPECIAL;
10990 temp[i++] = K_SECOND(n);
10991 temp[i++] = K_THIRD(n);
10992 }
10993#ifdef FEAT_MBYTE
10994 else if (has_mbyte)
10995 i += (*mb_char2bytes)(n, temp + i);
10996#endif
10997 else
10998 temp[i++] = n;
10999 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011000 rettv->v_type = VAR_STRING;
11001 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011002
11003#ifdef FEAT_MOUSE
11004 if (n == K_LEFTMOUSE
11005 || n == K_LEFTMOUSE_NM
11006 || n == K_LEFTDRAG
11007 || n == K_LEFTRELEASE
11008 || n == K_LEFTRELEASE_NM
11009 || n == K_MIDDLEMOUSE
11010 || n == K_MIDDLEDRAG
11011 || n == K_MIDDLERELEASE
11012 || n == K_RIGHTMOUSE
11013 || n == K_RIGHTDRAG
11014 || n == K_RIGHTRELEASE
11015 || n == K_X1MOUSE
11016 || n == K_X1DRAG
11017 || n == K_X1RELEASE
11018 || n == K_X2MOUSE
11019 || n == K_X2DRAG
11020 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011021 || n == K_MOUSELEFT
11022 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011023 || n == K_MOUSEDOWN
11024 || n == K_MOUSEUP)
11025 {
11026 int row = mouse_row;
11027 int col = mouse_col;
11028 win_T *win;
11029 linenr_T lnum;
11030# ifdef FEAT_WINDOWS
11031 win_T *wp;
11032# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011033 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011034
11035 if (row >= 0 && col >= 0)
11036 {
11037 /* Find the window at the mouse coordinates and compute the
11038 * text position. */
11039 win = mouse_find_win(&row, &col);
11040 (void)mouse_comp_pos(win, &row, &col, &lnum);
11041# ifdef FEAT_WINDOWS
11042 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011043 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011044# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011045 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011046 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11047 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11048 }
11049 }
11050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 }
11052}
11053
11054/*
11055 * "getcharmod()" function
11056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011058f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011059 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011062 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063}
11064
11065/*
11066 * "getcmdline()" function
11067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011069f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011070 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011073 rettv->v_type = VAR_STRING;
11074 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075}
11076
11077/*
11078 * "getcmdpos()" function
11079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011081f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011082 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011085 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086}
11087
11088/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011089 * "getcmdtype()" function
11090 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011091 static void
11092f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011093 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011094 typval_T *rettv;
11095{
11096 rettv->v_type = VAR_STRING;
11097 rettv->vval.v_string = alloc(2);
11098 if (rettv->vval.v_string != NULL)
11099 {
11100 rettv->vval.v_string[0] = get_cmdline_type();
11101 rettv->vval.v_string[1] = NUL;
11102 }
11103}
11104
11105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 * "getcwd()" function
11107 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011109f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011110 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011113 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011115 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011116 rettv->vval.v_string = NULL;
11117 cwd = alloc(MAXPATHL);
11118 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011120 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11121 {
11122 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011124 if (rettv->vval.v_string != NULL)
11125 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011127 }
11128 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129 }
11130}
11131
11132/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011133 * "getfontname()" function
11134 */
11135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011136f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011137 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011138 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011139{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140 rettv->v_type = VAR_STRING;
11141 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011142#ifdef FEAT_GUI
11143 if (gui.in_use)
11144 {
11145 GuiFont font;
11146 char_u *name = NULL;
11147
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011148 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011149 {
11150 /* Get the "Normal" font. Either the name saved by
11151 * hl_set_font_name() or from the font ID. */
11152 font = gui.norm_font;
11153 name = hl_get_font_name();
11154 }
11155 else
11156 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011157 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011158 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11159 return;
11160 font = gui_mch_get_font(name, FALSE);
11161 if (font == NOFONT)
11162 return; /* Invalid font name, return empty string. */
11163 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011165 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011166 gui_mch_free_font(font);
11167 }
11168#endif
11169}
11170
11171/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011172 * "getfperm({fname})" function
11173 */
11174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011175f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011176 typval_T *argvars;
11177 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011178{
11179 char_u *fname;
11180 struct stat st;
11181 char_u *perm = NULL;
11182 char_u flags[] = "rwx";
11183 int i;
11184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011185 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011187 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011188 if (mch_stat((char *)fname, &st) >= 0)
11189 {
11190 perm = vim_strsave((char_u *)"---------");
11191 if (perm != NULL)
11192 {
11193 for (i = 0; i < 9; i++)
11194 {
11195 if (st.st_mode & (1 << (8 - i)))
11196 perm[i] = flags[i % 3];
11197 }
11198 }
11199 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011200 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011201}
11202
11203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204 * "getfsize({fname})" function
11205 */
11206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011207f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011208 typval_T *argvars;
11209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210{
11211 char_u *fname;
11212 struct stat st;
11213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011214 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011216 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217
11218 if (mch_stat((char *)fname, &st) >= 0)
11219 {
11220 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011221 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011223 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011224 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011225
11226 /* non-perfect check for overflow */
11227 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11228 rettv->vval.v_number = -2;
11229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230 }
11231 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011232 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233}
11234
11235/*
11236 * "getftime({fname})" function
11237 */
11238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011239f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011240 typval_T *argvars;
11241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242{
11243 char_u *fname;
11244 struct stat st;
11245
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011246 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247
11248 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011249 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011251 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252}
11253
11254/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011255 * "getftype({fname})" function
11256 */
11257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011258f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011259 typval_T *argvars;
11260 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011261{
11262 char_u *fname;
11263 struct stat st;
11264 char_u *type = NULL;
11265 char *t;
11266
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011267 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011269 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011270 if (mch_lstat((char *)fname, &st) >= 0)
11271 {
11272#ifdef S_ISREG
11273 if (S_ISREG(st.st_mode))
11274 t = "file";
11275 else if (S_ISDIR(st.st_mode))
11276 t = "dir";
11277# ifdef S_ISLNK
11278 else if (S_ISLNK(st.st_mode))
11279 t = "link";
11280# endif
11281# ifdef S_ISBLK
11282 else if (S_ISBLK(st.st_mode))
11283 t = "bdev";
11284# endif
11285# ifdef S_ISCHR
11286 else if (S_ISCHR(st.st_mode))
11287 t = "cdev";
11288# endif
11289# ifdef S_ISFIFO
11290 else if (S_ISFIFO(st.st_mode))
11291 t = "fifo";
11292# endif
11293# ifdef S_ISSOCK
11294 else if (S_ISSOCK(st.st_mode))
11295 t = "fifo";
11296# endif
11297 else
11298 t = "other";
11299#else
11300# ifdef S_IFMT
11301 switch (st.st_mode & S_IFMT)
11302 {
11303 case S_IFREG: t = "file"; break;
11304 case S_IFDIR: t = "dir"; break;
11305# ifdef S_IFLNK
11306 case S_IFLNK: t = "link"; break;
11307# endif
11308# ifdef S_IFBLK
11309 case S_IFBLK: t = "bdev"; break;
11310# endif
11311# ifdef S_IFCHR
11312 case S_IFCHR: t = "cdev"; break;
11313# endif
11314# ifdef S_IFIFO
11315 case S_IFIFO: t = "fifo"; break;
11316# endif
11317# ifdef S_IFSOCK
11318 case S_IFSOCK: t = "socket"; break;
11319# endif
11320 default: t = "other";
11321 }
11322# else
11323 if (mch_isdir(fname))
11324 t = "dir";
11325 else
11326 t = "file";
11327# endif
11328#endif
11329 type = vim_strsave((char_u *)t);
11330 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011331 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011332}
11333
11334/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011335 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011336 */
11337 static void
11338f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011339 typval_T *argvars;
11340 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011341{
11342 linenr_T lnum;
11343 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011344 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011345
11346 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011347 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011348 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011349 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011350 retlist = FALSE;
11351 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011352 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011353 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011354 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011355 retlist = TRUE;
11356 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011357
Bram Moolenaar342337a2005-07-21 21:11:17 +000011358 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011359}
11360
11361/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011362 * "getmatches()" function
11363 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011364 static void
11365f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011366 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011367 typval_T *rettv;
11368{
11369#ifdef FEAT_SEARCH_EXTRA
11370 dict_T *dict;
11371 matchitem_T *cur = curwin->w_match_head;
11372
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011373 if (rettv_list_alloc(rettv) == OK)
11374 {
11375 while (cur != NULL)
11376 {
11377 dict = dict_alloc();
11378 if (dict == NULL)
11379 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011380 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11381 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11382 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11383 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11384 list_append_dict(rettv->vval.v_list, dict);
11385 cur = cur->next;
11386 }
11387 }
11388#endif
11389}
11390
11391/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011392 * "getpid()" function
11393 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011394 static void
11395f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011396 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011397 typval_T *rettv;
11398{
11399 rettv->vval.v_number = mch_get_pid();
11400}
11401
11402/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011403 * "getpos(string)" function
11404 */
11405 static void
11406f_getpos(argvars, rettv)
11407 typval_T *argvars;
11408 typval_T *rettv;
11409{
11410 pos_T *fp;
11411 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011412 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011413
11414 if (rettv_list_alloc(rettv) == OK)
11415 {
11416 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011417 fp = var2fpos(&argvars[0], TRUE, &fnum);
11418 if (fnum != -1)
11419 list_append_number(l, (varnumber_T)fnum);
11420 else
11421 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011422 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11423 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011424 list_append_number(l, (fp != NULL)
11425 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011426 : (varnumber_T)0);
11427 list_append_number(l,
11428#ifdef FEAT_VIRTUALEDIT
11429 (fp != NULL) ? (varnumber_T)fp->coladd :
11430#endif
11431 (varnumber_T)0);
11432 }
11433 else
11434 rettv->vval.v_number = FALSE;
11435}
11436
11437/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011438 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011439 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011440 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011441f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011442 typval_T *argvars UNUSED;
11443 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011444{
11445#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011446 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011447#endif
11448
Bram Moolenaar2641f772005-03-25 21:58:17 +000011449#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011450 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011451 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011452 wp = NULL;
11453 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11454 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011455 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011456 if (wp == NULL)
11457 return;
11458 }
11459
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011460 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011461 }
11462#endif
11463}
11464
11465/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466 * "getreg()" function
11467 */
11468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011470 typval_T *argvars;
11471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472{
11473 char_u *strregname;
11474 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011475 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011476 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011478 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011479 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011480 strregname = get_tv_string_chk(&argvars[0]);
11481 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011482 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011483 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011486 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487 regname = (strregname == NULL ? '"' : *strregname);
11488 if (regname == 0)
11489 regname = '"';
11490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011491 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011492 rettv->vval.v_string = error ? NULL :
11493 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494}
11495
11496/*
11497 * "getregtype()" function
11498 */
11499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011500f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011501 typval_T *argvars;
11502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503{
11504 char_u *strregname;
11505 int regname;
11506 char_u buf[NUMBUFLEN + 2];
11507 long reglen = 0;
11508
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011509 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011510 {
11511 strregname = get_tv_string_chk(&argvars[0]);
11512 if (strregname == NULL) /* type error; errmsg already given */
11513 {
11514 rettv->v_type = VAR_STRING;
11515 rettv->vval.v_string = NULL;
11516 return;
11517 }
11518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519 else
11520 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011521 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522
11523 regname = (strregname == NULL ? '"' : *strregname);
11524 if (regname == 0)
11525 regname = '"';
11526
11527 buf[0] = NUL;
11528 buf[1] = NUL;
11529 switch (get_reg_type(regname, &reglen))
11530 {
11531 case MLINE: buf[0] = 'V'; break;
11532 case MCHAR: buf[0] = 'v'; break;
11533#ifdef FEAT_VISUAL
11534 case MBLOCK:
11535 buf[0] = Ctrl_V;
11536 sprintf((char *)buf + 1, "%ld", reglen + 1);
11537 break;
11538#endif
11539 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011540 rettv->v_type = VAR_STRING;
11541 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542}
11543
11544/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011545 * "gettabvar()" function
11546 */
11547 static void
11548f_gettabvar(argvars, rettv)
11549 typval_T *argvars;
11550 typval_T *rettv;
11551{
11552 tabpage_T *tp;
11553 dictitem_T *v;
11554 char_u *varname;
11555
11556 rettv->v_type = VAR_STRING;
11557 rettv->vval.v_string = NULL;
11558
11559 varname = get_tv_string_chk(&argvars[1]);
11560 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11561 if (tp != NULL && varname != NULL)
11562 {
11563 /* look up the variable */
11564 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11565 if (v != NULL)
11566 copy_tv(&v->di_tv, rettv);
11567 }
11568}
11569
11570/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011571 * "gettabwinvar()" function
11572 */
11573 static void
11574f_gettabwinvar(argvars, rettv)
11575 typval_T *argvars;
11576 typval_T *rettv;
11577{
11578 getwinvar(argvars, rettv, 1);
11579}
11580
11581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582 * "getwinposx()" function
11583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011585f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011586 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011589 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590#ifdef FEAT_GUI
11591 if (gui.in_use)
11592 {
11593 int x, y;
11594
11595 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011596 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597 }
11598#endif
11599}
11600
11601/*
11602 * "getwinposy()" function
11603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011605f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011606 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011609 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610#ifdef FEAT_GUI
11611 if (gui.in_use)
11612 {
11613 int x, y;
11614
11615 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011616 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617 }
11618#endif
11619}
11620
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011621/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011622 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011623 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011624 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011625find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011626 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011627 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011628{
11629#ifdef FEAT_WINDOWS
11630 win_T *wp;
11631#endif
11632 int nr;
11633
11634 nr = get_tv_number_chk(vp, NULL);
11635
11636#ifdef FEAT_WINDOWS
11637 if (nr < 0)
11638 return NULL;
11639 if (nr == 0)
11640 return curwin;
11641
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011642 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11643 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011644 if (--nr <= 0)
11645 break;
11646 return wp;
11647#else
11648 if (nr == 0 || nr == 1)
11649 return curwin;
11650 return NULL;
11651#endif
11652}
11653
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654/*
11655 * "getwinvar()" function
11656 */
11657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011658f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011659 typval_T *argvars;
11660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011662 getwinvar(argvars, rettv, 0);
11663}
11664
11665/*
11666 * getwinvar() and gettabwinvar()
11667 */
11668 static void
11669getwinvar(argvars, rettv, off)
11670 typval_T *argvars;
11671 typval_T *rettv;
11672 int off; /* 1 for gettabwinvar() */
11673{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674 win_T *win, *oldcurwin;
11675 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011676 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011677 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011678
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011679#ifdef FEAT_WINDOWS
11680 if (off == 1)
11681 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11682 else
11683 tp = curtab;
11684#endif
11685 win = find_win_by_nr(&argvars[off], tp);
11686 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011687 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011689 rettv->v_type = VAR_STRING;
11690 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691
11692 if (win != NULL && varname != NULL)
11693 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011694 /* Set curwin to be our win, temporarily. Also set curbuf, so
11695 * that we can get buffer-local options. */
11696 oldcurwin = curwin;
11697 curwin = win;
11698 curbuf = win->w_buffer;
11699
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011701 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702 else
11703 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011704 if (*varname == NUL)
11705 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11706 * scope prefix before the NUL byte is required by
11707 * find_var_in_ht(). */
11708 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011710 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011711 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011712 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011714
11715 /* restore previous notion of curwin */
11716 curwin = oldcurwin;
11717 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718 }
11719
11720 --emsg_off;
11721}
11722
11723/*
11724 * "glob()" function
11725 */
11726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011727f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011728 typval_T *argvars;
11729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011731 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011733 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011735 /* When the optional second argument is non-zero, don't remove matches
11736 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11737 if (argvars[1].v_type != VAR_UNKNOWN
11738 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011739 options |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011740 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011741 if (!error)
11742 {
11743 ExpandInit(&xpc);
11744 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011745 if (p_wic)
11746 options += WILD_ICASE;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011747 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011748 NULL, options, WILD_ALL);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011749 }
11750 else
11751 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752}
11753
11754/*
11755 * "globpath()" function
11756 */
11757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011758f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011759 typval_T *argvars;
11760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011762 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011764 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011765 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011767 /* When the optional second argument is non-zero, don't remove matches
11768 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11769 if (argvars[2].v_type != VAR_UNKNOWN
11770 && get_tv_number_chk(&argvars[2], &error))
11771 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011772 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011773 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011774 rettv->vval.v_string = NULL;
11775 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011776 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11777 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778}
11779
11780/*
11781 * "has()" function
11782 */
11783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011784f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011785 typval_T *argvars;
11786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787{
11788 int i;
11789 char_u *name;
11790 int n = FALSE;
11791 static char *(has_list[]) =
11792 {
11793#ifdef AMIGA
11794 "amiga",
11795# ifdef FEAT_ARP
11796 "arp",
11797# endif
11798#endif
11799#ifdef __BEOS__
11800 "beos",
11801#endif
11802#ifdef MSDOS
11803# ifdef DJGPP
11804 "dos32",
11805# else
11806 "dos16",
11807# endif
11808#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011809#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 "mac",
11811#endif
11812#if defined(MACOS_X_UNIX)
11813 "macunix",
11814#endif
11815#ifdef OS2
11816 "os2",
11817#endif
11818#ifdef __QNX__
11819 "qnx",
11820#endif
11821#ifdef RISCOS
11822 "riscos",
11823#endif
11824#ifdef UNIX
11825 "unix",
11826#endif
11827#ifdef VMS
11828 "vms",
11829#endif
11830#ifdef WIN16
11831 "win16",
11832#endif
11833#ifdef WIN32
11834 "win32",
11835#endif
11836#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11837 "win32unix",
11838#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011839#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840 "win64",
11841#endif
11842#ifdef EBCDIC
11843 "ebcdic",
11844#endif
11845#ifndef CASE_INSENSITIVE_FILENAME
11846 "fname_case",
11847#endif
11848#ifdef FEAT_ARABIC
11849 "arabic",
11850#endif
11851#ifdef FEAT_AUTOCMD
11852 "autocmd",
11853#endif
11854#ifdef FEAT_BEVAL
11855 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011856# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11857 "balloon_multiline",
11858# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859#endif
11860#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11861 "builtin_terms",
11862# ifdef ALL_BUILTIN_TCAPS
11863 "all_builtin_terms",
11864# endif
11865#endif
11866#ifdef FEAT_BYTEOFF
11867 "byte_offset",
11868#endif
11869#ifdef FEAT_CINDENT
11870 "cindent",
11871#endif
11872#ifdef FEAT_CLIENTSERVER
11873 "clientserver",
11874#endif
11875#ifdef FEAT_CLIPBOARD
11876 "clipboard",
11877#endif
11878#ifdef FEAT_CMDL_COMPL
11879 "cmdline_compl",
11880#endif
11881#ifdef FEAT_CMDHIST
11882 "cmdline_hist",
11883#endif
11884#ifdef FEAT_COMMENTS
11885 "comments",
11886#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011887#ifdef FEAT_CONCEAL
11888 "conceal",
11889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011890#ifdef FEAT_CRYPT
11891 "cryptv",
11892#endif
11893#ifdef FEAT_CSCOPE
11894 "cscope",
11895#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011896#ifdef FEAT_CURSORBIND
11897 "cursorbind",
11898#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011899#ifdef CURSOR_SHAPE
11900 "cursorshape",
11901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902#ifdef DEBUG
11903 "debug",
11904#endif
11905#ifdef FEAT_CON_DIALOG
11906 "dialog_con",
11907#endif
11908#ifdef FEAT_GUI_DIALOG
11909 "dialog_gui",
11910#endif
11911#ifdef FEAT_DIFF
11912 "diff",
11913#endif
11914#ifdef FEAT_DIGRAPHS
11915 "digraphs",
11916#endif
11917#ifdef FEAT_DND
11918 "dnd",
11919#endif
11920#ifdef FEAT_EMACS_TAGS
11921 "emacs_tags",
11922#endif
11923 "eval", /* always present, of course! */
11924#ifdef FEAT_EX_EXTRA
11925 "ex_extra",
11926#endif
11927#ifdef FEAT_SEARCH_EXTRA
11928 "extra_search",
11929#endif
11930#ifdef FEAT_FKMAP
11931 "farsi",
11932#endif
11933#ifdef FEAT_SEARCHPATH
11934 "file_in_path",
11935#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011936#if defined(UNIX) && !defined(USE_SYSTEM)
11937 "filterpipe",
11938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939#ifdef FEAT_FIND_ID
11940 "find_in_path",
11941#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011942#ifdef FEAT_FLOAT
11943 "float",
11944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945#ifdef FEAT_FOLDING
11946 "folding",
11947#endif
11948#ifdef FEAT_FOOTER
11949 "footer",
11950#endif
11951#if !defined(USE_SYSTEM) && defined(UNIX)
11952 "fork",
11953#endif
11954#ifdef FEAT_GETTEXT
11955 "gettext",
11956#endif
11957#ifdef FEAT_GUI
11958 "gui",
11959#endif
11960#ifdef FEAT_GUI_ATHENA
11961# ifdef FEAT_GUI_NEXTAW
11962 "gui_neXtaw",
11963# else
11964 "gui_athena",
11965# endif
11966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967#ifdef FEAT_GUI_GTK
11968 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011971#ifdef FEAT_GUI_GNOME
11972 "gui_gnome",
11973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974#ifdef FEAT_GUI_MAC
11975 "gui_mac",
11976#endif
11977#ifdef FEAT_GUI_MOTIF
11978 "gui_motif",
11979#endif
11980#ifdef FEAT_GUI_PHOTON
11981 "gui_photon",
11982#endif
11983#ifdef FEAT_GUI_W16
11984 "gui_win16",
11985#endif
11986#ifdef FEAT_GUI_W32
11987 "gui_win32",
11988#endif
11989#ifdef FEAT_HANGULIN
11990 "hangul_input",
11991#endif
11992#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11993 "iconv",
11994#endif
11995#ifdef FEAT_INS_EXPAND
11996 "insert_expand",
11997#endif
11998#ifdef FEAT_JUMPLIST
11999 "jumplist",
12000#endif
12001#ifdef FEAT_KEYMAP
12002 "keymap",
12003#endif
12004#ifdef FEAT_LANGMAP
12005 "langmap",
12006#endif
12007#ifdef FEAT_LIBCALL
12008 "libcall",
12009#endif
12010#ifdef FEAT_LINEBREAK
12011 "linebreak",
12012#endif
12013#ifdef FEAT_LISP
12014 "lispindent",
12015#endif
12016#ifdef FEAT_LISTCMDS
12017 "listcmds",
12018#endif
12019#ifdef FEAT_LOCALMAP
12020 "localmap",
12021#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012022#ifdef FEAT_LUA
12023# ifndef DYNAMIC_LUA
12024 "lua",
12025# endif
12026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027#ifdef FEAT_MENU
12028 "menu",
12029#endif
12030#ifdef FEAT_SESSION
12031 "mksession",
12032#endif
12033#ifdef FEAT_MODIFY_FNAME
12034 "modify_fname",
12035#endif
12036#ifdef FEAT_MOUSE
12037 "mouse",
12038#endif
12039#ifdef FEAT_MOUSESHAPE
12040 "mouseshape",
12041#endif
12042#if defined(UNIX) || defined(VMS)
12043# ifdef FEAT_MOUSE_DEC
12044 "mouse_dec",
12045# endif
12046# ifdef FEAT_MOUSE_GPM
12047 "mouse_gpm",
12048# endif
12049# ifdef FEAT_MOUSE_JSB
12050 "mouse_jsbterm",
12051# endif
12052# ifdef FEAT_MOUSE_NET
12053 "mouse_netterm",
12054# endif
12055# ifdef FEAT_MOUSE_PTERM
12056 "mouse_pterm",
12057# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012058# ifdef FEAT_SYSMOUSE
12059 "mouse_sysmouse",
12060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061# ifdef FEAT_MOUSE_XTERM
12062 "mouse_xterm",
12063# endif
12064#endif
12065#ifdef FEAT_MBYTE
12066 "multi_byte",
12067#endif
12068#ifdef FEAT_MBYTE_IME
12069 "multi_byte_ime",
12070#endif
12071#ifdef FEAT_MULTI_LANG
12072 "multi_lang",
12073#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012074#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012075#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012076 "mzscheme",
12077#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079#ifdef FEAT_OLE
12080 "ole",
12081#endif
12082#ifdef FEAT_OSFILETYPE
12083 "osfiletype",
12084#endif
12085#ifdef FEAT_PATH_EXTRA
12086 "path_extra",
12087#endif
12088#ifdef FEAT_PERL
12089#ifndef DYNAMIC_PERL
12090 "perl",
12091#endif
12092#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012093#ifdef FEAT_PERSISTENT_UNDO
12094 "persistent_undo",
12095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096#ifdef FEAT_PYTHON
12097#ifndef DYNAMIC_PYTHON
12098 "python",
12099#endif
12100#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012101#ifdef FEAT_PYTHON3
12102#ifndef DYNAMIC_PYTHON3
12103 "python3",
12104#endif
12105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106#ifdef FEAT_POSTSCRIPT
12107 "postscript",
12108#endif
12109#ifdef FEAT_PRINTER
12110 "printer",
12111#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012112#ifdef FEAT_PROFILE
12113 "profile",
12114#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012115#ifdef FEAT_RELTIME
12116 "reltime",
12117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118#ifdef FEAT_QUICKFIX
12119 "quickfix",
12120#endif
12121#ifdef FEAT_RIGHTLEFT
12122 "rightleft",
12123#endif
12124#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12125 "ruby",
12126#endif
12127#ifdef FEAT_SCROLLBIND
12128 "scrollbind",
12129#endif
12130#ifdef FEAT_CMDL_INFO
12131 "showcmd",
12132 "cmdline_info",
12133#endif
12134#ifdef FEAT_SIGNS
12135 "signs",
12136#endif
12137#ifdef FEAT_SMARTINDENT
12138 "smartindent",
12139#endif
12140#ifdef FEAT_SNIFF
12141 "sniff",
12142#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012143#ifdef STARTUPTIME
12144 "startuptime",
12145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146#ifdef FEAT_STL_OPT
12147 "statusline",
12148#endif
12149#ifdef FEAT_SUN_WORKSHOP
12150 "sun_workshop",
12151#endif
12152#ifdef FEAT_NETBEANS_INTG
12153 "netbeans_intg",
12154#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012155#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012156 "spell",
12157#endif
12158#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159 "syntax",
12160#endif
12161#if defined(USE_SYSTEM) || !defined(UNIX)
12162 "system",
12163#endif
12164#ifdef FEAT_TAG_BINS
12165 "tag_binary",
12166#endif
12167#ifdef FEAT_TAG_OLDSTATIC
12168 "tag_old_static",
12169#endif
12170#ifdef FEAT_TAG_ANYWHITE
12171 "tag_any_white",
12172#endif
12173#ifdef FEAT_TCL
12174# ifndef DYNAMIC_TCL
12175 "tcl",
12176# endif
12177#endif
12178#ifdef TERMINFO
12179 "terminfo",
12180#endif
12181#ifdef FEAT_TERMRESPONSE
12182 "termresponse",
12183#endif
12184#ifdef FEAT_TEXTOBJ
12185 "textobjects",
12186#endif
12187#ifdef HAVE_TGETENT
12188 "tgetent",
12189#endif
12190#ifdef FEAT_TITLE
12191 "title",
12192#endif
12193#ifdef FEAT_TOOLBAR
12194 "toolbar",
12195#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012196#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12197 "unnamedplus",
12198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199#ifdef FEAT_USR_CMDS
12200 "user-commands", /* was accidentally included in 5.4 */
12201 "user_commands",
12202#endif
12203#ifdef FEAT_VIMINFO
12204 "viminfo",
12205#endif
12206#ifdef FEAT_VERTSPLIT
12207 "vertsplit",
12208#endif
12209#ifdef FEAT_VIRTUALEDIT
12210 "virtualedit",
12211#endif
12212#ifdef FEAT_VISUAL
12213 "visual",
12214#endif
12215#ifdef FEAT_VISUALEXTRA
12216 "visualextra",
12217#endif
12218#ifdef FEAT_VREPLACE
12219 "vreplace",
12220#endif
12221#ifdef FEAT_WILDIGN
12222 "wildignore",
12223#endif
12224#ifdef FEAT_WILDMENU
12225 "wildmenu",
12226#endif
12227#ifdef FEAT_WINDOWS
12228 "windows",
12229#endif
12230#ifdef FEAT_WAK
12231 "winaltkeys",
12232#endif
12233#ifdef FEAT_WRITEBACKUP
12234 "writebackup",
12235#endif
12236#ifdef FEAT_XIM
12237 "xim",
12238#endif
12239#ifdef FEAT_XFONTSET
12240 "xfontset",
12241#endif
12242#ifdef USE_XSMP
12243 "xsmp",
12244#endif
12245#ifdef USE_XSMP_INTERACT
12246 "xsmp_interact",
12247#endif
12248#ifdef FEAT_XCLIPBOARD
12249 "xterm_clipboard",
12250#endif
12251#ifdef FEAT_XTERM_SAVE
12252 "xterm_save",
12253#endif
12254#if defined(UNIX) && defined(FEAT_X11)
12255 "X11",
12256#endif
12257 NULL
12258 };
12259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012260 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261 for (i = 0; has_list[i] != NULL; ++i)
12262 if (STRICMP(name, has_list[i]) == 0)
12263 {
12264 n = TRUE;
12265 break;
12266 }
12267
12268 if (n == FALSE)
12269 {
12270 if (STRNICMP(name, "patch", 5) == 0)
12271 n = has_patch(atoi((char *)name + 5));
12272 else if (STRICMP(name, "vim_starting") == 0)
12273 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012274#ifdef FEAT_MBYTE
12275 else if (STRICMP(name, "multi_byte_encoding") == 0)
12276 n = has_mbyte;
12277#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012278#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12279 else if (STRICMP(name, "balloon_multiline") == 0)
12280 n = multiline_balloon_available();
12281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282#ifdef DYNAMIC_TCL
12283 else if (STRICMP(name, "tcl") == 0)
12284 n = tcl_enabled(FALSE);
12285#endif
12286#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12287 else if (STRICMP(name, "iconv") == 0)
12288 n = iconv_enabled(FALSE);
12289#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012290#ifdef DYNAMIC_LUA
12291 else if (STRICMP(name, "lua") == 0)
12292 n = lua_enabled(FALSE);
12293#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012294#ifdef DYNAMIC_MZSCHEME
12295 else if (STRICMP(name, "mzscheme") == 0)
12296 n = mzscheme_enabled(FALSE);
12297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298#ifdef DYNAMIC_RUBY
12299 else if (STRICMP(name, "ruby") == 0)
12300 n = ruby_enabled(FALSE);
12301#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012302#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303#ifdef DYNAMIC_PYTHON
12304 else if (STRICMP(name, "python") == 0)
12305 n = python_enabled(FALSE);
12306#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012307#endif
12308#ifdef FEAT_PYTHON3
12309#ifdef DYNAMIC_PYTHON3
12310 else if (STRICMP(name, "python3") == 0)
12311 n = python3_enabled(FALSE);
12312#endif
12313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314#ifdef DYNAMIC_PERL
12315 else if (STRICMP(name, "perl") == 0)
12316 n = perl_enabled(FALSE);
12317#endif
12318#ifdef FEAT_GUI
12319 else if (STRICMP(name, "gui_running") == 0)
12320 n = (gui.in_use || gui.starting);
12321# ifdef FEAT_GUI_W32
12322 else if (STRICMP(name, "gui_win32s") == 0)
12323 n = gui_is_win32s();
12324# endif
12325# ifdef FEAT_BROWSE
12326 else if (STRICMP(name, "browse") == 0)
12327 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12328# endif
12329#endif
12330#ifdef FEAT_SYN_HL
12331 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012332 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333#endif
12334#if defined(WIN3264)
12335 else if (STRICMP(name, "win95") == 0)
12336 n = mch_windows95();
12337#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012338#ifdef FEAT_NETBEANS_INTG
12339 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012340 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342 }
12343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012344 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345}
12346
12347/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012348 * "has_key()" function
12349 */
12350 static void
12351f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012352 typval_T *argvars;
12353 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012354{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012355 if (argvars[0].v_type != VAR_DICT)
12356 {
12357 EMSG(_(e_dictreq));
12358 return;
12359 }
12360 if (argvars[0].vval.v_dict == NULL)
12361 return;
12362
12363 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012364 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012365}
12366
12367/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012368 * "haslocaldir()" function
12369 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012370 static void
12371f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012372 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012373 typval_T *rettv;
12374{
12375 rettv->vval.v_number = (curwin->w_localdir != NULL);
12376}
12377
12378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379 * "hasmapto()" function
12380 */
12381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012382f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012383 typval_T *argvars;
12384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385{
12386 char_u *name;
12387 char_u *mode;
12388 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012389 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012391 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012392 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012393 mode = (char_u *)"nvo";
12394 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012395 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012396 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012397 if (argvars[2].v_type != VAR_UNKNOWN)
12398 abbr = get_tv_number(&argvars[2]);
12399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400
Bram Moolenaar2c932302006-03-18 21:42:09 +000012401 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012402 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012404 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405}
12406
12407/*
12408 * "histadd()" function
12409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012411f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012412 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012414{
12415#ifdef FEAT_CMDHIST
12416 int histype;
12417 char_u *str;
12418 char_u buf[NUMBUFLEN];
12419#endif
12420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012421 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012422 if (check_restricted() || check_secure())
12423 return;
12424#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012425 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12426 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 if (histype >= 0)
12428 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012429 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430 if (*str != NUL)
12431 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012432 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012434 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435 return;
12436 }
12437 }
12438#endif
12439}
12440
12441/*
12442 * "histdel()" function
12443 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012445f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012446 typval_T *argvars UNUSED;
12447 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012448{
12449#ifdef FEAT_CMDHIST
12450 int n;
12451 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012452 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012454 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12455 if (str == NULL)
12456 n = 0;
12457 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012458 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012459 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012460 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012462 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012463 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012464 else
12465 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012466 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012467 get_tv_string_buf(&argvars[1], buf));
12468 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469#endif
12470}
12471
12472/*
12473 * "histget()" function
12474 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012476f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012477 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479{
12480#ifdef FEAT_CMDHIST
12481 int type;
12482 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012483 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012485 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12486 if (str == NULL)
12487 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012489 {
12490 type = get_histtype(str);
12491 if (argvars[1].v_type == VAR_UNKNOWN)
12492 idx = get_history_idx(type);
12493 else
12494 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12495 /* -1 on type error */
12496 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012499 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012501 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502}
12503
12504/*
12505 * "histnr()" function
12506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012508f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012509 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511{
12512 int i;
12513
12514#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012515 char_u *history = get_tv_string_chk(&argvars[0]);
12516
12517 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012518 if (i >= HIST_CMD && i < HIST_COUNT)
12519 i = get_history_idx(i);
12520 else
12521#endif
12522 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012523 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524}
12525
12526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527 * "highlightID(name)" function
12528 */
12529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012530f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012531 typval_T *argvars;
12532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012534 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535}
12536
12537/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012538 * "highlight_exists()" function
12539 */
12540 static void
12541f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012542 typval_T *argvars;
12543 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012544{
12545 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12546}
12547
12548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012549 * "hostname()" function
12550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012552f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012553 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555{
12556 char_u hostname[256];
12557
12558 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012559 rettv->v_type = VAR_STRING;
12560 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561}
12562
12563/*
12564 * iconv() function
12565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012567f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012568 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012570{
12571#ifdef FEAT_MBYTE
12572 char_u buf1[NUMBUFLEN];
12573 char_u buf2[NUMBUFLEN];
12574 char_u *from, *to, *str;
12575 vimconv_T vimconv;
12576#endif
12577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012578 rettv->v_type = VAR_STRING;
12579 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012580
12581#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012582 str = get_tv_string(&argvars[0]);
12583 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12584 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585 vimconv.vc_type = CONV_NONE;
12586 convert_setup(&vimconv, from, to);
12587
12588 /* If the encodings are equal, no conversion needed. */
12589 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012590 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012591 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012592 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012593
12594 convert_setup(&vimconv, NULL, NULL);
12595 vim_free(from);
12596 vim_free(to);
12597#endif
12598}
12599
12600/*
12601 * "indent()" function
12602 */
12603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012604f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012605 typval_T *argvars;
12606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012607{
12608 linenr_T lnum;
12609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012610 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012611 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012612 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012614 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615}
12616
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012617/*
12618 * "index()" function
12619 */
12620 static void
12621f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012622 typval_T *argvars;
12623 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012624{
Bram Moolenaar33570922005-01-25 22:26:29 +000012625 list_T *l;
12626 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012627 long idx = 0;
12628 int ic = FALSE;
12629
12630 rettv->vval.v_number = -1;
12631 if (argvars[0].v_type != VAR_LIST)
12632 {
12633 EMSG(_(e_listreq));
12634 return;
12635 }
12636 l = argvars[0].vval.v_list;
12637 if (l != NULL)
12638 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012639 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012640 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012641 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012642 int error = FALSE;
12643
Bram Moolenaar758711c2005-02-02 23:11:38 +000012644 /* Start at specified item. Use the cached index that list_find()
12645 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012646 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012647 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012648 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012649 ic = get_tv_number_chk(&argvars[3], &error);
12650 if (error)
12651 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012652 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012653
Bram Moolenaar758711c2005-02-02 23:11:38 +000012654 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012655 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012656 {
12657 rettv->vval.v_number = idx;
12658 break;
12659 }
12660 }
12661}
12662
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663static int inputsecret_flag = 0;
12664
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012665static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12666
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012668 * This function is used by f_input() and f_inputdialog() functions. The third
12669 * argument to f_input() specifies the type of completion to use at the
12670 * prompt. The third argument to f_inputdialog() specifies the value to return
12671 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672 */
12673 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012674get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012675 typval_T *argvars;
12676 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012677 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012679 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680 char_u *p = NULL;
12681 int c;
12682 char_u buf[NUMBUFLEN];
12683 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012684 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012685 int xp_type = EXPAND_NOTHING;
12686 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012687
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012688 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012689 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690
12691#ifdef NO_CONSOLE_INPUT
12692 /* While starting up, there is no place to enter text. */
12693 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695#endif
12696
12697 cmd_silent = FALSE; /* Want to see the prompt. */
12698 if (prompt != NULL)
12699 {
12700 /* Only the part of the message after the last NL is considered as
12701 * prompt for the command line */
12702 p = vim_strrchr(prompt, '\n');
12703 if (p == NULL)
12704 p = prompt;
12705 else
12706 {
12707 ++p;
12708 c = *p;
12709 *p = NUL;
12710 msg_start();
12711 msg_clr_eos();
12712 msg_puts_attr(prompt, echo_attr);
12713 msg_didout = FALSE;
12714 msg_starthere();
12715 *p = c;
12716 }
12717 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012719 if (argvars[1].v_type != VAR_UNKNOWN)
12720 {
12721 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12722 if (defstr != NULL)
12723 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012725 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012726 {
12727 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012728 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012729 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012730
Bram Moolenaar4463f292005-09-25 22:20:24 +000012731 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012732
Bram Moolenaar4463f292005-09-25 22:20:24 +000012733 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12734 if (xp_name == NULL)
12735 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012736
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012737 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012738
Bram Moolenaar4463f292005-09-25 22:20:24 +000012739 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12740 &xp_arg) == FAIL)
12741 return;
12742 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012743 }
12744
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012745 if (defstr != NULL)
12746 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012747 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12748 xp_type, xp_arg);
12749
12750 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012752 /* since the user typed this, no need to wait for return */
12753 need_wait_return = FALSE;
12754 msg_didout = FALSE;
12755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756 cmd_silent = cmd_silent_save;
12757}
12758
12759/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012760 * "input()" function
12761 * Also handles inputsecret() when inputsecret is set.
12762 */
12763 static void
12764f_input(argvars, rettv)
12765 typval_T *argvars;
12766 typval_T *rettv;
12767{
12768 get_user_input(argvars, rettv, FALSE);
12769}
12770
12771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772 * "inputdialog()" function
12773 */
12774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012775f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012776 typval_T *argvars;
12777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778{
12779#if defined(FEAT_GUI_TEXTDIALOG)
12780 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12781 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12782 {
12783 char_u *message;
12784 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012785 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012787 message = get_tv_string_chk(&argvars[0]);
12788 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012789 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012790 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791 else
12792 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012793 if (message != NULL && defstr != NULL
12794 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010012795 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012796 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797 else
12798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012799 if (message != NULL && defstr != NULL
12800 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012801 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012802 rettv->vval.v_string = vim_strsave(
12803 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012805 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012807 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808 }
12809 else
12810#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012811 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812}
12813
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012814/*
12815 * "inputlist()" function
12816 */
12817 static void
12818f_inputlist(argvars, rettv)
12819 typval_T *argvars;
12820 typval_T *rettv;
12821{
12822 listitem_T *li;
12823 int selected;
12824 int mouse_used;
12825
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012826#ifdef NO_CONSOLE_INPUT
12827 /* While starting up, there is no place to enter text. */
12828 if (no_console_input())
12829 return;
12830#endif
12831 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12832 {
12833 EMSG2(_(e_listarg), "inputlist()");
12834 return;
12835 }
12836
12837 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012838 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012839 lines_left = Rows; /* avoid more prompt */
12840 msg_scroll = TRUE;
12841 msg_clr_eos();
12842
12843 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12844 {
12845 msg_puts(get_tv_string(&li->li_tv));
12846 msg_putchar('\n');
12847 }
12848
12849 /* Ask for choice. */
12850 selected = prompt_for_number(&mouse_used);
12851 if (mouse_used)
12852 selected -= lines_left;
12853
12854 rettv->vval.v_number = selected;
12855}
12856
12857
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12859
12860/*
12861 * "inputrestore()" function
12862 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012864f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012865 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012866 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867{
12868 if (ga_userinput.ga_len > 0)
12869 {
12870 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12872 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012873 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874 }
12875 else if (p_verbose > 1)
12876 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012877 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012878 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879 }
12880}
12881
12882/*
12883 * "inputsave()" function
12884 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012886f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012887 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012888 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012890 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891 if (ga_grow(&ga_userinput, 1) == OK)
12892 {
12893 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12894 + ga_userinput.ga_len);
12895 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012896 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897 }
12898 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012899 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900}
12901
12902/*
12903 * "inputsecret()" function
12904 */
12905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012906f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012907 typval_T *argvars;
12908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909{
12910 ++cmdline_star;
12911 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012912 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 --cmdline_star;
12914 --inputsecret_flag;
12915}
12916
12917/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012918 * "insert()" function
12919 */
12920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012921f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012922 typval_T *argvars;
12923 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012924{
12925 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012926 listitem_T *item;
12927 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012928 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012929
12930 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012931 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012932 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012933 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012934 {
12935 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012936 before = get_tv_number_chk(&argvars[2], &error);
12937 if (error)
12938 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012939
Bram Moolenaar758711c2005-02-02 23:11:38 +000012940 if (before == l->lv_len)
12941 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012942 else
12943 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012944 item = list_find(l, before);
12945 if (item == NULL)
12946 {
12947 EMSGN(_(e_listidx), before);
12948 l = NULL;
12949 }
12950 }
12951 if (l != NULL)
12952 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012953 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012954 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012955 }
12956 }
12957}
12958
12959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960 * "isdirectory()" function
12961 */
12962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012963f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012964 typval_T *argvars;
12965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012967 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968}
12969
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012970/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012971 * "islocked()" function
12972 */
12973 static void
12974f_islocked(argvars, rettv)
12975 typval_T *argvars;
12976 typval_T *rettv;
12977{
12978 lval_T lv;
12979 char_u *end;
12980 dictitem_T *di;
12981
12982 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012983 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12984 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012985 if (end != NULL && lv.ll_name != NULL)
12986 {
12987 if (*end != NUL)
12988 EMSG(_(e_trailing));
12989 else
12990 {
12991 if (lv.ll_tv == NULL)
12992 {
12993 if (check_changedtick(lv.ll_name))
12994 rettv->vval.v_number = 1; /* always locked */
12995 else
12996 {
12997 di = find_var(lv.ll_name, NULL);
12998 if (di != NULL)
12999 {
13000 /* Consider a variable locked when:
13001 * 1. the variable itself is locked
13002 * 2. the value of the variable is locked.
13003 * 3. the List or Dict value is locked.
13004 */
13005 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13006 || tv_islocked(&di->di_tv));
13007 }
13008 }
13009 }
13010 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013011 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013012 else if (lv.ll_newkey != NULL)
13013 EMSG2(_(e_dictkey), lv.ll_newkey);
13014 else if (lv.ll_list != NULL)
13015 /* List item. */
13016 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13017 else
13018 /* Dictionary item. */
13019 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13020 }
13021 }
13022
13023 clear_lval(&lv);
13024}
13025
Bram Moolenaar33570922005-01-25 22:26:29 +000013026static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013027
13028/*
13029 * Turn a dict into a list:
13030 * "what" == 0: list of keys
13031 * "what" == 1: list of values
13032 * "what" == 2: list of items
13033 */
13034 static void
13035dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013036 typval_T *argvars;
13037 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013038 int what;
13039{
Bram Moolenaar33570922005-01-25 22:26:29 +000013040 list_T *l2;
13041 dictitem_T *di;
13042 hashitem_T *hi;
13043 listitem_T *li;
13044 listitem_T *li2;
13045 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013046 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013047
Bram Moolenaar8c711452005-01-14 21:53:12 +000013048 if (argvars[0].v_type != VAR_DICT)
13049 {
13050 EMSG(_(e_dictreq));
13051 return;
13052 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013053 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013054 return;
13055
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013056 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013057 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013058
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013059 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013060 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013061 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013062 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013063 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013064 --todo;
13065 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013066
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013067 li = listitem_alloc();
13068 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013069 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013070 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013071
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013072 if (what == 0)
13073 {
13074 /* keys() */
13075 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013076 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013077 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13078 }
13079 else if (what == 1)
13080 {
13081 /* values() */
13082 copy_tv(&di->di_tv, &li->li_tv);
13083 }
13084 else
13085 {
13086 /* items() */
13087 l2 = list_alloc();
13088 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013089 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013090 li->li_tv.vval.v_list = l2;
13091 if (l2 == NULL)
13092 break;
13093 ++l2->lv_refcount;
13094
13095 li2 = listitem_alloc();
13096 if (li2 == NULL)
13097 break;
13098 list_append(l2, li2);
13099 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013100 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013101 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13102
13103 li2 = listitem_alloc();
13104 if (li2 == NULL)
13105 break;
13106 list_append(l2, li2);
13107 copy_tv(&di->di_tv, &li2->li_tv);
13108 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013109 }
13110 }
13111}
13112
13113/*
13114 * "items(dict)" function
13115 */
13116 static void
13117f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013118 typval_T *argvars;
13119 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013120{
13121 dict_list(argvars, rettv, 2);
13122}
13123
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013125 * "join()" function
13126 */
13127 static void
13128f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013129 typval_T *argvars;
13130 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013131{
13132 garray_T ga;
13133 char_u *sep;
13134
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013135 if (argvars[0].v_type != VAR_LIST)
13136 {
13137 EMSG(_(e_listreq));
13138 return;
13139 }
13140 if (argvars[0].vval.v_list == NULL)
13141 return;
13142 if (argvars[1].v_type == VAR_UNKNOWN)
13143 sep = (char_u *)" ";
13144 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013145 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013146
13147 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013148
13149 if (sep != NULL)
13150 {
13151 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013152 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013153 ga_append(&ga, NUL);
13154 rettv->vval.v_string = (char_u *)ga.ga_data;
13155 }
13156 else
13157 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013158}
13159
13160/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013161 * "keys()" function
13162 */
13163 static void
13164f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013165 typval_T *argvars;
13166 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013167{
13168 dict_list(argvars, rettv, 0);
13169}
13170
13171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013172 * "last_buffer_nr()" function.
13173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013175f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013176 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178{
13179 int n = 0;
13180 buf_T *buf;
13181
13182 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13183 if (n < buf->b_fnum)
13184 n = buf->b_fnum;
13185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013186 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013187}
13188
13189/*
13190 * "len()" function
13191 */
13192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013193f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013194 typval_T *argvars;
13195 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013196{
13197 switch (argvars[0].v_type)
13198 {
13199 case VAR_STRING:
13200 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013201 rettv->vval.v_number = (varnumber_T)STRLEN(
13202 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013203 break;
13204 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013205 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013206 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013207 case VAR_DICT:
13208 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13209 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013210 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013211 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013212 break;
13213 }
13214}
13215
Bram Moolenaar33570922005-01-25 22:26:29 +000013216static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013217
13218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013219libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013220 typval_T *argvars;
13221 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013222 int type;
13223{
13224#ifdef FEAT_LIBCALL
13225 char_u *string_in;
13226 char_u **string_result;
13227 int nr_result;
13228#endif
13229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013230 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013231 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013232 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013233
13234 if (check_restricted() || check_secure())
13235 return;
13236
13237#ifdef FEAT_LIBCALL
13238 /* The first two args must be strings, otherwise its meaningless */
13239 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13240 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013241 string_in = NULL;
13242 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013243 string_in = argvars[2].vval.v_string;
13244 if (type == VAR_NUMBER)
13245 string_result = NULL;
13246 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013247 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013248 if (mch_libcall(argvars[0].vval.v_string,
13249 argvars[1].vval.v_string,
13250 string_in,
13251 argvars[2].vval.v_number,
13252 string_result,
13253 &nr_result) == OK
13254 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013255 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013256 }
13257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258}
13259
13260/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013261 * "libcall()" function
13262 */
13263 static void
13264f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013265 typval_T *argvars;
13266 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013267{
13268 libcall_common(argvars, rettv, VAR_STRING);
13269}
13270
13271/*
13272 * "libcallnr()" function
13273 */
13274 static void
13275f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013276 typval_T *argvars;
13277 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013278{
13279 libcall_common(argvars, rettv, VAR_NUMBER);
13280}
13281
13282/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013283 * "line(string)" function
13284 */
13285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013286f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013287 typval_T *argvars;
13288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289{
13290 linenr_T lnum = 0;
13291 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013292 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013294 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295 if (fp != NULL)
13296 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013297 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298}
13299
13300/*
13301 * "line2byte(lnum)" function
13302 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013304f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013305 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307{
13308#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013309 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310#else
13311 linenr_T lnum;
13312
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013313 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013315 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013317 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13318 if (rettv->vval.v_number >= 0)
13319 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320#endif
13321}
13322
13323/*
13324 * "lispindent(lnum)" function
13325 */
13326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013327f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013328 typval_T *argvars;
13329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330{
13331#ifdef FEAT_LISP
13332 pos_T pos;
13333 linenr_T lnum;
13334
13335 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013336 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013337 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13338 {
13339 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013340 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341 curwin->w_cursor = pos;
13342 }
13343 else
13344#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013345 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346}
13347
13348/*
13349 * "localtime()" function
13350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013352f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013353 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013356 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357}
13358
Bram Moolenaar33570922005-01-25 22:26:29 +000013359static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360
13361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013362get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013363 typval_T *argvars;
13364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365 int exact;
13366{
13367 char_u *keys;
13368 char_u *which;
13369 char_u buf[NUMBUFLEN];
13370 char_u *keys_buf = NULL;
13371 char_u *rhs;
13372 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013373 int abbr = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013374 int get_dict = FALSE;
13375 mapblock_T *mp;
13376 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377
13378 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013379 rettv->v_type = VAR_STRING;
13380 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013382 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383 if (*keys == NUL)
13384 return;
13385
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013386 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013387 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013388 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013389 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013390 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013391 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013392 if (argvars[3].v_type != VAR_UNKNOWN)
13393 get_dict = get_tv_number(&argvars[3]);
13394 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013396 else
13397 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013398 if (which == NULL)
13399 return;
13400
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401 mode = get_map_mode(&which, 0);
13402
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013403 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013404 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013406
13407 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013409 /* Return a string. */
13410 if (rhs != NULL)
13411 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412
Bram Moolenaarbd743252010-10-20 21:23:33 +020013413 }
13414 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13415 {
13416 /* Return a dictionary. */
13417 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13418 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13419 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420
Bram Moolenaarbd743252010-10-20 21:23:33 +020013421 dict_add_nr_str(dict, "lhs", 0L, lhs);
13422 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13423 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13424 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13425 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13426 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13427 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13428 dict_add_nr_str(dict, "mode", 0L, mapmode);
13429
13430 vim_free(lhs);
13431 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432 }
13433}
13434
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013435#ifdef FEAT_FLOAT
13436/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013437 * "log()" function
13438 */
13439 static void
13440f_log(argvars, rettv)
13441 typval_T *argvars;
13442 typval_T *rettv;
13443{
13444 float_T f;
13445
13446 rettv->v_type = VAR_FLOAT;
13447 if (get_float_arg(argvars, &f) == OK)
13448 rettv->vval.v_float = log(f);
13449 else
13450 rettv->vval.v_float = 0.0;
13451}
13452
13453/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013454 * "log10()" function
13455 */
13456 static void
13457f_log10(argvars, rettv)
13458 typval_T *argvars;
13459 typval_T *rettv;
13460{
13461 float_T f;
13462
13463 rettv->v_type = VAR_FLOAT;
13464 if (get_float_arg(argvars, &f) == OK)
13465 rettv->vval.v_float = log10(f);
13466 else
13467 rettv->vval.v_float = 0.0;
13468}
13469#endif
13470
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013472 * "map()" function
13473 */
13474 static void
13475f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013476 typval_T *argvars;
13477 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013478{
13479 filter_map(argvars, rettv, TRUE);
13480}
13481
13482/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013483 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484 */
13485 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013486f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013487 typval_T *argvars;
13488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013490 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491}
13492
13493/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013494 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495 */
13496 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013497f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013498 typval_T *argvars;
13499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013501 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502}
13503
Bram Moolenaar33570922005-01-25 22:26:29 +000013504static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013505
13506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013507find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013508 typval_T *argvars;
13509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510 int type;
13511{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013512 char_u *str = NULL;
13513 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514 char_u *pat;
13515 regmatch_T regmatch;
13516 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013517 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518 char_u *save_cpo;
13519 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013520 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013521 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013522 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013523 list_T *l = NULL;
13524 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013525 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013526 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527
13528 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13529 save_cpo = p_cpo;
13530 p_cpo = (char_u *)"";
13531
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013532 rettv->vval.v_number = -1;
13533 if (type == 3)
13534 {
13535 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013536 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013537 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013538 }
13539 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013541 rettv->v_type = VAR_STRING;
13542 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013545 if (argvars[0].v_type == VAR_LIST)
13546 {
13547 if ((l = argvars[0].vval.v_list) == NULL)
13548 goto theend;
13549 li = l->lv_first;
13550 }
13551 else
13552 expr = str = get_tv_string(&argvars[0]);
13553
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013554 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13555 if (pat == NULL)
13556 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013557
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013558 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013560 int error = FALSE;
13561
13562 start = get_tv_number_chk(&argvars[2], &error);
13563 if (error)
13564 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013565 if (l != NULL)
13566 {
13567 li = list_find(l, start);
13568 if (li == NULL)
13569 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013570 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013571 }
13572 else
13573 {
13574 if (start < 0)
13575 start = 0;
13576 if (start > (long)STRLEN(str))
13577 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013578 /* When "count" argument is there ignore matches before "start",
13579 * otherwise skip part of the string. Differs when pattern is "^"
13580 * or "\<". */
13581 if (argvars[3].v_type != VAR_UNKNOWN)
13582 startcol = start;
13583 else
13584 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013585 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013586
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013587 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013588 nth = get_tv_number_chk(&argvars[3], &error);
13589 if (error)
13590 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591 }
13592
13593 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13594 if (regmatch.regprog != NULL)
13595 {
13596 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013597
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013598 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013599 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013600 if (l != NULL)
13601 {
13602 if (li == NULL)
13603 {
13604 match = FALSE;
13605 break;
13606 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013607 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013608 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013609 if (str == NULL)
13610 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013611 }
13612
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013613 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013614
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013615 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013616 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013617 if (l == NULL && !match)
13618 break;
13619
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013620 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013621 if (l != NULL)
13622 {
13623 li = li->li_next;
13624 ++idx;
13625 }
13626 else
13627 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013628#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013629 startcol = (colnr_T)(regmatch.startp[0]
13630 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013631#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013632 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013633#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013634 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013635 }
13636
13637 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013639 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013640 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013641 int i;
13642
13643 /* return list with matched string and submatches */
13644 for (i = 0; i < NSUBEXP; ++i)
13645 {
13646 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013647 {
13648 if (list_append_string(rettv->vval.v_list,
13649 (char_u *)"", 0) == FAIL)
13650 break;
13651 }
13652 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013653 regmatch.startp[i],
13654 (int)(regmatch.endp[i] - regmatch.startp[i]))
13655 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013656 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013657 }
13658 }
13659 else if (type == 2)
13660 {
13661 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013662 if (l != NULL)
13663 copy_tv(&li->li_tv, rettv);
13664 else
13665 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013667 }
13668 else if (l != NULL)
13669 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670 else
13671 {
13672 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013673 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674 (varnumber_T)(regmatch.startp[0] - str);
13675 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013676 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013678 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679 }
13680 }
13681 vim_free(regmatch.regprog);
13682 }
13683
13684theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013685 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686 p_cpo = save_cpo;
13687}
13688
13689/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013690 * "match()" function
13691 */
13692 static void
13693f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013694 typval_T *argvars;
13695 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013696{
13697 find_some_match(argvars, rettv, 1);
13698}
13699
13700/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013701 * "matchadd()" function
13702 */
13703 static void
13704f_matchadd(argvars, rettv)
13705 typval_T *argvars;
13706 typval_T *rettv;
13707{
13708#ifdef FEAT_SEARCH_EXTRA
13709 char_u buf[NUMBUFLEN];
13710 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13711 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13712 int prio = 10; /* default priority */
13713 int id = -1;
13714 int error = FALSE;
13715
13716 rettv->vval.v_number = -1;
13717
13718 if (grp == NULL || pat == NULL)
13719 return;
13720 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013721 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013722 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013723 if (argvars[3].v_type != VAR_UNKNOWN)
13724 id = get_tv_number_chk(&argvars[3], &error);
13725 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013726 if (error == TRUE)
13727 return;
13728 if (id >= 1 && id <= 3)
13729 {
13730 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13731 return;
13732 }
13733
13734 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13735#endif
13736}
13737
13738/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013739 * "matcharg()" function
13740 */
13741 static void
13742f_matcharg(argvars, rettv)
13743 typval_T *argvars;
13744 typval_T *rettv;
13745{
13746 if (rettv_list_alloc(rettv) == OK)
13747 {
13748#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013749 int id = get_tv_number(&argvars[0]);
13750 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013751
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013752 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013753 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013754 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13755 {
13756 list_append_string(rettv->vval.v_list,
13757 syn_id2name(m->hlg_id), -1);
13758 list_append_string(rettv->vval.v_list, m->pattern, -1);
13759 }
13760 else
13761 {
13762 list_append_string(rettv->vval.v_list, NUL, -1);
13763 list_append_string(rettv->vval.v_list, NUL, -1);
13764 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013765 }
13766#endif
13767 }
13768}
13769
13770/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013771 * "matchdelete()" function
13772 */
13773 static void
13774f_matchdelete(argvars, rettv)
13775 typval_T *argvars;
13776 typval_T *rettv;
13777{
13778#ifdef FEAT_SEARCH_EXTRA
13779 rettv->vval.v_number = match_delete(curwin,
13780 (int)get_tv_number(&argvars[0]), TRUE);
13781#endif
13782}
13783
13784/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013785 * "matchend()" function
13786 */
13787 static void
13788f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013789 typval_T *argvars;
13790 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013791{
13792 find_some_match(argvars, rettv, 0);
13793}
13794
13795/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013796 * "matchlist()" function
13797 */
13798 static void
13799f_matchlist(argvars, rettv)
13800 typval_T *argvars;
13801 typval_T *rettv;
13802{
13803 find_some_match(argvars, rettv, 3);
13804}
13805
13806/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013807 * "matchstr()" function
13808 */
13809 static void
13810f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013811 typval_T *argvars;
13812 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013813{
13814 find_some_match(argvars, rettv, 2);
13815}
13816
Bram Moolenaar33570922005-01-25 22:26:29 +000013817static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013818
13819 static void
13820max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013821 typval_T *argvars;
13822 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013823 int domax;
13824{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013825 long n = 0;
13826 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013827 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013828
13829 if (argvars[0].v_type == VAR_LIST)
13830 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013831 list_T *l;
13832 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013833
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013834 l = argvars[0].vval.v_list;
13835 if (l != NULL)
13836 {
13837 li = l->lv_first;
13838 if (li != NULL)
13839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013840 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013841 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013842 {
13843 li = li->li_next;
13844 if (li == NULL)
13845 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013846 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013847 if (domax ? i > n : i < n)
13848 n = i;
13849 }
13850 }
13851 }
13852 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013853 else if (argvars[0].v_type == VAR_DICT)
13854 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013855 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013856 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013857 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013858 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013859
13860 d = argvars[0].vval.v_dict;
13861 if (d != NULL)
13862 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013863 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013864 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013865 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013866 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013867 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013868 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013869 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013870 if (first)
13871 {
13872 n = i;
13873 first = FALSE;
13874 }
13875 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013876 n = i;
13877 }
13878 }
13879 }
13880 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013881 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013882 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013883 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013884}
13885
13886/*
13887 * "max()" function
13888 */
13889 static void
13890f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013891 typval_T *argvars;
13892 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013893{
13894 max_min(argvars, rettv, TRUE);
13895}
13896
13897/*
13898 * "min()" function
13899 */
13900 static void
13901f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013902 typval_T *argvars;
13903 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013904{
13905 max_min(argvars, rettv, FALSE);
13906}
13907
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013908static int mkdir_recurse __ARGS((char_u *dir, int prot));
13909
13910/*
13911 * Create the directory in which "dir" is located, and higher levels when
13912 * needed.
13913 */
13914 static int
13915mkdir_recurse(dir, prot)
13916 char_u *dir;
13917 int prot;
13918{
13919 char_u *p;
13920 char_u *updir;
13921 int r = FAIL;
13922
13923 /* Get end of directory name in "dir".
13924 * We're done when it's "/" or "c:/". */
13925 p = gettail_sep(dir);
13926 if (p <= get_past_head(dir))
13927 return OK;
13928
13929 /* If the directory exists we're done. Otherwise: create it.*/
13930 updir = vim_strnsave(dir, (int)(p - dir));
13931 if (updir == NULL)
13932 return FAIL;
13933 if (mch_isdir(updir))
13934 r = OK;
13935 else if (mkdir_recurse(updir, prot) == OK)
13936 r = vim_mkdir_emsg(updir, prot);
13937 vim_free(updir);
13938 return r;
13939}
13940
13941#ifdef vim_mkdir
13942/*
13943 * "mkdir()" function
13944 */
13945 static void
13946f_mkdir(argvars, rettv)
13947 typval_T *argvars;
13948 typval_T *rettv;
13949{
13950 char_u *dir;
13951 char_u buf[NUMBUFLEN];
13952 int prot = 0755;
13953
13954 rettv->vval.v_number = FAIL;
13955 if (check_restricted() || check_secure())
13956 return;
13957
13958 dir = get_tv_string_buf(&argvars[0], buf);
13959 if (argvars[1].v_type != VAR_UNKNOWN)
13960 {
13961 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013962 prot = get_tv_number_chk(&argvars[2], NULL);
13963 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013964 mkdir_recurse(dir, prot);
13965 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013966 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013967}
13968#endif
13969
Bram Moolenaar0d660222005-01-07 21:51:51 +000013970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971 * "mode()" function
13972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013974f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013975 typval_T *argvars;
13976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013978 char_u buf[3];
13979
13980 buf[1] = NUL;
13981 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982
13983#ifdef FEAT_VISUAL
13984 if (VIsual_active)
13985 {
13986 if (VIsual_select)
13987 buf[0] = VIsual_mode + 's' - 'v';
13988 else
13989 buf[0] = VIsual_mode;
13990 }
13991 else
13992#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013993 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13994 || State == CONFIRM)
13995 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013997 if (State == ASKMORE)
13998 buf[1] = 'm';
13999 else if (State == CONFIRM)
14000 buf[1] = '?';
14001 }
14002 else if (State == EXTERNCMD)
14003 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004 else if (State & INSERT)
14005 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014006#ifdef FEAT_VREPLACE
14007 if (State & VREPLACE_FLAG)
14008 {
14009 buf[0] = 'R';
14010 buf[1] = 'v';
14011 }
14012 else
14013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014 if (State & REPLACE_FLAG)
14015 buf[0] = 'R';
14016 else
14017 buf[0] = 'i';
14018 }
14019 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014020 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014022 if (exmode_active)
14023 buf[1] = 'v';
14024 }
14025 else if (exmode_active)
14026 {
14027 buf[0] = 'c';
14028 buf[1] = 'e';
14029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014031 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014033 if (finish_op)
14034 buf[1] = 'o';
14035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014036
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014037 /* Clear out the minor mode when the argument is not a non-zero number or
14038 * non-empty string. */
14039 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014040 buf[1] = NUL;
14041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014042 rettv->vval.v_string = vim_strsave(buf);
14043 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044}
14045
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014046#ifdef FEAT_MZSCHEME
14047/*
14048 * "mzeval()" function
14049 */
14050 static void
14051f_mzeval(argvars, rettv)
14052 typval_T *argvars;
14053 typval_T *rettv;
14054{
14055 char_u *str;
14056 char_u buf[NUMBUFLEN];
14057
14058 str = get_tv_string_buf(&argvars[0], buf);
14059 do_mzeval(str, rettv);
14060}
14061#endif
14062
Bram Moolenaar071d4272004-06-13 20:20:40 +000014063/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014064 * "nextnonblank()" function
14065 */
14066 static void
14067f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014068 typval_T *argvars;
14069 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014070{
14071 linenr_T lnum;
14072
14073 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14074 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014075 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014076 {
14077 lnum = 0;
14078 break;
14079 }
14080 if (*skipwhite(ml_get(lnum)) != NUL)
14081 break;
14082 }
14083 rettv->vval.v_number = lnum;
14084}
14085
14086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087 * "nr2char()" function
14088 */
14089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014090f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014091 typval_T *argvars;
14092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093{
14094 char_u buf[NUMBUFLEN];
14095
14096#ifdef FEAT_MBYTE
14097 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014098 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099 else
14100#endif
14101 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014102 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103 buf[1] = NUL;
14104 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014105 rettv->v_type = VAR_STRING;
14106 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014107}
14108
14109/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014110 * "pathshorten()" function
14111 */
14112 static void
14113f_pathshorten(argvars, rettv)
14114 typval_T *argvars;
14115 typval_T *rettv;
14116{
14117 char_u *p;
14118
14119 rettv->v_type = VAR_STRING;
14120 p = get_tv_string_chk(&argvars[0]);
14121 if (p == NULL)
14122 rettv->vval.v_string = NULL;
14123 else
14124 {
14125 p = vim_strsave(p);
14126 rettv->vval.v_string = p;
14127 if (p != NULL)
14128 shorten_dir(p);
14129 }
14130}
14131
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014132#ifdef FEAT_FLOAT
14133/*
14134 * "pow()" function
14135 */
14136 static void
14137f_pow(argvars, rettv)
14138 typval_T *argvars;
14139 typval_T *rettv;
14140{
14141 float_T fx, fy;
14142
14143 rettv->v_type = VAR_FLOAT;
14144 if (get_float_arg(argvars, &fx) == OK
14145 && get_float_arg(&argvars[1], &fy) == OK)
14146 rettv->vval.v_float = pow(fx, fy);
14147 else
14148 rettv->vval.v_float = 0.0;
14149}
14150#endif
14151
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014152/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014153 * "prevnonblank()" function
14154 */
14155 static void
14156f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014157 typval_T *argvars;
14158 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014159{
14160 linenr_T lnum;
14161
14162 lnum = get_tv_lnum(argvars);
14163 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14164 lnum = 0;
14165 else
14166 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14167 --lnum;
14168 rettv->vval.v_number = lnum;
14169}
14170
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014171#ifdef HAVE_STDARG_H
14172/* This dummy va_list is here because:
14173 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14174 * - locally in the function results in a "used before set" warning
14175 * - using va_start() to initialize it gives "function with fixed args" error */
14176static va_list ap;
14177#endif
14178
Bram Moolenaar8c711452005-01-14 21:53:12 +000014179/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014180 * "printf()" function
14181 */
14182 static void
14183f_printf(argvars, rettv)
14184 typval_T *argvars;
14185 typval_T *rettv;
14186{
14187 rettv->v_type = VAR_STRING;
14188 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014189#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014190 {
14191 char_u buf[NUMBUFLEN];
14192 int len;
14193 char_u *s;
14194 int saved_did_emsg = did_emsg;
14195 char *fmt;
14196
14197 /* Get the required length, allocate the buffer and do it for real. */
14198 did_emsg = FALSE;
14199 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014200 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014201 if (!did_emsg)
14202 {
14203 s = alloc(len + 1);
14204 if (s != NULL)
14205 {
14206 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014207 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014208 }
14209 }
14210 did_emsg |= saved_did_emsg;
14211 }
14212#endif
14213}
14214
14215/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014216 * "pumvisible()" function
14217 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014218 static void
14219f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014220 typval_T *argvars UNUSED;
14221 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014222{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014223#ifdef FEAT_INS_EXPAND
14224 if (pum_visible())
14225 rettv->vval.v_number = 1;
14226#endif
14227}
14228
14229/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014230 * "range()" function
14231 */
14232 static void
14233f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014234 typval_T *argvars;
14235 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014236{
14237 long start;
14238 long end;
14239 long stride = 1;
14240 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014241 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014243 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014244 if (argvars[1].v_type == VAR_UNKNOWN)
14245 {
14246 end = start - 1;
14247 start = 0;
14248 }
14249 else
14250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014251 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014252 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014253 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014254 }
14255
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014256 if (error)
14257 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014258 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014259 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014260 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014261 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014262 else
14263 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014264 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014265 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014266 if (list_append_number(rettv->vval.v_list,
14267 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014268 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014269 }
14270}
14271
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014272/*
14273 * "readfile()" function
14274 */
14275 static void
14276f_readfile(argvars, rettv)
14277 typval_T *argvars;
14278 typval_T *rettv;
14279{
14280 int binary = FALSE;
14281 char_u *fname;
14282 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014283 listitem_T *li;
14284#define FREAD_SIZE 200 /* optimized for text lines */
14285 char_u buf[FREAD_SIZE];
14286 int readlen; /* size of last fread() */
14287 int buflen; /* nr of valid chars in buf[] */
14288 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14289 int tolist; /* first byte in buf[] still to be put in list */
14290 int chop; /* how many CR to chop off */
14291 char_u *prev = NULL; /* previously read bytes, if any */
14292 int prevlen = 0; /* length of "prev" if not NULL */
14293 char_u *s;
14294 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014295 long maxline = MAXLNUM;
14296 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014297
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014298 if (argvars[1].v_type != VAR_UNKNOWN)
14299 {
14300 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14301 binary = TRUE;
14302 if (argvars[2].v_type != VAR_UNKNOWN)
14303 maxline = get_tv_number(&argvars[2]);
14304 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014305
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014306 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014307 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014308
14309 /* Always open the file in binary mode, library functions have a mind of
14310 * their own about CR-LF conversion. */
14311 fname = get_tv_string(&argvars[0]);
14312 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14313 {
14314 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14315 return;
14316 }
14317
14318 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014319 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014320 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014321 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014322 buflen = filtd + readlen;
14323 tolist = 0;
14324 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14325 {
14326 if (buf[filtd] == '\n' || readlen <= 0)
14327 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014328 /* In binary mode add an empty list item when the last
14329 * non-empty line ends in a '\n'. */
14330 if (!binary && readlen == 0 && filtd == 0 && prev == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014331 break;
14332
14333 /* Found end-of-line or end-of-file: add a text line to the
14334 * list. */
14335 chop = 0;
14336 if (!binary)
14337 while (filtd - chop - 1 >= tolist
14338 && buf[filtd - chop - 1] == '\r')
14339 ++chop;
14340 len = filtd - tolist - chop;
14341 if (prev == NULL)
14342 s = vim_strnsave(buf + tolist, len);
14343 else
14344 {
14345 s = alloc((unsigned)(prevlen + len + 1));
14346 if (s != NULL)
14347 {
14348 mch_memmove(s, prev, prevlen);
14349 vim_free(prev);
14350 prev = NULL;
14351 mch_memmove(s + prevlen, buf + tolist, len);
14352 s[prevlen + len] = NUL;
14353 }
14354 }
14355 tolist = filtd + 1;
14356
14357 li = listitem_alloc();
14358 if (li == NULL)
14359 {
14360 vim_free(s);
14361 break;
14362 }
14363 li->li_tv.v_type = VAR_STRING;
14364 li->li_tv.v_lock = 0;
14365 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014366 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014367
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014368 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014369 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014370 if (readlen <= 0)
14371 break;
14372 }
14373 else if (buf[filtd] == NUL)
14374 buf[filtd] = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014375#ifdef FEAT_MBYTE
14376 else if (buf[filtd] == 0xef
14377 && enc_utf8
14378 && filtd + 2 < buflen
14379 && !binary
14380 && buf[filtd + 1] == 0xbb
14381 && buf[filtd + 2] == 0xbf)
14382 {
14383 /* remove utf-8 byte order mark */
14384 mch_memmove(buf + filtd, buf + filtd + 3, buflen - filtd - 3);
14385 --filtd;
14386 buflen -= 3;
14387 }
14388#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014389 }
14390 if (readlen <= 0)
14391 break;
14392
14393 if (tolist == 0)
14394 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014395 if (buflen >= FREAD_SIZE / 2)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014396 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014397 /* "buf" is full, need to move text to an allocated buffer */
14398 if (prev == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014399 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014400 prev = vim_strnsave(buf, buflen);
14401 prevlen = buflen;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014402 }
Bram Moolenaar27b60562011-04-01 16:07:46 +020014403 else
14404 {
14405 s = alloc((unsigned)(prevlen + buflen));
14406 if (s != NULL)
14407 {
14408 mch_memmove(s, prev, prevlen);
14409 mch_memmove(s + prevlen, buf, buflen);
14410 vim_free(prev);
14411 prev = s;
14412 prevlen += buflen;
14413 }
14414 }
14415 filtd = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014416 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014417 }
14418 else
14419 {
14420 mch_memmove(buf, buf + tolist, buflen - tolist);
14421 filtd -= tolist;
14422 }
14423 }
14424
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014425 /*
14426 * For a negative line count use only the lines at the end of the file,
14427 * free the rest.
14428 */
14429 if (maxline < 0)
14430 while (cnt > -maxline)
14431 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014432 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014433 --cnt;
14434 }
14435
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014436 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014437 fclose(fd);
14438}
14439
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014440#if defined(FEAT_RELTIME)
14441static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14442
14443/*
14444 * Convert a List to proftime_T.
14445 * Return FAIL when there is something wrong.
14446 */
14447 static int
14448list2proftime(arg, tm)
14449 typval_T *arg;
14450 proftime_T *tm;
14451{
14452 long n1, n2;
14453 int error = FALSE;
14454
14455 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14456 || arg->vval.v_list->lv_len != 2)
14457 return FAIL;
14458 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14459 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14460# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014461 tm->HighPart = n1;
14462 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014463# else
14464 tm->tv_sec = n1;
14465 tm->tv_usec = n2;
14466# endif
14467 return error ? FAIL : OK;
14468}
14469#endif /* FEAT_RELTIME */
14470
14471/*
14472 * "reltime()" function
14473 */
14474 static void
14475f_reltime(argvars, rettv)
14476 typval_T *argvars;
14477 typval_T *rettv;
14478{
14479#ifdef FEAT_RELTIME
14480 proftime_T res;
14481 proftime_T start;
14482
14483 if (argvars[0].v_type == VAR_UNKNOWN)
14484 {
14485 /* No arguments: get current time. */
14486 profile_start(&res);
14487 }
14488 else if (argvars[1].v_type == VAR_UNKNOWN)
14489 {
14490 if (list2proftime(&argvars[0], &res) == FAIL)
14491 return;
14492 profile_end(&res);
14493 }
14494 else
14495 {
14496 /* Two arguments: compute the difference. */
14497 if (list2proftime(&argvars[0], &start) == FAIL
14498 || list2proftime(&argvars[1], &res) == FAIL)
14499 return;
14500 profile_sub(&res, &start);
14501 }
14502
14503 if (rettv_list_alloc(rettv) == OK)
14504 {
14505 long n1, n2;
14506
14507# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014508 n1 = res.HighPart;
14509 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014510# else
14511 n1 = res.tv_sec;
14512 n2 = res.tv_usec;
14513# endif
14514 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14515 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14516 }
14517#endif
14518}
14519
14520/*
14521 * "reltimestr()" function
14522 */
14523 static void
14524f_reltimestr(argvars, rettv)
14525 typval_T *argvars;
14526 typval_T *rettv;
14527{
14528#ifdef FEAT_RELTIME
14529 proftime_T tm;
14530#endif
14531
14532 rettv->v_type = VAR_STRING;
14533 rettv->vval.v_string = NULL;
14534#ifdef FEAT_RELTIME
14535 if (list2proftime(&argvars[0], &tm) == OK)
14536 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14537#endif
14538}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014539
Bram Moolenaar0d660222005-01-07 21:51:51 +000014540#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14541static void make_connection __ARGS((void));
14542static int check_connection __ARGS((void));
14543
14544 static void
14545make_connection()
14546{
14547 if (X_DISPLAY == NULL
14548# ifdef FEAT_GUI
14549 && !gui.in_use
14550# endif
14551 )
14552 {
14553 x_force_connect = TRUE;
14554 setup_term_clip();
14555 x_force_connect = FALSE;
14556 }
14557}
14558
14559 static int
14560check_connection()
14561{
14562 make_connection();
14563 if (X_DISPLAY == NULL)
14564 {
14565 EMSG(_("E240: No connection to Vim server"));
14566 return FAIL;
14567 }
14568 return OK;
14569}
14570#endif
14571
14572#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014573static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014574
14575 static void
14576remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014577 typval_T *argvars;
14578 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014579 int expr;
14580{
14581 char_u *server_name;
14582 char_u *keys;
14583 char_u *r = NULL;
14584 char_u buf[NUMBUFLEN];
14585# ifdef WIN32
14586 HWND w;
14587# else
14588 Window w;
14589# endif
14590
14591 if (check_restricted() || check_secure())
14592 return;
14593
14594# ifdef FEAT_X11
14595 if (check_connection() == FAIL)
14596 return;
14597# endif
14598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014599 server_name = get_tv_string_chk(&argvars[0]);
14600 if (server_name == NULL)
14601 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014602 keys = get_tv_string_buf(&argvars[1], buf);
14603# ifdef WIN32
14604 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14605# else
14606 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14607 < 0)
14608# endif
14609 {
14610 if (r != NULL)
14611 EMSG(r); /* sending worked but evaluation failed */
14612 else
14613 EMSG2(_("E241: Unable to send to %s"), server_name);
14614 return;
14615 }
14616
14617 rettv->vval.v_string = r;
14618
14619 if (argvars[2].v_type != VAR_UNKNOWN)
14620 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014621 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014622 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014623 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014624
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014625 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014626 v.di_tv.v_type = VAR_STRING;
14627 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014628 idvar = get_tv_string_chk(&argvars[2]);
14629 if (idvar != NULL)
14630 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014631 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014632 }
14633}
14634#endif
14635
14636/*
14637 * "remote_expr()" function
14638 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014639 static void
14640f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014641 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014642 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014643{
14644 rettv->v_type = VAR_STRING;
14645 rettv->vval.v_string = NULL;
14646#ifdef FEAT_CLIENTSERVER
14647 remote_common(argvars, rettv, TRUE);
14648#endif
14649}
14650
14651/*
14652 * "remote_foreground()" function
14653 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014654 static void
14655f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014656 typval_T *argvars UNUSED;
14657 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014658{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014659#ifdef FEAT_CLIENTSERVER
14660# ifdef WIN32
14661 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014662 {
14663 char_u *server_name = get_tv_string_chk(&argvars[0]);
14664
14665 if (server_name != NULL)
14666 serverForeground(server_name);
14667 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014668# else
14669 /* Send a foreground() expression to the server. */
14670 argvars[1].v_type = VAR_STRING;
14671 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14672 argvars[2].v_type = VAR_UNKNOWN;
14673 remote_common(argvars, rettv, TRUE);
14674 vim_free(argvars[1].vval.v_string);
14675# endif
14676#endif
14677}
14678
Bram Moolenaar0d660222005-01-07 21:51:51 +000014679 static void
14680f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014681 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014682 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014683{
14684#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014685 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014686 char_u *s = NULL;
14687# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014688 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014689# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014690 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014691
14692 if (check_restricted() || check_secure())
14693 {
14694 rettv->vval.v_number = -1;
14695 return;
14696 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014697 serverid = get_tv_string_chk(&argvars[0]);
14698 if (serverid == NULL)
14699 {
14700 rettv->vval.v_number = -1;
14701 return; /* type error; errmsg already given */
14702 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014703# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014704 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014705 if (n == 0)
14706 rettv->vval.v_number = -1;
14707 else
14708 {
14709 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14710 rettv->vval.v_number = (s != NULL);
14711 }
14712# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014713 if (check_connection() == FAIL)
14714 return;
14715
14716 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014717 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014718# endif
14719
14720 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14721 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014722 char_u *retvar;
14723
Bram Moolenaar33570922005-01-25 22:26:29 +000014724 v.di_tv.v_type = VAR_STRING;
14725 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014726 retvar = get_tv_string_chk(&argvars[1]);
14727 if (retvar != NULL)
14728 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014729 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014730 }
14731#else
14732 rettv->vval.v_number = -1;
14733#endif
14734}
14735
Bram Moolenaar0d660222005-01-07 21:51:51 +000014736 static void
14737f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014738 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014739 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014740{
14741 char_u *r = NULL;
14742
14743#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014744 char_u *serverid = get_tv_string_chk(&argvars[0]);
14745
14746 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014747 {
14748# ifdef WIN32
14749 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014750 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014751
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014752 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014753 if (n != 0)
14754 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14755 if (r == NULL)
14756# else
14757 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014758 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014759# endif
14760 EMSG(_("E277: Unable to read a server reply"));
14761 }
14762#endif
14763 rettv->v_type = VAR_STRING;
14764 rettv->vval.v_string = r;
14765}
14766
14767/*
14768 * "remote_send()" function
14769 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014770 static void
14771f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014772 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014773 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014774{
14775 rettv->v_type = VAR_STRING;
14776 rettv->vval.v_string = NULL;
14777#ifdef FEAT_CLIENTSERVER
14778 remote_common(argvars, rettv, FALSE);
14779#endif
14780}
14781
14782/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014783 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014784 */
14785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014786f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014787 typval_T *argvars;
14788 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014789{
Bram Moolenaar33570922005-01-25 22:26:29 +000014790 list_T *l;
14791 listitem_T *item, *item2;
14792 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014793 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014794 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014795 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014796 dict_T *d;
14797 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014798 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014799
Bram Moolenaar8c711452005-01-14 21:53:12 +000014800 if (argvars[0].v_type == VAR_DICT)
14801 {
14802 if (argvars[2].v_type != VAR_UNKNOWN)
14803 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014804 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014805 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014806 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014807 key = get_tv_string_chk(&argvars[1]);
14808 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014809 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014810 di = dict_find(d, key, -1);
14811 if (di == NULL)
14812 EMSG2(_(e_dictkey), key);
14813 else
14814 {
14815 *rettv = di->di_tv;
14816 init_tv(&di->di_tv);
14817 dictitem_remove(d, di);
14818 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014819 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014820 }
14821 }
14822 else if (argvars[0].v_type != VAR_LIST)
14823 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014824 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014825 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014827 int error = FALSE;
14828
14829 idx = get_tv_number_chk(&argvars[1], &error);
14830 if (error)
14831 ; /* type error: do nothing, errmsg already given */
14832 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014833 EMSGN(_(e_listidx), idx);
14834 else
14835 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014836 if (argvars[2].v_type == VAR_UNKNOWN)
14837 {
14838 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014839 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014840 *rettv = item->li_tv;
14841 vim_free(item);
14842 }
14843 else
14844 {
14845 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014846 end = get_tv_number_chk(&argvars[2], &error);
14847 if (error)
14848 ; /* type error: do nothing */
14849 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014850 EMSGN(_(e_listidx), end);
14851 else
14852 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014853 int cnt = 0;
14854
14855 for (li = item; li != NULL; li = li->li_next)
14856 {
14857 ++cnt;
14858 if (li == item2)
14859 break;
14860 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014861 if (li == NULL) /* didn't find "item2" after "item" */
14862 EMSG(_(e_invrange));
14863 else
14864 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014865 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014866 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014867 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014868 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014869 l->lv_first = item;
14870 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014871 item->li_prev = NULL;
14872 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014873 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014874 }
14875 }
14876 }
14877 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014878 }
14879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014880}
14881
14882/*
14883 * "rename({from}, {to})" function
14884 */
14885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014886f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014887 typval_T *argvars;
14888 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014889{
14890 char_u buf[NUMBUFLEN];
14891
14892 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014893 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014894 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014895 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14896 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014897}
14898
14899/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014900 * "repeat()" function
14901 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014902 static void
14903f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014904 typval_T *argvars;
14905 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014906{
14907 char_u *p;
14908 int n;
14909 int slen;
14910 int len;
14911 char_u *r;
14912 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014913
14914 n = get_tv_number(&argvars[1]);
14915 if (argvars[0].v_type == VAR_LIST)
14916 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014917 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014918 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014919 if (list_extend(rettv->vval.v_list,
14920 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014921 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014922 }
14923 else
14924 {
14925 p = get_tv_string(&argvars[0]);
14926 rettv->v_type = VAR_STRING;
14927 rettv->vval.v_string = NULL;
14928
14929 slen = (int)STRLEN(p);
14930 len = slen * n;
14931 if (len <= 0)
14932 return;
14933
14934 r = alloc(len + 1);
14935 if (r != NULL)
14936 {
14937 for (i = 0; i < n; i++)
14938 mch_memmove(r + i * slen, p, (size_t)slen);
14939 r[len] = NUL;
14940 }
14941
14942 rettv->vval.v_string = r;
14943 }
14944}
14945
14946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014947 * "resolve()" function
14948 */
14949 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014950f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014951 typval_T *argvars;
14952 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014953{
14954 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020014955#ifdef HAVE_READLINK
14956 char_u *buf = NULL;
14957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014959 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960#ifdef FEAT_SHORTCUT
14961 {
14962 char_u *v = NULL;
14963
14964 v = mch_resolve_shortcut(p);
14965 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014966 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014968 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014969 }
14970#else
14971# ifdef HAVE_READLINK
14972 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014973 char_u *cpy;
14974 int len;
14975 char_u *remain = NULL;
14976 char_u *q;
14977 int is_relative_to_current = FALSE;
14978 int has_trailing_pathsep = FALSE;
14979 int limit = 100;
14980
14981 p = vim_strsave(p);
14982
14983 if (p[0] == '.' && (vim_ispathsep(p[1])
14984 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14985 is_relative_to_current = TRUE;
14986
14987 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014988 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989 has_trailing_pathsep = TRUE;
14990
14991 q = getnextcomp(p);
14992 if (*q != NUL)
14993 {
14994 /* Separate the first path component in "p", and keep the
14995 * remainder (beginning with the path separator). */
14996 remain = vim_strsave(q - 1);
14997 q[-1] = NUL;
14998 }
14999
Bram Moolenaard9462e32011-04-11 21:35:11 +020015000 buf = alloc(MAXPATHL + 1);
15001 if (buf == NULL)
15002 goto fail;
15003
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004 for (;;)
15005 {
15006 for (;;)
15007 {
15008 len = readlink((char *)p, (char *)buf, MAXPATHL);
15009 if (len <= 0)
15010 break;
15011 buf[len] = NUL;
15012
15013 if (limit-- == 0)
15014 {
15015 vim_free(p);
15016 vim_free(remain);
15017 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015018 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015019 goto fail;
15020 }
15021
15022 /* Ensure that the result will have a trailing path separator
15023 * if the argument has one. */
15024 if (remain == NULL && has_trailing_pathsep)
15025 add_pathsep(buf);
15026
15027 /* Separate the first path component in the link value and
15028 * concatenate the remainders. */
15029 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15030 if (*q != NUL)
15031 {
15032 if (remain == NULL)
15033 remain = vim_strsave(q - 1);
15034 else
15035 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015036 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037 if (cpy != NULL)
15038 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039 vim_free(remain);
15040 remain = cpy;
15041 }
15042 }
15043 q[-1] = NUL;
15044 }
15045
15046 q = gettail(p);
15047 if (q > p && *q == NUL)
15048 {
15049 /* Ignore trailing path separator. */
15050 q[-1] = NUL;
15051 q = gettail(p);
15052 }
15053 if (q > p && !mch_isFullName(buf))
15054 {
15055 /* symlink is relative to directory of argument */
15056 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15057 if (cpy != NULL)
15058 {
15059 STRCPY(cpy, p);
15060 STRCPY(gettail(cpy), buf);
15061 vim_free(p);
15062 p = cpy;
15063 }
15064 }
15065 else
15066 {
15067 vim_free(p);
15068 p = vim_strsave(buf);
15069 }
15070 }
15071
15072 if (remain == NULL)
15073 break;
15074
15075 /* Append the first path component of "remain" to "p". */
15076 q = getnextcomp(remain + 1);
15077 len = q - remain - (*q != NUL);
15078 cpy = vim_strnsave(p, STRLEN(p) + len);
15079 if (cpy != NULL)
15080 {
15081 STRNCAT(cpy, remain, len);
15082 vim_free(p);
15083 p = cpy;
15084 }
15085 /* Shorten "remain". */
15086 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015087 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088 else
15089 {
15090 vim_free(remain);
15091 remain = NULL;
15092 }
15093 }
15094
15095 /* If the result is a relative path name, make it explicitly relative to
15096 * the current directory if and only if the argument had this form. */
15097 if (!vim_ispathsep(*p))
15098 {
15099 if (is_relative_to_current
15100 && *p != NUL
15101 && !(p[0] == '.'
15102 && (p[1] == NUL
15103 || vim_ispathsep(p[1])
15104 || (p[1] == '.'
15105 && (p[2] == NUL
15106 || vim_ispathsep(p[2]))))))
15107 {
15108 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015109 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110 if (cpy != NULL)
15111 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015112 vim_free(p);
15113 p = cpy;
15114 }
15115 }
15116 else if (!is_relative_to_current)
15117 {
15118 /* Strip leading "./". */
15119 q = p;
15120 while (q[0] == '.' && vim_ispathsep(q[1]))
15121 q += 2;
15122 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015123 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015124 }
15125 }
15126
15127 /* Ensure that the result will have no trailing path separator
15128 * if the argument had none. But keep "/" or "//". */
15129 if (!has_trailing_pathsep)
15130 {
15131 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015132 if (after_pathsep(p, q))
15133 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015134 }
15135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015136 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015137 }
15138# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015139 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140# endif
15141#endif
15142
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015143 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144
15145#ifdef HAVE_READLINK
15146fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015147 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015148#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015149 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150}
15151
15152/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015153 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015154 */
15155 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015156f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015157 typval_T *argvars;
15158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015159{
Bram Moolenaar33570922005-01-25 22:26:29 +000015160 list_T *l;
15161 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162
Bram Moolenaar0d660222005-01-07 21:51:51 +000015163 if (argvars[0].v_type != VAR_LIST)
15164 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015165 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015166 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015167 {
15168 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015169 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015170 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015171 while (li != NULL)
15172 {
15173 ni = li->li_prev;
15174 list_append(l, li);
15175 li = ni;
15176 }
15177 rettv->vval.v_list = l;
15178 rettv->v_type = VAR_LIST;
15179 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015180 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182}
15183
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015184#define SP_NOMOVE 0x01 /* don't move cursor */
15185#define SP_REPEAT 0x02 /* repeat to find outer pair */
15186#define SP_RETCOUNT 0x04 /* return matchcount */
15187#define SP_SETPCMARK 0x08 /* set previous context mark */
15188#define SP_START 0x10 /* accept match at start position */
15189#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15190#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015191
Bram Moolenaar33570922005-01-25 22:26:29 +000015192static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015193
15194/*
15195 * Get flags for a search function.
15196 * Possibly sets "p_ws".
15197 * Returns BACKWARD, FORWARD or zero (for an error).
15198 */
15199 static int
15200get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015201 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015202 int *flagsp;
15203{
15204 int dir = FORWARD;
15205 char_u *flags;
15206 char_u nbuf[NUMBUFLEN];
15207 int mask;
15208
15209 if (varp->v_type != VAR_UNKNOWN)
15210 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015211 flags = get_tv_string_buf_chk(varp, nbuf);
15212 if (flags == NULL)
15213 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015214 while (*flags != NUL)
15215 {
15216 switch (*flags)
15217 {
15218 case 'b': dir = BACKWARD; break;
15219 case 'w': p_ws = TRUE; break;
15220 case 'W': p_ws = FALSE; break;
15221 default: mask = 0;
15222 if (flagsp != NULL)
15223 switch (*flags)
15224 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015225 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015226 case 'e': mask = SP_END; break;
15227 case 'm': mask = SP_RETCOUNT; break;
15228 case 'n': mask = SP_NOMOVE; break;
15229 case 'p': mask = SP_SUBPAT; break;
15230 case 'r': mask = SP_REPEAT; break;
15231 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015232 }
15233 if (mask == 0)
15234 {
15235 EMSG2(_(e_invarg2), flags);
15236 dir = 0;
15237 }
15238 else
15239 *flagsp |= mask;
15240 }
15241 if (dir == 0)
15242 break;
15243 ++flags;
15244 }
15245 }
15246 return dir;
15247}
15248
Bram Moolenaar071d4272004-06-13 20:20:40 +000015249/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015250 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015251 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015252 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015253search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015254 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015255 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015256 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015257{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015258 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015259 char_u *pat;
15260 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015261 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015262 int save_p_ws = p_ws;
15263 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015264 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015265 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015266 proftime_T tm;
15267#ifdef FEAT_RELTIME
15268 long time_limit = 0;
15269#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015270 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015271 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015273 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015274 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015275 if (dir == 0)
15276 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015277 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015278 if (flags & SP_START)
15279 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015280 if (flags & SP_END)
15281 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015282
Bram Moolenaar76929292008-01-06 19:07:36 +000015283 /* Optional arguments: line number to stop searching and timeout. */
15284 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015285 {
15286 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15287 if (lnum_stop < 0)
15288 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015289#ifdef FEAT_RELTIME
15290 if (argvars[3].v_type != VAR_UNKNOWN)
15291 {
15292 time_limit = get_tv_number_chk(&argvars[3], NULL);
15293 if (time_limit < 0)
15294 goto theend;
15295 }
15296#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015297 }
15298
Bram Moolenaar76929292008-01-06 19:07:36 +000015299#ifdef FEAT_RELTIME
15300 /* Set the time limit, if there is one. */
15301 profile_setlimit(time_limit, &tm);
15302#endif
15303
Bram Moolenaar231334e2005-07-25 20:46:57 +000015304 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015305 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015306 * Check to make sure only those flags are set.
15307 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15308 * flags cannot be set. Check for that condition also.
15309 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015310 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015311 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015312 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015313 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015314 goto theend;
15315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015316
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015317 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015318 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015319 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015320 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015321 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015322 if (flags & SP_SUBPAT)
15323 retval = subpatnum;
15324 else
15325 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015326 if (flags & SP_SETPCMARK)
15327 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015329 if (match_pos != NULL)
15330 {
15331 /* Store the match cursor position */
15332 match_pos->lnum = pos.lnum;
15333 match_pos->col = pos.col + 1;
15334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335 /* "/$" will put the cursor after the end of the line, may need to
15336 * correct that here */
15337 check_cursor();
15338 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015339
15340 /* If 'n' flag is used: restore cursor position. */
15341 if (flags & SP_NOMOVE)
15342 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015343 else
15344 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015345theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015346 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015347
15348 return retval;
15349}
15350
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015351#ifdef FEAT_FLOAT
15352/*
15353 * "round({float})" function
15354 */
15355 static void
15356f_round(argvars, rettv)
15357 typval_T *argvars;
15358 typval_T *rettv;
15359{
15360 float_T f;
15361
15362 rettv->v_type = VAR_FLOAT;
15363 if (get_float_arg(argvars, &f) == OK)
15364 /* round() is not in C90, use ceil() or floor() instead. */
15365 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15366 else
15367 rettv->vval.v_float = 0.0;
15368}
15369#endif
15370
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015371/*
15372 * "search()" function
15373 */
15374 static void
15375f_search(argvars, rettv)
15376 typval_T *argvars;
15377 typval_T *rettv;
15378{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015379 int flags = 0;
15380
15381 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015382}
15383
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015385 * "searchdecl()" function
15386 */
15387 static void
15388f_searchdecl(argvars, rettv)
15389 typval_T *argvars;
15390 typval_T *rettv;
15391{
15392 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015393 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015394 int error = FALSE;
15395 char_u *name;
15396
15397 rettv->vval.v_number = 1; /* default: FAIL */
15398
15399 name = get_tv_string_chk(&argvars[0]);
15400 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015401 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015402 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015403 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15404 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15405 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015406 if (!error && name != NULL)
15407 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015408 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015409}
15410
15411/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015412 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015414 static int
15415searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015416 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015417 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418{
15419 char_u *spat, *mpat, *epat;
15420 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015421 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015422 int dir;
15423 int flags = 0;
15424 char_u nbuf1[NUMBUFLEN];
15425 char_u nbuf2[NUMBUFLEN];
15426 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015427 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015428 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015429 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015432 spat = get_tv_string_chk(&argvars[0]);
15433 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15434 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15435 if (spat == NULL || mpat == NULL || epat == NULL)
15436 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015437
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438 /* Handle the optional fourth argument: flags */
15439 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015440 if (dir == 0)
15441 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015442
15443 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015444 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15445 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015446 if ((flags & (SP_END | SP_SUBPAT)) != 0
15447 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015448 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015449 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015450 goto theend;
15451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015452
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015453 /* Using 'r' implies 'W', otherwise it doesn't work. */
15454 if (flags & SP_REPEAT)
15455 p_ws = FALSE;
15456
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015457 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015458 if (argvars[3].v_type == VAR_UNKNOWN
15459 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460 skip = (char_u *)"";
15461 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015463 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015464 if (argvars[5].v_type != VAR_UNKNOWN)
15465 {
15466 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15467 if (lnum_stop < 0)
15468 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015469#ifdef FEAT_RELTIME
15470 if (argvars[6].v_type != VAR_UNKNOWN)
15471 {
15472 time_limit = get_tv_number_chk(&argvars[6], NULL);
15473 if (time_limit < 0)
15474 goto theend;
15475 }
15476#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015477 }
15478 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015479 if (skip == NULL)
15480 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015481
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015482 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015483 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015484
15485theend:
15486 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015487
15488 return retval;
15489}
15490
15491/*
15492 * "searchpair()" function
15493 */
15494 static void
15495f_searchpair(argvars, rettv)
15496 typval_T *argvars;
15497 typval_T *rettv;
15498{
15499 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15500}
15501
15502/*
15503 * "searchpairpos()" function
15504 */
15505 static void
15506f_searchpairpos(argvars, rettv)
15507 typval_T *argvars;
15508 typval_T *rettv;
15509{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015510 pos_T match_pos;
15511 int lnum = 0;
15512 int col = 0;
15513
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015514 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015515 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015516
15517 if (searchpair_cmn(argvars, &match_pos) > 0)
15518 {
15519 lnum = match_pos.lnum;
15520 col = match_pos.col;
15521 }
15522
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015523 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15524 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015525}
15526
15527/*
15528 * Search for a start/middle/end thing.
15529 * Used by searchpair(), see its documentation for the details.
15530 * Returns 0 or -1 for no match,
15531 */
15532 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015533do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15534 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015535 char_u *spat; /* start pattern */
15536 char_u *mpat; /* middle pattern */
15537 char_u *epat; /* end pattern */
15538 int dir; /* BACKWARD or FORWARD */
15539 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015540 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015541 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015542 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015543 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015544{
15545 char_u *save_cpo;
15546 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15547 long retval = 0;
15548 pos_T pos;
15549 pos_T firstpos;
15550 pos_T foundpos;
15551 pos_T save_cursor;
15552 pos_T save_pos;
15553 int n;
15554 int r;
15555 int nest = 1;
15556 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015557 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015558 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015559
15560 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15561 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015562 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015563
Bram Moolenaar76929292008-01-06 19:07:36 +000015564#ifdef FEAT_RELTIME
15565 /* Set the time limit, if there is one. */
15566 profile_setlimit(time_limit, &tm);
15567#endif
15568
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015569 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15570 * start/middle/end (pat3, for the top pair). */
15571 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15572 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15573 if (pat2 == NULL || pat3 == NULL)
15574 goto theend;
15575 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15576 if (*mpat == NUL)
15577 STRCPY(pat3, pat2);
15578 else
15579 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15580 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015581 if (flags & SP_START)
15582 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015583
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584 save_cursor = curwin->w_cursor;
15585 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015586 clearpos(&firstpos);
15587 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588 pat = pat3;
15589 for (;;)
15590 {
15591 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015592 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15594 /* didn't find it or found the first match again: FAIL */
15595 break;
15596
15597 if (firstpos.lnum == 0)
15598 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015599 if (equalpos(pos, foundpos))
15600 {
15601 /* Found the same position again. Can happen with a pattern that
15602 * has "\zs" at the end and searching backwards. Advance one
15603 * character and try again. */
15604 if (dir == BACKWARD)
15605 decl(&pos);
15606 else
15607 incl(&pos);
15608 }
15609 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015611 /* clear the start flag to avoid getting stuck here */
15612 options &= ~SEARCH_START;
15613
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614 /* If the skip pattern matches, ignore this match. */
15615 if (*skip != NUL)
15616 {
15617 save_pos = curwin->w_cursor;
15618 curwin->w_cursor = pos;
15619 r = eval_to_bool(skip, &err, NULL, FALSE);
15620 curwin->w_cursor = save_pos;
15621 if (err)
15622 {
15623 /* Evaluating {skip} caused an error, break here. */
15624 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015625 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015626 break;
15627 }
15628 if (r)
15629 continue;
15630 }
15631
15632 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15633 {
15634 /* Found end when searching backwards or start when searching
15635 * forward: nested pair. */
15636 ++nest;
15637 pat = pat2; /* nested, don't search for middle */
15638 }
15639 else
15640 {
15641 /* Found end when searching forward or start when searching
15642 * backward: end of (nested) pair; or found middle in outer pair. */
15643 if (--nest == 1)
15644 pat = pat3; /* outer level, search for middle */
15645 }
15646
15647 if (nest == 0)
15648 {
15649 /* Found the match: return matchcount or line number. */
15650 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015651 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015653 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015654 if (flags & SP_SETPCMARK)
15655 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015656 curwin->w_cursor = pos;
15657 if (!(flags & SP_REPEAT))
15658 break;
15659 nest = 1; /* search for next unmatched */
15660 }
15661 }
15662
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015663 if (match_pos != NULL)
15664 {
15665 /* Store the match cursor position */
15666 match_pos->lnum = curwin->w_cursor.lnum;
15667 match_pos->col = curwin->w_cursor.col + 1;
15668 }
15669
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015671 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672 curwin->w_cursor = save_cursor;
15673
15674theend:
15675 vim_free(pat2);
15676 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015677 if (p_cpo == empty_option)
15678 p_cpo = save_cpo;
15679 else
15680 /* Darn, evaluating the {skip} expression changed the value. */
15681 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015682
15683 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684}
15685
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015686/*
15687 * "searchpos()" function
15688 */
15689 static void
15690f_searchpos(argvars, rettv)
15691 typval_T *argvars;
15692 typval_T *rettv;
15693{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015694 pos_T match_pos;
15695 int lnum = 0;
15696 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015697 int n;
15698 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015699
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015700 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015701 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015702
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015703 n = search_cmn(argvars, &match_pos, &flags);
15704 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015705 {
15706 lnum = match_pos.lnum;
15707 col = match_pos.col;
15708 }
15709
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015710 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15711 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015712 if (flags & SP_SUBPAT)
15713 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015714}
15715
15716
Bram Moolenaar0d660222005-01-07 21:51:51 +000015717 static void
15718f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015719 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015722#ifdef FEAT_CLIENTSERVER
15723 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015724 char_u *server = get_tv_string_chk(&argvars[0]);
15725 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015726
Bram Moolenaar0d660222005-01-07 21:51:51 +000015727 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015728 if (server == NULL || reply == NULL)
15729 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015730 if (check_restricted() || check_secure())
15731 return;
15732# ifdef FEAT_X11
15733 if (check_connection() == FAIL)
15734 return;
15735# endif
15736
15737 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015739 EMSG(_("E258: Unable to send to client"));
15740 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015742 rettv->vval.v_number = 0;
15743#else
15744 rettv->vval.v_number = -1;
15745#endif
15746}
15747
Bram Moolenaar0d660222005-01-07 21:51:51 +000015748 static void
15749f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015750 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015751 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015752{
15753 char_u *r = NULL;
15754
15755#ifdef FEAT_CLIENTSERVER
15756# ifdef WIN32
15757 r = serverGetVimNames();
15758# else
15759 make_connection();
15760 if (X_DISPLAY != NULL)
15761 r = serverGetVimNames(X_DISPLAY);
15762# endif
15763#endif
15764 rettv->v_type = VAR_STRING;
15765 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015766}
15767
15768/*
15769 * "setbufvar()" function
15770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015772f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015773 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015774 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775{
15776 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015779 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780 char_u nbuf[NUMBUFLEN];
15781
15782 if (check_restricted() || check_secure())
15783 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015784 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15785 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015786 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015787 varp = &argvars[2];
15788
15789 if (buf != NULL && varname != NULL && varp != NULL)
15790 {
15791 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015792 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015793
15794 if (*varname == '&')
15795 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015796 long numval;
15797 char_u *strval;
15798 int error = FALSE;
15799
Bram Moolenaar071d4272004-06-13 20:20:40 +000015800 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015801 numval = get_tv_number_chk(varp, &error);
15802 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015803 if (!error && strval != NULL)
15804 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805 }
15806 else
15807 {
15808 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15809 if (bufvarname != NULL)
15810 {
15811 STRCPY(bufvarname, "b:");
15812 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015813 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814 vim_free(bufvarname);
15815 }
15816 }
15817
15818 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015819 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821}
15822
15823/*
15824 * "setcmdpos()" function
15825 */
15826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015827f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015828 typval_T *argvars;
15829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015830{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015831 int pos = (int)get_tv_number(&argvars[0]) - 1;
15832
15833 if (pos >= 0)
15834 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835}
15836
15837/*
15838 * "setline()" function
15839 */
15840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015841f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015842 typval_T *argvars;
15843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015844{
15845 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015846 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015847 list_T *l = NULL;
15848 listitem_T *li = NULL;
15849 long added = 0;
15850 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015852 lnum = get_tv_lnum(&argvars[0]);
15853 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015855 l = argvars[1].vval.v_list;
15856 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015858 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015859 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015860
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015861 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015862 for (;;)
15863 {
15864 if (l != NULL)
15865 {
15866 /* list argument, get next string */
15867 if (li == NULL)
15868 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015869 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015870 li = li->li_next;
15871 }
15872
15873 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015874 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015875 break;
15876 if (lnum <= curbuf->b_ml.ml_line_count)
15877 {
15878 /* existing line, replace it */
15879 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15880 {
15881 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015882 if (lnum == curwin->w_cursor.lnum)
15883 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015884 rettv->vval.v_number = 0; /* OK */
15885 }
15886 }
15887 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15888 {
15889 /* lnum is one past the last line, append the line */
15890 ++added;
15891 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15892 rettv->vval.v_number = 0; /* OK */
15893 }
15894
15895 if (l == NULL) /* only one string argument */
15896 break;
15897 ++lnum;
15898 }
15899
15900 if (added > 0)
15901 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015902}
15903
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015904static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15905
Bram Moolenaar071d4272004-06-13 20:20:40 +000015906/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015907 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015908 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015909 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015910set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015911 win_T *wp UNUSED;
15912 typval_T *list_arg UNUSED;
15913 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015914 typval_T *rettv;
15915{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015916#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015917 char_u *act;
15918 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015919#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015920
Bram Moolenaar2641f772005-03-25 21:58:17 +000015921 rettv->vval.v_number = -1;
15922
15923#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015924 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015925 EMSG(_(e_listreq));
15926 else
15927 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015928 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015929
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015930 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015931 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015932 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015933 if (act == NULL)
15934 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015935 if (*act == 'a' || *act == 'r')
15936 action = *act;
15937 }
15938
Bram Moolenaarbc226b62010-08-09 22:14:48 +020015939 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015940 rettv->vval.v_number = 0;
15941 }
15942#endif
15943}
15944
15945/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015946 * "setloclist()" function
15947 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015948 static void
15949f_setloclist(argvars, rettv)
15950 typval_T *argvars;
15951 typval_T *rettv;
15952{
15953 win_T *win;
15954
15955 rettv->vval.v_number = -1;
15956
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015957 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015958 if (win != NULL)
15959 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15960}
15961
15962/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015963 * "setmatches()" function
15964 */
15965 static void
15966f_setmatches(argvars, rettv)
15967 typval_T *argvars;
15968 typval_T *rettv;
15969{
15970#ifdef FEAT_SEARCH_EXTRA
15971 list_T *l;
15972 listitem_T *li;
15973 dict_T *d;
15974
15975 rettv->vval.v_number = -1;
15976 if (argvars[0].v_type != VAR_LIST)
15977 {
15978 EMSG(_(e_listreq));
15979 return;
15980 }
15981 if ((l = argvars[0].vval.v_list) != NULL)
15982 {
15983
15984 /* To some extent make sure that we are dealing with a list from
15985 * "getmatches()". */
15986 li = l->lv_first;
15987 while (li != NULL)
15988 {
15989 if (li->li_tv.v_type != VAR_DICT
15990 || (d = li->li_tv.vval.v_dict) == NULL)
15991 {
15992 EMSG(_(e_invarg));
15993 return;
15994 }
15995 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15996 && dict_find(d, (char_u *)"pattern", -1) != NULL
15997 && dict_find(d, (char_u *)"priority", -1) != NULL
15998 && dict_find(d, (char_u *)"id", -1) != NULL))
15999 {
16000 EMSG(_(e_invarg));
16001 return;
16002 }
16003 li = li->li_next;
16004 }
16005
16006 clear_matches(curwin);
16007 li = l->lv_first;
16008 while (li != NULL)
16009 {
16010 d = li->li_tv.vval.v_dict;
16011 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16012 get_dict_string(d, (char_u *)"pattern", FALSE),
16013 (int)get_dict_number(d, (char_u *)"priority"),
16014 (int)get_dict_number(d, (char_u *)"id"));
16015 li = li->li_next;
16016 }
16017 rettv->vval.v_number = 0;
16018 }
16019#endif
16020}
16021
16022/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016023 * "setpos()" function
16024 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016025 static void
16026f_setpos(argvars, rettv)
16027 typval_T *argvars;
16028 typval_T *rettv;
16029{
16030 pos_T pos;
16031 int fnum;
16032 char_u *name;
16033
Bram Moolenaar08250432008-02-13 11:42:46 +000016034 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016035 name = get_tv_string_chk(argvars);
16036 if (name != NULL)
16037 {
16038 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16039 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016040 if (--pos.col < 0)
16041 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016042 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016043 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016044 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016045 if (fnum == curbuf->b_fnum)
16046 {
16047 curwin->w_cursor = pos;
16048 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016049 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016050 }
16051 else
16052 EMSG(_(e_invarg));
16053 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016054 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16055 {
16056 /* set mark */
16057 if (setmark_pos(name[1], &pos, fnum) == OK)
16058 rettv->vval.v_number = 0;
16059 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016060 else
16061 EMSG(_(e_invarg));
16062 }
16063 }
16064}
16065
16066/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016067 * "setqflist()" function
16068 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016069 static void
16070f_setqflist(argvars, rettv)
16071 typval_T *argvars;
16072 typval_T *rettv;
16073{
16074 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16075}
16076
16077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016078 * "setreg()" function
16079 */
16080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016081f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016082 typval_T *argvars;
16083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084{
16085 int regname;
16086 char_u *strregname;
16087 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016088 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089 int append;
16090 char_u yank_type;
16091 long block_len;
16092
16093 block_len = -1;
16094 yank_type = MAUTO;
16095 append = FALSE;
16096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016097 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016098 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016099
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016100 if (strregname == NULL)
16101 return; /* type error; errmsg already given */
16102 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103 if (regname == 0 || regname == '@')
16104 regname = '"';
16105 else if (regname == '=')
16106 return;
16107
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016108 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016110 stropt = get_tv_string_chk(&argvars[2]);
16111 if (stropt == NULL)
16112 return; /* type error */
16113 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016114 switch (*stropt)
16115 {
16116 case 'a': case 'A': /* append */
16117 append = TRUE;
16118 break;
16119 case 'v': case 'c': /* character-wise selection */
16120 yank_type = MCHAR;
16121 break;
16122 case 'V': case 'l': /* line-wise selection */
16123 yank_type = MLINE;
16124 break;
16125#ifdef FEAT_VISUAL
16126 case 'b': case Ctrl_V: /* block-wise selection */
16127 yank_type = MBLOCK;
16128 if (VIM_ISDIGIT(stropt[1]))
16129 {
16130 ++stropt;
16131 block_len = getdigits(&stropt) - 1;
16132 --stropt;
16133 }
16134 break;
16135#endif
16136 }
16137 }
16138
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016139 strval = get_tv_string_chk(&argvars[1]);
16140 if (strval != NULL)
16141 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016143 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016144}
16145
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016146/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016147 * "settabvar()" function
16148 */
16149 static void
16150f_settabvar(argvars, rettv)
16151 typval_T *argvars;
16152 typval_T *rettv;
16153{
16154 tabpage_T *save_curtab;
16155 char_u *varname, *tabvarname;
16156 typval_T *varp;
16157 tabpage_T *tp;
16158
16159 rettv->vval.v_number = 0;
16160
16161 if (check_restricted() || check_secure())
16162 return;
16163
16164 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16165 varname = get_tv_string_chk(&argvars[1]);
16166 varp = &argvars[2];
16167
16168 if (tp != NULL && varname != NULL && varp != NULL)
16169 {
16170 save_curtab = curtab;
16171 goto_tabpage_tp(tp);
16172
16173 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16174 if (tabvarname != NULL)
16175 {
16176 STRCPY(tabvarname, "t:");
16177 STRCPY(tabvarname + 2, varname);
16178 set_var(tabvarname, varp, TRUE);
16179 vim_free(tabvarname);
16180 }
16181
16182 /* Restore current tabpage */
16183 if (valid_tabpage(save_curtab))
16184 goto_tabpage_tp(save_curtab);
16185 }
16186}
16187
16188/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016189 * "settabwinvar()" function
16190 */
16191 static void
16192f_settabwinvar(argvars, rettv)
16193 typval_T *argvars;
16194 typval_T *rettv;
16195{
16196 setwinvar(argvars, rettv, 1);
16197}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016198
16199/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016200 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016203f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016204 typval_T *argvars;
16205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016207 setwinvar(argvars, rettv, 0);
16208}
16209
16210/*
16211 * "setwinvar()" and "settabwinvar()" functions
16212 */
16213 static void
16214setwinvar(argvars, rettv, off)
16215 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016216 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016217 int off;
16218{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016219 win_T *win;
16220#ifdef FEAT_WINDOWS
16221 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016222 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223#endif
16224 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016225 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016227 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228
16229 if (check_restricted() || check_secure())
16230 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016231
16232#ifdef FEAT_WINDOWS
16233 if (off == 1)
16234 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16235 else
16236 tp = curtab;
16237#endif
16238 win = find_win_by_nr(&argvars[off], tp);
16239 varname = get_tv_string_chk(&argvars[off + 1]);
16240 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016241
16242 if (win != NULL && varname != NULL && varp != NULL)
16243 {
16244#ifdef FEAT_WINDOWS
16245 /* set curwin to be our win, temporarily */
16246 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016247 save_curtab = curtab;
16248 goto_tabpage_tp(tp);
16249 if (!win_valid(win))
16250 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251 curwin = win;
16252 curbuf = curwin->w_buffer;
16253#endif
16254
16255 if (*varname == '&')
16256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016257 long numval;
16258 char_u *strval;
16259 int error = FALSE;
16260
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016262 numval = get_tv_number_chk(varp, &error);
16263 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016264 if (!error && strval != NULL)
16265 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266 }
16267 else
16268 {
16269 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16270 if (winvarname != NULL)
16271 {
16272 STRCPY(winvarname, "w:");
16273 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016274 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016275 vim_free(winvarname);
16276 }
16277 }
16278
16279#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016280 /* Restore current tabpage and window, if still valid (autocomands can
16281 * make them invalid). */
16282 if (valid_tabpage(save_curtab))
16283 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284 if (win_valid(save_curwin))
16285 {
16286 curwin = save_curwin;
16287 curbuf = curwin->w_buffer;
16288 }
16289#endif
16290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291}
16292
16293/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016294 * "shellescape({string})" function
16295 */
16296 static void
16297f_shellescape(argvars, rettv)
16298 typval_T *argvars;
16299 typval_T *rettv;
16300{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016301 rettv->vval.v_string = vim_strsave_shellescape(
16302 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016303 rettv->v_type = VAR_STRING;
16304}
16305
16306/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016307 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308 */
16309 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016310f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016311 typval_T *argvars;
16312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016313{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016314 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315
Bram Moolenaar0d660222005-01-07 21:51:51 +000016316 p = get_tv_string(&argvars[0]);
16317 rettv->vval.v_string = vim_strsave(p);
16318 simplify_filename(rettv->vval.v_string); /* simplify in place */
16319 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320}
16321
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016322#ifdef FEAT_FLOAT
16323/*
16324 * "sin()" function
16325 */
16326 static void
16327f_sin(argvars, rettv)
16328 typval_T *argvars;
16329 typval_T *rettv;
16330{
16331 float_T f;
16332
16333 rettv->v_type = VAR_FLOAT;
16334 if (get_float_arg(argvars, &f) == OK)
16335 rettv->vval.v_float = sin(f);
16336 else
16337 rettv->vval.v_float = 0.0;
16338}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016339
16340/*
16341 * "sinh()" function
16342 */
16343 static void
16344f_sinh(argvars, rettv)
16345 typval_T *argvars;
16346 typval_T *rettv;
16347{
16348 float_T f;
16349
16350 rettv->v_type = VAR_FLOAT;
16351 if (get_float_arg(argvars, &f) == OK)
16352 rettv->vval.v_float = sinh(f);
16353 else
16354 rettv->vval.v_float = 0.0;
16355}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016356#endif
16357
Bram Moolenaar0d660222005-01-07 21:51:51 +000016358static int
16359#ifdef __BORLANDC__
16360 _RTLENTRYF
16361#endif
16362 item_compare __ARGS((const void *s1, const void *s2));
16363static int
16364#ifdef __BORLANDC__
16365 _RTLENTRYF
16366#endif
16367 item_compare2 __ARGS((const void *s1, const void *s2));
16368
16369static int item_compare_ic;
16370static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016371static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016372#define ITEM_COMPARE_FAIL 999
16373
Bram Moolenaar071d4272004-06-13 20:20:40 +000016374/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016375 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016377 static int
16378#ifdef __BORLANDC__
16379_RTLENTRYF
16380#endif
16381item_compare(s1, s2)
16382 const void *s1;
16383 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016384{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016385 char_u *p1, *p2;
16386 char_u *tofree1, *tofree2;
16387 int res;
16388 char_u numbuf1[NUMBUFLEN];
16389 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016391 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16392 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016393 if (p1 == NULL)
16394 p1 = (char_u *)"";
16395 if (p2 == NULL)
16396 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016397 if (item_compare_ic)
16398 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016399 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016400 res = STRCMP(p1, p2);
16401 vim_free(tofree1);
16402 vim_free(tofree2);
16403 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404}
16405
16406 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016407#ifdef __BORLANDC__
16408_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016410item_compare2(s1, s2)
16411 const void *s1;
16412 const void *s2;
16413{
16414 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016415 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016416 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016417 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016419 /* shortcut after failure in previous call; compare all items equal */
16420 if (item_compare_func_err)
16421 return 0;
16422
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016423 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16424 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016425 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16426 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016427
16428 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016429 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016430 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016431 clear_tv(&argv[0]);
16432 clear_tv(&argv[1]);
16433
16434 if (res == FAIL)
16435 res = ITEM_COMPARE_FAIL;
16436 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016437 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16438 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016439 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016440 clear_tv(&rettv);
16441 return res;
16442}
16443
16444/*
16445 * "sort({list})" function
16446 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016448f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016449 typval_T *argvars;
16450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451{
Bram Moolenaar33570922005-01-25 22:26:29 +000016452 list_T *l;
16453 listitem_T *li;
16454 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016455 long len;
16456 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457
Bram Moolenaar0d660222005-01-07 21:51:51 +000016458 if (argvars[0].v_type != VAR_LIST)
16459 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 else
16461 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016462 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016463 if (l == NULL || tv_check_lock(l->lv_lock,
16464 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016465 return;
16466 rettv->vval.v_list = l;
16467 rettv->v_type = VAR_LIST;
16468 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469
Bram Moolenaar0d660222005-01-07 21:51:51 +000016470 len = list_len(l);
16471 if (len <= 1)
16472 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016473
Bram Moolenaar0d660222005-01-07 21:51:51 +000016474 item_compare_ic = FALSE;
16475 item_compare_func = NULL;
16476 if (argvars[1].v_type != VAR_UNKNOWN)
16477 {
16478 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016479 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016480 else
16481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016482 int error = FALSE;
16483
16484 i = get_tv_number_chk(&argvars[1], &error);
16485 if (error)
16486 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016487 if (i == 1)
16488 item_compare_ic = TRUE;
16489 else
16490 item_compare_func = get_tv_string(&argvars[1]);
16491 }
16492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016493
Bram Moolenaar0d660222005-01-07 21:51:51 +000016494 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016495 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016496 if (ptrs == NULL)
16497 return;
16498 i = 0;
16499 for (li = l->lv_first; li != NULL; li = li->li_next)
16500 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016502 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016503 /* test the compare function */
16504 if (item_compare_func != NULL
16505 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16506 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016507 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016508 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016509 {
16510 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016511 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016512 item_compare_func == NULL ? item_compare : item_compare2);
16513
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016514 if (!item_compare_func_err)
16515 {
16516 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016517 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016518 l->lv_len = 0;
16519 for (i = 0; i < len; ++i)
16520 list_append(l, ptrs[i]);
16521 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016522 }
16523
16524 vim_free(ptrs);
16525 }
16526}
16527
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016528/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016529 * "soundfold({word})" function
16530 */
16531 static void
16532f_soundfold(argvars, rettv)
16533 typval_T *argvars;
16534 typval_T *rettv;
16535{
16536 char_u *s;
16537
16538 rettv->v_type = VAR_STRING;
16539 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016540#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016541 rettv->vval.v_string = eval_soundfold(s);
16542#else
16543 rettv->vval.v_string = vim_strsave(s);
16544#endif
16545}
16546
16547/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016548 * "spellbadword()" function
16549 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016550 static void
16551f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016552 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016553 typval_T *rettv;
16554{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016555 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016556 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016557 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016558
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016559 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016560 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016561
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016562#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016563 if (argvars[0].v_type == VAR_UNKNOWN)
16564 {
16565 /* Find the start and length of the badly spelled word. */
16566 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16567 if (len != 0)
16568 word = ml_get_cursor();
16569 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016570 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016571 {
16572 char_u *str = get_tv_string_chk(&argvars[0]);
16573 int capcol = -1;
16574
16575 if (str != NULL)
16576 {
16577 /* Check the argument for spelling. */
16578 while (*str != NUL)
16579 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016580 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016581 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016582 {
16583 word = str;
16584 break;
16585 }
16586 str += len;
16587 }
16588 }
16589 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016590#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016591
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016592 list_append_string(rettv->vval.v_list, word, len);
16593 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016594 attr == HLF_SPB ? "bad" :
16595 attr == HLF_SPR ? "rare" :
16596 attr == HLF_SPL ? "local" :
16597 attr == HLF_SPC ? "caps" :
16598 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016599}
16600
16601/*
16602 * "spellsuggest()" function
16603 */
16604 static void
16605f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016606 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016607 typval_T *rettv;
16608{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016609#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016610 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016611 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016612 int maxcount;
16613 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016614 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016615 listitem_T *li;
16616 int need_capital = FALSE;
16617#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016618
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016619 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016620 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016621
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016622#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016623 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016624 {
16625 str = get_tv_string(&argvars[0]);
16626 if (argvars[1].v_type != VAR_UNKNOWN)
16627 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016628 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016629 if (maxcount <= 0)
16630 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016631 if (argvars[2].v_type != VAR_UNKNOWN)
16632 {
16633 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16634 if (typeerr)
16635 return;
16636 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016637 }
16638 else
16639 maxcount = 25;
16640
Bram Moolenaar4770d092006-01-12 23:22:24 +000016641 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016642
16643 for (i = 0; i < ga.ga_len; ++i)
16644 {
16645 str = ((char_u **)ga.ga_data)[i];
16646
16647 li = listitem_alloc();
16648 if (li == NULL)
16649 vim_free(str);
16650 else
16651 {
16652 li->li_tv.v_type = VAR_STRING;
16653 li->li_tv.v_lock = 0;
16654 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016655 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016656 }
16657 }
16658 ga_clear(&ga);
16659 }
16660#endif
16661}
16662
Bram Moolenaar0d660222005-01-07 21:51:51 +000016663 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016664f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016665 typval_T *argvars;
16666 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016667{
16668 char_u *str;
16669 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016670 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016671 regmatch_T regmatch;
16672 char_u patbuf[NUMBUFLEN];
16673 char_u *save_cpo;
16674 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016675 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016676 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016677 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016678
16679 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16680 save_cpo = p_cpo;
16681 p_cpo = (char_u *)"";
16682
16683 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016684 if (argvars[1].v_type != VAR_UNKNOWN)
16685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016686 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16687 if (pat == NULL)
16688 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016689 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016690 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016691 }
16692 if (pat == NULL || *pat == NUL)
16693 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016694
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016695 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016696 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016697 if (typeerr)
16698 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699
Bram Moolenaar0d660222005-01-07 21:51:51 +000016700 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16701 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016702 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016703 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016704 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016705 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016706 if (*str == NUL)
16707 match = FALSE; /* empty item at the end */
16708 else
16709 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016710 if (match)
16711 end = regmatch.startp[0];
16712 else
16713 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016714 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16715 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016716 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016717 if (list_append_string(rettv->vval.v_list, str,
16718 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016719 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016720 }
16721 if (!match)
16722 break;
16723 /* Advance to just after the match. */
16724 if (regmatch.endp[0] > str)
16725 col = 0;
16726 else
16727 {
16728 /* Don't get stuck at the same match. */
16729#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016730 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016731#else
16732 col = 1;
16733#endif
16734 }
16735 str = regmatch.endp[0];
16736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737
Bram Moolenaar0d660222005-01-07 21:51:51 +000016738 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740
Bram Moolenaar0d660222005-01-07 21:51:51 +000016741 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016742}
16743
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016744#ifdef FEAT_FLOAT
16745/*
16746 * "sqrt()" function
16747 */
16748 static void
16749f_sqrt(argvars, rettv)
16750 typval_T *argvars;
16751 typval_T *rettv;
16752{
16753 float_T f;
16754
16755 rettv->v_type = VAR_FLOAT;
16756 if (get_float_arg(argvars, &f) == OK)
16757 rettv->vval.v_float = sqrt(f);
16758 else
16759 rettv->vval.v_float = 0.0;
16760}
16761
16762/*
16763 * "str2float()" function
16764 */
16765 static void
16766f_str2float(argvars, rettv)
16767 typval_T *argvars;
16768 typval_T *rettv;
16769{
16770 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16771
16772 if (*p == '+')
16773 p = skipwhite(p + 1);
16774 (void)string2float(p, &rettv->vval.v_float);
16775 rettv->v_type = VAR_FLOAT;
16776}
16777#endif
16778
Bram Moolenaar2c932302006-03-18 21:42:09 +000016779/*
16780 * "str2nr()" function
16781 */
16782 static void
16783f_str2nr(argvars, rettv)
16784 typval_T *argvars;
16785 typval_T *rettv;
16786{
16787 int base = 10;
16788 char_u *p;
16789 long n;
16790
16791 if (argvars[1].v_type != VAR_UNKNOWN)
16792 {
16793 base = get_tv_number(&argvars[1]);
16794 if (base != 8 && base != 10 && base != 16)
16795 {
16796 EMSG(_(e_invarg));
16797 return;
16798 }
16799 }
16800
16801 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016802 if (*p == '+')
16803 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016804 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16805 rettv->vval.v_number = n;
16806}
16807
Bram Moolenaar071d4272004-06-13 20:20:40 +000016808#ifdef HAVE_STRFTIME
16809/*
16810 * "strftime({format}[, {time}])" function
16811 */
16812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016813f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016814 typval_T *argvars;
16815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816{
16817 char_u result_buf[256];
16818 struct tm *curtime;
16819 time_t seconds;
16820 char_u *p;
16821
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016822 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016823
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016824 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016825 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826 seconds = time(NULL);
16827 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016828 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016829 curtime = localtime(&seconds);
16830 /* MSVC returns NULL for an invalid value of seconds. */
16831 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016832 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016833 else
16834 {
16835# ifdef FEAT_MBYTE
16836 vimconv_T conv;
16837 char_u *enc;
16838
16839 conv.vc_type = CONV_NONE;
16840 enc = enc_locale();
16841 convert_setup(&conv, p_enc, enc);
16842 if (conv.vc_type != CONV_NONE)
16843 p = string_convert(&conv, p, NULL);
16844# endif
16845 if (p != NULL)
16846 (void)strftime((char *)result_buf, sizeof(result_buf),
16847 (char *)p, curtime);
16848 else
16849 result_buf[0] = NUL;
16850
16851# ifdef FEAT_MBYTE
16852 if (conv.vc_type != CONV_NONE)
16853 vim_free(p);
16854 convert_setup(&conv, enc, p_enc);
16855 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016856 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857 else
16858# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016859 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016860
16861# ifdef FEAT_MBYTE
16862 /* Release conversion descriptors */
16863 convert_setup(&conv, NULL, NULL);
16864 vim_free(enc);
16865# endif
16866 }
16867}
16868#endif
16869
16870/*
16871 * "stridx()" function
16872 */
16873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016874f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016875 typval_T *argvars;
16876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016877{
16878 char_u buf[NUMBUFLEN];
16879 char_u *needle;
16880 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016881 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016882 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016883 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016885 needle = get_tv_string_chk(&argvars[1]);
16886 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016887 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016888 if (needle == NULL || haystack == NULL)
16889 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890
Bram Moolenaar33570922005-01-25 22:26:29 +000016891 if (argvars[2].v_type != VAR_UNKNOWN)
16892 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016893 int error = FALSE;
16894
16895 start_idx = get_tv_number_chk(&argvars[2], &error);
16896 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016897 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016898 if (start_idx >= 0)
16899 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016900 }
16901
16902 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16903 if (pos != NULL)
16904 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016905}
16906
16907/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016908 * "string()" function
16909 */
16910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016911f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016912 typval_T *argvars;
16913 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016914{
16915 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016916 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016917
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016918 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016919 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016920 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016921 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016922 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016923}
16924
16925/*
16926 * "strlen()" function
16927 */
16928 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016929f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016930 typval_T *argvars;
16931 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016933 rettv->vval.v_number = (varnumber_T)(STRLEN(
16934 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935}
16936
16937/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016938 * "strchars()" function
16939 */
16940 static void
16941f_strchars(argvars, rettv)
16942 typval_T *argvars;
16943 typval_T *rettv;
16944{
16945 char_u *s = get_tv_string(&argvars[0]);
16946#ifdef FEAT_MBYTE
16947 varnumber_T len = 0;
16948
16949 while (*s != NUL)
16950 {
16951 mb_cptr2char_adv(&s);
16952 ++len;
16953 }
16954 rettv->vval.v_number = len;
16955#else
16956 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16957#endif
16958}
16959
16960/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016961 * "strdisplaywidth()" function
16962 */
16963 static void
16964f_strdisplaywidth(argvars, rettv)
16965 typval_T *argvars;
16966 typval_T *rettv;
16967{
16968 char_u *s = get_tv_string(&argvars[0]);
16969 int col = 0;
16970
16971 if (argvars[1].v_type != VAR_UNKNOWN)
16972 col = get_tv_number(&argvars[1]);
16973
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016974 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016975}
16976
16977/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016978 * "strwidth()" function
16979 */
16980 static void
16981f_strwidth(argvars, rettv)
16982 typval_T *argvars;
16983 typval_T *rettv;
16984{
16985 char_u *s = get_tv_string(&argvars[0]);
16986
16987 rettv->vval.v_number = (varnumber_T)(
16988#ifdef FEAT_MBYTE
16989 mb_string2cells(s, -1)
16990#else
16991 STRLEN(s)
16992#endif
16993 );
16994}
16995
16996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997 * "strpart()" function
16998 */
16999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017000f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017001 typval_T *argvars;
17002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003{
17004 char_u *p;
17005 int n;
17006 int len;
17007 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017008 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017010 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011 slen = (int)STRLEN(p);
17012
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017013 n = get_tv_number_chk(&argvars[1], &error);
17014 if (error)
17015 len = 0;
17016 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017017 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018 else
17019 len = slen - n; /* default len: all bytes that are available. */
17020
17021 /*
17022 * Only return the overlap between the specified part and the actual
17023 * string.
17024 */
17025 if (n < 0)
17026 {
17027 len += n;
17028 n = 0;
17029 }
17030 else if (n > slen)
17031 n = slen;
17032 if (len < 0)
17033 len = 0;
17034 else if (n + len > slen)
17035 len = slen - n;
17036
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017037 rettv->v_type = VAR_STRING;
17038 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039}
17040
17041/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017042 * "strridx()" function
17043 */
17044 static void
17045f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017046 typval_T *argvars;
17047 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017048{
17049 char_u buf[NUMBUFLEN];
17050 char_u *needle;
17051 char_u *haystack;
17052 char_u *rest;
17053 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017054 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017056 needle = get_tv_string_chk(&argvars[1]);
17057 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017058
17059 rettv->vval.v_number = -1;
17060 if (needle == NULL || haystack == NULL)
17061 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017062
17063 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017064 if (argvars[2].v_type != VAR_UNKNOWN)
17065 {
17066 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017067 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017068 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017069 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017070 }
17071 else
17072 end_idx = haystack_len;
17073
Bram Moolenaar0d660222005-01-07 21:51:51 +000017074 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017075 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017076 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017077 lastmatch = haystack + end_idx;
17078 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017079 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017080 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017081 for (rest = haystack; *rest != '\0'; ++rest)
17082 {
17083 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017084 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017085 break;
17086 lastmatch = rest;
17087 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017088 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017089
17090 if (lastmatch == NULL)
17091 rettv->vval.v_number = -1;
17092 else
17093 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17094}
17095
17096/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097 * "strtrans()" function
17098 */
17099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017100f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017101 typval_T *argvars;
17102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017103{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017104 rettv->v_type = VAR_STRING;
17105 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106}
17107
17108/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017109 * "submatch()" function
17110 */
17111 static void
17112f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017113 typval_T *argvars;
17114 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017115{
17116 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017117 rettv->vval.v_string =
17118 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017119}
17120
17121/*
17122 * "substitute()" function
17123 */
17124 static void
17125f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017126 typval_T *argvars;
17127 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017128{
17129 char_u patbuf[NUMBUFLEN];
17130 char_u subbuf[NUMBUFLEN];
17131 char_u flagsbuf[NUMBUFLEN];
17132
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017133 char_u *str = get_tv_string_chk(&argvars[0]);
17134 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17135 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17136 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17137
Bram Moolenaar0d660222005-01-07 21:51:51 +000017138 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017139 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17140 rettv->vval.v_string = NULL;
17141 else
17142 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017143}
17144
17145/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017146 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017149f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017150 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152{
17153 int id = 0;
17154#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017155 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156 long col;
17157 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017158 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017160 lnum = get_tv_lnum(argvars); /* -1 on type error */
17161 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17162 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017164 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017165 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017166 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167#endif
17168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017169 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170}
17171
17172/*
17173 * "synIDattr(id, what [, mode])" function
17174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017176f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017177 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179{
17180 char_u *p = NULL;
17181#ifdef FEAT_SYN_HL
17182 int id;
17183 char_u *what;
17184 char_u *mode;
17185 char_u modebuf[NUMBUFLEN];
17186 int modec;
17187
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017188 id = get_tv_number(&argvars[0]);
17189 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017190 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017192 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017194 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017195 modec = 0; /* replace invalid with current */
17196 }
17197 else
17198 {
17199#ifdef FEAT_GUI
17200 if (gui.in_use)
17201 modec = 'g';
17202 else
17203#endif
17204 if (t_colors > 1)
17205 modec = 'c';
17206 else
17207 modec = 't';
17208 }
17209
17210
17211 switch (TOLOWER_ASC(what[0]))
17212 {
17213 case 'b':
17214 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17215 p = highlight_color(id, what, modec);
17216 else /* bold */
17217 p = highlight_has_attr(id, HL_BOLD, modec);
17218 break;
17219
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017220 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221 p = highlight_color(id, what, modec);
17222 break;
17223
17224 case 'i':
17225 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17226 p = highlight_has_attr(id, HL_INVERSE, modec);
17227 else /* italic */
17228 p = highlight_has_attr(id, HL_ITALIC, modec);
17229 break;
17230
17231 case 'n': /* name */
17232 p = get_highlight_name(NULL, id - 1);
17233 break;
17234
17235 case 'r': /* reverse */
17236 p = highlight_has_attr(id, HL_INVERSE, modec);
17237 break;
17238
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017239 case 's':
17240 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17241 p = highlight_color(id, what, modec);
17242 else /* standout */
17243 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244 break;
17245
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017246 case 'u':
17247 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17248 /* underline */
17249 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17250 else
17251 /* undercurl */
17252 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253 break;
17254 }
17255
17256 if (p != NULL)
17257 p = vim_strsave(p);
17258#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017259 rettv->v_type = VAR_STRING;
17260 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261}
17262
17263/*
17264 * "synIDtrans(id)" function
17265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017267f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017268 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270{
17271 int id;
17272
17273#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017274 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275
17276 if (id > 0)
17277 id = syn_get_final_id(id);
17278 else
17279#endif
17280 id = 0;
17281
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017282 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283}
17284
17285/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017286 * "synconcealed(lnum, col)" function
17287 */
17288 static void
17289f_synconcealed(argvars, rettv)
17290 typval_T *argvars UNUSED;
17291 typval_T *rettv;
17292{
17293#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17294 long lnum;
17295 long col;
17296 int syntax_flags = 0;
17297 int cchar;
17298 int matchid = 0;
17299 char_u str[NUMBUFLEN];
17300#endif
17301
17302 rettv->v_type = VAR_LIST;
17303 rettv->vval.v_list = NULL;
17304
17305#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17306 lnum = get_tv_lnum(argvars); /* -1 on type error */
17307 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17308
17309 vim_memset(str, NUL, sizeof(str));
17310
17311 if (rettv_list_alloc(rettv) != FAIL)
17312 {
17313 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17314 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17315 && curwin->w_p_cole > 0)
17316 {
17317 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17318 syntax_flags = get_syntax_info(&matchid);
17319
17320 /* get the conceal character */
17321 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17322 {
17323 cchar = syn_get_sub_char();
17324 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17325 cchar = lcs_conceal;
17326 if (cchar != NUL)
17327 {
17328# ifdef FEAT_MBYTE
17329 if (has_mbyte)
17330 (*mb_char2bytes)(cchar, str);
17331 else
17332# endif
17333 str[0] = cchar;
17334 }
17335 }
17336 }
17337
17338 list_append_number(rettv->vval.v_list,
17339 (syntax_flags & HL_CONCEAL) != 0);
17340 /* -1 to auto-determine strlen */
17341 list_append_string(rettv->vval.v_list, str, -1);
17342 list_append_number(rettv->vval.v_list, matchid);
17343 }
17344#endif
17345}
17346
17347/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017348 * "synstack(lnum, col)" function
17349 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017350 static void
17351f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017352 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017353 typval_T *rettv;
17354{
17355#ifdef FEAT_SYN_HL
17356 long lnum;
17357 long col;
17358 int i;
17359 int id;
17360#endif
17361
17362 rettv->v_type = VAR_LIST;
17363 rettv->vval.v_list = NULL;
17364
17365#ifdef FEAT_SYN_HL
17366 lnum = get_tv_lnum(argvars); /* -1 on type error */
17367 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17368
17369 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017370 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017371 && rettv_list_alloc(rettv) != FAIL)
17372 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017373 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017374 for (i = 0; ; ++i)
17375 {
17376 id = syn_get_stack_item(i);
17377 if (id < 0)
17378 break;
17379 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17380 break;
17381 }
17382 }
17383#endif
17384}
17385
17386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387 * "system()" function
17388 */
17389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017390f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017391 typval_T *argvars;
17392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017394 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017396 char_u *infile = NULL;
17397 char_u buf[NUMBUFLEN];
17398 int err = FALSE;
17399 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017400
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017401 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017402 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017403
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017404 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017405 {
17406 /*
17407 * Write the string to a temp file, to be used for input of the shell
17408 * command.
17409 */
17410 if ((infile = vim_tempname('i')) == NULL)
17411 {
17412 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017413 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017414 }
17415
17416 fd = mch_fopen((char *)infile, WRITEBIN);
17417 if (fd == NULL)
17418 {
17419 EMSG2(_(e_notopen), infile);
17420 goto done;
17421 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017422 p = get_tv_string_buf_chk(&argvars[1], buf);
17423 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017424 {
17425 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017426 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017427 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017428 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17429 err = TRUE;
17430 if (fclose(fd) != 0)
17431 err = TRUE;
17432 if (err)
17433 {
17434 EMSG(_("E677: Error writing temp file"));
17435 goto done;
17436 }
17437 }
17438
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017439 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17440 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017441
Bram Moolenaar071d4272004-06-13 20:20:40 +000017442#ifdef USE_CR
17443 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017444 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017445 {
17446 char_u *s;
17447
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017448 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017449 {
17450 if (*s == CAR)
17451 *s = NL;
17452 }
17453 }
17454#else
17455# ifdef USE_CRNL
17456 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017457 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017458 {
17459 char_u *s, *d;
17460
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017461 d = res;
17462 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463 {
17464 if (s[0] == CAR && s[1] == NL)
17465 ++s;
17466 *d++ = *s;
17467 }
17468 *d = NUL;
17469 }
17470# endif
17471#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017472
17473done:
17474 if (infile != NULL)
17475 {
17476 mch_remove(infile);
17477 vim_free(infile);
17478 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017479 rettv->v_type = VAR_STRING;
17480 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481}
17482
17483/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017484 * "tabpagebuflist()" function
17485 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017486 static void
17487f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017488 typval_T *argvars UNUSED;
17489 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017490{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017491#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017492 tabpage_T *tp;
17493 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017494
17495 if (argvars[0].v_type == VAR_UNKNOWN)
17496 wp = firstwin;
17497 else
17498 {
17499 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17500 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017501 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017502 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017503 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017504 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017505 for (; wp != NULL; wp = wp->w_next)
17506 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017507 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017508 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017509 }
17510#endif
17511}
17512
17513
17514/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017515 * "tabpagenr()" function
17516 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017517 static void
17518f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017519 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017520 typval_T *rettv;
17521{
17522 int nr = 1;
17523#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017524 char_u *arg;
17525
17526 if (argvars[0].v_type != VAR_UNKNOWN)
17527 {
17528 arg = get_tv_string_chk(&argvars[0]);
17529 nr = 0;
17530 if (arg != NULL)
17531 {
17532 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017533 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017534 else
17535 EMSG2(_(e_invexpr2), arg);
17536 }
17537 }
17538 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017539 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017540#endif
17541 rettv->vval.v_number = nr;
17542}
17543
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017544
17545#ifdef FEAT_WINDOWS
17546static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17547
17548/*
17549 * Common code for tabpagewinnr() and winnr().
17550 */
17551 static int
17552get_winnr(tp, argvar)
17553 tabpage_T *tp;
17554 typval_T *argvar;
17555{
17556 win_T *twin;
17557 int nr = 1;
17558 win_T *wp;
17559 char_u *arg;
17560
17561 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17562 if (argvar->v_type != VAR_UNKNOWN)
17563 {
17564 arg = get_tv_string_chk(argvar);
17565 if (arg == NULL)
17566 nr = 0; /* type error; errmsg already given */
17567 else if (STRCMP(arg, "$") == 0)
17568 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17569 else if (STRCMP(arg, "#") == 0)
17570 {
17571 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17572 if (twin == NULL)
17573 nr = 0;
17574 }
17575 else
17576 {
17577 EMSG2(_(e_invexpr2), arg);
17578 nr = 0;
17579 }
17580 }
17581
17582 if (nr > 0)
17583 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17584 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017585 {
17586 if (wp == NULL)
17587 {
17588 /* didn't find it in this tabpage */
17589 nr = 0;
17590 break;
17591 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017592 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017593 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017594 return nr;
17595}
17596#endif
17597
17598/*
17599 * "tabpagewinnr()" function
17600 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017601 static void
17602f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017603 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017604 typval_T *rettv;
17605{
17606 int nr = 1;
17607#ifdef FEAT_WINDOWS
17608 tabpage_T *tp;
17609
17610 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17611 if (tp == NULL)
17612 nr = 0;
17613 else
17614 nr = get_winnr(tp, &argvars[1]);
17615#endif
17616 rettv->vval.v_number = nr;
17617}
17618
17619
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017620/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017621 * "tagfiles()" function
17622 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017623 static void
17624f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017625 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017626 typval_T *rettv;
17627{
Bram Moolenaard9462e32011-04-11 21:35:11 +020017628 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017629 tagname_T tn;
17630 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017631
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017632 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017633 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017634 fname = alloc(MAXPATHL);
17635 if (fname == NULL)
17636 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017637
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017638 for (first = TRUE; ; first = FALSE)
17639 if (get_tagfname(&tn, first, fname) == FAIL
17640 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017641 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017642 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020017643 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017644}
17645
17646/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017647 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017648 */
17649 static void
17650f_taglist(argvars, rettv)
17651 typval_T *argvars;
17652 typval_T *rettv;
17653{
17654 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017655
17656 tag_pattern = get_tv_string(&argvars[0]);
17657
17658 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017659 if (*tag_pattern == NUL)
17660 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017661
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017662 if (rettv_list_alloc(rettv) == OK)
17663 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017664}
17665
17666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667 * "tempname()" function
17668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017670f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017671 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673{
17674 static int x = 'A';
17675
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017676 rettv->v_type = VAR_STRING;
17677 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678
17679 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17680 * names. Skip 'I' and 'O', they are used for shell redirection. */
17681 do
17682 {
17683 if (x == 'Z')
17684 x = '0';
17685 else if (x == '9')
17686 x = 'A';
17687 else
17688 {
17689#ifdef EBCDIC
17690 if (x == 'I')
17691 x = 'J';
17692 else if (x == 'R')
17693 x = 'S';
17694 else
17695#endif
17696 ++x;
17697 }
17698 } while (x == 'I' || x == 'O');
17699}
17700
17701/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017702 * "test(list)" function: Just checking the walls...
17703 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017704 static void
17705f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017706 typval_T *argvars UNUSED;
17707 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017708{
17709 /* Used for unit testing. Change the code below to your liking. */
17710#if 0
17711 listitem_T *li;
17712 list_T *l;
17713 char_u *bad, *good;
17714
17715 if (argvars[0].v_type != VAR_LIST)
17716 return;
17717 l = argvars[0].vval.v_list;
17718 if (l == NULL)
17719 return;
17720 li = l->lv_first;
17721 if (li == NULL)
17722 return;
17723 bad = get_tv_string(&li->li_tv);
17724 li = li->li_next;
17725 if (li == NULL)
17726 return;
17727 good = get_tv_string(&li->li_tv);
17728 rettv->vval.v_number = test_edit_score(bad, good);
17729#endif
17730}
17731
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017732#ifdef FEAT_FLOAT
17733/*
17734 * "tan()" function
17735 */
17736 static void
17737f_tan(argvars, rettv)
17738 typval_T *argvars;
17739 typval_T *rettv;
17740{
17741 float_T f;
17742
17743 rettv->v_type = VAR_FLOAT;
17744 if (get_float_arg(argvars, &f) == OK)
17745 rettv->vval.v_float = tan(f);
17746 else
17747 rettv->vval.v_float = 0.0;
17748}
17749
17750/*
17751 * "tanh()" function
17752 */
17753 static void
17754f_tanh(argvars, rettv)
17755 typval_T *argvars;
17756 typval_T *rettv;
17757{
17758 float_T f;
17759
17760 rettv->v_type = VAR_FLOAT;
17761 if (get_float_arg(argvars, &f) == OK)
17762 rettv->vval.v_float = tanh(f);
17763 else
17764 rettv->vval.v_float = 0.0;
17765}
17766#endif
17767
Bram Moolenaard52d9742005-08-21 22:20:28 +000017768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769 * "tolower(string)" function
17770 */
17771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017772f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017773 typval_T *argvars;
17774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017775{
17776 char_u *p;
17777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017778 p = vim_strsave(get_tv_string(&argvars[0]));
17779 rettv->v_type = VAR_STRING;
17780 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781
17782 if (p != NULL)
17783 while (*p != NUL)
17784 {
17785#ifdef FEAT_MBYTE
17786 int l;
17787
17788 if (enc_utf8)
17789 {
17790 int c, lc;
17791
17792 c = utf_ptr2char(p);
17793 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017794 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795 /* TODO: reallocate string when byte count changes. */
17796 if (utf_char2len(lc) == l)
17797 utf_char2bytes(lc, p);
17798 p += l;
17799 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017800 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801 p += l; /* skip multi-byte character */
17802 else
17803#endif
17804 {
17805 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17806 ++p;
17807 }
17808 }
17809}
17810
17811/*
17812 * "toupper(string)" function
17813 */
17814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017815f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017816 typval_T *argvars;
17817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017818{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017819 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017820 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017821}
17822
17823/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017824 * "tr(string, fromstr, tostr)" function
17825 */
17826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017827f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017828 typval_T *argvars;
17829 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017830{
17831 char_u *instr;
17832 char_u *fromstr;
17833 char_u *tostr;
17834 char_u *p;
17835#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017836 int inlen;
17837 int fromlen;
17838 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017839 int idx;
17840 char_u *cpstr;
17841 int cplen;
17842 int first = TRUE;
17843#endif
17844 char_u buf[NUMBUFLEN];
17845 char_u buf2[NUMBUFLEN];
17846 garray_T ga;
17847
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017848 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017849 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17850 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017851
17852 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017853 rettv->v_type = VAR_STRING;
17854 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017855 if (fromstr == NULL || tostr == NULL)
17856 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017857 ga_init2(&ga, (int)sizeof(char), 80);
17858
17859#ifdef FEAT_MBYTE
17860 if (!has_mbyte)
17861#endif
17862 /* not multi-byte: fromstr and tostr must be the same length */
17863 if (STRLEN(fromstr) != STRLEN(tostr))
17864 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017865#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017866error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017867#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017868 EMSG2(_(e_invarg2), fromstr);
17869 ga_clear(&ga);
17870 return;
17871 }
17872
17873 /* fromstr and tostr have to contain the same number of chars */
17874 while (*instr != NUL)
17875 {
17876#ifdef FEAT_MBYTE
17877 if (has_mbyte)
17878 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017879 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017880 cpstr = instr;
17881 cplen = inlen;
17882 idx = 0;
17883 for (p = fromstr; *p != NUL; p += fromlen)
17884 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017885 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017886 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17887 {
17888 for (p = tostr; *p != NUL; p += tolen)
17889 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017890 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017891 if (idx-- == 0)
17892 {
17893 cplen = tolen;
17894 cpstr = p;
17895 break;
17896 }
17897 }
17898 if (*p == NUL) /* tostr is shorter than fromstr */
17899 goto error;
17900 break;
17901 }
17902 ++idx;
17903 }
17904
17905 if (first && cpstr == instr)
17906 {
17907 /* Check that fromstr and tostr have the same number of
17908 * (multi-byte) characters. Done only once when a character
17909 * of instr doesn't appear in fromstr. */
17910 first = FALSE;
17911 for (p = tostr; *p != NUL; p += tolen)
17912 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017913 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017914 --idx;
17915 }
17916 if (idx != 0)
17917 goto error;
17918 }
17919
17920 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017921 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017922 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017923
17924 instr += inlen;
17925 }
17926 else
17927#endif
17928 {
17929 /* When not using multi-byte chars we can do it faster. */
17930 p = vim_strchr(fromstr, *instr);
17931 if (p != NULL)
17932 ga_append(&ga, tostr[p - fromstr]);
17933 else
17934 ga_append(&ga, *instr);
17935 ++instr;
17936 }
17937 }
17938
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017939 /* add a terminating NUL */
17940 ga_grow(&ga, 1);
17941 ga_append(&ga, NUL);
17942
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017943 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017944}
17945
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017946#ifdef FEAT_FLOAT
17947/*
17948 * "trunc({float})" function
17949 */
17950 static void
17951f_trunc(argvars, rettv)
17952 typval_T *argvars;
17953 typval_T *rettv;
17954{
17955 float_T f;
17956
17957 rettv->v_type = VAR_FLOAT;
17958 if (get_float_arg(argvars, &f) == OK)
17959 /* trunc() is not in C90, use floor() or ceil() instead. */
17960 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17961 else
17962 rettv->vval.v_float = 0.0;
17963}
17964#endif
17965
Bram Moolenaar8299df92004-07-10 09:47:34 +000017966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017967 * "type(expr)" function
17968 */
17969 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017970f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017971 typval_T *argvars;
17972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017974 int n;
17975
17976 switch (argvars[0].v_type)
17977 {
17978 case VAR_NUMBER: n = 0; break;
17979 case VAR_STRING: n = 1; break;
17980 case VAR_FUNC: n = 2; break;
17981 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017982 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017983#ifdef FEAT_FLOAT
17984 case VAR_FLOAT: n = 5; break;
17985#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017986 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17987 }
17988 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989}
17990
17991/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017992 * "undofile(name)" function
17993 */
17994 static void
17995f_undofile(argvars, rettv)
17996 typval_T *argvars;
17997 typval_T *rettv;
17998{
17999 rettv->v_type = VAR_STRING;
18000#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018001 {
18002 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
18003
18004 if (ffname != NULL)
18005 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18006 vim_free(ffname);
18007 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018008#else
18009 rettv->vval.v_string = NULL;
18010#endif
18011}
18012
18013/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018014 * "undotree()" function
18015 */
18016 static void
18017f_undotree(argvars, rettv)
18018 typval_T *argvars UNUSED;
18019 typval_T *rettv;
18020{
18021 if (rettv_dict_alloc(rettv) == OK)
18022 {
18023 dict_T *dict = rettv->vval.v_dict;
18024 list_T *list;
18025
Bram Moolenaar730cde92010-06-27 05:18:54 +020018026 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018027 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018028 dict_add_nr_str(dict, "save_last",
18029 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018030 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18031 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018032 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018033
18034 list = list_alloc();
18035 if (list != NULL)
18036 {
18037 u_eval_tree(curbuf->b_u_oldhead, list);
18038 dict_add_list(dict, "entries", list);
18039 }
18040 }
18041}
18042
18043/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018044 * "values(dict)" function
18045 */
18046 static void
18047f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018048 typval_T *argvars;
18049 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018050{
18051 dict_list(argvars, rettv, 1);
18052}
18053
18054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055 * "virtcol(string)" function
18056 */
18057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018058f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018059 typval_T *argvars;
18060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061{
18062 colnr_T vcol = 0;
18063 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018064 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018066 fp = var2fpos(&argvars[0], FALSE, &fnum);
18067 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18068 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018069 {
18070 getvvcol(curwin, fp, NULL, NULL, &vcol);
18071 ++vcol;
18072 }
18073
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018074 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075}
18076
18077/*
18078 * "visualmode()" function
18079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018081f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018082 typval_T *argvars UNUSED;
18083 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084{
18085#ifdef FEAT_VISUAL
18086 char_u str[2];
18087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018088 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089 str[0] = curbuf->b_visual_mode_eval;
18090 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018091 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092
18093 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018094 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018095 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096#endif
18097}
18098
18099/*
18100 * "winbufnr(nr)" function
18101 */
18102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018103f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018104 typval_T *argvars;
18105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106{
18107 win_T *wp;
18108
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018109 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018111 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018113 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018114}
18115
18116/*
18117 * "wincol()" function
18118 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018120f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018121 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018123{
18124 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018125 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126}
18127
18128/*
18129 * "winheight(nr)" function
18130 */
18131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018132f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018133 typval_T *argvars;
18134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135{
18136 win_T *wp;
18137
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018138 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018140 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018141 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018142 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143}
18144
18145/*
18146 * "winline()" function
18147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018149f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018150 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018152{
18153 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018154 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018155}
18156
18157/*
18158 * "winnr()" function
18159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018161f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018162 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164{
18165 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018166
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018168 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018170 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171}
18172
18173/*
18174 * "winrestcmd()" function
18175 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018177f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018178 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180{
18181#ifdef FEAT_WINDOWS
18182 win_T *wp;
18183 int winnr = 1;
18184 garray_T ga;
18185 char_u buf[50];
18186
18187 ga_init2(&ga, (int)sizeof(char), 70);
18188 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18189 {
18190 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18191 ga_concat(&ga, buf);
18192# ifdef FEAT_VERTSPLIT
18193 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18194 ga_concat(&ga, buf);
18195# endif
18196 ++winnr;
18197 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018198 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018200 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018201#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018202 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018204 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018205}
18206
18207/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018208 * "winrestview()" function
18209 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018210 static void
18211f_winrestview(argvars, rettv)
18212 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018213 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018214{
18215 dict_T *dict;
18216
18217 if (argvars[0].v_type != VAR_DICT
18218 || (dict = argvars[0].vval.v_dict) == NULL)
18219 EMSG(_(e_invarg));
18220 else
18221 {
18222 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18223 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18224#ifdef FEAT_VIRTUALEDIT
18225 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18226#endif
18227 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018228 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018229
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018230 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018231#ifdef FEAT_DIFF
18232 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18233#endif
18234 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18235 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18236
18237 check_cursor();
18238 changed_cline_bef_curs();
18239 invalidate_botline();
18240 redraw_later(VALID);
18241
18242 if (curwin->w_topline == 0)
18243 curwin->w_topline = 1;
18244 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18245 curwin->w_topline = curbuf->b_ml.ml_line_count;
18246#ifdef FEAT_DIFF
18247 check_topfill(curwin, TRUE);
18248#endif
18249 }
18250}
18251
18252/*
18253 * "winsaveview()" function
18254 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018255 static void
18256f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018257 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018258 typval_T *rettv;
18259{
18260 dict_T *dict;
18261
Bram Moolenaara800b422010-06-27 01:15:55 +020018262 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018263 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018264 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018265
18266 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18267 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18268#ifdef FEAT_VIRTUALEDIT
18269 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18270#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018271 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018272 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18273
18274 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18275#ifdef FEAT_DIFF
18276 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18277#endif
18278 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18279 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18280}
18281
18282/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018283 * "winwidth(nr)" function
18284 */
18285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018286f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018287 typval_T *argvars;
18288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289{
18290 win_T *wp;
18291
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018292 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018294 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018295 else
18296#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018297 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018298#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018299 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018300#endif
18301}
18302
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018304 * "writefile()" function
18305 */
18306 static void
18307f_writefile(argvars, rettv)
18308 typval_T *argvars;
18309 typval_T *rettv;
18310{
18311 int binary = FALSE;
18312 char_u *fname;
18313 FILE *fd;
18314 listitem_T *li;
18315 char_u *s;
18316 int ret = 0;
18317 int c;
18318
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018319 if (check_restricted() || check_secure())
18320 return;
18321
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018322 if (argvars[0].v_type != VAR_LIST)
18323 {
18324 EMSG2(_(e_listarg), "writefile()");
18325 return;
18326 }
18327 if (argvars[0].vval.v_list == NULL)
18328 return;
18329
18330 if (argvars[2].v_type != VAR_UNKNOWN
18331 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18332 binary = TRUE;
18333
18334 /* Always open the file in binary mode, library functions have a mind of
18335 * their own about CR-LF conversion. */
18336 fname = get_tv_string(&argvars[1]);
18337 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18338 {
18339 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18340 ret = -1;
18341 }
18342 else
18343 {
18344 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18345 li = li->li_next)
18346 {
18347 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18348 {
18349 if (*s == '\n')
18350 c = putc(NUL, fd);
18351 else
18352 c = putc(*s, fd);
18353 if (c == EOF)
18354 {
18355 ret = -1;
18356 break;
18357 }
18358 }
18359 if (!binary || li->li_next != NULL)
18360 if (putc('\n', fd) == EOF)
18361 {
18362 ret = -1;
18363 break;
18364 }
18365 if (ret < 0)
18366 {
18367 EMSG(_(e_write));
18368 break;
18369 }
18370 }
18371 fclose(fd);
18372 }
18373
18374 rettv->vval.v_number = ret;
18375}
18376
18377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018378 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018379 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380 */
18381 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018382var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018383 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018384 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018385 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018387 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018388 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018389 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018390
Bram Moolenaara5525202006-03-02 22:52:09 +000018391 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018392 if (varp->v_type == VAR_LIST)
18393 {
18394 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018395 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018396 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018397 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018398
18399 l = varp->vval.v_list;
18400 if (l == NULL)
18401 return NULL;
18402
18403 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018404 pos.lnum = list_find_nr(l, 0L, &error);
18405 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018406 return NULL; /* invalid line number */
18407
18408 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018409 pos.col = list_find_nr(l, 1L, &error);
18410 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018411 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018412 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018413
18414 /* We accept "$" for the column number: last column. */
18415 li = list_find(l, 1L);
18416 if (li != NULL && li->li_tv.v_type == VAR_STRING
18417 && li->li_tv.vval.v_string != NULL
18418 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18419 pos.col = len + 1;
18420
Bram Moolenaara5525202006-03-02 22:52:09 +000018421 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018422 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018423 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018424 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018425
Bram Moolenaara5525202006-03-02 22:52:09 +000018426#ifdef FEAT_VIRTUALEDIT
18427 /* Get the virtual offset. Defaults to zero. */
18428 pos.coladd = list_find_nr(l, 2L, &error);
18429 if (error)
18430 pos.coladd = 0;
18431#endif
18432
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018433 return &pos;
18434 }
18435
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018436 name = get_tv_string_chk(varp);
18437 if (name == NULL)
18438 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018439 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018441#ifdef FEAT_VISUAL
18442 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18443 {
18444 if (VIsual_active)
18445 return &VIsual;
18446 return &curwin->w_cursor;
18447 }
18448#endif
18449 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018451 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18453 return NULL;
18454 return pp;
18455 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018456
18457#ifdef FEAT_VIRTUALEDIT
18458 pos.coladd = 0;
18459#endif
18460
Bram Moolenaar477933c2007-07-17 14:32:23 +000018461 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018462 {
18463 pos.col = 0;
18464 if (name[1] == '0') /* "w0": first visible line */
18465 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018466 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018467 pos.lnum = curwin->w_topline;
18468 return &pos;
18469 }
18470 else if (name[1] == '$') /* "w$": last visible line */
18471 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018472 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018473 pos.lnum = curwin->w_botline - 1;
18474 return &pos;
18475 }
18476 }
18477 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018479 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480 {
18481 pos.lnum = curbuf->b_ml.ml_line_count;
18482 pos.col = 0;
18483 }
18484 else
18485 {
18486 pos.lnum = curwin->w_cursor.lnum;
18487 pos.col = (colnr_T)STRLEN(ml_get_curline());
18488 }
18489 return &pos;
18490 }
18491 return NULL;
18492}
18493
18494/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018495 * Convert list in "arg" into a position and optional file number.
18496 * When "fnump" is NULL there is no file number, only 3 items.
18497 * Note that the column is passed on as-is, the caller may want to decrement
18498 * it to use 1 for the first column.
18499 * Return FAIL when conversion is not possible, doesn't check the position for
18500 * validity.
18501 */
18502 static int
18503list2fpos(arg, posp, fnump)
18504 typval_T *arg;
18505 pos_T *posp;
18506 int *fnump;
18507{
18508 list_T *l = arg->vval.v_list;
18509 long i = 0;
18510 long n;
18511
Bram Moolenaarbde35262006-07-23 20:12:24 +000018512 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18513 * when "fnump" isn't NULL and "coladd" is optional. */
18514 if (arg->v_type != VAR_LIST
18515 || l == NULL
18516 || l->lv_len < (fnump == NULL ? 2 : 3)
18517 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018518 return FAIL;
18519
18520 if (fnump != NULL)
18521 {
18522 n = list_find_nr(l, i++, NULL); /* fnum */
18523 if (n < 0)
18524 return FAIL;
18525 if (n == 0)
18526 n = curbuf->b_fnum; /* current buffer */
18527 *fnump = n;
18528 }
18529
18530 n = list_find_nr(l, i++, NULL); /* lnum */
18531 if (n < 0)
18532 return FAIL;
18533 posp->lnum = n;
18534
18535 n = list_find_nr(l, i++, NULL); /* col */
18536 if (n < 0)
18537 return FAIL;
18538 posp->col = n;
18539
18540#ifdef FEAT_VIRTUALEDIT
18541 n = list_find_nr(l, i, NULL);
18542 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018543 posp->coladd = 0;
18544 else
18545 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018546#endif
18547
18548 return OK;
18549}
18550
18551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552 * Get the length of an environment variable name.
18553 * Advance "arg" to the first character after the name.
18554 * Return 0 for error.
18555 */
18556 static int
18557get_env_len(arg)
18558 char_u **arg;
18559{
18560 char_u *p;
18561 int len;
18562
18563 for (p = *arg; vim_isIDc(*p); ++p)
18564 ;
18565 if (p == *arg) /* no name found */
18566 return 0;
18567
18568 len = (int)(p - *arg);
18569 *arg = p;
18570 return len;
18571}
18572
18573/*
18574 * Get the length of the name of a function or internal variable.
18575 * "arg" is advanced to the first non-white character after the name.
18576 * Return 0 if something is wrong.
18577 */
18578 static int
18579get_id_len(arg)
18580 char_u **arg;
18581{
18582 char_u *p;
18583 int len;
18584
18585 /* Find the end of the name. */
18586 for (p = *arg; eval_isnamec(*p); ++p)
18587 ;
18588 if (p == *arg) /* no name found */
18589 return 0;
18590
18591 len = (int)(p - *arg);
18592 *arg = skipwhite(p);
18593
18594 return len;
18595}
18596
18597/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018598 * Get the length of the name of a variable or function.
18599 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018600 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018601 * Return -1 if curly braces expansion failed.
18602 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603 * If the name contains 'magic' {}'s, expand them and return the
18604 * expanded name in an allocated string via 'alias' - caller must free.
18605 */
18606 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018607get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018608 char_u **arg;
18609 char_u **alias;
18610 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018611 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612{
18613 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614 char_u *p;
18615 char_u *expr_start;
18616 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018617
18618 *alias = NULL; /* default to no alias */
18619
18620 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18621 && (*arg)[2] == (int)KE_SNR)
18622 {
18623 /* hard coded <SNR>, already translated */
18624 *arg += 3;
18625 return get_id_len(arg) + 3;
18626 }
18627 len = eval_fname_script(*arg);
18628 if (len > 0)
18629 {
18630 /* literal "<SID>", "s:" or "<SNR>" */
18631 *arg += len;
18632 }
18633
Bram Moolenaar071d4272004-06-13 20:20:40 +000018634 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018635 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018637 p = find_name_end(*arg, &expr_start, &expr_end,
18638 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018639 if (expr_start != NULL)
18640 {
18641 char_u *temp_string;
18642
18643 if (!evaluate)
18644 {
18645 len += (int)(p - *arg);
18646 *arg = skipwhite(p);
18647 return len;
18648 }
18649
18650 /*
18651 * Include any <SID> etc in the expanded string:
18652 * Thus the -len here.
18653 */
18654 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18655 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018656 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 *alias = temp_string;
18658 *arg = skipwhite(p);
18659 return (int)STRLEN(temp_string);
18660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661
18662 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018663 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664 EMSG2(_(e_invexpr2), *arg);
18665
18666 return len;
18667}
18668
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018669/*
18670 * Find the end of a variable or function name, taking care of magic braces.
18671 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18672 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018673 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018674 * Return a pointer to just after the name. Equal to "arg" if there is no
18675 * valid name.
18676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018678find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679 char_u *arg;
18680 char_u **expr_start;
18681 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018682 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018684 int mb_nest = 0;
18685 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686 char_u *p;
18687
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018688 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018689 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018690 *expr_start = NULL;
18691 *expr_end = NULL;
18692 }
18693
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018694 /* Quick check for valid starting character. */
18695 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18696 return arg;
18697
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018698 for (p = arg; *p != NUL
18699 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018700 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018701 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018702 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018703 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018704 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018705 if (*p == '\'')
18706 {
18707 /* skip over 'string' to avoid counting [ and ] inside it. */
18708 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18709 ;
18710 if (*p == NUL)
18711 break;
18712 }
18713 else if (*p == '"')
18714 {
18715 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18716 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18717 if (*p == '\\' && p[1] != NUL)
18718 ++p;
18719 if (*p == NUL)
18720 break;
18721 }
18722
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018723 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018724 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018725 if (*p == '[')
18726 ++br_nest;
18727 else if (*p == ']')
18728 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018730
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018731 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018733 if (*p == '{')
18734 {
18735 mb_nest++;
18736 if (expr_start != NULL && *expr_start == NULL)
18737 *expr_start = p;
18738 }
18739 else if (*p == '}')
18740 {
18741 mb_nest--;
18742 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18743 *expr_end = p;
18744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746 }
18747
18748 return p;
18749}
18750
18751/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018752 * Expands out the 'magic' {}'s in a variable/function name.
18753 * Note that this can call itself recursively, to deal with
18754 * constructs like foo{bar}{baz}{bam}
18755 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18756 * "in_start" ^
18757 * "expr_start" ^
18758 * "expr_end" ^
18759 * "in_end" ^
18760 *
18761 * Returns a new allocated string, which the caller must free.
18762 * Returns NULL for failure.
18763 */
18764 static char_u *
18765make_expanded_name(in_start, expr_start, expr_end, in_end)
18766 char_u *in_start;
18767 char_u *expr_start;
18768 char_u *expr_end;
18769 char_u *in_end;
18770{
18771 char_u c1;
18772 char_u *retval = NULL;
18773 char_u *temp_result;
18774 char_u *nextcmd = NULL;
18775
18776 if (expr_end == NULL || in_end == NULL)
18777 return NULL;
18778 *expr_start = NUL;
18779 *expr_end = NUL;
18780 c1 = *in_end;
18781 *in_end = NUL;
18782
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018783 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018784 if (temp_result != NULL && nextcmd == NULL)
18785 {
18786 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18787 + (in_end - expr_end) + 1));
18788 if (retval != NULL)
18789 {
18790 STRCPY(retval, in_start);
18791 STRCAT(retval, temp_result);
18792 STRCAT(retval, expr_end + 1);
18793 }
18794 }
18795 vim_free(temp_result);
18796
18797 *in_end = c1; /* put char back for error messages */
18798 *expr_start = '{';
18799 *expr_end = '}';
18800
18801 if (retval != NULL)
18802 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018803 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018804 if (expr_start != NULL)
18805 {
18806 /* Further expansion! */
18807 temp_result = make_expanded_name(retval, expr_start,
18808 expr_end, temp_result);
18809 vim_free(retval);
18810 retval = temp_result;
18811 }
18812 }
18813
18814 return retval;
18815}
18816
18817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018819 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820 */
18821 static int
18822eval_isnamec(c)
18823 int c;
18824{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018825 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18826}
18827
18828/*
18829 * Return TRUE if character "c" can be used as the first character in a
18830 * variable or function name (excluding '{' and '}').
18831 */
18832 static int
18833eval_isnamec1(c)
18834 int c;
18835{
18836 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837}
18838
18839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840 * Set number v: variable to "val".
18841 */
18842 void
18843set_vim_var_nr(idx, val)
18844 int idx;
18845 long val;
18846{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018847 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018848}
18849
18850/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018851 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852 */
18853 long
18854get_vim_var_nr(idx)
18855 int idx;
18856{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018857 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858}
18859
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018860/*
18861 * Get string v: variable value. Uses a static buffer, can only be used once.
18862 */
18863 char_u *
18864get_vim_var_str(idx)
18865 int idx;
18866{
18867 return get_tv_string(&vimvars[idx].vv_tv);
18868}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018869
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018871 * Get List v: variable value. Caller must take care of reference count when
18872 * needed.
18873 */
18874 list_T *
18875get_vim_var_list(idx)
18876 int idx;
18877{
18878 return vimvars[idx].vv_list;
18879}
18880
18881/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018882 * Set v:char to character "c".
18883 */
18884 void
18885set_vim_var_char(c)
18886 int c;
18887{
18888#ifdef FEAT_MBYTE
18889 char_u buf[MB_MAXBYTES];
18890#else
18891 char_u buf[2];
18892#endif
18893
18894#ifdef FEAT_MBYTE
18895 if (has_mbyte)
18896 buf[(*mb_char2bytes)(c, buf)] = NUL;
18897 else
18898#endif
18899 {
18900 buf[0] = c;
18901 buf[1] = NUL;
18902 }
18903 set_vim_var_string(VV_CHAR, buf, -1);
18904}
18905
18906/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018907 * Set v:count to "count" and v:count1 to "count1".
18908 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909 */
18910 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018911set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912 long count;
18913 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018914 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018916 if (set_prevcount)
18917 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018918 vimvars[VV_COUNT].vv_nr = count;
18919 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920}
18921
18922/*
18923 * Set string v: variable to a copy of "val".
18924 */
18925 void
18926set_vim_var_string(idx, val, len)
18927 int idx;
18928 char_u *val;
18929 int len; /* length of "val" to use or -1 (whole string) */
18930{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018931 /* Need to do this (at least) once, since we can't initialize a union.
18932 * Will always be invoked when "v:progname" is set. */
18933 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18934
Bram Moolenaare9a41262005-01-15 22:18:47 +000018935 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018936 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018937 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018938 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018939 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018941 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018942}
18943
18944/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018945 * Set List v: variable to "val".
18946 */
18947 void
18948set_vim_var_list(idx, val)
18949 int idx;
18950 list_T *val;
18951{
18952 list_unref(vimvars[idx].vv_list);
18953 vimvars[idx].vv_list = val;
18954 if (val != NULL)
18955 ++val->lv_refcount;
18956}
18957
18958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959 * Set v:register if needed.
18960 */
18961 void
18962set_reg_var(c)
18963 int c;
18964{
18965 char_u regname;
18966
18967 if (c == 0 || c == ' ')
18968 regname = '"';
18969 else
18970 regname = c;
18971 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018972 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973 set_vim_var_string(VV_REG, &regname, 1);
18974}
18975
18976/*
18977 * Get or set v:exception. If "oldval" == NULL, return the current value.
18978 * Otherwise, restore the value to "oldval" and return NULL.
18979 * Must always be called in pairs to save and restore v:exception! Does not
18980 * take care of memory allocations.
18981 */
18982 char_u *
18983v_exception(oldval)
18984 char_u *oldval;
18985{
18986 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018987 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988
Bram Moolenaare9a41262005-01-15 22:18:47 +000018989 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990 return NULL;
18991}
18992
18993/*
18994 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18995 * Otherwise, restore the value to "oldval" and return NULL.
18996 * Must always be called in pairs to save and restore v:throwpoint! Does not
18997 * take care of memory allocations.
18998 */
18999 char_u *
19000v_throwpoint(oldval)
19001 char_u *oldval;
19002{
19003 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019004 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005
Bram Moolenaare9a41262005-01-15 22:18:47 +000019006 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007 return NULL;
19008}
19009
19010#if defined(FEAT_AUTOCMD) || defined(PROTO)
19011/*
19012 * Set v:cmdarg.
19013 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19014 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19015 * Must always be called in pairs!
19016 */
19017 char_u *
19018set_cmdarg(eap, oldarg)
19019 exarg_T *eap;
19020 char_u *oldarg;
19021{
19022 char_u *oldval;
19023 char_u *newval;
19024 unsigned len;
19025
Bram Moolenaare9a41262005-01-15 22:18:47 +000019026 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019027 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019029 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019030 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019031 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032 }
19033
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019034 if (eap->force_bin == FORCE_BIN)
19035 len = 6;
19036 else if (eap->force_bin == FORCE_NOBIN)
19037 len = 8;
19038 else
19039 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019040
19041 if (eap->read_edit)
19042 len += 7;
19043
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019044 if (eap->force_ff != 0)
19045 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19046# ifdef FEAT_MBYTE
19047 if (eap->force_enc != 0)
19048 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019049 if (eap->bad_char != 0)
19050 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019051# endif
19052
19053 newval = alloc(len + 1);
19054 if (newval == NULL)
19055 return NULL;
19056
19057 if (eap->force_bin == FORCE_BIN)
19058 sprintf((char *)newval, " ++bin");
19059 else if (eap->force_bin == FORCE_NOBIN)
19060 sprintf((char *)newval, " ++nobin");
19061 else
19062 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019063
19064 if (eap->read_edit)
19065 STRCAT(newval, " ++edit");
19066
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019067 if (eap->force_ff != 0)
19068 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19069 eap->cmd + eap->force_ff);
19070# ifdef FEAT_MBYTE
19071 if (eap->force_enc != 0)
19072 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19073 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019074 if (eap->bad_char == BAD_KEEP)
19075 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19076 else if (eap->bad_char == BAD_DROP)
19077 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19078 else if (eap->bad_char != 0)
19079 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019080# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019081 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019082 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083}
19084#endif
19085
19086/*
19087 * Get the value of internal variable "name".
19088 * Return OK or FAIL.
19089 */
19090 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019091get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092 char_u *name;
19093 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019094 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019095 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096{
19097 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019098 typval_T *tv = NULL;
19099 typval_T atv;
19100 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102
19103 /* truncate the name, so that we can use strcmp() */
19104 cc = name[len];
19105 name[len] = NUL;
19106
19107 /*
19108 * Check for "b:changedtick".
19109 */
19110 if (STRCMP(name, "b:changedtick") == 0)
19111 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019112 atv.v_type = VAR_NUMBER;
19113 atv.vval.v_number = curbuf->b_changedtick;
19114 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115 }
19116
19117 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118 * Check for user-defined variables.
19119 */
19120 else
19121 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019122 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019123 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019124 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125 }
19126
Bram Moolenaare9a41262005-01-15 22:18:47 +000019127 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019129 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019130 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131 ret = FAIL;
19132 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019133 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019134 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135
19136 name[len] = cc;
19137
19138 return ret;
19139}
19140
19141/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019142 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19143 * Also handle function call with Funcref variable: func(expr)
19144 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19145 */
19146 static int
19147handle_subscript(arg, rettv, evaluate, verbose)
19148 char_u **arg;
19149 typval_T *rettv;
19150 int evaluate; /* do more than finding the end */
19151 int verbose; /* give error messages */
19152{
19153 int ret = OK;
19154 dict_T *selfdict = NULL;
19155 char_u *s;
19156 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019157 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019158
19159 while (ret == OK
19160 && (**arg == '['
19161 || (**arg == '.' && rettv->v_type == VAR_DICT)
19162 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19163 && !vim_iswhite(*(*arg - 1)))
19164 {
19165 if (**arg == '(')
19166 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019167 /* need to copy the funcref so that we can clear rettv */
19168 functv = *rettv;
19169 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019170
19171 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019172 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019173 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019174 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19175 &len, evaluate, selfdict);
19176
19177 /* Clear the funcref afterwards, so that deleting it while
19178 * evaluating the arguments is possible (see test55). */
19179 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019180
19181 /* Stop the expression evaluation when immediately aborting on
19182 * error, or when an interrupt occurred or an exception was thrown
19183 * but not caught. */
19184 if (aborting())
19185 {
19186 if (ret == OK)
19187 clear_tv(rettv);
19188 ret = FAIL;
19189 }
19190 dict_unref(selfdict);
19191 selfdict = NULL;
19192 }
19193 else /* **arg == '[' || **arg == '.' */
19194 {
19195 dict_unref(selfdict);
19196 if (rettv->v_type == VAR_DICT)
19197 {
19198 selfdict = rettv->vval.v_dict;
19199 if (selfdict != NULL)
19200 ++selfdict->dv_refcount;
19201 }
19202 else
19203 selfdict = NULL;
19204 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19205 {
19206 clear_tv(rettv);
19207 ret = FAIL;
19208 }
19209 }
19210 }
19211 dict_unref(selfdict);
19212 return ret;
19213}
19214
19215/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019216 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019217 * value).
19218 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019219 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019220alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019221{
Bram Moolenaar33570922005-01-25 22:26:29 +000019222 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019223}
19224
19225/*
19226 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019227 * The string "s" must have been allocated, it is consumed.
19228 * Return NULL for out of memory, the variable otherwise.
19229 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019230 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019231alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019232 char_u *s;
19233{
Bram Moolenaar33570922005-01-25 22:26:29 +000019234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019236 rettv = alloc_tv();
19237 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019238 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019239 rettv->v_type = VAR_STRING;
19240 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019241 }
19242 else
19243 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019244 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245}
19246
19247/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019248 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019249 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019250 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019251free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019252 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253{
19254 if (varp != NULL)
19255 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019256 switch (varp->v_type)
19257 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019258 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019259 func_unref(varp->vval.v_string);
19260 /*FALLTHROUGH*/
19261 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019262 vim_free(varp->vval.v_string);
19263 break;
19264 case VAR_LIST:
19265 list_unref(varp->vval.v_list);
19266 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019267 case VAR_DICT:
19268 dict_unref(varp->vval.v_dict);
19269 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019270 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019271#ifdef FEAT_FLOAT
19272 case VAR_FLOAT:
19273#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019274 case VAR_UNKNOWN:
19275 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019276 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019277 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019278 break;
19279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019280 vim_free(varp);
19281 }
19282}
19283
19284/*
19285 * Free the memory for a variable value and set the value to NULL or 0.
19286 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019287 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019288clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019289 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290{
19291 if (varp != NULL)
19292 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019293 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019294 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019295 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019296 func_unref(varp->vval.v_string);
19297 /*FALLTHROUGH*/
19298 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019299 vim_free(varp->vval.v_string);
19300 varp->vval.v_string = NULL;
19301 break;
19302 case VAR_LIST:
19303 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019304 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019305 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019306 case VAR_DICT:
19307 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019308 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019309 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019310 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019311 varp->vval.v_number = 0;
19312 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019313#ifdef FEAT_FLOAT
19314 case VAR_FLOAT:
19315 varp->vval.v_float = 0.0;
19316 break;
19317#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019318 case VAR_UNKNOWN:
19319 break;
19320 default:
19321 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019322 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019323 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324 }
19325}
19326
19327/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019328 * Set the value of a variable to NULL without freeing items.
19329 */
19330 static void
19331init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019332 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019333{
19334 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019335 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019336}
19337
19338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339 * Get the number value of a variable.
19340 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019341 * For incompatible types, return 0.
19342 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19343 * caller of incompatible types: it sets *denote to TRUE if "denote"
19344 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345 */
19346 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019347get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019348 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019349{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019350 int error = FALSE;
19351
19352 return get_tv_number_chk(varp, &error); /* return 0L on error */
19353}
19354
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019355 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019356get_tv_number_chk(varp, denote)
19357 typval_T *varp;
19358 int *denote;
19359{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019360 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019361
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019362 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019364 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019365 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019366#ifdef FEAT_FLOAT
19367 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019368 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019369 break;
19370#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019371 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019372 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019373 break;
19374 case VAR_STRING:
19375 if (varp->vval.v_string != NULL)
19376 vim_str2nr(varp->vval.v_string, NULL, NULL,
19377 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019378 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019379 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019380 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019381 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019382 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019383 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019384 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019385 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019386 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019387 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019389 if (denote == NULL) /* useful for values that must be unsigned */
19390 n = -1;
19391 else
19392 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019393 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019394}
19395
19396/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019397 * Get the lnum from the first argument.
19398 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019399 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400 */
19401 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019402get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019403 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404{
Bram Moolenaar33570922005-01-25 22:26:29 +000019405 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019406 linenr_T lnum;
19407
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019408 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409 if (lnum == 0) /* no valid number, try using line() */
19410 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019411 rettv.v_type = VAR_NUMBER;
19412 f_line(argvars, &rettv);
19413 lnum = rettv.vval.v_number;
19414 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019415 }
19416 return lnum;
19417}
19418
19419/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019420 * Get the lnum from the first argument.
19421 * Also accepts "$", then "buf" is used.
19422 * Returns 0 on error.
19423 */
19424 static linenr_T
19425get_tv_lnum_buf(argvars, buf)
19426 typval_T *argvars;
19427 buf_T *buf;
19428{
19429 if (argvars[0].v_type == VAR_STRING
19430 && argvars[0].vval.v_string != NULL
19431 && argvars[0].vval.v_string[0] == '$'
19432 && buf != NULL)
19433 return buf->b_ml.ml_line_count;
19434 return get_tv_number_chk(&argvars[0], NULL);
19435}
19436
19437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019438 * Get the string value of a variable.
19439 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019440 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19441 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019442 * If the String variable has never been set, return an empty string.
19443 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019444 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19445 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446 */
19447 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019448get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019449 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450{
19451 static char_u mybuf[NUMBUFLEN];
19452
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019453 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454}
19455
19456 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019457get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019458 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019459 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019460{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019461 char_u *res = get_tv_string_buf_chk(varp, buf);
19462
19463 return res != NULL ? res : (char_u *)"";
19464}
19465
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019466 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019467get_tv_string_chk(varp)
19468 typval_T *varp;
19469{
19470 static char_u mybuf[NUMBUFLEN];
19471
19472 return get_tv_string_buf_chk(varp, mybuf);
19473}
19474
19475 static char_u *
19476get_tv_string_buf_chk(varp, buf)
19477 typval_T *varp;
19478 char_u *buf;
19479{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019480 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019482 case VAR_NUMBER:
19483 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19484 return buf;
19485 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019486 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019487 break;
19488 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019489 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019490 break;
19491 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019492 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019493 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019494#ifdef FEAT_FLOAT
19495 case VAR_FLOAT:
19496 EMSG(_("E806: using Float as a String"));
19497 break;
19498#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019499 case VAR_STRING:
19500 if (varp->vval.v_string != NULL)
19501 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019502 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019503 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019504 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019505 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019507 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508}
19509
19510/*
19511 * Find variable "name" in the list of variables.
19512 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019513 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019514 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019515 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019517 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019518find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019520 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019523 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524
Bram Moolenaara7043832005-01-21 11:56:39 +000019525 ht = find_var_ht(name, &varname);
19526 if (htp != NULL)
19527 *htp = ht;
19528 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019530 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531}
19532
19533/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019534 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019535 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019536 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019537 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019538find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019539 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019540 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019541 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019542{
Bram Moolenaar33570922005-01-25 22:26:29 +000019543 hashitem_T *hi;
19544
19545 if (*varname == NUL)
19546 {
19547 /* Must be something like "s:", otherwise "ht" would be NULL. */
19548 switch (varname[-2])
19549 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019550 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019551 case 'g': return &globvars_var;
19552 case 'v': return &vimvars_var;
19553 case 'b': return &curbuf->b_bufvar;
19554 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019555#ifdef FEAT_WINDOWS
19556 case 't': return &curtab->tp_winvar;
19557#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019558 case 'l': return current_funccal == NULL
19559 ? NULL : &current_funccal->l_vars_var;
19560 case 'a': return current_funccal == NULL
19561 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019562 }
19563 return NULL;
19564 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019565
19566 hi = hash_find(ht, varname);
19567 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019568 {
19569 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019570 * worked find the variable again. Don't auto-load a script if it was
19571 * loaded already, otherwise it would be loaded every time when
19572 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019573 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019574 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019575 hi = hash_find(ht, varname);
19576 if (HASHITEM_EMPTY(hi))
19577 return NULL;
19578 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019579 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019580}
19581
19582/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019583 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019584 * Set "varname" to the start of name without ':'.
19585 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019586 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019587find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019588 char_u *name;
19589 char_u **varname;
19590{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019591 hashitem_T *hi;
19592
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593 if (name[1] != ':')
19594 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019595 /* The name must not start with a colon or #. */
19596 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597 return NULL;
19598 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019599
19600 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019601 hi = hash_find(&compat_hashtab, name);
19602 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019603 return &compat_hashtab;
19604
Bram Moolenaar071d4272004-06-13 20:20:40 +000019605 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019606 return &globvarht; /* global variable */
19607 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608 }
19609 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019610 if (*name == 'g') /* global variable */
19611 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019612 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19613 */
19614 if (vim_strchr(name + 2, ':') != NULL
19615 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019616 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019618 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019620 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019621#ifdef FEAT_WINDOWS
19622 if (*name == 't') /* tab page variable */
19623 return &curtab->tp_vars.dv_hashtab;
19624#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019625 if (*name == 'v') /* v: variable */
19626 return &vimvarht;
19627 if (*name == 'a' && current_funccal != NULL) /* function argument */
19628 return &current_funccal->l_avars.dv_hashtab;
19629 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19630 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019631 if (*name == 's' /* script variable */
19632 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19633 return &SCRIPT_VARS(current_SID);
19634 return NULL;
19635}
19636
19637/*
19638 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019639 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640 * Returns NULL when it doesn't exist.
19641 */
19642 char_u *
19643get_var_value(name)
19644 char_u *name;
19645{
Bram Moolenaar33570922005-01-25 22:26:29 +000019646 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647
Bram Moolenaara7043832005-01-21 11:56:39 +000019648 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649 if (v == NULL)
19650 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019651 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652}
19653
19654/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019655 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656 * sourcing this script and when executing functions defined in the script.
19657 */
19658 void
19659new_script_vars(id)
19660 scid_T id;
19661{
Bram Moolenaara7043832005-01-21 11:56:39 +000019662 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019663 hashtab_T *ht;
19664 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019665
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19667 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019668 /* Re-allocating ga_data means that an ht_array pointing to
19669 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019670 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019671 for (i = 1; i <= ga_scripts.ga_len; ++i)
19672 {
19673 ht = &SCRIPT_VARS(i);
19674 if (ht->ht_mask == HT_INIT_SIZE - 1)
19675 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019676 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019677 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019678 }
19679
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680 while (ga_scripts.ga_len < id)
19681 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019682 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019683 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019684 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686 }
19687 }
19688}
19689
19690/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019691 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19692 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693 */
19694 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019695init_var_dict(dict, dict_var)
19696 dict_T *dict;
19697 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698{
Bram Moolenaar33570922005-01-25 22:26:29 +000019699 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019700 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019701 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019702 dict_var->di_tv.vval.v_dict = dict;
19703 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019704 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019705 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19706 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019707}
19708
19709/*
19710 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019711 * Frees all allocated variables and the value they contain.
19712 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019713 */
19714 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019715vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019716 hashtab_T *ht;
19717{
19718 vars_clear_ext(ht, TRUE);
19719}
19720
19721/*
19722 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19723 */
19724 static void
19725vars_clear_ext(ht, free_val)
19726 hashtab_T *ht;
19727 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728{
Bram Moolenaara7043832005-01-21 11:56:39 +000019729 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019730 hashitem_T *hi;
19731 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732
Bram Moolenaar33570922005-01-25 22:26:29 +000019733 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019734 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019735 for (hi = ht->ht_array; todo > 0; ++hi)
19736 {
19737 if (!HASHITEM_EMPTY(hi))
19738 {
19739 --todo;
19740
Bram Moolenaar33570922005-01-25 22:26:29 +000019741 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019742 * ht_array might change then. hash_clear() takes care of it
19743 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019744 v = HI2DI(hi);
19745 if (free_val)
19746 clear_tv(&v->di_tv);
19747 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19748 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019749 }
19750 }
19751 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019752 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019753}
19754
Bram Moolenaara7043832005-01-21 11:56:39 +000019755/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019756 * Delete a variable from hashtab "ht" at item "hi".
19757 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019759 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019760delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019761 hashtab_T *ht;
19762 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019763{
Bram Moolenaar33570922005-01-25 22:26:29 +000019764 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019765
19766 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019767 clear_tv(&di->di_tv);
19768 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769}
19770
19771/*
19772 * List the value of one internal variable.
19773 */
19774 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019775list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019776 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019778 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019780 char_u *tofree;
19781 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019782 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019783
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019784 current_copyID += COPYID_INC;
19785 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019786 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019787 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019788 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789}
19790
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019792list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793 char_u *prefix;
19794 char_u *name;
19795 int type;
19796 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019797 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019798{
Bram Moolenaar31859182007-08-14 20:41:13 +000019799 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19800 msg_start();
19801 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802 if (name != NULL) /* "a:" vars don't have a name stored */
19803 msg_puts(name);
19804 msg_putchar(' ');
19805 msg_advance(22);
19806 if (type == VAR_NUMBER)
19807 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019808 else if (type == VAR_FUNC)
19809 msg_putchar('*');
19810 else if (type == VAR_LIST)
19811 {
19812 msg_putchar('[');
19813 if (*string == '[')
19814 ++string;
19815 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019816 else if (type == VAR_DICT)
19817 {
19818 msg_putchar('{');
19819 if (*string == '{')
19820 ++string;
19821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822 else
19823 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019824
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019826
19827 if (type == VAR_FUNC)
19828 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019829 if (*first)
19830 {
19831 msg_clr_eos();
19832 *first = FALSE;
19833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834}
19835
19836/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019837 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838 * If the variable already exists, the value is updated.
19839 * Otherwise the variable is created.
19840 */
19841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019842set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019844 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019845 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019846{
Bram Moolenaar33570922005-01-25 22:26:29 +000019847 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019848 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019849 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019851 ht = find_var_ht(name, &varname);
19852 if (ht == NULL || *varname == NUL)
19853 {
19854 EMSG2(_(e_illvar), name);
19855 return;
19856 }
19857 v = find_var_in_ht(ht, varname, TRUE);
19858
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019859 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
19860 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019861
Bram Moolenaar33570922005-01-25 22:26:29 +000019862 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019864 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019865 if (var_check_ro(v->di_flags, name)
19866 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019867 return;
19868 if (v->di_tv.v_type != tv->v_type
19869 && !((v->di_tv.v_type == VAR_STRING
19870 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019871 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019872 || tv->v_type == VAR_NUMBER))
19873#ifdef FEAT_FLOAT
19874 && !((v->di_tv.v_type == VAR_NUMBER
19875 || v->di_tv.v_type == VAR_FLOAT)
19876 && (tv->v_type == VAR_NUMBER
19877 || tv->v_type == VAR_FLOAT))
19878#endif
19879 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019880 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019881 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019882 return;
19883 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019884
19885 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019886 * Handle setting internal v: variables separately: we don't change
19887 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019888 */
19889 if (ht == &vimvarht)
19890 {
19891 if (v->di_tv.v_type == VAR_STRING)
19892 {
19893 vim_free(v->di_tv.vval.v_string);
19894 if (copy || tv->v_type != VAR_STRING)
19895 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19896 else
19897 {
19898 /* Take over the string to avoid an extra alloc/free. */
19899 v->di_tv.vval.v_string = tv->vval.v_string;
19900 tv->vval.v_string = NULL;
19901 }
19902 }
19903 else if (v->di_tv.v_type != VAR_NUMBER)
19904 EMSG2(_(e_intern2), "set_var()");
19905 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019906 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019907 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019908 if (STRCMP(varname, "searchforward") == 0)
19909 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19910 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019911 return;
19912 }
19913
19914 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019915 }
19916 else /* add a new variable */
19917 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019918 /* Can't add "v:" variable. */
19919 if (ht == &vimvarht)
19920 {
19921 EMSG2(_(e_illvar), name);
19922 return;
19923 }
19924
Bram Moolenaar92124a32005-06-17 22:03:40 +000019925 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019926 if (!valid_varname(varname))
19927 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019928
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019929 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19930 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019931 if (v == NULL)
19932 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019933 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019934 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019936 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937 return;
19938 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019939 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019941
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019942 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019943 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019944 else
19945 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019946 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019947 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019948 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950}
19951
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019952/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019953 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019954 * Also give an error message.
19955 */
19956 static int
19957var_check_ro(flags, name)
19958 int flags;
19959 char_u *name;
19960{
19961 if (flags & DI_FLAGS_RO)
19962 {
19963 EMSG2(_(e_readonlyvar), name);
19964 return TRUE;
19965 }
19966 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19967 {
19968 EMSG2(_(e_readonlysbx), name);
19969 return TRUE;
19970 }
19971 return FALSE;
19972}
19973
19974/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019975 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19976 * Also give an error message.
19977 */
19978 static int
19979var_check_fixed(flags, name)
19980 int flags;
19981 char_u *name;
19982{
19983 if (flags & DI_FLAGS_FIX)
19984 {
19985 EMSG2(_("E795: Cannot delete variable %s"), name);
19986 return TRUE;
19987 }
19988 return FALSE;
19989}
19990
19991/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019992 * Check if a funcref is assigned to a valid variable name.
19993 * Return TRUE and give an error if not.
19994 */
19995 static int
19996var_check_func_name(name, new_var)
19997 char_u *name; /* points to start of variable name */
19998 int new_var; /* TRUE when creating the variable */
19999{
20000 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20001 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20002 ? name[2] : name[0]))
20003 {
20004 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20005 name);
20006 return TRUE;
20007 }
20008 /* Don't allow hiding a function. When "v" is not NULL we might be
20009 * assigning another function to the same var, the type is checked
20010 * below. */
20011 if (new_var && function_exists(name))
20012 {
20013 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20014 name);
20015 return TRUE;
20016 }
20017 return FALSE;
20018}
20019
20020/*
20021 * Check if a variable name is valid.
20022 * Return FALSE and give an error if not.
20023 */
20024 static int
20025valid_varname(varname)
20026 char_u *varname;
20027{
20028 char_u *p;
20029
20030 for (p = varname; *p != NUL; ++p)
20031 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20032 && *p != AUTOLOAD_CHAR)
20033 {
20034 EMSG2(_(e_illvar), varname);
20035 return FALSE;
20036 }
20037 return TRUE;
20038}
20039
20040/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020041 * Return TRUE if typeval "tv" is set to be locked (immutable).
20042 * Also give an error message, using "name".
20043 */
20044 static int
20045tv_check_lock(lock, name)
20046 int lock;
20047 char_u *name;
20048{
20049 if (lock & VAR_LOCKED)
20050 {
20051 EMSG2(_("E741: Value is locked: %s"),
20052 name == NULL ? (char_u *)_("Unknown") : name);
20053 return TRUE;
20054 }
20055 if (lock & VAR_FIXED)
20056 {
20057 EMSG2(_("E742: Cannot change value of %s"),
20058 name == NULL ? (char_u *)_("Unknown") : name);
20059 return TRUE;
20060 }
20061 return FALSE;
20062}
20063
20064/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020065 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020066 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020067 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020068 * It is OK for "from" and "to" to point to the same item. This is used to
20069 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020070 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020071 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020072copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020073 typval_T *from;
20074 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020076 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020077 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020078 switch (from->v_type)
20079 {
20080 case VAR_NUMBER:
20081 to->vval.v_number = from->vval.v_number;
20082 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020083#ifdef FEAT_FLOAT
20084 case VAR_FLOAT:
20085 to->vval.v_float = from->vval.v_float;
20086 break;
20087#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020088 case VAR_STRING:
20089 case VAR_FUNC:
20090 if (from->vval.v_string == NULL)
20091 to->vval.v_string = NULL;
20092 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020093 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020094 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020095 if (from->v_type == VAR_FUNC)
20096 func_ref(to->vval.v_string);
20097 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020098 break;
20099 case VAR_LIST:
20100 if (from->vval.v_list == NULL)
20101 to->vval.v_list = NULL;
20102 else
20103 {
20104 to->vval.v_list = from->vval.v_list;
20105 ++to->vval.v_list->lv_refcount;
20106 }
20107 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020108 case VAR_DICT:
20109 if (from->vval.v_dict == NULL)
20110 to->vval.v_dict = NULL;
20111 else
20112 {
20113 to->vval.v_dict = from->vval.v_dict;
20114 ++to->vval.v_dict->dv_refcount;
20115 }
20116 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020117 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020118 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020119 break;
20120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121}
20122
20123/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020124 * Make a copy of an item.
20125 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020126 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20127 * reference to an already copied list/dict can be used.
20128 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020129 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020130 static int
20131item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020132 typval_T *from;
20133 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020134 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020135 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020136{
20137 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020138 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020139
Bram Moolenaar33570922005-01-25 22:26:29 +000020140 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020141 {
20142 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020143 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020144 }
20145 ++recurse;
20146
20147 switch (from->v_type)
20148 {
20149 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020150#ifdef FEAT_FLOAT
20151 case VAR_FLOAT:
20152#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020153 case VAR_STRING:
20154 case VAR_FUNC:
20155 copy_tv(from, to);
20156 break;
20157 case VAR_LIST:
20158 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020159 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020160 if (from->vval.v_list == NULL)
20161 to->vval.v_list = NULL;
20162 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20163 {
20164 /* use the copy made earlier */
20165 to->vval.v_list = from->vval.v_list->lv_copylist;
20166 ++to->vval.v_list->lv_refcount;
20167 }
20168 else
20169 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20170 if (to->vval.v_list == NULL)
20171 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020172 break;
20173 case VAR_DICT:
20174 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020175 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020176 if (from->vval.v_dict == NULL)
20177 to->vval.v_dict = NULL;
20178 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20179 {
20180 /* use the copy made earlier */
20181 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20182 ++to->vval.v_dict->dv_refcount;
20183 }
20184 else
20185 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20186 if (to->vval.v_dict == NULL)
20187 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020188 break;
20189 default:
20190 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020191 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020192 }
20193 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020194 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020195}
20196
20197/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198 * ":echo expr1 ..." print each argument separated with a space, add a
20199 * newline at the end.
20200 * ":echon expr1 ..." print each argument plain.
20201 */
20202 void
20203ex_echo(eap)
20204 exarg_T *eap;
20205{
20206 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020207 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020208 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020209 char_u *p;
20210 int needclr = TRUE;
20211 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020212 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213
20214 if (eap->skip)
20215 ++emsg_skip;
20216 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20217 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020218 /* If eval1() causes an error message the text from the command may
20219 * still need to be cleared. E.g., "echo 22,44". */
20220 need_clr_eos = needclr;
20221
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020223 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224 {
20225 /*
20226 * Report the invalid expression unless the expression evaluation
20227 * has been cancelled due to an aborting error, an interrupt, or an
20228 * exception.
20229 */
20230 if (!aborting())
20231 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020232 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020233 break;
20234 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020235 need_clr_eos = FALSE;
20236
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237 if (!eap->skip)
20238 {
20239 if (atstart)
20240 {
20241 atstart = FALSE;
20242 /* Call msg_start() after eval1(), evaluating the expression
20243 * may cause a message to appear. */
20244 if (eap->cmdidx == CMD_echo)
20245 msg_start();
20246 }
20247 else if (eap->cmdidx == CMD_echo)
20248 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020249 current_copyID += COPYID_INC;
20250 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020251 if (p != NULL)
20252 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020254 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020256 if (*p != TAB && needclr)
20257 {
20258 /* remove any text still there from the command */
20259 msg_clr_eos();
20260 needclr = FALSE;
20261 }
20262 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020263 }
20264 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020265 {
20266#ifdef FEAT_MBYTE
20267 if (has_mbyte)
20268 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020269 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020270
20271 (void)msg_outtrans_len_attr(p, i, echo_attr);
20272 p += i - 1;
20273 }
20274 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020276 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020279 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020281 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282 arg = skipwhite(arg);
20283 }
20284 eap->nextcmd = check_nextcmd(arg);
20285
20286 if (eap->skip)
20287 --emsg_skip;
20288 else
20289 {
20290 /* remove text that may still be there from the command */
20291 if (needclr)
20292 msg_clr_eos();
20293 if (eap->cmdidx == CMD_echo)
20294 msg_end();
20295 }
20296}
20297
20298/*
20299 * ":echohl {name}".
20300 */
20301 void
20302ex_echohl(eap)
20303 exarg_T *eap;
20304{
20305 int id;
20306
20307 id = syn_name2id(eap->arg);
20308 if (id == 0)
20309 echo_attr = 0;
20310 else
20311 echo_attr = syn_id2attr(id);
20312}
20313
20314/*
20315 * ":execute expr1 ..." execute the result of an expression.
20316 * ":echomsg expr1 ..." Print a message
20317 * ":echoerr expr1 ..." Print an error
20318 * Each gets spaces around each argument and a newline at the end for
20319 * echo commands
20320 */
20321 void
20322ex_execute(eap)
20323 exarg_T *eap;
20324{
20325 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020326 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327 int ret = OK;
20328 char_u *p;
20329 garray_T ga;
20330 int len;
20331 int save_did_emsg;
20332
20333 ga_init2(&ga, 1, 80);
20334
20335 if (eap->skip)
20336 ++emsg_skip;
20337 while (*arg != NUL && *arg != '|' && *arg != '\n')
20338 {
20339 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020340 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341 {
20342 /*
20343 * Report the invalid expression unless the expression evaluation
20344 * has been cancelled due to an aborting error, an interrupt, or an
20345 * exception.
20346 */
20347 if (!aborting())
20348 EMSG2(_(e_invexpr2), p);
20349 ret = FAIL;
20350 break;
20351 }
20352
20353 if (!eap->skip)
20354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020355 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356 len = (int)STRLEN(p);
20357 if (ga_grow(&ga, len + 2) == FAIL)
20358 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020359 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360 ret = FAIL;
20361 break;
20362 }
20363 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366 ga.ga_len += len;
20367 }
20368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020369 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370 arg = skipwhite(arg);
20371 }
20372
20373 if (ret != FAIL && ga.ga_data != NULL)
20374 {
20375 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020376 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020378 out_flush();
20379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 else if (eap->cmdidx == CMD_echoerr)
20381 {
20382 /* We don't want to abort following commands, restore did_emsg. */
20383 save_did_emsg = did_emsg;
20384 EMSG((char_u *)ga.ga_data);
20385 if (!force_abort)
20386 did_emsg = save_did_emsg;
20387 }
20388 else if (eap->cmdidx == CMD_execute)
20389 do_cmdline((char_u *)ga.ga_data,
20390 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20391 }
20392
20393 ga_clear(&ga);
20394
20395 if (eap->skip)
20396 --emsg_skip;
20397
20398 eap->nextcmd = check_nextcmd(arg);
20399}
20400
20401/*
20402 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20403 * "arg" points to the "&" or '+' when called, to "option" when returning.
20404 * Returns NULL when no option name found. Otherwise pointer to the char
20405 * after the option name.
20406 */
20407 static char_u *
20408find_option_end(arg, opt_flags)
20409 char_u **arg;
20410 int *opt_flags;
20411{
20412 char_u *p = *arg;
20413
20414 ++p;
20415 if (*p == 'g' && p[1] == ':')
20416 {
20417 *opt_flags = OPT_GLOBAL;
20418 p += 2;
20419 }
20420 else if (*p == 'l' && p[1] == ':')
20421 {
20422 *opt_flags = OPT_LOCAL;
20423 p += 2;
20424 }
20425 else
20426 *opt_flags = 0;
20427
20428 if (!ASCII_ISALPHA(*p))
20429 return NULL;
20430 *arg = p;
20431
20432 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20433 p += 4; /* termcap option */
20434 else
20435 while (ASCII_ISALPHA(*p))
20436 ++p;
20437 return p;
20438}
20439
20440/*
20441 * ":function"
20442 */
20443 void
20444ex_function(eap)
20445 exarg_T *eap;
20446{
20447 char_u *theline;
20448 int j;
20449 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020450 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451 char_u *name = NULL;
20452 char_u *p;
20453 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020454 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455 garray_T newargs;
20456 garray_T newlines;
20457 int varargs = FALSE;
20458 int mustend = FALSE;
20459 int flags = 0;
20460 ufunc_T *fp;
20461 int indent;
20462 int nesting;
20463 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020464 dictitem_T *v;
20465 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020466 static int func_nr = 0; /* number for nameless function */
20467 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020468 hashtab_T *ht;
20469 int todo;
20470 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020471 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020472
20473 /*
20474 * ":function" without argument: list functions.
20475 */
20476 if (ends_excmd(*eap->arg))
20477 {
20478 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020479 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020480 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020481 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020482 {
20483 if (!HASHITEM_EMPTY(hi))
20484 {
20485 --todo;
20486 fp = HI2UF(hi);
20487 if (!isdigit(*fp->uf_name))
20488 list_func_head(fp, FALSE);
20489 }
20490 }
20491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492 eap->nextcmd = check_nextcmd(eap->arg);
20493 return;
20494 }
20495
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020496 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020497 * ":function /pat": list functions matching pattern.
20498 */
20499 if (*eap->arg == '/')
20500 {
20501 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20502 if (!eap->skip)
20503 {
20504 regmatch_T regmatch;
20505
20506 c = *p;
20507 *p = NUL;
20508 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20509 *p = c;
20510 if (regmatch.regprog != NULL)
20511 {
20512 regmatch.rm_ic = p_ic;
20513
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020514 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020515 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20516 {
20517 if (!HASHITEM_EMPTY(hi))
20518 {
20519 --todo;
20520 fp = HI2UF(hi);
20521 if (!isdigit(*fp->uf_name)
20522 && vim_regexec(&regmatch, fp->uf_name, 0))
20523 list_func_head(fp, FALSE);
20524 }
20525 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020526 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020527 }
20528 }
20529 if (*p == '/')
20530 ++p;
20531 eap->nextcmd = check_nextcmd(p);
20532 return;
20533 }
20534
20535 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020536 * Get the function name. There are these situations:
20537 * func normal function name
20538 * "name" == func, "fudi.fd_dict" == NULL
20539 * dict.func new dictionary entry
20540 * "name" == NULL, "fudi.fd_dict" set,
20541 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20542 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020543 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020544 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20545 * dict.func existing dict entry that's not a Funcref
20546 * "name" == NULL, "fudi.fd_dict" set,
20547 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20548 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020549 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020550 name = trans_function_name(&p, eap->skip, 0, &fudi);
20551 paren = (vim_strchr(p, '(') != NULL);
20552 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553 {
20554 /*
20555 * Return on an invalid expression in braces, unless the expression
20556 * evaluation has been cancelled due to an aborting error, an
20557 * interrupt, or an exception.
20558 */
20559 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020560 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020561 if (!eap->skip && fudi.fd_newkey != NULL)
20562 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020563 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566 else
20567 eap->skip = TRUE;
20568 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020569
Bram Moolenaar071d4272004-06-13 20:20:40 +000020570 /* An error in a function call during evaluation of an expression in magic
20571 * braces should not cause the function not to be defined. */
20572 saved_did_emsg = did_emsg;
20573 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020574
20575 /*
20576 * ":function func" with only function name: list function.
20577 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020578 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020579 {
20580 if (!ends_excmd(*skipwhite(p)))
20581 {
20582 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020583 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584 }
20585 eap->nextcmd = check_nextcmd(p);
20586 if (eap->nextcmd != NULL)
20587 *p = NUL;
20588 if (!eap->skip && !got_int)
20589 {
20590 fp = find_func(name);
20591 if (fp != NULL)
20592 {
20593 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020594 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020596 if (FUNCLINE(fp, j) == NULL)
20597 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020598 msg_putchar('\n');
20599 msg_outnum((long)(j + 1));
20600 if (j < 9)
20601 msg_putchar(' ');
20602 if (j < 99)
20603 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020604 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605 out_flush(); /* show a line at a time */
20606 ui_breakcheck();
20607 }
20608 if (!got_int)
20609 {
20610 msg_putchar('\n');
20611 msg_puts((char_u *)" endfunction");
20612 }
20613 }
20614 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020615 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020617 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020618 }
20619
20620 /*
20621 * ":function name(arg1, arg2)" Define function.
20622 */
20623 p = skipwhite(p);
20624 if (*p != '(')
20625 {
20626 if (!eap->skip)
20627 {
20628 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020629 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020630 }
20631 /* attempt to continue by skipping some text */
20632 if (vim_strchr(p, '(') != NULL)
20633 p = vim_strchr(p, '(');
20634 }
20635 p = skipwhite(p + 1);
20636
20637 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20638 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20639
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020640 if (!eap->skip)
20641 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020642 /* Check the name of the function. Unless it's a dictionary function
20643 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020644 if (name != NULL)
20645 arg = name;
20646 else
20647 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020648 if (arg != NULL && (fudi.fd_di == NULL
20649 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020650 {
20651 if (*arg == K_SPECIAL)
20652 j = 3;
20653 else
20654 j = 0;
20655 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20656 : eval_isnamec(arg[j])))
20657 ++j;
20658 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020659 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020660 }
20661 }
20662
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663 /*
20664 * Isolate the arguments: "arg1, arg2, ...)"
20665 */
20666 while (*p != ')')
20667 {
20668 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20669 {
20670 varargs = TRUE;
20671 p += 3;
20672 mustend = TRUE;
20673 }
20674 else
20675 {
20676 arg = p;
20677 while (ASCII_ISALNUM(*p) || *p == '_')
20678 ++p;
20679 if (arg == p || isdigit(*arg)
20680 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20681 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20682 {
20683 if (!eap->skip)
20684 EMSG2(_("E125: Illegal argument: %s"), arg);
20685 break;
20686 }
20687 if (ga_grow(&newargs, 1) == FAIL)
20688 goto erret;
20689 c = *p;
20690 *p = NUL;
20691 arg = vim_strsave(arg);
20692 if (arg == NULL)
20693 goto erret;
20694 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20695 *p = c;
20696 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697 if (*p == ',')
20698 ++p;
20699 else
20700 mustend = TRUE;
20701 }
20702 p = skipwhite(p);
20703 if (mustend && *p != ')')
20704 {
20705 if (!eap->skip)
20706 EMSG2(_(e_invarg2), eap->arg);
20707 break;
20708 }
20709 }
20710 ++p; /* skip the ')' */
20711
Bram Moolenaare9a41262005-01-15 22:18:47 +000020712 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713 for (;;)
20714 {
20715 p = skipwhite(p);
20716 if (STRNCMP(p, "range", 5) == 0)
20717 {
20718 flags |= FC_RANGE;
20719 p += 5;
20720 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020721 else if (STRNCMP(p, "dict", 4) == 0)
20722 {
20723 flags |= FC_DICT;
20724 p += 4;
20725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726 else if (STRNCMP(p, "abort", 5) == 0)
20727 {
20728 flags |= FC_ABORT;
20729 p += 5;
20730 }
20731 else
20732 break;
20733 }
20734
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020735 /* When there is a line break use what follows for the function body.
20736 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20737 if (*p == '\n')
20738 line_arg = p + 1;
20739 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740 EMSG(_(e_trailing));
20741
20742 /*
20743 * Read the body of the function, until ":endfunction" is found.
20744 */
20745 if (KeyTyped)
20746 {
20747 /* Check if the function already exists, don't let the user type the
20748 * whole function before telling him it doesn't work! For a script we
20749 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020750 if (!eap->skip && !eap->forceit)
20751 {
20752 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20753 EMSG(_(e_funcdict));
20754 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020755 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020757
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020758 if (!eap->skip && did_emsg)
20759 goto erret;
20760
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761 msg_putchar('\n'); /* don't overwrite the function name */
20762 cmdline_row = msg_row;
20763 }
20764
20765 indent = 2;
20766 nesting = 0;
20767 for (;;)
20768 {
20769 msg_scroll = TRUE;
20770 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020771 sourcing_lnum_off = sourcing_lnum;
20772
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020773 if (line_arg != NULL)
20774 {
20775 /* Use eap->arg, split up in parts by line breaks. */
20776 theline = line_arg;
20777 p = vim_strchr(theline, '\n');
20778 if (p == NULL)
20779 line_arg += STRLEN(line_arg);
20780 else
20781 {
20782 *p = NUL;
20783 line_arg = p + 1;
20784 }
20785 }
20786 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787 theline = getcmdline(':', 0L, indent);
20788 else
20789 theline = eap->getline(':', eap->cookie, indent);
20790 if (KeyTyped)
20791 lines_left = Rows - 1;
20792 if (theline == NULL)
20793 {
20794 EMSG(_("E126: Missing :endfunction"));
20795 goto erret;
20796 }
20797
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020798 /* Detect line continuation: sourcing_lnum increased more than one. */
20799 if (sourcing_lnum > sourcing_lnum_off + 1)
20800 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20801 else
20802 sourcing_lnum_off = 0;
20803
Bram Moolenaar071d4272004-06-13 20:20:40 +000020804 if (skip_until != NULL)
20805 {
20806 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20807 * don't check for ":endfunc". */
20808 if (STRCMP(theline, skip_until) == 0)
20809 {
20810 vim_free(skip_until);
20811 skip_until = NULL;
20812 }
20813 }
20814 else
20815 {
20816 /* skip ':' and blanks*/
20817 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20818 ;
20819
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020820 /* Check for "endfunction". */
20821 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020822 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020823 if (line_arg == NULL)
20824 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020825 break;
20826 }
20827
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020828 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829 * at "end". */
20830 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20831 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020832 else if (STRNCMP(p, "if", 2) == 0
20833 || STRNCMP(p, "wh", 2) == 0
20834 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835 || STRNCMP(p, "try", 3) == 0)
20836 indent += 2;
20837
20838 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020839 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020841 if (*p == '!')
20842 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020843 p += eval_fname_script(p);
20844 if (ASCII_ISALPHA(*p))
20845 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020846 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847 if (*skipwhite(p) == '(')
20848 {
20849 ++nesting;
20850 indent += 2;
20851 }
20852 }
20853 }
20854
20855 /* Check for ":append" or ":insert". */
20856 p = skip_range(p, NULL);
20857 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20858 || (p[0] == 'i'
20859 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20860 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20861 skip_until = vim_strsave((char_u *)".");
20862
20863 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20864 arg = skipwhite(skiptowhite(p));
20865 if (arg[0] == '<' && arg[1] =='<'
20866 && ((p[0] == 'p' && p[1] == 'y'
20867 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20868 || (p[0] == 'p' && p[1] == 'e'
20869 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20870 || (p[0] == 't' && p[1] == 'c'
20871 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20872 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20873 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020874 || (p[0] == 'm' && p[1] == 'z'
20875 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020876 ))
20877 {
20878 /* ":python <<" continues until a dot, like ":append" */
20879 p = skipwhite(arg + 2);
20880 if (*p == NUL)
20881 skip_until = vim_strsave((char_u *)".");
20882 else
20883 skip_until = vim_strsave(p);
20884 }
20885 }
20886
20887 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020888 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020889 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020890 if (line_arg == NULL)
20891 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020893 }
20894
20895 /* Copy the line to newly allocated memory. get_one_sourceline()
20896 * allocates 250 bytes per line, this saves 80% on average. The cost
20897 * is an extra alloc/free. */
20898 p = vim_strsave(theline);
20899 if (p != NULL)
20900 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020901 if (line_arg == NULL)
20902 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020903 theline = p;
20904 }
20905
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020906 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20907
20908 /* Add NULL lines for continuation lines, so that the line count is
20909 * equal to the index in the growarray. */
20910 while (sourcing_lnum_off-- > 0)
20911 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020912
20913 /* Check for end of eap->arg. */
20914 if (line_arg != NULL && *line_arg == NUL)
20915 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020916 }
20917
20918 /* Don't define the function when skipping commands or when an error was
20919 * detected. */
20920 if (eap->skip || did_emsg)
20921 goto erret;
20922
20923 /*
20924 * If there are no errors, add the function
20925 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020926 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020927 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020928 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020929 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020930 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020931 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020932 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020933 goto erret;
20934 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020935
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020936 fp = find_func(name);
20937 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020939 if (!eap->forceit)
20940 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020941 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020942 goto erret;
20943 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020944 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020945 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020946 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020947 name);
20948 goto erret;
20949 }
20950 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020951 ga_clear_strings(&(fp->uf_args));
20952 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020953 vim_free(name);
20954 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956 }
20957 else
20958 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020959 char numbuf[20];
20960
20961 fp = NULL;
20962 if (fudi.fd_newkey == NULL && !eap->forceit)
20963 {
20964 EMSG(_(e_funcdict));
20965 goto erret;
20966 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020967 if (fudi.fd_di == NULL)
20968 {
20969 /* Can't add a function to a locked dictionary */
20970 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20971 goto erret;
20972 }
20973 /* Can't change an existing function if it is locked */
20974 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20975 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020976
20977 /* Give the function a sequential number. Can only be used with a
20978 * Funcref! */
20979 vim_free(name);
20980 sprintf(numbuf, "%d", ++func_nr);
20981 name = vim_strsave((char_u *)numbuf);
20982 if (name == NULL)
20983 goto erret;
20984 }
20985
20986 if (fp == NULL)
20987 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020988 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020989 {
20990 int slen, plen;
20991 char_u *scriptname;
20992
20993 /* Check that the autoload name matches the script name. */
20994 j = FAIL;
20995 if (sourcing_name != NULL)
20996 {
20997 scriptname = autoload_name(name);
20998 if (scriptname != NULL)
20999 {
21000 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021001 plen = (int)STRLEN(p);
21002 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021003 if (slen > plen && fnamecmp(p,
21004 sourcing_name + slen - plen) == 0)
21005 j = OK;
21006 vim_free(scriptname);
21007 }
21008 }
21009 if (j == FAIL)
21010 {
21011 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21012 goto erret;
21013 }
21014 }
21015
21016 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 if (fp == NULL)
21018 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021019
21020 if (fudi.fd_dict != NULL)
21021 {
21022 if (fudi.fd_di == NULL)
21023 {
21024 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021025 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021026 if (fudi.fd_di == NULL)
21027 {
21028 vim_free(fp);
21029 goto erret;
21030 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021031 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21032 {
21033 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021034 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021035 goto erret;
21036 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021037 }
21038 else
21039 /* overwrite existing dict entry */
21040 clear_tv(&fudi.fd_di->di_tv);
21041 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021042 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021043 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021044 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021045
21046 /* behave like "dict" was used */
21047 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021048 }
21049
Bram Moolenaar071d4272004-06-13 20:20:40 +000021050 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021051 STRCPY(fp->uf_name, name);
21052 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021054 fp->uf_args = newargs;
21055 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021056#ifdef FEAT_PROFILE
21057 fp->uf_tml_count = NULL;
21058 fp->uf_tml_total = NULL;
21059 fp->uf_tml_self = NULL;
21060 fp->uf_profiling = FALSE;
21061 if (prof_def_func())
21062 func_do_profile(fp);
21063#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021064 fp->uf_varargs = varargs;
21065 fp->uf_flags = flags;
21066 fp->uf_calls = 0;
21067 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021068 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021069
21070erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071 ga_clear_strings(&newargs);
21072 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021073ret_free:
21074 vim_free(skip_until);
21075 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021076 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078}
21079
21080/*
21081 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021082 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021083 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021084 * flags:
21085 * TFN_INT: internal function name OK
21086 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087 * Advances "pp" to just after the function name (if no error).
21088 */
21089 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021090trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091 char_u **pp;
21092 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021093 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021094 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021095{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021096 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097 char_u *start;
21098 char_u *end;
21099 int lead;
21100 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021101 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021102 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021103
21104 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021105 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021107
21108 /* Check for hard coded <SNR>: already translated function ID (from a user
21109 * command). */
21110 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21111 && (*pp)[2] == (int)KE_SNR)
21112 {
21113 *pp += 3;
21114 len = get_id_len(pp) + 3;
21115 return vim_strnsave(start, len);
21116 }
21117
21118 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21119 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021121 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021123
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021124 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21125 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021126 if (end == start)
21127 {
21128 if (!skip)
21129 EMSG(_("E129: Function name required"));
21130 goto theend;
21131 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021132 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021133 {
21134 /*
21135 * Report an invalid expression in braces, unless the expression
21136 * evaluation has been cancelled due to an aborting error, an
21137 * interrupt, or an exception.
21138 */
21139 if (!aborting())
21140 {
21141 if (end != NULL)
21142 EMSG2(_(e_invarg2), start);
21143 }
21144 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021145 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021146 goto theend;
21147 }
21148
21149 if (lv.ll_tv != NULL)
21150 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021151 if (fdp != NULL)
21152 {
21153 fdp->fd_dict = lv.ll_dict;
21154 fdp->fd_newkey = lv.ll_newkey;
21155 lv.ll_newkey = NULL;
21156 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021157 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021158 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21159 {
21160 name = vim_strsave(lv.ll_tv->vval.v_string);
21161 *pp = end;
21162 }
21163 else
21164 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021165 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21166 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021167 EMSG(_(e_funcref));
21168 else
21169 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021170 name = NULL;
21171 }
21172 goto theend;
21173 }
21174
21175 if (lv.ll_name == NULL)
21176 {
21177 /* Error found, but continue after the function name. */
21178 *pp = end;
21179 goto theend;
21180 }
21181
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021182 /* Check if the name is a Funcref. If so, use the value. */
21183 if (lv.ll_exp_name != NULL)
21184 {
21185 len = (int)STRLEN(lv.ll_exp_name);
21186 name = deref_func_name(lv.ll_exp_name, &len);
21187 if (name == lv.ll_exp_name)
21188 name = NULL;
21189 }
21190 else
21191 {
21192 len = (int)(end - *pp);
21193 name = deref_func_name(*pp, &len);
21194 if (name == *pp)
21195 name = NULL;
21196 }
21197 if (name != NULL)
21198 {
21199 name = vim_strsave(name);
21200 *pp = end;
21201 goto theend;
21202 }
21203
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021204 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021205 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021206 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021207 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21208 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21209 {
21210 /* When there was "s:" already or the name expanded to get a
21211 * leading "s:" then remove it. */
21212 lv.ll_name += 2;
21213 len -= 2;
21214 lead = 2;
21215 }
21216 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021217 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021218 {
21219 if (lead == 2) /* skip over "s:" */
21220 lv.ll_name += 2;
21221 len = (int)(end - lv.ll_name);
21222 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021223
21224 /*
21225 * Copy the function name to allocated memory.
21226 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21227 * Accept <SNR>123_name() outside a script.
21228 */
21229 if (skip)
21230 lead = 0; /* do nothing */
21231 else if (lead > 0)
21232 {
21233 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021234 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21235 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021236 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021237 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021238 if (current_SID <= 0)
21239 {
21240 EMSG(_(e_usingsid));
21241 goto theend;
21242 }
21243 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21244 lead += (int)STRLEN(sid_buf);
21245 }
21246 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021247 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021248 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021249 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021250 goto theend;
21251 }
21252 name = alloc((unsigned)(len + lead + 1));
21253 if (name != NULL)
21254 {
21255 if (lead > 0)
21256 {
21257 name[0] = K_SPECIAL;
21258 name[1] = KS_EXTRA;
21259 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021260 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021261 STRCPY(name + 3, sid_buf);
21262 }
21263 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21264 name[len + lead] = NUL;
21265 }
21266 *pp = end;
21267
21268theend:
21269 clear_lval(&lv);
21270 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271}
21272
21273/*
21274 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21275 * Return 2 if "p" starts with "s:".
21276 * Return 0 otherwise.
21277 */
21278 static int
21279eval_fname_script(p)
21280 char_u *p;
21281{
21282 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21283 || STRNICMP(p + 1, "SNR>", 4) == 0))
21284 return 5;
21285 if (p[0] == 's' && p[1] == ':')
21286 return 2;
21287 return 0;
21288}
21289
21290/*
21291 * Return TRUE if "p" starts with "<SID>" or "s:".
21292 * Only works if eval_fname_script() returned non-zero for "p"!
21293 */
21294 static int
21295eval_fname_sid(p)
21296 char_u *p;
21297{
21298 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21299}
21300
21301/*
21302 * List the head of the function: "name(arg1, arg2)".
21303 */
21304 static void
21305list_func_head(fp, indent)
21306 ufunc_T *fp;
21307 int indent;
21308{
21309 int j;
21310
21311 msg_start();
21312 if (indent)
21313 MSG_PUTS(" ");
21314 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021315 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316 {
21317 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021318 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319 }
21320 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021321 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021322 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021323 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324 {
21325 if (j)
21326 MSG_PUTS(", ");
21327 msg_puts(FUNCARG(fp, j));
21328 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021329 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021330 {
21331 if (j)
21332 MSG_PUTS(", ");
21333 MSG_PUTS("...");
21334 }
21335 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021336 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021337 if (p_verbose > 0)
21338 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021339}
21340
21341/*
21342 * Find a function by name, return pointer to it in ufuncs.
21343 * Return NULL for unknown function.
21344 */
21345 static ufunc_T *
21346find_func(name)
21347 char_u *name;
21348{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021349 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021351 hi = hash_find(&func_hashtab, name);
21352 if (!HASHITEM_EMPTY(hi))
21353 return HI2UF(hi);
21354 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355}
21356
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021357#if defined(EXITFREE) || defined(PROTO)
21358 void
21359free_all_functions()
21360{
21361 hashitem_T *hi;
21362
21363 /* Need to start all over every time, because func_free() may change the
21364 * hash table. */
21365 while (func_hashtab.ht_used > 0)
21366 for (hi = func_hashtab.ht_array; ; ++hi)
21367 if (!HASHITEM_EMPTY(hi))
21368 {
21369 func_free(HI2UF(hi));
21370 break;
21371 }
21372}
21373#endif
21374
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021375/*
21376 * Return TRUE if a function "name" exists.
21377 */
21378 static int
21379function_exists(name)
21380 char_u *name;
21381{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021382 char_u *nm = name;
21383 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021384 int n = FALSE;
21385
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021386 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021387 nm = skipwhite(nm);
21388
21389 /* Only accept "funcname", "funcname ", "funcname (..." and
21390 * "funcname(...", not "funcname!...". */
21391 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021392 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021393 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021394 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021395 else
21396 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021397 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021398 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021399 return n;
21400}
21401
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021402/*
21403 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021404 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021405 */
21406 static int
21407builtin_function(name)
21408 char_u *name;
21409{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021410 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21411 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021412}
21413
Bram Moolenaar05159a02005-02-26 23:04:13 +000021414#if defined(FEAT_PROFILE) || defined(PROTO)
21415/*
21416 * Start profiling function "fp".
21417 */
21418 static void
21419func_do_profile(fp)
21420 ufunc_T *fp;
21421{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021422 int len = fp->uf_lines.ga_len;
21423
21424 if (len == 0)
21425 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021426 fp->uf_tm_count = 0;
21427 profile_zero(&fp->uf_tm_self);
21428 profile_zero(&fp->uf_tm_total);
21429 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021430 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021431 if (fp->uf_tml_total == NULL)
21432 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021433 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021434 if (fp->uf_tml_self == NULL)
21435 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021436 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021437 fp->uf_tml_idx = -1;
21438 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21439 || fp->uf_tml_self == NULL)
21440 return; /* out of memory */
21441
21442 fp->uf_profiling = TRUE;
21443}
21444
21445/*
21446 * Dump the profiling results for all functions in file "fd".
21447 */
21448 void
21449func_dump_profile(fd)
21450 FILE *fd;
21451{
21452 hashitem_T *hi;
21453 int todo;
21454 ufunc_T *fp;
21455 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021456 ufunc_T **sorttab;
21457 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021458
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021459 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021460 if (todo == 0)
21461 return; /* nothing to dump */
21462
Bram Moolenaar73830342005-02-28 22:48:19 +000021463 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21464
Bram Moolenaar05159a02005-02-26 23:04:13 +000021465 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21466 {
21467 if (!HASHITEM_EMPTY(hi))
21468 {
21469 --todo;
21470 fp = HI2UF(hi);
21471 if (fp->uf_profiling)
21472 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021473 if (sorttab != NULL)
21474 sorttab[st_len++] = fp;
21475
Bram Moolenaar05159a02005-02-26 23:04:13 +000021476 if (fp->uf_name[0] == K_SPECIAL)
21477 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21478 else
21479 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21480 if (fp->uf_tm_count == 1)
21481 fprintf(fd, "Called 1 time\n");
21482 else
21483 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21484 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21485 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21486 fprintf(fd, "\n");
21487 fprintf(fd, "count total (s) self (s)\n");
21488
21489 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21490 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021491 if (FUNCLINE(fp, i) == NULL)
21492 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021493 prof_func_line(fd, fp->uf_tml_count[i],
21494 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021495 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21496 }
21497 fprintf(fd, "\n");
21498 }
21499 }
21500 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021501
21502 if (sorttab != NULL && st_len > 0)
21503 {
21504 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21505 prof_total_cmp);
21506 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21507 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21508 prof_self_cmp);
21509 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21510 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021511
21512 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021513}
Bram Moolenaar73830342005-02-28 22:48:19 +000021514
21515 static void
21516prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21517 FILE *fd;
21518 ufunc_T **sorttab;
21519 int st_len;
21520 char *title;
21521 int prefer_self; /* when equal print only self time */
21522{
21523 int i;
21524 ufunc_T *fp;
21525
21526 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21527 fprintf(fd, "count total (s) self (s) function\n");
21528 for (i = 0; i < 20 && i < st_len; ++i)
21529 {
21530 fp = sorttab[i];
21531 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21532 prefer_self);
21533 if (fp->uf_name[0] == K_SPECIAL)
21534 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21535 else
21536 fprintf(fd, " %s()\n", fp->uf_name);
21537 }
21538 fprintf(fd, "\n");
21539}
21540
21541/*
21542 * Print the count and times for one function or function line.
21543 */
21544 static void
21545prof_func_line(fd, count, total, self, prefer_self)
21546 FILE *fd;
21547 int count;
21548 proftime_T *total;
21549 proftime_T *self;
21550 int prefer_self; /* when equal print only self time */
21551{
21552 if (count > 0)
21553 {
21554 fprintf(fd, "%5d ", count);
21555 if (prefer_self && profile_equal(total, self))
21556 fprintf(fd, " ");
21557 else
21558 fprintf(fd, "%s ", profile_msg(total));
21559 if (!prefer_self && profile_equal(total, self))
21560 fprintf(fd, " ");
21561 else
21562 fprintf(fd, "%s ", profile_msg(self));
21563 }
21564 else
21565 fprintf(fd, " ");
21566}
21567
21568/*
21569 * Compare function for total time sorting.
21570 */
21571 static int
21572#ifdef __BORLANDC__
21573_RTLENTRYF
21574#endif
21575prof_total_cmp(s1, s2)
21576 const void *s1;
21577 const void *s2;
21578{
21579 ufunc_T *p1, *p2;
21580
21581 p1 = *(ufunc_T **)s1;
21582 p2 = *(ufunc_T **)s2;
21583 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21584}
21585
21586/*
21587 * Compare function for self time sorting.
21588 */
21589 static int
21590#ifdef __BORLANDC__
21591_RTLENTRYF
21592#endif
21593prof_self_cmp(s1, s2)
21594 const void *s1;
21595 const void *s2;
21596{
21597 ufunc_T *p1, *p2;
21598
21599 p1 = *(ufunc_T **)s1;
21600 p2 = *(ufunc_T **)s2;
21601 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21602}
21603
Bram Moolenaar05159a02005-02-26 23:04:13 +000021604#endif
21605
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021606/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021607 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021608 * Return TRUE if a package was loaded.
21609 */
21610 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021611script_autoload(name, reload)
21612 char_u *name;
21613 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021614{
21615 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021616 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021617 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021618 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021619
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021620 /* Return quickly when autoload disabled. */
21621 if (no_autoload)
21622 return FALSE;
21623
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021624 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021625 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021626 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021627 return FALSE;
21628
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021629 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021630
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021631 /* Find the name in the list of previously loaded package names. Skip
21632 * "autoload/", it's always the same. */
21633 for (i = 0; i < ga_loaded.ga_len; ++i)
21634 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21635 break;
21636 if (!reload && i < ga_loaded.ga_len)
21637 ret = FALSE; /* was loaded already */
21638 else
21639 {
21640 /* Remember the name if it wasn't loaded already. */
21641 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21642 {
21643 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21644 tofree = NULL;
21645 }
21646
21647 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021648 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021649 ret = TRUE;
21650 }
21651
21652 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021653 return ret;
21654}
21655
21656/*
21657 * Return the autoload script name for a function or variable name.
21658 * Returns NULL when out of memory.
21659 */
21660 static char_u *
21661autoload_name(name)
21662 char_u *name;
21663{
21664 char_u *p;
21665 char_u *scriptname;
21666
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021667 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021668 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21669 if (scriptname == NULL)
21670 return FALSE;
21671 STRCPY(scriptname, "autoload/");
21672 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021673 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021674 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021675 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021676 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021677 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021678}
21679
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21681
21682/*
21683 * Function given to ExpandGeneric() to obtain the list of user defined
21684 * function names.
21685 */
21686 char_u *
21687get_user_func_name(xp, idx)
21688 expand_T *xp;
21689 int idx;
21690{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021691 static long_u done;
21692 static hashitem_T *hi;
21693 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694
21695 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021696 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021697 done = 0;
21698 hi = func_hashtab.ht_array;
21699 }
21700 if (done < func_hashtab.ht_used)
21701 {
21702 if (done++ > 0)
21703 ++hi;
21704 while (HASHITEM_EMPTY(hi))
21705 ++hi;
21706 fp = HI2UF(hi);
21707
21708 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21709 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710
21711 cat_func_name(IObuff, fp);
21712 if (xp->xp_context != EXPAND_USER_FUNC)
21713 {
21714 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021715 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021716 STRCAT(IObuff, ")");
21717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021718 return IObuff;
21719 }
21720 return NULL;
21721}
21722
21723#endif /* FEAT_CMDL_COMPL */
21724
21725/*
21726 * Copy the function name of "fp" to buffer "buf".
21727 * "buf" must be able to hold the function name plus three bytes.
21728 * Takes care of script-local function names.
21729 */
21730 static void
21731cat_func_name(buf, fp)
21732 char_u *buf;
21733 ufunc_T *fp;
21734{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021735 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736 {
21737 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021738 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021739 }
21740 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021741 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021742}
21743
21744/*
21745 * ":delfunction {name}"
21746 */
21747 void
21748ex_delfunction(eap)
21749 exarg_T *eap;
21750{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021751 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752 char_u *p;
21753 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021754 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021755
21756 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021757 name = trans_function_name(&p, eap->skip, 0, &fudi);
21758 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021760 {
21761 if (fudi.fd_dict != NULL && !eap->skip)
21762 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021763 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765 if (!ends_excmd(*skipwhite(p)))
21766 {
21767 vim_free(name);
21768 EMSG(_(e_trailing));
21769 return;
21770 }
21771 eap->nextcmd = check_nextcmd(p);
21772 if (eap->nextcmd != NULL)
21773 *p = NUL;
21774
21775 if (!eap->skip)
21776 fp = find_func(name);
21777 vim_free(name);
21778
21779 if (!eap->skip)
21780 {
21781 if (fp == NULL)
21782 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021783 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021784 return;
21785 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021786 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787 {
21788 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21789 return;
21790 }
21791
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021792 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021793 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021794 /* Delete the dict item that refers to the function, it will
21795 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021796 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021797 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021798 else
21799 func_free(fp);
21800 }
21801}
21802
21803/*
21804 * Free a function and remove it from the list of functions.
21805 */
21806 static void
21807func_free(fp)
21808 ufunc_T *fp;
21809{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021810 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021811
21812 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021813 ga_clear_strings(&(fp->uf_args));
21814 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021815#ifdef FEAT_PROFILE
21816 vim_free(fp->uf_tml_count);
21817 vim_free(fp->uf_tml_total);
21818 vim_free(fp->uf_tml_self);
21819#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021820
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021821 /* remove the function from the function hashtable */
21822 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21823 if (HASHITEM_EMPTY(hi))
21824 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021825 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021826 hash_remove(&func_hashtab, hi);
21827
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021828 vim_free(fp);
21829}
21830
21831/*
21832 * Unreference a Function: decrement the reference count and free it when it
21833 * becomes zero. Only for numbered functions.
21834 */
21835 static void
21836func_unref(name)
21837 char_u *name;
21838{
21839 ufunc_T *fp;
21840
21841 if (name != NULL && isdigit(*name))
21842 {
21843 fp = find_func(name);
21844 if (fp == NULL)
21845 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021846 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021847 {
21848 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021849 * when "uf_calls" becomes zero. */
21850 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021851 func_free(fp);
21852 }
21853 }
21854}
21855
21856/*
21857 * Count a reference to a Function.
21858 */
21859 static void
21860func_ref(name)
21861 char_u *name;
21862{
21863 ufunc_T *fp;
21864
21865 if (name != NULL && isdigit(*name))
21866 {
21867 fp = find_func(name);
21868 if (fp == NULL)
21869 EMSG2(_(e_intern2), "func_ref()");
21870 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021871 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021872 }
21873}
21874
21875/*
21876 * Call a user function.
21877 */
21878 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021879call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880 ufunc_T *fp; /* pointer to function */
21881 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021882 typval_T *argvars; /* arguments */
21883 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884 linenr_T firstline; /* first line of range */
21885 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021886 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021887{
Bram Moolenaar33570922005-01-25 22:26:29 +000021888 char_u *save_sourcing_name;
21889 linenr_T save_sourcing_lnum;
21890 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021891 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021892 int save_did_emsg;
21893 static int depth = 0;
21894 dictitem_T *v;
21895 int fixvar_idx = 0; /* index in fixvar[] */
21896 int i;
21897 int ai;
21898 char_u numbuf[NUMBUFLEN];
21899 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021900#ifdef FEAT_PROFILE
21901 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021902 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021904
21905 /* If depth of calling is getting too high, don't execute the function */
21906 if (depth >= p_mfd)
21907 {
21908 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021909 rettv->v_type = VAR_NUMBER;
21910 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021911 return;
21912 }
21913 ++depth;
21914
21915 line_breakcheck(); /* check for CTRL-C hit */
21916
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021917 fc = (funccall_T *)alloc(sizeof(funccall_T));
21918 fc->caller = current_funccal;
21919 current_funccal = fc;
21920 fc->func = fp;
21921 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021922 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021923 fc->linenr = 0;
21924 fc->returned = FALSE;
21925 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021927 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21928 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021929
Bram Moolenaar33570922005-01-25 22:26:29 +000021930 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021931 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021932 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21933 * each argument variable and saves a lot of time.
21934 */
21935 /*
21936 * Init l: variables.
21937 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021938 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021939 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021940 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021941 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21942 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021943 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021944 name = v->di_key;
21945 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021946 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021947 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021948 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021949 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021950 v->di_tv.vval.v_dict = selfdict;
21951 ++selfdict->dv_refcount;
21952 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021953
Bram Moolenaar33570922005-01-25 22:26:29 +000021954 /*
21955 * Init a: variables.
21956 * Set a:0 to "argcount".
21957 * Set a:000 to a list with room for the "..." arguments.
21958 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021959 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21960 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021961 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021962 /* Use "name" to avoid a warning from some compiler that checks the
21963 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021964 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021965 name = v->di_key;
21966 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021967 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021968 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021969 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021970 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021971 v->di_tv.vval.v_list = &fc->l_varlist;
21972 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21973 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21974 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021975
21976 /*
21977 * Set a:firstline to "firstline" and a:lastline to "lastline".
21978 * Set a:name to named arguments.
21979 * Set a:N to the "..." arguments.
21980 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021981 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021982 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021983 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021984 (varnumber_T)lastline);
21985 for (i = 0; i < argcount; ++i)
21986 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021987 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021988 if (ai < 0)
21989 /* named argument a:name */
21990 name = FUNCARG(fp, i);
21991 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021992 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021993 /* "..." argument a:1, a:2, etc. */
21994 sprintf((char *)numbuf, "%d", ai + 1);
21995 name = numbuf;
21996 }
21997 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21998 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021999 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022000 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22001 }
22002 else
22003 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022004 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22005 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022006 if (v == NULL)
22007 break;
22008 v->di_flags = DI_FLAGS_RO;
22009 }
22010 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022011 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022012
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022013 /* Note: the values are copied directly to avoid alloc/free.
22014 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022015 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022016 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022017
22018 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22019 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022020 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22021 fc->l_listitems[ai].li_tv = argvars[i];
22022 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022023 }
22024 }
22025
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026 /* Don't redraw while executing the function. */
22027 ++RedrawingDisabled;
22028 save_sourcing_name = sourcing_name;
22029 save_sourcing_lnum = sourcing_lnum;
22030 sourcing_lnum = 1;
22031 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022032 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022033 if (sourcing_name != NULL)
22034 {
22035 if (save_sourcing_name != NULL
22036 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22037 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22038 else
22039 STRCPY(sourcing_name, "function ");
22040 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22041
22042 if (p_verbose >= 12)
22043 {
22044 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022045 verbose_enter_scroll();
22046
Bram Moolenaar555b2802005-05-19 21:08:39 +000022047 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 if (p_verbose >= 14)
22049 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022051 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022052 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022053 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054
22055 msg_puts((char_u *)"(");
22056 for (i = 0; i < argcount; ++i)
22057 {
22058 if (i > 0)
22059 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022060 if (argvars[i].v_type == VAR_NUMBER)
22061 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022062 else
22063 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022064 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22065 if (s != NULL)
22066 {
22067 trunc_string(s, buf, MSG_BUF_CLEN);
22068 msg_puts(buf);
22069 vim_free(tofree);
22070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 }
22072 }
22073 msg_puts((char_u *)")");
22074 }
22075 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022076
22077 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022078 --no_wait_return;
22079 }
22080 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022081#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022082 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022083 {
22084 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22085 func_do_profile(fp);
22086 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022087 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022088 {
22089 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022090 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022091 profile_zero(&fp->uf_tm_children);
22092 }
22093 script_prof_save(&wait_start);
22094 }
22095#endif
22096
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022098 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022099 save_did_emsg = did_emsg;
22100 did_emsg = FALSE;
22101
22102 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022103 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022104 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22105
22106 --RedrawingDisabled;
22107
22108 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022109 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022110 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022111 clear_tv(rettv);
22112 rettv->v_type = VAR_NUMBER;
22113 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022114 }
22115
Bram Moolenaar05159a02005-02-26 23:04:13 +000022116#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022117 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022118 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022119 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022120 profile_end(&call_start);
22121 profile_sub_wait(&wait_start, &call_start);
22122 profile_add(&fp->uf_tm_total, &call_start);
22123 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022124 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022125 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022126 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22127 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022128 }
22129 }
22130#endif
22131
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132 /* when being verbose, mention the return value */
22133 if (p_verbose >= 12)
22134 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022136 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137
Bram Moolenaar071d4272004-06-13 20:20:40 +000022138 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022139 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022140 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022141 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022142 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022143 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022145 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022146 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022147 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022148 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022149
Bram Moolenaar555b2802005-05-19 21:08:39 +000022150 /* The value may be very long. Skip the middle part, so that we
22151 * have some idea how it starts and ends. smsg() would always
22152 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022153 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022154 if (s != NULL)
22155 {
22156 trunc_string(s, buf, MSG_BUF_CLEN);
22157 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
22158 vim_free(tofree);
22159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022160 }
22161 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022162
22163 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164 --no_wait_return;
22165 }
22166
22167 vim_free(sourcing_name);
22168 sourcing_name = save_sourcing_name;
22169 sourcing_lnum = save_sourcing_lnum;
22170 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022171#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022172 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022173 script_prof_restore(&wait_start);
22174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175
22176 if (p_verbose >= 12 && sourcing_name != NULL)
22177 {
22178 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022179 verbose_enter_scroll();
22180
Bram Moolenaar555b2802005-05-19 21:08:39 +000022181 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022183
22184 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022185 --no_wait_return;
22186 }
22187
22188 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022189 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022190 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022191
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022192 /* If the a:000 list and the l: and a: dicts are not referenced we can
22193 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022194 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22195 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22196 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22197 {
22198 free_funccal(fc, FALSE);
22199 }
22200 else
22201 {
22202 hashitem_T *hi;
22203 listitem_T *li;
22204 int todo;
22205
22206 /* "fc" is still in use. This can happen when returning "a:000" or
22207 * assigning "l:" to a global variable.
22208 * Link "fc" in the list for garbage collection later. */
22209 fc->caller = previous_funccal;
22210 previous_funccal = fc;
22211
22212 /* Make a copy of the a: variables, since we didn't do that above. */
22213 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22214 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22215 {
22216 if (!HASHITEM_EMPTY(hi))
22217 {
22218 --todo;
22219 v = HI2DI(hi);
22220 copy_tv(&v->di_tv, &v->di_tv);
22221 }
22222 }
22223
22224 /* Make a copy of the a:000 items, since we didn't do that above. */
22225 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22226 copy_tv(&li->li_tv, &li->li_tv);
22227 }
22228}
22229
22230/*
22231 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022232 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022233 */
22234 static int
22235can_free_funccal(fc, copyID)
22236 funccall_T *fc;
22237 int copyID;
22238{
22239 return (fc->l_varlist.lv_copyID != copyID
22240 && fc->l_vars.dv_copyID != copyID
22241 && fc->l_avars.dv_copyID != copyID);
22242}
22243
22244/*
22245 * Free "fc" and what it contains.
22246 */
22247 static void
22248free_funccal(fc, free_val)
22249 funccall_T *fc;
22250 int free_val; /* a: vars were allocated */
22251{
22252 listitem_T *li;
22253
22254 /* The a: variables typevals may not have been allocated, only free the
22255 * allocated variables. */
22256 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22257
22258 /* free all l: variables */
22259 vars_clear(&fc->l_vars.dv_hashtab);
22260
22261 /* Free the a:000 variables if they were allocated. */
22262 if (free_val)
22263 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22264 clear_tv(&li->li_tv);
22265
22266 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022267}
22268
22269/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022270 * Add a number variable "name" to dict "dp" with value "nr".
22271 */
22272 static void
22273add_nr_var(dp, v, name, nr)
22274 dict_T *dp;
22275 dictitem_T *v;
22276 char *name;
22277 varnumber_T nr;
22278{
22279 STRCPY(v->di_key, name);
22280 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22281 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22282 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022283 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022284 v->di_tv.vval.v_number = nr;
22285}
22286
22287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288 * ":return [expr]"
22289 */
22290 void
22291ex_return(eap)
22292 exarg_T *eap;
22293{
22294 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022295 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296 int returning = FALSE;
22297
22298 if (current_funccal == NULL)
22299 {
22300 EMSG(_("E133: :return not inside a function"));
22301 return;
22302 }
22303
22304 if (eap->skip)
22305 ++emsg_skip;
22306
22307 eap->nextcmd = NULL;
22308 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022309 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022310 {
22311 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022312 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022313 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022314 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022315 }
22316 /* It's safer to return also on error. */
22317 else if (!eap->skip)
22318 {
22319 /*
22320 * Return unless the expression evaluation has been cancelled due to an
22321 * aborting error, an interrupt, or an exception.
22322 */
22323 if (!aborting())
22324 returning = do_return(eap, FALSE, TRUE, NULL);
22325 }
22326
22327 /* When skipping or the return gets pending, advance to the next command
22328 * in this line (!returning). Otherwise, ignore the rest of the line.
22329 * Following lines will be ignored by get_func_line(). */
22330 if (returning)
22331 eap->nextcmd = NULL;
22332 else if (eap->nextcmd == NULL) /* no argument */
22333 eap->nextcmd = check_nextcmd(arg);
22334
22335 if (eap->skip)
22336 --emsg_skip;
22337}
22338
22339/*
22340 * Return from a function. Possibly makes the return pending. Also called
22341 * for a pending return at the ":endtry" or after returning from an extra
22342 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022343 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022344 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345 * FALSE when the return gets pending.
22346 */
22347 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022348do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349 exarg_T *eap;
22350 int reanimate;
22351 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022352 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022353{
22354 int idx;
22355 struct condstack *cstack = eap->cstack;
22356
22357 if (reanimate)
22358 /* Undo the return. */
22359 current_funccal->returned = FALSE;
22360
22361 /*
22362 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22363 * not in its finally clause (which then is to be executed next) is found.
22364 * In this case, make the ":return" pending for execution at the ":endtry".
22365 * Otherwise, return normally.
22366 */
22367 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22368 if (idx >= 0)
22369 {
22370 cstack->cs_pending[idx] = CSTP_RETURN;
22371
22372 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022373 /* A pending return again gets pending. "rettv" points to an
22374 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022375 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022376 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022377 else
22378 {
22379 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022380 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022381 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022382 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022383
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022384 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385 {
22386 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022387 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022388 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022389 else
22390 EMSG(_(e_outofmem));
22391 }
22392 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022393 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022394
22395 if (reanimate)
22396 {
22397 /* The pending return value could be overwritten by a ":return"
22398 * without argument in a finally clause; reset the default
22399 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022400 current_funccal->rettv->v_type = VAR_NUMBER;
22401 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022402 }
22403 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022404 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405 }
22406 else
22407 {
22408 current_funccal->returned = TRUE;
22409
22410 /* If the return is carried out now, store the return value. For
22411 * a return immediately after reanimation, the value is already
22412 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022413 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022415 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022416 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022417 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022418 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419 }
22420 }
22421
22422 return idx < 0;
22423}
22424
22425/*
22426 * Free the variable with a pending return value.
22427 */
22428 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022429discard_pending_return(rettv)
22430 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022431{
Bram Moolenaar33570922005-01-25 22:26:29 +000022432 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022433}
22434
22435/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022436 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022437 * is an allocated string. Used by report_pending() for verbose messages.
22438 */
22439 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022440get_return_cmd(rettv)
22441 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022443 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022444 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022445 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022446
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022447 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022448 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022449 if (s == NULL)
22450 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022451
22452 STRCPY(IObuff, ":return ");
22453 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22454 if (STRLEN(s) + 8 >= IOSIZE)
22455 STRCPY(IObuff + IOSIZE - 4, "...");
22456 vim_free(tofree);
22457 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022458}
22459
22460/*
22461 * Get next function line.
22462 * Called by do_cmdline() to get the next line.
22463 * Returns allocated string, or NULL for end of function.
22464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465 char_u *
22466get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022467 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022468 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022469 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470{
Bram Moolenaar33570922005-01-25 22:26:29 +000022471 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022472 ufunc_T *fp = fcp->func;
22473 char_u *retval;
22474 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475
22476 /* If breakpoints have been added/deleted need to check for it. */
22477 if (fcp->dbg_tick != debug_tick)
22478 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022479 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022480 sourcing_lnum);
22481 fcp->dbg_tick = debug_tick;
22482 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022483#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022484 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022485 func_line_end(cookie);
22486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022487
Bram Moolenaar05159a02005-02-26 23:04:13 +000022488 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022489 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22490 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022491 retval = NULL;
22492 else
22493 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022494 /* Skip NULL lines (continuation lines). */
22495 while (fcp->linenr < gap->ga_len
22496 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22497 ++fcp->linenr;
22498 if (fcp->linenr >= gap->ga_len)
22499 retval = NULL;
22500 else
22501 {
22502 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22503 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022504#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022505 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022506 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022507#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509 }
22510
22511 /* Did we encounter a breakpoint? */
22512 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22513 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022514 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022515 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022516 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517 sourcing_lnum);
22518 fcp->dbg_tick = debug_tick;
22519 }
22520
22521 return retval;
22522}
22523
Bram Moolenaar05159a02005-02-26 23:04:13 +000022524#if defined(FEAT_PROFILE) || defined(PROTO)
22525/*
22526 * Called when starting to read a function line.
22527 * "sourcing_lnum" must be correct!
22528 * When skipping lines it may not actually be executed, but we won't find out
22529 * until later and we need to store the time now.
22530 */
22531 void
22532func_line_start(cookie)
22533 void *cookie;
22534{
22535 funccall_T *fcp = (funccall_T *)cookie;
22536 ufunc_T *fp = fcp->func;
22537
22538 if (fp->uf_profiling && sourcing_lnum >= 1
22539 && sourcing_lnum <= fp->uf_lines.ga_len)
22540 {
22541 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022542 /* Skip continuation lines. */
22543 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22544 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022545 fp->uf_tml_execed = FALSE;
22546 profile_start(&fp->uf_tml_start);
22547 profile_zero(&fp->uf_tml_children);
22548 profile_get_wait(&fp->uf_tml_wait);
22549 }
22550}
22551
22552/*
22553 * Called when actually executing a function line.
22554 */
22555 void
22556func_line_exec(cookie)
22557 void *cookie;
22558{
22559 funccall_T *fcp = (funccall_T *)cookie;
22560 ufunc_T *fp = fcp->func;
22561
22562 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22563 fp->uf_tml_execed = TRUE;
22564}
22565
22566/*
22567 * Called when done with a function line.
22568 */
22569 void
22570func_line_end(cookie)
22571 void *cookie;
22572{
22573 funccall_T *fcp = (funccall_T *)cookie;
22574 ufunc_T *fp = fcp->func;
22575
22576 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22577 {
22578 if (fp->uf_tml_execed)
22579 {
22580 ++fp->uf_tml_count[fp->uf_tml_idx];
22581 profile_end(&fp->uf_tml_start);
22582 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022583 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022584 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22585 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022586 }
22587 fp->uf_tml_idx = -1;
22588 }
22589}
22590#endif
22591
Bram Moolenaar071d4272004-06-13 20:20:40 +000022592/*
22593 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022594 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022595 */
22596 int
22597func_has_ended(cookie)
22598 void *cookie;
22599{
Bram Moolenaar33570922005-01-25 22:26:29 +000022600 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601
22602 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22603 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022604 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605 || fcp->returned);
22606}
22607
22608/*
22609 * return TRUE if cookie indicates a function which "abort"s on errors.
22610 */
22611 int
22612func_has_abort(cookie)
22613 void *cookie;
22614{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022615 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616}
22617
22618#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22619typedef enum
22620{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022621 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22622 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22623 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022624} var_flavour_T;
22625
22626static var_flavour_T var_flavour __ARGS((char_u *varname));
22627
22628 static var_flavour_T
22629var_flavour(varname)
22630 char_u *varname;
22631{
22632 char_u *p = varname;
22633
22634 if (ASCII_ISUPPER(*p))
22635 {
22636 while (*(++p))
22637 if (ASCII_ISLOWER(*p))
22638 return VAR_FLAVOUR_SESSION;
22639 return VAR_FLAVOUR_VIMINFO;
22640 }
22641 else
22642 return VAR_FLAVOUR_DEFAULT;
22643}
22644#endif
22645
22646#if defined(FEAT_VIMINFO) || defined(PROTO)
22647/*
22648 * Restore global vars that start with a capital from the viminfo file
22649 */
22650 int
22651read_viminfo_varlist(virp, writing)
22652 vir_T *virp;
22653 int writing;
22654{
22655 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022656 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022657 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022658
22659 if (!writing && (find_viminfo_parameter('!') != NULL))
22660 {
22661 tab = vim_strchr(virp->vir_line + 1, '\t');
22662 if (tab != NULL)
22663 {
22664 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022665 switch (*tab)
22666 {
22667 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022668#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022669 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022670#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022671 case 'D': type = VAR_DICT; break;
22672 case 'L': type = VAR_LIST; break;
22673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022674
22675 tab = vim_strchr(tab, '\t');
22676 if (tab != NULL)
22677 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022678 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022679 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022680 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022682#ifdef FEAT_FLOAT
22683 else if (type == VAR_FLOAT)
22684 (void)string2float(tab + 1, &tv.vval.v_float);
22685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022687 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022688 if (type == VAR_DICT || type == VAR_LIST)
22689 {
22690 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
22691
22692 if (etv == NULL)
22693 /* Failed to parse back the dict or list, use it as a
22694 * string. */
22695 tv.v_type = VAR_STRING;
22696 else
22697 {
22698 vim_free(tv.vval.v_string);
22699 tv = *etv;
22700 }
22701 }
22702
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022703 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022704
22705 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022706 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022707 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
22708 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022709 }
22710 }
22711 }
22712
22713 return viminfo_readline(virp);
22714}
22715
22716/*
22717 * Write global vars that start with a capital to the viminfo file
22718 */
22719 void
22720write_viminfo_varlist(fp)
22721 FILE *fp;
22722{
Bram Moolenaar33570922005-01-25 22:26:29 +000022723 hashitem_T *hi;
22724 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022725 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022726 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022727 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022728 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022729 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730
22731 if (find_viminfo_parameter('!') == NULL)
22732 return;
22733
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022734 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022735
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022736 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022737 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022739 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022741 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022742 this_var = HI2DI(hi);
22743 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022744 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022745 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022746 {
22747 case VAR_STRING: s = "STR"; break;
22748 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022749#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022750 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022751#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022752 case VAR_DICT: s = "DIC"; break;
22753 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000022754 default: continue;
22755 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022756 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022757 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022758 if (p != NULL)
22759 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022760 vim_free(tofree);
22761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762 }
22763 }
22764}
22765#endif
22766
22767#if defined(FEAT_SESSION) || defined(PROTO)
22768 int
22769store_session_globals(fd)
22770 FILE *fd;
22771{
Bram Moolenaar33570922005-01-25 22:26:29 +000022772 hashitem_T *hi;
22773 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022774 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775 char_u *p, *t;
22776
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022777 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022778 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022779 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022780 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022781 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022782 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022783 this_var = HI2DI(hi);
22784 if ((this_var->di_tv.v_type == VAR_NUMBER
22785 || this_var->di_tv.v_type == VAR_STRING)
22786 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022787 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022788 /* Escape special characters with a backslash. Turn a LF and
22789 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022790 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022791 (char_u *)"\\\"\n\r");
22792 if (p == NULL) /* out of memory */
22793 break;
22794 for (t = p; *t != NUL; ++t)
22795 if (*t == '\n')
22796 *t = 'n';
22797 else if (*t == '\r')
22798 *t = 'r';
22799 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022800 this_var->di_key,
22801 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22802 : ' ',
22803 p,
22804 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22805 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022806 || put_eol(fd) == FAIL)
22807 {
22808 vim_free(p);
22809 return FAIL;
22810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022811 vim_free(p);
22812 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022813#ifdef FEAT_FLOAT
22814 else if (this_var->di_tv.v_type == VAR_FLOAT
22815 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22816 {
22817 float_T f = this_var->di_tv.vval.v_float;
22818 int sign = ' ';
22819
22820 if (f < 0)
22821 {
22822 f = -f;
22823 sign = '-';
22824 }
22825 if ((fprintf(fd, "let %s = %c&%f",
22826 this_var->di_key, sign, f) < 0)
22827 || put_eol(fd) == FAIL)
22828 return FAIL;
22829 }
22830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831 }
22832 }
22833 return OK;
22834}
22835#endif
22836
Bram Moolenaar661b1822005-07-28 22:36:45 +000022837/*
22838 * Display script name where an item was last set.
22839 * Should only be invoked when 'verbose' is non-zero.
22840 */
22841 void
22842last_set_msg(scriptID)
22843 scid_T scriptID;
22844{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022845 char_u *p;
22846
Bram Moolenaar661b1822005-07-28 22:36:45 +000022847 if (scriptID != 0)
22848 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022849 p = home_replace_save(NULL, get_scriptname(scriptID));
22850 if (p != NULL)
22851 {
22852 verbose_enter();
22853 MSG_PUTS(_("\n\tLast set from "));
22854 MSG_PUTS(p);
22855 vim_free(p);
22856 verbose_leave();
22857 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022858 }
22859}
22860
Bram Moolenaard812df62008-11-09 12:46:09 +000022861/*
22862 * List v:oldfiles in a nice way.
22863 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022864 void
22865ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022866 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022867{
22868 list_T *l = vimvars[VV_OLDFILES].vv_list;
22869 listitem_T *li;
22870 int nr = 0;
22871
22872 if (l == NULL)
22873 msg((char_u *)_("No old files"));
22874 else
22875 {
22876 msg_start();
22877 msg_scroll = TRUE;
22878 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22879 {
22880 msg_outnum((long)++nr);
22881 MSG_PUTS(": ");
22882 msg_outtrans(get_tv_string(&li->li_tv));
22883 msg_putchar('\n');
22884 out_flush(); /* output one line at a time */
22885 ui_breakcheck();
22886 }
22887 /* Assume "got_int" was set to truncate the listing. */
22888 got_int = FALSE;
22889
22890#ifdef FEAT_BROWSE_CMD
22891 if (cmdmod.browse)
22892 {
22893 quit_more = FALSE;
22894 nr = prompt_for_number(FALSE);
22895 msg_starthere();
22896 if (nr > 0)
22897 {
22898 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22899 (long)nr);
22900
22901 if (p != NULL)
22902 {
22903 p = expand_env_save(p);
22904 eap->arg = p;
22905 eap->cmdidx = CMD_edit;
22906 cmdmod.browse = FALSE;
22907 do_exedit(eap, NULL);
22908 vim_free(p);
22909 }
22910 }
22911 }
22912#endif
22913 }
22914}
22915
Bram Moolenaar071d4272004-06-13 20:20:40 +000022916#endif /* FEAT_EVAL */
22917
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022919#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022920
22921#ifdef WIN3264
22922/*
22923 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22924 */
22925static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22926static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22927static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22928
22929/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022930 * Get the short path (8.3) for the filename in "fnamep".
22931 * Only works for a valid file name.
22932 * When the path gets longer "fnamep" is changed and the allocated buffer
22933 * is put in "bufp".
22934 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22935 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936 */
22937 static int
22938get_short_pathname(fnamep, bufp, fnamelen)
22939 char_u **fnamep;
22940 char_u **bufp;
22941 int *fnamelen;
22942{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022943 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022944 char_u *newbuf;
22945
22946 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947 l = GetShortPathName(*fnamep, *fnamep, len);
22948 if (l > len - 1)
22949 {
22950 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022951 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952 newbuf = vim_strnsave(*fnamep, l);
22953 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022954 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955
22956 vim_free(*bufp);
22957 *fnamep = *bufp = newbuf;
22958
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022959 /* Really should always succeed, as the buffer is big enough. */
22960 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 }
22962
22963 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022964 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965}
22966
22967/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022968 * Get the short path (8.3) for the filename in "fname". The converted
22969 * path is returned in "bufp".
22970 *
22971 * Some of the directories specified in "fname" may not exist. This function
22972 * will shorten the existing directories at the beginning of the path and then
22973 * append the remaining non-existing path.
22974 *
22975 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022976 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022977 * bufp - Pointer to an allocated buffer for the filename.
22978 * fnamelen - Length of the filename pointed to by fname
22979 *
22980 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981 */
22982 static int
22983shortpath_for_invalid_fname(fname, bufp, fnamelen)
22984 char_u **fname;
22985 char_u **bufp;
22986 int *fnamelen;
22987{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022988 char_u *short_fname, *save_fname, *pbuf_unused;
22989 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022990 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022991 int old_len, len;
22992 int new_len, sfx_len;
22993 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022994
22995 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022996 old_len = *fnamelen;
22997 save_fname = vim_strnsave(*fname, old_len);
22998 pbuf_unused = NULL;
22999 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023000
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023001 endp = save_fname + old_len - 1; /* Find the end of the copy */
23002 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023004 /*
23005 * Try shortening the supplied path till it succeeds by removing one
23006 * directory at a time from the tail of the path.
23007 */
23008 len = 0;
23009 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023011 /* go back one path-separator */
23012 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23013 --endp;
23014 if (endp <= save_fname)
23015 break; /* processed the complete path */
23016
23017 /*
23018 * Replace the path separator with a NUL and try to shorten the
23019 * resulting path.
23020 */
23021 ch = *endp;
23022 *endp = 0;
23023 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023024 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023025 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23026 {
23027 retval = FAIL;
23028 goto theend;
23029 }
23030 *endp = ch; /* preserve the string */
23031
23032 if (len > 0)
23033 break; /* successfully shortened the path */
23034
23035 /* failed to shorten the path. Skip the path separator */
23036 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023037 }
23038
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023039 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023040 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023041 /*
23042 * Succeeded in shortening the path. Now concatenate the shortened
23043 * path with the remaining path at the tail.
23044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023045
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023046 /* Compute the length of the new path. */
23047 sfx_len = (int)(save_endp - endp) + 1;
23048 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023049
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023050 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023051 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023052 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023053 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023054 /* There is not enough space in the currently allocated string,
23055 * copy it to a buffer big enough. */
23056 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023058 {
23059 retval = FAIL;
23060 goto theend;
23061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023062 }
23063 else
23064 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023065 /* Transfer short_fname to the main buffer (it's big enough),
23066 * unless get_short_pathname() did its work in-place. */
23067 *fname = *bufp = save_fname;
23068 if (short_fname != save_fname)
23069 vim_strncpy(save_fname, short_fname, len);
23070 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023071 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023072
23073 /* concat the not-shortened part of the path */
23074 vim_strncpy(*fname + len, endp, sfx_len);
23075 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023076 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023077
23078theend:
23079 vim_free(pbuf_unused);
23080 vim_free(save_fname);
23081
23082 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023083}
23084
23085/*
23086 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023087 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023088 */
23089 static int
23090shortpath_for_partial(fnamep, bufp, fnamelen)
23091 char_u **fnamep;
23092 char_u **bufp;
23093 int *fnamelen;
23094{
23095 int sepcount, len, tflen;
23096 char_u *p;
23097 char_u *pbuf, *tfname;
23098 int hasTilde;
23099
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023100 /* Count up the path separators from the RHS.. so we know which part
23101 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023102 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023103 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023104 if (vim_ispathsep(*p))
23105 ++sepcount;
23106
23107 /* Need full path first (use expand_env() to remove a "~/") */
23108 hasTilde = (**fnamep == '~');
23109 if (hasTilde)
23110 pbuf = tfname = expand_env_save(*fnamep);
23111 else
23112 pbuf = tfname = FullName_save(*fnamep, FALSE);
23113
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023114 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023116 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23117 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118
23119 if (len == 0)
23120 {
23121 /* Don't have a valid filename, so shorten the rest of the
23122 * path if we can. This CAN give us invalid 8.3 filenames, but
23123 * there's not a lot of point in guessing what it might be.
23124 */
23125 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023126 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23127 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128 }
23129
23130 /* Count the paths backward to find the beginning of the desired string. */
23131 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023132 {
23133#ifdef FEAT_MBYTE
23134 if (has_mbyte)
23135 p -= mb_head_off(tfname, p);
23136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023137 if (vim_ispathsep(*p))
23138 {
23139 if (sepcount == 0 || (hasTilde && sepcount == 1))
23140 break;
23141 else
23142 sepcount --;
23143 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 if (hasTilde)
23146 {
23147 --p;
23148 if (p >= tfname)
23149 *p = '~';
23150 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023151 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023152 }
23153 else
23154 ++p;
23155
23156 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23157 vim_free(*bufp);
23158 *fnamelen = (int)STRLEN(p);
23159 *bufp = pbuf;
23160 *fnamep = p;
23161
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023162 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023163}
23164#endif /* WIN3264 */
23165
23166/*
23167 * Adjust a filename, according to a string of modifiers.
23168 * *fnamep must be NUL terminated when called. When returning, the length is
23169 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023170 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 * When there is an error, *fnamep is set to NULL.
23172 */
23173 int
23174modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23175 char_u *src; /* string with modifiers */
23176 int *usedlen; /* characters after src that are used */
23177 char_u **fnamep; /* file name so far */
23178 char_u **bufp; /* buffer for allocated file name or NULL */
23179 int *fnamelen; /* length of fnamep */
23180{
23181 int valid = 0;
23182 char_u *tail;
23183 char_u *s, *p, *pbuf;
23184 char_u dirname[MAXPATHL];
23185 int c;
23186 int has_fullname = 0;
23187#ifdef WIN3264
23188 int has_shortname = 0;
23189#endif
23190
23191repeat:
23192 /* ":p" - full path/file_name */
23193 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23194 {
23195 has_fullname = 1;
23196
23197 valid |= VALID_PATH;
23198 *usedlen += 2;
23199
23200 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23201 if ((*fnamep)[0] == '~'
23202#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23203 && ((*fnamep)[1] == '/'
23204# ifdef BACKSLASH_IN_FILENAME
23205 || (*fnamep)[1] == '\\'
23206# endif
23207 || (*fnamep)[1] == NUL)
23208
23209#endif
23210 )
23211 {
23212 *fnamep = expand_env_save(*fnamep);
23213 vim_free(*bufp); /* free any allocated file name */
23214 *bufp = *fnamep;
23215 if (*fnamep == NULL)
23216 return -1;
23217 }
23218
23219 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023220 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023221 {
23222 if (vim_ispathsep(*p)
23223 && p[1] == '.'
23224 && (p[2] == NUL
23225 || vim_ispathsep(p[2])
23226 || (p[2] == '.'
23227 && (p[3] == NUL || vim_ispathsep(p[3])))))
23228 break;
23229 }
23230
23231 /* FullName_save() is slow, don't use it when not needed. */
23232 if (*p != NUL || !vim_isAbsName(*fnamep))
23233 {
23234 *fnamep = FullName_save(*fnamep, *p != NUL);
23235 vim_free(*bufp); /* free any allocated file name */
23236 *bufp = *fnamep;
23237 if (*fnamep == NULL)
23238 return -1;
23239 }
23240
23241 /* Append a path separator to a directory. */
23242 if (mch_isdir(*fnamep))
23243 {
23244 /* Make room for one or two extra characters. */
23245 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23246 vim_free(*bufp); /* free any allocated file name */
23247 *bufp = *fnamep;
23248 if (*fnamep == NULL)
23249 return -1;
23250 add_pathsep(*fnamep);
23251 }
23252 }
23253
23254 /* ":." - path relative to the current directory */
23255 /* ":~" - path relative to the home directory */
23256 /* ":8" - shortname path - postponed till after */
23257 while (src[*usedlen] == ':'
23258 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23259 {
23260 *usedlen += 2;
23261 if (c == '8')
23262 {
23263#ifdef WIN3264
23264 has_shortname = 1; /* Postpone this. */
23265#endif
23266 continue;
23267 }
23268 pbuf = NULL;
23269 /* Need full path first (use expand_env() to remove a "~/") */
23270 if (!has_fullname)
23271 {
23272 if (c == '.' && **fnamep == '~')
23273 p = pbuf = expand_env_save(*fnamep);
23274 else
23275 p = pbuf = FullName_save(*fnamep, FALSE);
23276 }
23277 else
23278 p = *fnamep;
23279
23280 has_fullname = 0;
23281
23282 if (p != NULL)
23283 {
23284 if (c == '.')
23285 {
23286 mch_dirname(dirname, MAXPATHL);
23287 s = shorten_fname(p, dirname);
23288 if (s != NULL)
23289 {
23290 *fnamep = s;
23291 if (pbuf != NULL)
23292 {
23293 vim_free(*bufp); /* free any allocated file name */
23294 *bufp = pbuf;
23295 pbuf = NULL;
23296 }
23297 }
23298 }
23299 else
23300 {
23301 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23302 /* Only replace it when it starts with '~' */
23303 if (*dirname == '~')
23304 {
23305 s = vim_strsave(dirname);
23306 if (s != NULL)
23307 {
23308 *fnamep = s;
23309 vim_free(*bufp);
23310 *bufp = s;
23311 }
23312 }
23313 }
23314 vim_free(pbuf);
23315 }
23316 }
23317
23318 tail = gettail(*fnamep);
23319 *fnamelen = (int)STRLEN(*fnamep);
23320
23321 /* ":h" - head, remove "/file_name", can be repeated */
23322 /* Don't remove the first "/" or "c:\" */
23323 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23324 {
23325 valid |= VALID_HEAD;
23326 *usedlen += 2;
23327 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023328 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023329 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330 *fnamelen = (int)(tail - *fnamep);
23331#ifdef VMS
23332 if (*fnamelen > 0)
23333 *fnamelen += 1; /* the path separator is part of the path */
23334#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023335 if (*fnamelen == 0)
23336 {
23337 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23338 p = vim_strsave((char_u *)".");
23339 if (p == NULL)
23340 return -1;
23341 vim_free(*bufp);
23342 *bufp = *fnamep = tail = p;
23343 *fnamelen = 1;
23344 }
23345 else
23346 {
23347 while (tail > s && !after_pathsep(s, tail))
23348 mb_ptr_back(*fnamep, tail);
23349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023350 }
23351
23352 /* ":8" - shortname */
23353 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23354 {
23355 *usedlen += 2;
23356#ifdef WIN3264
23357 has_shortname = 1;
23358#endif
23359 }
23360
23361#ifdef WIN3264
23362 /* Check shortname after we have done 'heads' and before we do 'tails'
23363 */
23364 if (has_shortname)
23365 {
23366 pbuf = NULL;
23367 /* Copy the string if it is shortened by :h */
23368 if (*fnamelen < (int)STRLEN(*fnamep))
23369 {
23370 p = vim_strnsave(*fnamep, *fnamelen);
23371 if (p == 0)
23372 return -1;
23373 vim_free(*bufp);
23374 *bufp = *fnamep = p;
23375 }
23376
23377 /* Split into two implementations - makes it easier. First is where
23378 * there isn't a full name already, second is where there is.
23379 */
23380 if (!has_fullname && !vim_isAbsName(*fnamep))
23381 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023382 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023383 return -1;
23384 }
23385 else
23386 {
23387 int l;
23388
23389 /* Simple case, already have the full-name
23390 * Nearly always shorter, so try first time. */
23391 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023392 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023393 return -1;
23394
23395 if (l == 0)
23396 {
23397 /* Couldn't find the filename.. search the paths.
23398 */
23399 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023400 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023401 return -1;
23402 }
23403 *fnamelen = l;
23404 }
23405 }
23406#endif /* WIN3264 */
23407
23408 /* ":t" - tail, just the basename */
23409 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23410 {
23411 *usedlen += 2;
23412 *fnamelen -= (int)(tail - *fnamep);
23413 *fnamep = tail;
23414 }
23415
23416 /* ":e" - extension, can be repeated */
23417 /* ":r" - root, without extension, can be repeated */
23418 while (src[*usedlen] == ':'
23419 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23420 {
23421 /* find a '.' in the tail:
23422 * - for second :e: before the current fname
23423 * - otherwise: The last '.'
23424 */
23425 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23426 s = *fnamep - 2;
23427 else
23428 s = *fnamep + *fnamelen - 1;
23429 for ( ; s > tail; --s)
23430 if (s[0] == '.')
23431 break;
23432 if (src[*usedlen + 1] == 'e') /* :e */
23433 {
23434 if (s > tail)
23435 {
23436 *fnamelen += (int)(*fnamep - (s + 1));
23437 *fnamep = s + 1;
23438#ifdef VMS
23439 /* cut version from the extension */
23440 s = *fnamep + *fnamelen - 1;
23441 for ( ; s > *fnamep; --s)
23442 if (s[0] == ';')
23443 break;
23444 if (s > *fnamep)
23445 *fnamelen = s - *fnamep;
23446#endif
23447 }
23448 else if (*fnamep <= tail)
23449 *fnamelen = 0;
23450 }
23451 else /* :r */
23452 {
23453 if (s > tail) /* remove one extension */
23454 *fnamelen = (int)(s - *fnamep);
23455 }
23456 *usedlen += 2;
23457 }
23458
23459 /* ":s?pat?foo?" - substitute */
23460 /* ":gs?pat?foo?" - global substitute */
23461 if (src[*usedlen] == ':'
23462 && (src[*usedlen + 1] == 's'
23463 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23464 {
23465 char_u *str;
23466 char_u *pat;
23467 char_u *sub;
23468 int sep;
23469 char_u *flags;
23470 int didit = FALSE;
23471
23472 flags = (char_u *)"";
23473 s = src + *usedlen + 2;
23474 if (src[*usedlen + 1] == 'g')
23475 {
23476 flags = (char_u *)"g";
23477 ++s;
23478 }
23479
23480 sep = *s++;
23481 if (sep)
23482 {
23483 /* find end of pattern */
23484 p = vim_strchr(s, sep);
23485 if (p != NULL)
23486 {
23487 pat = vim_strnsave(s, (int)(p - s));
23488 if (pat != NULL)
23489 {
23490 s = p + 1;
23491 /* find end of substitution */
23492 p = vim_strchr(s, sep);
23493 if (p != NULL)
23494 {
23495 sub = vim_strnsave(s, (int)(p - s));
23496 str = vim_strnsave(*fnamep, *fnamelen);
23497 if (sub != NULL && str != NULL)
23498 {
23499 *usedlen = (int)(p + 1 - src);
23500 s = do_string_sub(str, pat, sub, flags);
23501 if (s != NULL)
23502 {
23503 *fnamep = s;
23504 *fnamelen = (int)STRLEN(s);
23505 vim_free(*bufp);
23506 *bufp = s;
23507 didit = TRUE;
23508 }
23509 }
23510 vim_free(sub);
23511 vim_free(str);
23512 }
23513 vim_free(pat);
23514 }
23515 }
23516 /* after using ":s", repeat all the modifiers */
23517 if (didit)
23518 goto repeat;
23519 }
23520 }
23521
23522 return valid;
23523}
23524
23525/*
23526 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23527 * "flags" can be "g" to do a global substitute.
23528 * Returns an allocated string, NULL for error.
23529 */
23530 char_u *
23531do_string_sub(str, pat, sub, flags)
23532 char_u *str;
23533 char_u *pat;
23534 char_u *sub;
23535 char_u *flags;
23536{
23537 int sublen;
23538 regmatch_T regmatch;
23539 int i;
23540 int do_all;
23541 char_u *tail;
23542 garray_T ga;
23543 char_u *ret;
23544 char_u *save_cpo;
23545
23546 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23547 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023548 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023549
23550 ga_init2(&ga, 1, 200);
23551
23552 do_all = (flags[0] == 'g');
23553
23554 regmatch.rm_ic = p_ic;
23555 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23556 if (regmatch.regprog != NULL)
23557 {
23558 tail = str;
23559 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23560 {
23561 /*
23562 * Get some space for a temporary buffer to do the substitution
23563 * into. It will contain:
23564 * - The text up to where the match is.
23565 * - The substituted text.
23566 * - The text after the match.
23567 */
23568 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23569 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23570 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23571 {
23572 ga_clear(&ga);
23573 break;
23574 }
23575
23576 /* copy the text up to where the match is */
23577 i = (int)(regmatch.startp[0] - tail);
23578 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23579 /* add the substituted text */
23580 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23581 + ga.ga_len + i, TRUE, TRUE, FALSE);
23582 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583 /* avoid getting stuck on a match with an empty string */
23584 if (tail == regmatch.endp[0])
23585 {
23586 if (*tail == NUL)
23587 break;
23588 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23589 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023590 }
23591 else
23592 {
23593 tail = regmatch.endp[0];
23594 if (*tail == NUL)
23595 break;
23596 }
23597 if (!do_all)
23598 break;
23599 }
23600
23601 if (ga.ga_data != NULL)
23602 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23603
23604 vim_free(regmatch.regprog);
23605 }
23606
23607 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23608 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023609 if (p_cpo == empty_option)
23610 p_cpo = save_cpo;
23611 else
23612 /* Darn, evaluating {sub} expression changed the value. */
23613 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023614
23615 return ret;
23616}
23617
23618#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */