blob: af4ba893ff54d589f281ed5168b7c65d865ea95b [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 Moolenaar2e6aff32005-01-31 19:25:36 +0000792static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000793static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000794static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
795static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
796static int eval_fname_script __ARGS((char_u *p));
797static int eval_fname_sid __ARGS((char_u *p));
798static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000799static ufunc_T *find_func __ARGS((char_u *name));
800static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000801static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000802#ifdef FEAT_PROFILE
803static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000804static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
805static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
806static int
807# ifdef __BORLANDC__
808 _RTLENTRYF
809# endif
810 prof_total_cmp __ARGS((const void *s1, const void *s2));
811static int
812# ifdef __BORLANDC__
813 _RTLENTRYF
814# endif
815 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000816#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000817static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000818static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000819static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000820static void func_free __ARGS((ufunc_T *fp));
821static void func_unref __ARGS((char_u *name));
822static void func_ref __ARGS((char_u *name));
823static 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 +0000824static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
825static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000826static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000827static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
828static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000829static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000830static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000831static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000832
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200833
834#ifdef EBCDIC
835static int compare_func_name __ARGS((const void *s1, const void *s2));
836static void sortFunctions __ARGS(());
837#endif
838
839
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000840/* Character used as separated in autoload function/variable names. */
841#define AUTOLOAD_CHAR '#'
842
Bram Moolenaar33570922005-01-25 22:26:29 +0000843/*
844 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000845 */
846 void
847eval_init()
848{
Bram Moolenaar33570922005-01-25 22:26:29 +0000849 int i;
850 struct vimvar *p;
851
852 init_var_dict(&globvardict, &globvars_var);
853 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000854 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000855 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000856
857 for (i = 0; i < VV_LEN; ++i)
858 {
859 p = &vimvars[i];
860 STRCPY(p->vv_di.di_key, p->vv_name);
861 if (p->vv_flags & VV_RO)
862 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
863 else if (p->vv_flags & VV_RO_SBX)
864 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
865 else
866 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000867
868 /* add to v: scope dict, unless the value is not always available */
869 if (p->vv_type != VAR_UNKNOWN)
870 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000871 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000872 /* add to compat scope dict */
873 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000874 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000875 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200876
877#ifdef EBCDIC
878 /*
879 * Sort the function table, to enable binary sort.
880 */
881 sortFunctions();
882#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000883}
884
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000885#if defined(EXITFREE) || defined(PROTO)
886 void
887eval_clear()
888{
889 int i;
890 struct vimvar *p;
891
892 for (i = 0; i < VV_LEN; ++i)
893 {
894 p = &vimvars[i];
895 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000896 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000897 vim_free(p->vv_str);
898 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000899 }
900 else if (p->vv_di.di_tv.v_type == VAR_LIST)
901 {
902 list_unref(p->vv_list);
903 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000904 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000905 }
906 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000907 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908 hash_clear(&compat_hashtab);
909
Bram Moolenaard9fba312005-06-26 22:34:35 +0000910 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911
912 /* global variables */
913 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000914
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000915 /* autoloaded script names */
916 ga_clear_strings(&ga_loaded);
917
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200918 /* script-local variables */
919 for (i = 1; i <= ga_scripts.ga_len; ++i)
920 {
921 vars_clear(&SCRIPT_VARS(i));
922 vim_free(SCRIPT_SV(i));
923 }
924 ga_clear(&ga_scripts);
925
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000926 /* unreferenced lists and dicts */
927 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000928
929 /* functions */
930 free_all_functions();
931 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000932}
933#endif
934
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 * Return the name of the executed function.
937 */
938 char_u *
939func_name(cookie)
940 void *cookie;
941{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000942 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943}
944
945/*
946 * Return the address holding the next breakpoint line for a funccall cookie.
947 */
948 linenr_T *
949func_breakpoint(cookie)
950 void *cookie;
951{
Bram Moolenaar33570922005-01-25 22:26:29 +0000952 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953}
954
955/*
956 * Return the address holding the debug tick for a funccall cookie.
957 */
958 int *
959func_dbg_tick(cookie)
960 void *cookie;
961{
Bram Moolenaar33570922005-01-25 22:26:29 +0000962 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the nesting level for a funccall cookie.
967 */
968 int
969func_level(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000976funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000978/* pointer to list of previously used funccal, still around because some
979 * item in it is still being used. */
980funccall_T *previous_funccal = NULL;
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982/*
983 * Return TRUE when a function was ended by a ":return" command.
984 */
985 int
986current_func_returned()
987{
988 return current_funccal->returned;
989}
990
991
992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 * Set an internal variable to a string value. Creates the variable if it does
994 * not already exist.
995 */
996 void
997set_internal_string_var(name, value)
998 char_u *name;
999 char_u *value;
1000{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001001 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001002 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
1004 val = vim_strsave(value);
1005 if (val != NULL)
1006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001007 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001010 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001011 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
1013 }
1014}
1015
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001017static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001018static char_u *redir_endp = NULL;
1019static char_u *redir_varname = NULL;
1020
1021/*
1022 * Start recording command output to a variable
1023 * Returns OK if successfully completed the setup. FAIL otherwise.
1024 */
1025 int
1026var_redir_start(name, append)
1027 char_u *name;
1028 int append; /* append to an existing variable */
1029{
1030 int save_emsg;
1031 int err;
1032 typval_T tv;
1033
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001034 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001035 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036 {
1037 EMSG(_(e_invarg));
1038 return FAIL;
1039 }
1040
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001041 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001042 redir_varname = vim_strsave(name);
1043 if (redir_varname == NULL)
1044 return FAIL;
1045
1046 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1047 if (redir_lval == NULL)
1048 {
1049 var_redir_stop();
1050 return FAIL;
1051 }
1052
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001053 /* The output is stored in growarray "redir_ga" until redirection ends. */
1054 ga_init2(&redir_ga, (int)sizeof(char), 500);
1055
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001057 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1058 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1060 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001061 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 if (redir_endp != NULL && *redir_endp != NUL)
1063 /* Trailing characters are present after the variable name */
1064 EMSG(_(e_trailing));
1065 else
1066 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001067 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001068 var_redir_stop();
1069 return FAIL;
1070 }
1071
1072 /* check if we can write to the variable: set it to or append an empty
1073 * string */
1074 save_emsg = did_emsg;
1075 did_emsg = FALSE;
1076 tv.v_type = VAR_STRING;
1077 tv.vval.v_string = (char_u *)"";
1078 if (append)
1079 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1080 else
1081 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001082 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001084 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085 if (err)
1086 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001087 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 var_redir_stop();
1089 return FAIL;
1090 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091
1092 return OK;
1093}
1094
1095/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001096 * Append "value[value_len]" to the variable set by var_redir_start().
1097 * The actual appending is postponed until redirection ends, because the value
1098 * appended may in fact be the string we write to, changing it may cause freed
1099 * memory to be used:
1100 * :redir => foo
1101 * :let foo
1102 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 */
1104 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001105var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001107 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001109 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110
1111 if (redir_lval == NULL)
1112 return;
1113
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001115 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001120 {
1121 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001123 }
1124 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126}
1127
1128/*
1129 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001130 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 */
1132 void
1133var_redir_stop()
1134{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001135 typval_T tv;
1136
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 if (redir_lval != NULL)
1138 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001139 /* If there was no error: assign the text to the variable. */
1140 if (redir_endp != NULL)
1141 {
1142 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1143 tv.v_type = VAR_STRING;
1144 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001145 /* Call get_lval() again, if it's inside a Dict or List it may
1146 * have changed. */
1147 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1148 FALSE, FALSE, FALSE, FNE_CHECK_START);
1149 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1150 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1151 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001153
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001154 /* free the collected output */
1155 vim_free(redir_ga.ga_data);
1156 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 vim_free(redir_lval);
1159 redir_lval = NULL;
1160 }
1161 vim_free(redir_varname);
1162 redir_varname = NULL;
1163}
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165# if defined(FEAT_MBYTE) || defined(PROTO)
1166 int
1167eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1168 char_u *enc_from;
1169 char_u *enc_to;
1170 char_u *fname_from;
1171 char_u *fname_to;
1172{
1173 int err = FALSE;
1174
1175 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1176 set_vim_var_string(VV_CC_TO, enc_to, -1);
1177 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1178 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1179 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1180 err = TRUE;
1181 set_vim_var_string(VV_CC_FROM, NULL, -1);
1182 set_vim_var_string(VV_CC_TO, NULL, -1);
1183 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1184 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1185
1186 if (err)
1187 return FAIL;
1188 return OK;
1189}
1190# endif
1191
1192# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1193 int
1194eval_printexpr(fname, args)
1195 char_u *fname;
1196 char_u *args;
1197{
1198 int err = FALSE;
1199
1200 set_vim_var_string(VV_FNAME_IN, fname, -1);
1201 set_vim_var_string(VV_CMDARG, args, -1);
1202 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1203 err = TRUE;
1204 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1205 set_vim_var_string(VV_CMDARG, NULL, -1);
1206
1207 if (err)
1208 {
1209 mch_remove(fname);
1210 return FAIL;
1211 }
1212 return OK;
1213}
1214# endif
1215
1216# if defined(FEAT_DIFF) || defined(PROTO)
1217 void
1218eval_diff(origfile, newfile, outfile)
1219 char_u *origfile;
1220 char_u *newfile;
1221 char_u *outfile;
1222{
1223 int err = FALSE;
1224
1225 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1226 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1227 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1228 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1229 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1230 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1231 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1232}
1233
1234 void
1235eval_patch(origfile, difffile, outfile)
1236 char_u *origfile;
1237 char_u *difffile;
1238 char_u *outfile;
1239{
1240 int err;
1241
1242 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1243 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1244 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1245 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1246 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1247 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1248 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1249}
1250# endif
1251
1252/*
1253 * Top level evaluation function, returning a boolean.
1254 * Sets "error" to TRUE if there was an error.
1255 * Return TRUE or FALSE.
1256 */
1257 int
1258eval_to_bool(arg, error, nextcmd, skip)
1259 char_u *arg;
1260 int *error;
1261 char_u **nextcmd;
1262 int skip; /* only parse, don't execute */
1263{
Bram Moolenaar33570922005-01-25 22:26:29 +00001264 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 int retval = FALSE;
1266
1267 if (skip)
1268 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001269 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 else
1272 {
1273 *error = FALSE;
1274 if (!skip)
1275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001276 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001277 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279 }
1280 if (skip)
1281 --emsg_skip;
1282
1283 return retval;
1284}
1285
1286/*
1287 * Top level evaluation function, returning a string. If "skip" is TRUE,
1288 * only parsing to "nextcmd" is done, without reporting errors. Return
1289 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1290 */
1291 char_u *
1292eval_to_string_skip(arg, nextcmd, skip)
1293 char_u *arg;
1294 char_u **nextcmd;
1295 int skip; /* only parse, don't execute */
1296{
Bram Moolenaar33570922005-01-25 22:26:29 +00001297 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 char_u *retval;
1299
1300 if (skip)
1301 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001302 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 retval = NULL;
1304 else
1305 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 retval = vim_strsave(get_tv_string(&tv));
1307 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 }
1309 if (skip)
1310 --emsg_skip;
1311
1312 return retval;
1313}
1314
1315/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001316 * Skip over an expression at "*pp".
1317 * Return FAIL for an error, OK otherwise.
1318 */
1319 int
1320skip_expr(pp)
1321 char_u **pp;
1322{
Bram Moolenaar33570922005-01-25 22:26:29 +00001323 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001324
1325 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001327}
1328
1329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001331 * When "convert" is TRUE convert a List into a sequence of lines and convert
1332 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 * Return pointer to allocated memory, or NULL for failure.
1334 */
1335 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001336eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 char_u *arg;
1338 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001339 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340{
Bram Moolenaar33570922005-01-25 22:26:29 +00001341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001343 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001344#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001345 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001348 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 retval = NULL;
1350 else
1351 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001353 {
1354 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001355 if (tv.vval.v_list != NULL)
1356 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001357 ga_append(&ga, NUL);
1358 retval = (char_u *)ga.ga_data;
1359 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001360#ifdef FEAT_FLOAT
1361 else if (convert && tv.v_type == VAR_FLOAT)
1362 {
1363 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1364 retval = vim_strsave(numbuf);
1365 }
1366#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001367 else
1368 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001369 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 }
1371
1372 return retval;
1373}
1374
1375/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001376 * Call eval_to_string() without using current local variables and using
1377 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 */
1379 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001380eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 char_u *arg;
1382 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001383 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384{
1385 char_u *retval;
1386 void *save_funccalp;
1387
1388 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001389 if (use_sandbox)
1390 ++sandbox;
1391 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001392 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393 if (use_sandbox)
1394 --sandbox;
1395 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 restore_funccal(save_funccalp);
1397 return retval;
1398}
1399
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400/*
1401 * Top level evaluation function, returning a number.
1402 * Evaluates "expr" silently.
1403 * Returns -1 for an error.
1404 */
1405 int
1406eval_to_number(expr)
1407 char_u *expr;
1408{
Bram Moolenaar33570922005-01-25 22:26:29 +00001409 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001411 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412
1413 ++emsg_off;
1414
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001415 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 retval = -1;
1417 else
1418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001419 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001420 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 }
1422 --emsg_off;
1423
1424 return retval;
1425}
1426
Bram Moolenaara40058a2005-07-11 22:42:07 +00001427/*
1428 * Prepare v: variable "idx" to be used.
1429 * Save the current typeval in "save_tv".
1430 * When not used yet add the variable to the v: hashtable.
1431 */
1432 static void
1433prepare_vimvar(idx, save_tv)
1434 int idx;
1435 typval_T *save_tv;
1436{
1437 *save_tv = vimvars[idx].vv_tv;
1438 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1439 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1440}
1441
1442/*
1443 * Restore v: variable "idx" to typeval "save_tv".
1444 * When no longer defined, remove the variable from the v: hashtable.
1445 */
1446 static void
1447restore_vimvar(idx, save_tv)
1448 int idx;
1449 typval_T *save_tv;
1450{
1451 hashitem_T *hi;
1452
Bram Moolenaara40058a2005-07-11 22:42:07 +00001453 vimvars[idx].vv_tv = *save_tv;
1454 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1455 {
1456 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1457 if (HASHITEM_EMPTY(hi))
1458 EMSG2(_(e_intern2), "restore_vimvar()");
1459 else
1460 hash_remove(&vimvarht, hi);
1461 }
1462}
1463
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001464#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001465/*
1466 * Evaluate an expression to a list with suggestions.
1467 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001468 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001469 */
1470 list_T *
1471eval_spell_expr(badword, expr)
1472 char_u *badword;
1473 char_u *expr;
1474{
1475 typval_T save_val;
1476 typval_T rettv;
1477 list_T *list = NULL;
1478 char_u *p = skipwhite(expr);
1479
1480 /* Set "v:val" to the bad word. */
1481 prepare_vimvar(VV_VAL, &save_val);
1482 vimvars[VV_VAL].vv_type = VAR_STRING;
1483 vimvars[VV_VAL].vv_str = badword;
1484 if (p_verbose == 0)
1485 ++emsg_off;
1486
1487 if (eval1(&p, &rettv, TRUE) == OK)
1488 {
1489 if (rettv.v_type != VAR_LIST)
1490 clear_tv(&rettv);
1491 else
1492 list = rettv.vval.v_list;
1493 }
1494
1495 if (p_verbose == 0)
1496 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001497 restore_vimvar(VV_VAL, &save_val);
1498
1499 return list;
1500}
1501
1502/*
1503 * "list" is supposed to contain two items: a word and a number. Return the
1504 * word in "pp" and the number as the return value.
1505 * Return -1 if anything isn't right.
1506 * Used to get the good word and score from the eval_spell_expr() result.
1507 */
1508 int
1509get_spellword(list, pp)
1510 list_T *list;
1511 char_u **pp;
1512{
1513 listitem_T *li;
1514
1515 li = list->lv_first;
1516 if (li == NULL)
1517 return -1;
1518 *pp = get_tv_string(&li->li_tv);
1519
1520 li = li->li_next;
1521 if (li == NULL)
1522 return -1;
1523 return get_tv_number(&li->li_tv);
1524}
1525#endif
1526
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001527/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001528 * Top level evaluation function.
1529 * Returns an allocated typval_T with the result.
1530 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001531 */
1532 typval_T *
1533eval_expr(arg, nextcmd)
1534 char_u *arg;
1535 char_u **nextcmd;
1536{
1537 typval_T *tv;
1538
1539 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001540 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001541 {
1542 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001543 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001544 }
1545
1546 return tv;
1547}
1548
1549
Bram Moolenaar4f688582007-07-24 12:34:30 +00001550#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1551 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001553 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001554 * Uses argv[argc] for the function arguments. Only Number and String
1555 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001556 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001558 static int
1559call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 char_u *func;
1561 int argc;
1562 char_u **argv;
1563 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565{
Bram Moolenaar33570922005-01-25 22:26:29 +00001566 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 long n;
1568 int len;
1569 int i;
1570 int doesrange;
1571 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001572 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001574 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001576 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
1578 for (i = 0; i < argc; i++)
1579 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001580 /* Pass a NULL or empty argument as an empty string */
1581 if (argv[i] == NULL || *argv[i] == NUL)
1582 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001583 argvars[i].v_type = VAR_STRING;
1584 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001585 continue;
1586 }
1587
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 /* Recognize a number argument, the others must be strings. */
1589 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1590 if (len != 0 && len == (int)STRLEN(argv[i]))
1591 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001592 argvars[i].v_type = VAR_NUMBER;
1593 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 }
1595 else
1596 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001597 argvars[i].v_type = VAR_STRING;
1598 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 }
1600 }
1601
1602 if (safe)
1603 {
1604 save_funccalp = save_funccal();
1605 ++sandbox;
1606 }
1607
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001608 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1609 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (safe)
1613 {
1614 --sandbox;
1615 restore_funccal(save_funccalp);
1616 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 vim_free(argvars);
1618
1619 if (ret == FAIL)
1620 clear_tv(rettv);
1621
1622 return ret;
1623}
1624
Bram Moolenaar4f688582007-07-24 12:34:30 +00001625# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001626/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001627 * Call vimL function "func" and return the result as a string.
1628 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001629 * Uses argv[argc] for the function arguments.
1630 */
1631 void *
1632call_func_retstr(func, argc, argv, safe)
1633 char_u *func;
1634 int argc;
1635 char_u **argv;
1636 int safe; /* use the sandbox */
1637{
1638 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001639 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001640
1641 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1642 return NULL;
1643
1644 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001645 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 return retval;
1647}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001648# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649
Bram Moolenaar4f688582007-07-24 12:34:30 +00001650# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001651/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001652 * Call vimL function "func" and return the result as a number.
1653 * Returns -1 when calling the function fails.
1654 * Uses argv[argc] for the function arguments.
1655 */
1656 long
1657call_func_retnr(func, argc, argv, safe)
1658 char_u *func;
1659 int argc;
1660 char_u **argv;
1661 int safe; /* use the sandbox */
1662{
1663 typval_T rettv;
1664 long retval;
1665
1666 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1667 return -1;
1668
1669 retval = get_tv_number_chk(&rettv, NULL);
1670 clear_tv(&rettv);
1671 return retval;
1672}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001673# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001674
1675/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001676 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001678 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 */
1680 void *
1681call_func_retlist(func, argc, argv, safe)
1682 char_u *func;
1683 int argc;
1684 char_u **argv;
1685 int safe; /* use the sandbox */
1686{
1687 typval_T rettv;
1688
1689 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1690 return NULL;
1691
1692 if (rettv.v_type != VAR_LIST)
1693 {
1694 clear_tv(&rettv);
1695 return NULL;
1696 }
1697
1698 return rettv.vval.v_list;
1699}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700#endif
1701
Bram Moolenaar4f688582007-07-24 12:34:30 +00001702
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703/*
1704 * Save the current function call pointer, and set it to NULL.
1705 * Used when executing autocommands and for ":source".
1706 */
1707 void *
1708save_funccal()
1709{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001710 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 current_funccal = NULL;
1713 return (void *)fc;
1714}
1715
1716 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001717restore_funccal(vfc)
1718 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001720 funccall_T *fc = (funccall_T *)vfc;
1721
1722 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723}
1724
Bram Moolenaar05159a02005-02-26 23:04:13 +00001725#if defined(FEAT_PROFILE) || defined(PROTO)
1726/*
1727 * Prepare profiling for entering a child or something else that is not
1728 * counted for the script/function itself.
1729 * Should always be called in pair with prof_child_exit().
1730 */
1731 void
1732prof_child_enter(tm)
1733 proftime_T *tm; /* place to store waittime */
1734{
1735 funccall_T *fc = current_funccal;
1736
1737 if (fc != NULL && fc->func->uf_profiling)
1738 profile_start(&fc->prof_child);
1739 script_prof_save(tm);
1740}
1741
1742/*
1743 * Take care of time spent in a child.
1744 * Should always be called after prof_child_enter().
1745 */
1746 void
1747prof_child_exit(tm)
1748 proftime_T *tm; /* where waittime was stored */
1749{
1750 funccall_T *fc = current_funccal;
1751
1752 if (fc != NULL && fc->func->uf_profiling)
1753 {
1754 profile_end(&fc->prof_child);
1755 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1756 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1757 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1758 }
1759 script_prof_restore(tm);
1760}
1761#endif
1762
1763
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764#ifdef FEAT_FOLDING
1765/*
1766 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1767 * it in "*cp". Doesn't give error messages.
1768 */
1769 int
1770eval_foldexpr(arg, cp)
1771 char_u *arg;
1772 int *cp;
1773{
Bram Moolenaar33570922005-01-25 22:26:29 +00001774 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 int retval;
1776 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001777 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1778 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
1780 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001781 if (use_sandbox)
1782 ++sandbox;
1783 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001785 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 retval = 0;
1787 else
1788 {
1789 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001790 if (tv.v_type == VAR_NUMBER)
1791 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001792 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 retval = 0;
1794 else
1795 {
1796 /* If the result is a string, check if there is a non-digit before
1797 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001798 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 if (!VIM_ISDIGIT(*s) && *s != '-')
1800 *cp = *s++;
1801 retval = atol((char *)s);
1802 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001803 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 }
1805 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001806 if (use_sandbox)
1807 --sandbox;
1808 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809
1810 return retval;
1811}
1812#endif
1813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001815 * ":let" list all variable values
1816 * ":let var1 var2" list variable values
1817 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001818 * ":let var += expr" assignment command.
1819 * ":let var -= expr" assignment command.
1820 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001821 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 */
1823 void
1824ex_let(eap)
1825 exarg_T *eap;
1826{
1827 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001828 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001829 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 int var_count = 0;
1832 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001833 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001834 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001835 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836
Bram Moolenaardb552d602006-03-23 22:59:57 +00001837 argend = skip_var_list(arg, &var_count, &semicolon);
1838 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001839 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001840 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1841 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001842 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001843 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001845 /*
1846 * ":let" without "=": list variables
1847 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001848 if (*arg == '[')
1849 EMSG(_(e_invarg));
1850 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001852 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001854 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001855 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001856 list_glob_vars(&first);
1857 list_buf_vars(&first);
1858 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001859#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001860 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001861#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001862 list_script_vars(&first);
1863 list_func_vars(&first);
1864 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 eap->nextcmd = check_nextcmd(arg);
1867 }
1868 else
1869 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001870 op[0] = '=';
1871 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001872 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001873 {
1874 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1875 op[0] = expr[-1]; /* +=, -= or .= */
1876 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001877 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 if (eap->skip)
1880 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (eap->skip)
1883 {
1884 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 --emsg_skip;
1887 }
1888 else if (i != FAIL)
1889 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001891 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 }
1894 }
1895}
1896
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897/*
1898 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1899 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 * When "nextchars" is not NULL it points to a string with characters that
1901 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1902 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 * Returns OK or FAIL;
1904 */
1905 static int
1906ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1907 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001908 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909 int copy; /* copy values from "tv", don't move */
1910 int semicolon; /* from skip_var_list() */
1911 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001912 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913{
1914 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001915 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001917 listitem_T *item;
1918 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919
1920 if (*arg != '[')
1921 {
1922 /*
1923 * ":let var = expr" or ":for var in list"
1924 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001925 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001926 return FAIL;
1927 return OK;
1928 }
1929
1930 /*
1931 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1932 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001933 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 {
1935 EMSG(_(e_listreq));
1936 return FAIL;
1937 }
1938
1939 i = list_len(l);
1940 if (semicolon == 0 && var_count < i)
1941 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001942 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943 return FAIL;
1944 }
1945 if (var_count - semicolon > i)
1946 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001947 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 return FAIL;
1949 }
1950
1951 item = l->lv_first;
1952 while (*arg != ']')
1953 {
1954 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001955 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 item = item->li_next;
1957 if (arg == NULL)
1958 return FAIL;
1959
1960 arg = skipwhite(arg);
1961 if (*arg == ';')
1962 {
1963 /* Put the rest of the list (may be empty) in the var after ';'.
1964 * Create a new list for this. */
1965 l = list_alloc();
1966 if (l == NULL)
1967 return FAIL;
1968 while (item != NULL)
1969 {
1970 list_append_tv(l, &item->li_tv);
1971 item = item->li_next;
1972 }
1973
1974 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001975 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001976 ltv.vval.v_list = l;
1977 l->lv_refcount = 1;
1978
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001979 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1980 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981 clear_tv(&ltv);
1982 if (arg == NULL)
1983 return FAIL;
1984 break;
1985 }
1986 else if (*arg != ',' && *arg != ']')
1987 {
1988 EMSG2(_(e_intern2), "ex_let_vars()");
1989 return FAIL;
1990 }
1991 }
1992
1993 return OK;
1994}
1995
1996/*
1997 * Skip over assignable variable "var" or list of variables "[var, var]".
1998 * Used for ":let varvar = expr" and ":for varvar in expr".
1999 * For "[var, var]" increment "*var_count" for each variable.
2000 * for "[var, var; var]" set "semicolon".
2001 * Return NULL for an error.
2002 */
2003 static char_u *
2004skip_var_list(arg, var_count, semicolon)
2005 char_u *arg;
2006 int *var_count;
2007 int *semicolon;
2008{
2009 char_u *p, *s;
2010
2011 if (*arg == '[')
2012 {
2013 /* "[var, var]": find the matching ']'. */
2014 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002015 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002016 {
2017 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2018 s = skip_var_one(p);
2019 if (s == p)
2020 {
2021 EMSG2(_(e_invarg2), p);
2022 return NULL;
2023 }
2024 ++*var_count;
2025
2026 p = skipwhite(s);
2027 if (*p == ']')
2028 break;
2029 else if (*p == ';')
2030 {
2031 if (*semicolon == 1)
2032 {
2033 EMSG(_("Double ; in list of variables"));
2034 return NULL;
2035 }
2036 *semicolon = 1;
2037 }
2038 else if (*p != ',')
2039 {
2040 EMSG2(_(e_invarg2), p);
2041 return NULL;
2042 }
2043 }
2044 return p + 1;
2045 }
2046 else
2047 return skip_var_one(arg);
2048}
2049
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002050/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002051 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002052 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002053 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002054 static char_u *
2055skip_var_one(arg)
2056 char_u *arg;
2057{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002058 if (*arg == '@' && arg[1] != NUL)
2059 return arg + 2;
2060 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2061 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002062}
2063
Bram Moolenaara7043832005-01-21 11:56:39 +00002064/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002065 * List variables for hashtab "ht" with prefix "prefix".
2066 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002067 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002068 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002069list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002070 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002071 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002072 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002073 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002074{
Bram Moolenaar33570922005-01-25 22:26:29 +00002075 hashitem_T *hi;
2076 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002077 int todo;
2078
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002079 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002080 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2081 {
2082 if (!HASHITEM_EMPTY(hi))
2083 {
2084 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002085 di = HI2DI(hi);
2086 if (empty || di->di_tv.v_type != VAR_STRING
2087 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002088 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 }
2090 }
2091}
2092
2093/*
2094 * List global variables.
2095 */
2096 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002097list_glob_vars(first)
2098 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002099{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002100 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002101}
2102
2103/*
2104 * List buffer variables.
2105 */
2106 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002107list_buf_vars(first)
2108 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002109{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002110 char_u numbuf[NUMBUFLEN];
2111
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002112 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2113 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002114
2115 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2117 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002118}
2119
2120/*
2121 * List window variables.
2122 */
2123 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002124list_win_vars(first)
2125 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002126{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002127 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2128 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002129}
2130
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002131#ifdef FEAT_WINDOWS
2132/*
2133 * List tab page variables.
2134 */
2135 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002136list_tab_vars(first)
2137 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002138{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2140 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002141}
2142#endif
2143
Bram Moolenaara7043832005-01-21 11:56:39 +00002144/*
2145 * List Vim variables.
2146 */
2147 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148list_vim_vars(first)
2149 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002150{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152}
2153
2154/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002155 * List script-local variables, if there is a script.
2156 */
2157 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158list_script_vars(first)
2159 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002160{
2161 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2163 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002164}
2165
2166/*
2167 * List function variables, if there is a function.
2168 */
2169 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170list_func_vars(first)
2171 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002172{
2173 if (current_funccal != NULL)
2174 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002175 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002176}
2177
2178/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002179 * List variables in "arg".
2180 */
2181 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002182list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183 exarg_T *eap;
2184 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002185 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186{
2187 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002188 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002190 char_u *name_start;
2191 char_u *arg_subsc;
2192 char_u *tofree;
2193 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194
2195 while (!ends_excmd(*arg) && !got_int)
2196 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002197 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002199 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002200 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2201 {
2202 emsg_severe = TRUE;
2203 EMSG(_(e_trailing));
2204 break;
2205 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002209 /* get_name_len() takes care of expanding curly braces */
2210 name_start = name = arg;
2211 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2212 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002214 /* This is mainly to keep test 49 working: when expanding
2215 * curly braces fails overrule the exception error message. */
2216 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 emsg_severe = TRUE;
2219 EMSG2(_(e_invarg2), arg);
2220 break;
2221 }
2222 error = TRUE;
2223 }
2224 else
2225 {
2226 if (tofree != NULL)
2227 name = tofree;
2228 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 else
2231 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 /* handle d.key, l[idx], f(expr) */
2233 arg_subsc = arg;
2234 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002235 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002237 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002238 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002241 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002242 case 'g': list_glob_vars(first); break;
2243 case 'b': list_buf_vars(first); break;
2244 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002245#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002246 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002247#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002248 case 'v': list_vim_vars(first); break;
2249 case 's': list_script_vars(first); break;
2250 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002251 default:
2252 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002253 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002254 }
2255 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 {
2257 char_u numbuf[NUMBUFLEN];
2258 char_u *tf;
2259 int c;
2260 char_u *s;
2261
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002262 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 c = *arg;
2264 *arg = NUL;
2265 list_one_var_a((char_u *)"",
2266 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002267 tv.v_type,
2268 s == NULL ? (char_u *)"" : s,
2269 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 *arg = c;
2271 vim_free(tf);
2272 }
2273 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002274 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 }
2276 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277
2278 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280
2281 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 }
2283
2284 return arg;
2285}
2286
2287/*
2288 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2289 * Returns a pointer to the char just after the var name.
2290 * Returns NULL if there is an error.
2291 */
2292 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002293ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002295 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002297 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002298 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299{
2300 int c1;
2301 char_u *name;
2302 char_u *p;
2303 char_u *arg_end = NULL;
2304 int len;
2305 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002306 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307
2308 /*
2309 * ":let $VAR = expr": Set environment variable.
2310 */
2311 if (*arg == '$')
2312 {
2313 /* Find the end of the name. */
2314 ++arg;
2315 name = arg;
2316 len = get_env_len(&arg);
2317 if (len == 0)
2318 EMSG2(_(e_invarg2), name - 1);
2319 else
2320 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002321 if (op != NULL && (*op == '+' || *op == '-'))
2322 EMSG2(_(e_letwrong), op);
2323 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002324 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002326 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 {
2328 c1 = name[len];
2329 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002330 p = get_tv_string_chk(tv);
2331 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002332 {
2333 int mustfree = FALSE;
2334 char_u *s = vim_getenv(name, &mustfree);
2335
2336 if (s != NULL)
2337 {
2338 p = tofree = concat_str(s, p);
2339 if (mustfree)
2340 vim_free(s);
2341 }
2342 }
2343 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002344 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002345 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002346 if (STRICMP(name, "HOME") == 0)
2347 init_homedir();
2348 else if (didset_vim && STRICMP(name, "VIM") == 0)
2349 didset_vim = FALSE;
2350 else if (didset_vimruntime
2351 && STRICMP(name, "VIMRUNTIME") == 0)
2352 didset_vimruntime = FALSE;
2353 arg_end = arg;
2354 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002356 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 }
2358 }
2359 }
2360
2361 /*
2362 * ":let &option = expr": Set option value.
2363 * ":let &l:option = expr": Set local option value.
2364 * ":let &g:option = expr": Set global option value.
2365 */
2366 else if (*arg == '&')
2367 {
2368 /* Find the end of the name. */
2369 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002370 if (p == NULL || (endchars != NULL
2371 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372 EMSG(_(e_letunexp));
2373 else
2374 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002375 long n;
2376 int opt_type;
2377 long numval;
2378 char_u *stringval = NULL;
2379 char_u *s;
2380
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 c1 = *p;
2382 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383
2384 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002385 s = get_tv_string_chk(tv); /* != NULL if number or string */
2386 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387 {
2388 opt_type = get_option_value(arg, &numval,
2389 &stringval, opt_flags);
2390 if ((opt_type == 1 && *op == '.')
2391 || (opt_type == 0 && *op != '.'))
2392 EMSG2(_(e_letwrong), op);
2393 else
2394 {
2395 if (opt_type == 1) /* number */
2396 {
2397 if (*op == '+')
2398 n = numval + n;
2399 else
2400 n = numval - n;
2401 }
2402 else if (opt_type == 0 && stringval != NULL) /* string */
2403 {
2404 s = concat_str(stringval, s);
2405 vim_free(stringval);
2406 stringval = s;
2407 }
2408 }
2409 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002410 if (s != NULL)
2411 {
2412 set_option_value(arg, n, s, opt_flags);
2413 arg_end = p;
2414 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002415 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002416 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 }
2418 }
2419
2420 /*
2421 * ":let @r = expr": Set register contents.
2422 */
2423 else if (*arg == '@')
2424 {
2425 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002426 if (op != NULL && (*op == '+' || *op == '-'))
2427 EMSG2(_(e_letwrong), op);
2428 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002429 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002430 EMSG(_(e_letunexp));
2431 else
2432 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002433 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002434 char_u *s;
2435
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002436 p = get_tv_string_chk(tv);
2437 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002439 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 if (s != NULL)
2441 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002442 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 vim_free(s);
2444 }
2445 }
2446 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002447 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002449 arg_end = arg + 1;
2450 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002451 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 }
2453 }
2454
2455 /*
2456 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002458 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002459 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002461 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002463 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2467 EMSG(_(e_letunexp));
2468 else
2469 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 arg_end = p;
2472 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 }
2476
2477 else
2478 EMSG2(_(e_invarg2), arg);
2479
2480 return arg_end;
2481}
2482
2483/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002484 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2485 */
2486 static int
2487check_changedtick(arg)
2488 char_u *arg;
2489{
2490 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2491 {
2492 EMSG2(_(e_readonlyvar), arg);
2493 return TRUE;
2494 }
2495 return FALSE;
2496}
2497
2498/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 * Get an lval: variable, Dict item or List item that can be assigned a value
2500 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2501 * "name.key", "name.key[expr]" etc.
2502 * Indexing only works if "name" is an existing List or Dictionary.
2503 * "name" points to the start of the name.
2504 * If "rettv" is not NULL it points to the value to be assigned.
2505 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2506 * wrong; must end in space or cmd separator.
2507 *
2508 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002509 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 */
2512 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002513get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002515 typval_T *rettv;
2516 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 int unlet;
2518 int skip;
2519 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002520 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 char_u *expr_start, *expr_end;
2524 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002525 dictitem_T *v;
2526 typval_T var1;
2527 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002529 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002530 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002531 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002532 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002533
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536
2537 if (skip)
2538 {
2539 /* When skipping just find the end of the name. */
2540 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002541 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 }
2543
2544 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002545 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 if (expr_start != NULL)
2547 {
2548 /* Don't expand the name when we already know there is an error. */
2549 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2550 && *p != '[' && *p != '.')
2551 {
2552 EMSG(_(e_trailing));
2553 return NULL;
2554 }
2555
2556 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2557 if (lp->ll_exp_name == NULL)
2558 {
2559 /* Report an invalid expression in braces, unless the
2560 * expression evaluation has been cancelled due to an
2561 * aborting error, an interrupt, or an exception. */
2562 if (!aborting() && !quiet)
2563 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002564 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 EMSG2(_(e_invarg2), name);
2566 return NULL;
2567 }
2568 }
2569 lp->ll_name = lp->ll_exp_name;
2570 }
2571 else
2572 lp->ll_name = name;
2573
2574 /* Without [idx] or .key we are done. */
2575 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2576 return p;
2577
2578 cc = *p;
2579 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002580 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 if (v == NULL && !quiet)
2582 EMSG2(_(e_undefvar), lp->ll_name);
2583 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584 if (v == NULL)
2585 return NULL;
2586
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 /*
2588 * Loop until no more [idx] or .key is following.
2589 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002590 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002592 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2594 && !(lp->ll_tv->v_type == VAR_DICT
2595 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 if (!quiet)
2598 EMSG(_("E689: Can only index a List or Dictionary"));
2599 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002602 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 if (!quiet)
2604 EMSG(_("E708: [:] must come last"));
2605 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002607
Bram Moolenaar8c711452005-01-14 21:53:12 +00002608 len = -1;
2609 if (*p == '.')
2610 {
2611 key = p + 1;
2612 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2613 ;
2614 if (len == 0)
2615 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 if (!quiet)
2617 EMSG(_(e_emptykey));
2618 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 }
2620 p = key + len;
2621 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002622 else
2623 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002624 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002625 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002626 if (*p == ':')
2627 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628 else
2629 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002630 empty1 = FALSE;
2631 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002633 if (get_tv_string_chk(&var1) == NULL)
2634 {
2635 /* not a number or string */
2636 clear_tv(&var1);
2637 return NULL;
2638 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 }
2640
2641 /* Optionally get the second index [ :expr]. */
2642 if (*p == ':')
2643 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002647 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002648 if (!empty1)
2649 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002651 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (rettv != NULL && (rettv->v_type != VAR_LIST
2653 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (!quiet)
2656 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 if (!empty1)
2658 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 }
2661 p = skipwhite(p + 1);
2662 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 else
2665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2668 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 if (!empty1)
2670 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002673 if (get_tv_string_chk(&var2) == NULL)
2674 {
2675 /* not a number or string */
2676 if (!empty1)
2677 clear_tv(&var1);
2678 clear_tv(&var2);
2679 return NULL;
2680 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002683 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 if (!quiet)
2690 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 if (!empty1)
2692 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 }
2697
2698 /* Skip to past ']'. */
2699 ++p;
2700 }
2701
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 {
2704 if (len == -1)
2705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002707 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 if (*key == NUL)
2709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (!quiet)
2711 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 }
2715 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002716 lp->ll_list = NULL;
2717 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002718 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002721 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 if (len == -1)
2727 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 }
2730 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 if (len == -1)
2735 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 p = NULL;
2738 break;
2739 }
2740 if (len == -1)
2741 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 }
2744 else
2745 {
2746 /*
2747 * Get the number and item for the only or first index of the List.
2748 */
2749 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 else
2752 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002753 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 clear_tv(&var1);
2755 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002756 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 lp->ll_list = lp->ll_tv->vval.v_list;
2758 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2759 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002761 if (lp->ll_n1 < 0)
2762 {
2763 lp->ll_n1 = 0;
2764 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2765 }
2766 }
2767 if (lp->ll_li == NULL)
2768 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 }
2773
2774 /*
2775 * May need to find the item or absolute index for the second
2776 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 * When no index given: "lp->ll_empty2" is TRUE.
2778 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002782 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 }
2791
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2793 if (lp->ll_n1 < 0)
2794 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2795 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002797 }
2798
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002800 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002801 }
2802
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 return p;
2804}
2805
2806/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002807 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 */
2809 static void
2810clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002811 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812{
2813 vim_free(lp->ll_exp_name);
2814 vim_free(lp->ll_newkey);
2815}
2816
2817/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002818 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002820 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 */
2822 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002823set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002824 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002826 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002828 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829{
2830 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002831 listitem_T *ri;
2832 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833
2834 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002835 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002837 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 cc = *endp;
2839 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002840 if (op != NULL && *op != '=')
2841 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002842 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002843
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002844 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002845 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002846 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002847 {
2848 if (tv_op(&tv, rettv, op) == OK)
2849 set_var(lp->ll_name, &tv, FALSE);
2850 clear_tv(&tv);
2851 }
2852 }
2853 else
2854 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002856 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002858 else if (tv_check_lock(lp->ll_newkey == NULL
2859 ? lp->ll_tv->v_lock
2860 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2861 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 else if (lp->ll_range)
2863 {
2864 /*
2865 * Assign the List values to the list items.
2866 */
2867 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002868 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002869 if (op != NULL && *op != '=')
2870 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2871 else
2872 {
2873 clear_tv(&lp->ll_li->li_tv);
2874 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2875 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 ri = ri->li_next;
2877 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2878 break;
2879 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002880 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002882 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002883 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 ri = NULL;
2885 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002886 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002887 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 lp->ll_li = lp->ll_li->li_next;
2889 ++lp->ll_n1;
2890 }
2891 if (ri != NULL)
2892 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002893 else if (lp->ll_empty2
2894 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 : lp->ll_n1 != lp->ll_n2)
2896 EMSG(_("E711: List value has not enough items"));
2897 }
2898 else
2899 {
2900 /*
2901 * Assign to a List or Dictionary item.
2902 */
2903 if (lp->ll_newkey != NULL)
2904 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002905 if (op != NULL && *op != '=')
2906 {
2907 EMSG2(_(e_letwrong), op);
2908 return;
2909 }
2910
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002912 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 if (di == NULL)
2914 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002915 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2916 {
2917 vim_free(di);
2918 return;
2919 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002921 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002922 else if (op != NULL && *op != '=')
2923 {
2924 tv_op(lp->ll_tv, rettv, op);
2925 return;
2926 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002927 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002929
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 /*
2931 * Assign the value to the variable or list item.
2932 */
2933 if (copy)
2934 copy_tv(rettv, lp->ll_tv);
2935 else
2936 {
2937 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002938 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002940 }
2941 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002942}
2943
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002944/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002945 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2946 * Returns OK or FAIL.
2947 */
2948 static int
2949tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002950 typval_T *tv1;
2951 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002952 char_u *op;
2953{
2954 long n;
2955 char_u numbuf[NUMBUFLEN];
2956 char_u *s;
2957
2958 /* Can't do anything with a Funcref or a Dict on the right. */
2959 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2960 {
2961 switch (tv1->v_type)
2962 {
2963 case VAR_DICT:
2964 case VAR_FUNC:
2965 break;
2966
2967 case VAR_LIST:
2968 if (*op != '+' || tv2->v_type != VAR_LIST)
2969 break;
2970 /* List += List */
2971 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2972 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2973 return OK;
2974
2975 case VAR_NUMBER:
2976 case VAR_STRING:
2977 if (tv2->v_type == VAR_LIST)
2978 break;
2979 if (*op == '+' || *op == '-')
2980 {
2981 /* nr += nr or nr -= nr*/
2982 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002983#ifdef FEAT_FLOAT
2984 if (tv2->v_type == VAR_FLOAT)
2985 {
2986 float_T f = n;
2987
2988 if (*op == '+')
2989 f += tv2->vval.v_float;
2990 else
2991 f -= tv2->vval.v_float;
2992 clear_tv(tv1);
2993 tv1->v_type = VAR_FLOAT;
2994 tv1->vval.v_float = f;
2995 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002996 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002997#endif
2998 {
2999 if (*op == '+')
3000 n += get_tv_number(tv2);
3001 else
3002 n -= get_tv_number(tv2);
3003 clear_tv(tv1);
3004 tv1->v_type = VAR_NUMBER;
3005 tv1->vval.v_number = n;
3006 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003007 }
3008 else
3009 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003010 if (tv2->v_type == VAR_FLOAT)
3011 break;
3012
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003013 /* str .= str */
3014 s = get_tv_string(tv1);
3015 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3016 clear_tv(tv1);
3017 tv1->v_type = VAR_STRING;
3018 tv1->vval.v_string = s;
3019 }
3020 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003021
3022#ifdef FEAT_FLOAT
3023 case VAR_FLOAT:
3024 {
3025 float_T f;
3026
3027 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3028 && tv2->v_type != VAR_NUMBER
3029 && tv2->v_type != VAR_STRING))
3030 break;
3031 if (tv2->v_type == VAR_FLOAT)
3032 f = tv2->vval.v_float;
3033 else
3034 f = get_tv_number(tv2);
3035 if (*op == '+')
3036 tv1->vval.v_float += f;
3037 else
3038 tv1->vval.v_float -= f;
3039 }
3040 return OK;
3041#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003042 }
3043 }
3044
3045 EMSG2(_(e_letwrong), op);
3046 return FAIL;
3047}
3048
3049/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003050 * Add a watcher to a list.
3051 */
3052 static void
3053list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003054 list_T *l;
3055 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003056{
3057 lw->lw_next = l->lv_watch;
3058 l->lv_watch = lw;
3059}
3060
3061/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003062 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003063 * No warning when it isn't found...
3064 */
3065 static void
3066list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003067 list_T *l;
3068 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003069{
Bram Moolenaar33570922005-01-25 22:26:29 +00003070 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003071
3072 lwp = &l->lv_watch;
3073 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3074 {
3075 if (lw == lwrem)
3076 {
3077 *lwp = lw->lw_next;
3078 break;
3079 }
3080 lwp = &lw->lw_next;
3081 }
3082}
3083
3084/*
3085 * Just before removing an item from a list: advance watchers to the next
3086 * item.
3087 */
3088 static void
3089list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003090 list_T *l;
3091 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003092{
Bram Moolenaar33570922005-01-25 22:26:29 +00003093 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003094
3095 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3096 if (lw->lw_item == item)
3097 lw->lw_item = item->li_next;
3098}
3099
3100/*
3101 * Evaluate the expression used in a ":for var in expr" command.
3102 * "arg" points to "var".
3103 * Set "*errp" to TRUE for an error, FALSE otherwise;
3104 * Return a pointer that holds the info. Null when there is an error.
3105 */
3106 void *
3107eval_for_line(arg, errp, nextcmdp, skip)
3108 char_u *arg;
3109 int *errp;
3110 char_u **nextcmdp;
3111 int skip;
3112{
Bram Moolenaar33570922005-01-25 22:26:29 +00003113 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003114 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003115 typval_T tv;
3116 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117
3118 *errp = TRUE; /* default: there is an error */
3119
Bram Moolenaar33570922005-01-25 22:26:29 +00003120 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003121 if (fi == NULL)
3122 return NULL;
3123
3124 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3125 if (expr == NULL)
3126 return fi;
3127
3128 expr = skipwhite(expr);
3129 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3130 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003131 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003132 return fi;
3133 }
3134
3135 if (skip)
3136 ++emsg_skip;
3137 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3138 {
3139 *errp = FALSE;
3140 if (!skip)
3141 {
3142 l = tv.vval.v_list;
3143 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003144 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003145 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003146 clear_tv(&tv);
3147 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003148 else
3149 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003150 /* No need to increment the refcount, it's already set for the
3151 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003152 fi->fi_list = l;
3153 list_add_watch(l, &fi->fi_lw);
3154 fi->fi_lw.lw_item = l->lv_first;
3155 }
3156 }
3157 }
3158 if (skip)
3159 --emsg_skip;
3160
3161 return fi;
3162}
3163
3164/*
3165 * Use the first item in a ":for" list. Advance to the next.
3166 * Assign the values to the variable (list). "arg" points to the first one.
3167 * Return TRUE when a valid item was found, FALSE when at end of list or
3168 * something wrong.
3169 */
3170 int
3171next_for_item(fi_void, arg)
3172 void *fi_void;
3173 char_u *arg;
3174{
Bram Moolenaar33570922005-01-25 22:26:29 +00003175 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003177 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178
3179 item = fi->fi_lw.lw_item;
3180 if (item == NULL)
3181 result = FALSE;
3182 else
3183 {
3184 fi->fi_lw.lw_item = item->li_next;
3185 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3186 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3187 }
3188 return result;
3189}
3190
3191/*
3192 * Free the structure used to store info used by ":for".
3193 */
3194 void
3195free_for_info(fi_void)
3196 void *fi_void;
3197{
Bram Moolenaar33570922005-01-25 22:26:29 +00003198 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003200 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003201 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003203 list_unref(fi->fi_list);
3204 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205 vim_free(fi);
3206}
3207
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3209
3210 void
3211set_context_for_expression(xp, arg, cmdidx)
3212 expand_T *xp;
3213 char_u *arg;
3214 cmdidx_T cmdidx;
3215{
3216 int got_eq = FALSE;
3217 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220 if (cmdidx == CMD_let)
3221 {
3222 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003223 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003224 {
3225 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003226 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227 {
3228 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003229 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003230 if (vim_iswhite(*p))
3231 break;
3232 }
3233 return;
3234 }
3235 }
3236 else
3237 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3238 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 while ((xp->xp_pattern = vim_strpbrk(arg,
3240 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3241 {
3242 c = *xp->xp_pattern;
3243 if (c == '&')
3244 {
3245 c = xp->xp_pattern[1];
3246 if (c == '&')
3247 {
3248 ++xp->xp_pattern;
3249 xp->xp_context = cmdidx != CMD_let || got_eq
3250 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3251 }
3252 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003253 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003255 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3256 xp->xp_pattern += 2;
3257
3258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 }
3260 else if (c == '$')
3261 {
3262 /* environment variable */
3263 xp->xp_context = EXPAND_ENV_VARS;
3264 }
3265 else if (c == '=')
3266 {
3267 got_eq = TRUE;
3268 xp->xp_context = EXPAND_EXPRESSION;
3269 }
3270 else if (c == '<'
3271 && xp->xp_context == EXPAND_FUNCTIONS
3272 && vim_strchr(xp->xp_pattern, '(') == NULL)
3273 {
3274 /* Function name can start with "<SNR>" */
3275 break;
3276 }
3277 else if (cmdidx != CMD_let || got_eq)
3278 {
3279 if (c == '"') /* string */
3280 {
3281 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3282 if (c == '\\' && xp->xp_pattern[1] != NUL)
3283 ++xp->xp_pattern;
3284 xp->xp_context = EXPAND_NOTHING;
3285 }
3286 else if (c == '\'') /* literal string */
3287 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003288 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3290 /* skip */ ;
3291 xp->xp_context = EXPAND_NOTHING;
3292 }
3293 else if (c == '|')
3294 {
3295 if (xp->xp_pattern[1] == '|')
3296 {
3297 ++xp->xp_pattern;
3298 xp->xp_context = EXPAND_EXPRESSION;
3299 }
3300 else
3301 xp->xp_context = EXPAND_COMMANDS;
3302 }
3303 else
3304 xp->xp_context = EXPAND_EXPRESSION;
3305 }
3306 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307 /* Doesn't look like something valid, expand as an expression
3308 * anyway. */
3309 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 arg = xp->xp_pattern;
3311 if (*arg != NUL)
3312 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3313 /* skip */ ;
3314 }
3315 xp->xp_pattern = arg;
3316}
3317
3318#endif /* FEAT_CMDL_COMPL */
3319
3320/*
3321 * ":1,25call func(arg1, arg2)" function call.
3322 */
3323 void
3324ex_call(eap)
3325 exarg_T *eap;
3326{
3327 char_u *arg = eap->arg;
3328 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003330 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003332 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 linenr_T lnum;
3334 int doesrange;
3335 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003336 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003338 if (eap->skip)
3339 {
3340 /* trans_function_name() doesn't work well when skipping, use eval0()
3341 * instead to skip to any following command, e.g. for:
3342 * :if 0 | call dict.foo().bar() | endif */
3343 eval0(eap->arg, &rettv, &eap->nextcmd, FALSE);
3344 return;
3345 }
3346
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003347 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003348 if (fudi.fd_newkey != NULL)
3349 {
3350 /* Still need to give an error message for missing key. */
3351 EMSG2(_(e_dictkey), fudi.fd_newkey);
3352 vim_free(fudi.fd_newkey);
3353 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003354 if (tofree == NULL)
3355 return;
3356
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003357 /* Increase refcount on dictionary, it could get deleted when evaluating
3358 * the arguments. */
3359 if (fudi.fd_dict != NULL)
3360 ++fudi.fd_dict->dv_refcount;
3361
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003362 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003363 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003364 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365
Bram Moolenaar532c7802005-01-27 14:44:31 +00003366 /* Skip white space to allow ":call func ()". Not good, but required for
3367 * backward compatibility. */
3368 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003369 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370
3371 if (*startarg != '(')
3372 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003373 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 goto end;
3375 }
3376
3377 /*
3378 * When skipping, evaluate the function once, to find the end of the
3379 * arguments.
3380 * When the function takes a range, this is discovered after the first
3381 * call, and the loop is broken.
3382 */
3383 if (eap->skip)
3384 {
3385 ++emsg_skip;
3386 lnum = eap->line2; /* do it once, also with an invalid range */
3387 }
3388 else
3389 lnum = eap->line1;
3390 for ( ; lnum <= eap->line2; ++lnum)
3391 {
3392 if (!eap->skip && eap->addr_count > 0)
3393 {
3394 curwin->w_cursor.lnum = lnum;
3395 curwin->w_cursor.col = 0;
3396 }
3397 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003398 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003399 eap->line1, eap->line2, &doesrange,
3400 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 {
3402 failed = TRUE;
3403 break;
3404 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003405
3406 /* Handle a function returning a Funcref, Dictionary or List. */
3407 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3408 {
3409 failed = TRUE;
3410 break;
3411 }
3412
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003413 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (doesrange || eap->skip)
3415 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003416
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003418 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003419 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003420 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 if (aborting())
3422 break;
3423 }
3424 if (eap->skip)
3425 --emsg_skip;
3426
3427 if (!failed)
3428 {
3429 /* Check for trailing illegal characters and a following command. */
3430 if (!ends_excmd(*arg))
3431 {
3432 emsg_severe = TRUE;
3433 EMSG(_(e_trailing));
3434 }
3435 else
3436 eap->nextcmd = check_nextcmd(arg);
3437 }
3438
3439end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003440 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003441 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442}
3443
3444/*
3445 * ":unlet[!] var1 ... " command.
3446 */
3447 void
3448ex_unlet(eap)
3449 exarg_T *eap;
3450{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003451 ex_unletlock(eap, eap->arg, 0);
3452}
3453
3454/*
3455 * ":lockvar" and ":unlockvar" commands
3456 */
3457 void
3458ex_lockvar(eap)
3459 exarg_T *eap;
3460{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003462 int deep = 2;
3463
3464 if (eap->forceit)
3465 deep = -1;
3466 else if (vim_isdigit(*arg))
3467 {
3468 deep = getdigits(&arg);
3469 arg = skipwhite(arg);
3470 }
3471
3472 ex_unletlock(eap, arg, deep);
3473}
3474
3475/*
3476 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3477 */
3478 static void
3479ex_unletlock(eap, argstart, deep)
3480 exarg_T *eap;
3481 char_u *argstart;
3482 int deep;
3483{
3484 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003487 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
3489 do
3490 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003491 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003492 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3493 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003494 if (lv.ll_name == NULL)
3495 error = TRUE; /* error but continue parsing */
3496 if (name_end == NULL || (!vim_iswhite(*name_end)
3497 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003499 if (name_end != NULL)
3500 {
3501 emsg_severe = TRUE;
3502 EMSG(_(e_trailing));
3503 }
3504 if (!(eap->skip || error))
3505 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 break;
3507 }
3508
3509 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003510 {
3511 if (eap->cmdidx == CMD_unlet)
3512 {
3513 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3514 error = TRUE;
3515 }
3516 else
3517 {
3518 if (do_lock_var(&lv, name_end, deep,
3519 eap->cmdidx == CMD_lockvar) == FAIL)
3520 error = TRUE;
3521 }
3522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003524 if (!eap->skip)
3525 clear_lval(&lv);
3526
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 arg = skipwhite(name_end);
3528 } while (!ends_excmd(*arg));
3529
3530 eap->nextcmd = check_nextcmd(arg);
3531}
3532
Bram Moolenaar8c711452005-01-14 21:53:12 +00003533 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003534do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003535 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003536 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003537 int forceit;
3538{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003539 int ret = OK;
3540 int cc;
3541
3542 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003543 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003544 cc = *name_end;
3545 *name_end = NUL;
3546
3547 /* Normal name or expanded name. */
3548 if (check_changedtick(lp->ll_name))
3549 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003550 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003551 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003552 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003553 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003554 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3555 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003556 else if (lp->ll_range)
3557 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003558 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003559
3560 /* Delete a range of List items. */
3561 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3562 {
3563 li = lp->ll_li->li_next;
3564 listitem_remove(lp->ll_list, lp->ll_li);
3565 lp->ll_li = li;
3566 ++lp->ll_n1;
3567 }
3568 }
3569 else
3570 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003571 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003572 /* unlet a List item. */
3573 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003574 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003575 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003576 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003577 }
3578
3579 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003580}
3581
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582/*
3583 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003584 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 */
3586 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003587do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003589 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590{
Bram Moolenaar33570922005-01-25 22:26:29 +00003591 hashtab_T *ht;
3592 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003593 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003594 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
Bram Moolenaar33570922005-01-25 22:26:29 +00003596 ht = find_var_ht(name, &varname);
3597 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003599 hi = hash_find(ht, varname);
3600 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003601 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003602 di = HI2DI(hi);
3603 if (var_check_fixed(di->di_flags, name)
3604 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003605 return FAIL;
3606 delete_var(ht, hi);
3607 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003610 if (forceit)
3611 return OK;
3612 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 return FAIL;
3614}
3615
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003616/*
3617 * Lock or unlock variable indicated by "lp".
3618 * "deep" is the levels to go (-1 for unlimited);
3619 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3620 */
3621 static int
3622do_lock_var(lp, name_end, deep, lock)
3623 lval_T *lp;
3624 char_u *name_end;
3625 int deep;
3626 int lock;
3627{
3628 int ret = OK;
3629 int cc;
3630 dictitem_T *di;
3631
3632 if (deep == 0) /* nothing to do */
3633 return OK;
3634
3635 if (lp->ll_tv == NULL)
3636 {
3637 cc = *name_end;
3638 *name_end = NUL;
3639
3640 /* Normal name or expanded name. */
3641 if (check_changedtick(lp->ll_name))
3642 ret = FAIL;
3643 else
3644 {
3645 di = find_var(lp->ll_name, NULL);
3646 if (di == NULL)
3647 ret = FAIL;
3648 else
3649 {
3650 if (lock)
3651 di->di_flags |= DI_FLAGS_LOCK;
3652 else
3653 di->di_flags &= ~DI_FLAGS_LOCK;
3654 item_lock(&di->di_tv, deep, lock);
3655 }
3656 }
3657 *name_end = cc;
3658 }
3659 else if (lp->ll_range)
3660 {
3661 listitem_T *li = lp->ll_li;
3662
3663 /* (un)lock a range of List items. */
3664 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3665 {
3666 item_lock(&li->li_tv, deep, lock);
3667 li = li->li_next;
3668 ++lp->ll_n1;
3669 }
3670 }
3671 else if (lp->ll_list != NULL)
3672 /* (un)lock a List item. */
3673 item_lock(&lp->ll_li->li_tv, deep, lock);
3674 else
3675 /* un(lock) a Dictionary item. */
3676 item_lock(&lp->ll_di->di_tv, deep, lock);
3677
3678 return ret;
3679}
3680
3681/*
3682 * Lock or unlock an item. "deep" is nr of levels to go.
3683 */
3684 static void
3685item_lock(tv, deep, lock)
3686 typval_T *tv;
3687 int deep;
3688 int lock;
3689{
3690 static int recurse = 0;
3691 list_T *l;
3692 listitem_T *li;
3693 dict_T *d;
3694 hashitem_T *hi;
3695 int todo;
3696
3697 if (recurse >= DICT_MAXNEST)
3698 {
3699 EMSG(_("E743: variable nested too deep for (un)lock"));
3700 return;
3701 }
3702 if (deep == 0)
3703 return;
3704 ++recurse;
3705
3706 /* lock/unlock the item itself */
3707 if (lock)
3708 tv->v_lock |= VAR_LOCKED;
3709 else
3710 tv->v_lock &= ~VAR_LOCKED;
3711
3712 switch (tv->v_type)
3713 {
3714 case VAR_LIST:
3715 if ((l = tv->vval.v_list) != NULL)
3716 {
3717 if (lock)
3718 l->lv_lock |= VAR_LOCKED;
3719 else
3720 l->lv_lock &= ~VAR_LOCKED;
3721 if (deep < 0 || deep > 1)
3722 /* recursive: lock/unlock the items the List contains */
3723 for (li = l->lv_first; li != NULL; li = li->li_next)
3724 item_lock(&li->li_tv, deep - 1, lock);
3725 }
3726 break;
3727 case VAR_DICT:
3728 if ((d = tv->vval.v_dict) != NULL)
3729 {
3730 if (lock)
3731 d->dv_lock |= VAR_LOCKED;
3732 else
3733 d->dv_lock &= ~VAR_LOCKED;
3734 if (deep < 0 || deep > 1)
3735 {
3736 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003737 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003738 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3739 {
3740 if (!HASHITEM_EMPTY(hi))
3741 {
3742 --todo;
3743 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3744 }
3745 }
3746 }
3747 }
3748 }
3749 --recurse;
3750}
3751
Bram Moolenaara40058a2005-07-11 22:42:07 +00003752/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003753 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3754 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003755 */
3756 static int
3757tv_islocked(tv)
3758 typval_T *tv;
3759{
3760 return (tv->v_lock & VAR_LOCKED)
3761 || (tv->v_type == VAR_LIST
3762 && tv->vval.v_list != NULL
3763 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3764 || (tv->v_type == VAR_DICT
3765 && tv->vval.v_dict != NULL
3766 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3767}
3768
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3770/*
3771 * Delete all "menutrans_" variables.
3772 */
3773 void
3774del_menutrans_vars()
3775{
Bram Moolenaar33570922005-01-25 22:26:29 +00003776 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003777 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778
Bram Moolenaar33570922005-01-25 22:26:29 +00003779 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003780 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003781 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003782 {
3783 if (!HASHITEM_EMPTY(hi))
3784 {
3785 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003786 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3787 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003788 }
3789 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003790 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791}
3792#endif
3793
3794#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3795
3796/*
3797 * Local string buffer for the next two functions to store a variable name
3798 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3799 * get_user_var_name().
3800 */
3801
3802static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3803
3804static char_u *varnamebuf = NULL;
3805static int varnamebuflen = 0;
3806
3807/*
3808 * Function to concatenate a prefix and a variable name.
3809 */
3810 static char_u *
3811cat_prefix_varname(prefix, name)
3812 int prefix;
3813 char_u *name;
3814{
3815 int len;
3816
3817 len = (int)STRLEN(name) + 3;
3818 if (len > varnamebuflen)
3819 {
3820 vim_free(varnamebuf);
3821 len += 10; /* some additional space */
3822 varnamebuf = alloc(len);
3823 if (varnamebuf == NULL)
3824 {
3825 varnamebuflen = 0;
3826 return NULL;
3827 }
3828 varnamebuflen = len;
3829 }
3830 *varnamebuf = prefix;
3831 varnamebuf[1] = ':';
3832 STRCPY(varnamebuf + 2, name);
3833 return varnamebuf;
3834}
3835
3836/*
3837 * Function given to ExpandGeneric() to obtain the list of user defined
3838 * (global/buffer/window/built-in) variable names.
3839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 char_u *
3841get_user_var_name(xp, idx)
3842 expand_T *xp;
3843 int idx;
3844{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003845 static long_u gdone;
3846 static long_u bdone;
3847 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003848#ifdef FEAT_WINDOWS
3849 static long_u tdone;
3850#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003851 static int vidx;
3852 static hashitem_T *hi;
3853 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854
3855 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003856 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003857 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003858#ifdef FEAT_WINDOWS
3859 tdone = 0;
3860#endif
3861 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003862
3863 /* Global variables */
3864 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003866 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003867 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003868 else
3869 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003870 while (HASHITEM_EMPTY(hi))
3871 ++hi;
3872 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3873 return cat_prefix_varname('g', hi->hi_key);
3874 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003876
3877 /* b: variables */
3878 ht = &curbuf->b_vars.dv_hashtab;
3879 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003881 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003882 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003883 else
3884 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003885 while (HASHITEM_EMPTY(hi))
3886 ++hi;
3887 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003889 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003891 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 return (char_u *)"b:changedtick";
3893 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003894
3895 /* w: variables */
3896 ht = &curwin->w_vars.dv_hashtab;
3897 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003899 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003900 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003901 else
3902 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003903 while (HASHITEM_EMPTY(hi))
3904 ++hi;
3905 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003907
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003908#ifdef FEAT_WINDOWS
3909 /* t: variables */
3910 ht = &curtab->tp_vars.dv_hashtab;
3911 if (tdone < ht->ht_used)
3912 {
3913 if (tdone++ == 0)
3914 hi = ht->ht_array;
3915 else
3916 ++hi;
3917 while (HASHITEM_EMPTY(hi))
3918 ++hi;
3919 return cat_prefix_varname('t', hi->hi_key);
3920 }
3921#endif
3922
Bram Moolenaar33570922005-01-25 22:26:29 +00003923 /* v: variables */
3924 if (vidx < VV_LEN)
3925 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
3927 vim_free(varnamebuf);
3928 varnamebuf = NULL;
3929 varnamebuflen = 0;
3930 return NULL;
3931}
3932
3933#endif /* FEAT_CMDL_COMPL */
3934
3935/*
3936 * types for expressions.
3937 */
3938typedef enum
3939{
3940 TYPE_UNKNOWN = 0
3941 , TYPE_EQUAL /* == */
3942 , TYPE_NEQUAL /* != */
3943 , TYPE_GREATER /* > */
3944 , TYPE_GEQUAL /* >= */
3945 , TYPE_SMALLER /* < */
3946 , TYPE_SEQUAL /* <= */
3947 , TYPE_MATCH /* =~ */
3948 , TYPE_NOMATCH /* !~ */
3949} exptype_T;
3950
3951/*
3952 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003953 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3955 */
3956
3957/*
3958 * Handle zero level expression.
3959 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003960 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003961 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 * Return OK or FAIL.
3963 */
3964 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003965eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 char_u **nextcmd;
3969 int evaluate;
3970{
3971 int ret;
3972 char_u *p;
3973
3974 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003975 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 if (ret == FAIL || !ends_excmd(*p))
3977 {
3978 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003979 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 /*
3981 * Report the invalid expression unless the expression evaluation has
3982 * been cancelled due to an aborting error, an interrupt, or an
3983 * exception.
3984 */
3985 if (!aborting())
3986 EMSG2(_(e_invexpr2), arg);
3987 ret = FAIL;
3988 }
3989 if (nextcmd != NULL)
3990 *nextcmd = check_nextcmd(p);
3991
3992 return ret;
3993}
3994
3995/*
3996 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003997 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 *
3999 * "arg" must point to the first non-white of the expression.
4000 * "arg" is advanced to the next non-white after the recognized expression.
4001 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004002 * Note: "rettv.v_lock" is not set.
4003 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 * Return OK or FAIL.
4005 */
4006 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004007eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 int evaluate;
4011{
4012 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004013 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014
4015 /*
4016 * Get the first variable.
4017 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004018 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 return FAIL;
4020
4021 if ((*arg)[0] == '?')
4022 {
4023 result = FALSE;
4024 if (evaluate)
4025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004026 int error = FALSE;
4027
4028 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004030 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004031 if (error)
4032 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
4034
4035 /*
4036 * Get the second variable.
4037 */
4038 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004039 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 return FAIL;
4041
4042 /*
4043 * Check for the ":".
4044 */
4045 if ((*arg)[0] != ':')
4046 {
4047 EMSG(_("E109: Missing ':' after '?'"));
4048 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004049 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 return FAIL;
4051 }
4052
4053 /*
4054 * Get the third variable.
4055 */
4056 *arg = skipwhite(*arg + 1);
4057 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4058 {
4059 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004060 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 return FAIL;
4062 }
4063 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004064 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 }
4066
4067 return OK;
4068}
4069
4070/*
4071 * Handle first level expression:
4072 * expr2 || expr2 || expr2 logical OR
4073 *
4074 * "arg" must point to the first non-white of the expression.
4075 * "arg" is advanced to the next non-white after the recognized expression.
4076 *
4077 * Return OK or FAIL.
4078 */
4079 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004080eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 int evaluate;
4084{
Bram Moolenaar33570922005-01-25 22:26:29 +00004085 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 long result;
4087 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004088 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089
4090 /*
4091 * Get the first variable.
4092 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004093 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 return FAIL;
4095
4096 /*
4097 * Repeat until there is no following "||".
4098 */
4099 first = TRUE;
4100 result = FALSE;
4101 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4102 {
4103 if (evaluate && first)
4104 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004105 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004107 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004108 if (error)
4109 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 first = FALSE;
4111 }
4112
4113 /*
4114 * Get the second variable.
4115 */
4116 *arg = skipwhite(*arg + 2);
4117 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4118 return FAIL;
4119
4120 /*
4121 * Compute the result.
4122 */
4123 if (evaluate && !result)
4124 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004125 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004128 if (error)
4129 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 }
4131 if (evaluate)
4132 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 rettv->v_type = VAR_NUMBER;
4134 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 }
4136 }
4137
4138 return OK;
4139}
4140
4141/*
4142 * Handle second level expression:
4143 * expr3 && expr3 && expr3 logical AND
4144 *
4145 * "arg" must point to the first non-white of the expression.
4146 * "arg" is advanced to the next non-white after the recognized expression.
4147 *
4148 * Return OK or FAIL.
4149 */
4150 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 int evaluate;
4155{
Bram Moolenaar33570922005-01-25 22:26:29 +00004156 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 long result;
4158 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004159 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
4161 /*
4162 * Get the first variable.
4163 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 return FAIL;
4166
4167 /*
4168 * Repeat until there is no following "&&".
4169 */
4170 first = TRUE;
4171 result = TRUE;
4172 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4173 {
4174 if (evaluate && first)
4175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004176 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004179 if (error)
4180 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 first = FALSE;
4182 }
4183
4184 /*
4185 * Get the second variable.
4186 */
4187 *arg = skipwhite(*arg + 2);
4188 if (eval4(arg, &var2, evaluate && result) == FAIL)
4189 return FAIL;
4190
4191 /*
4192 * Compute the result.
4193 */
4194 if (evaluate && result)
4195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004196 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004199 if (error)
4200 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 }
4202 if (evaluate)
4203 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 rettv->v_type = VAR_NUMBER;
4205 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 }
4207 }
4208
4209 return OK;
4210}
4211
4212/*
4213 * Handle third level expression:
4214 * var1 == var2
4215 * var1 =~ var2
4216 * var1 != var2
4217 * var1 !~ var2
4218 * var1 > var2
4219 * var1 >= var2
4220 * var1 < var2
4221 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004222 * var1 is var2
4223 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 *
4225 * "arg" must point to the first non-white of the expression.
4226 * "arg" is advanced to the next non-white after the recognized expression.
4227 *
4228 * Return OK or FAIL.
4229 */
4230 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 int evaluate;
4235{
Bram Moolenaar33570922005-01-25 22:26:29 +00004236 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 char_u *p;
4238 int i;
4239 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004240 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 int len = 2;
4242 long n1, n2;
4243 char_u *s1, *s2;
4244 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4245 regmatch_T regmatch;
4246 int ic;
4247 char_u *save_cpo;
4248
4249 /*
4250 * Get the first variable.
4251 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 return FAIL;
4254
4255 p = *arg;
4256 switch (p[0])
4257 {
4258 case '=': if (p[1] == '=')
4259 type = TYPE_EQUAL;
4260 else if (p[1] == '~')
4261 type = TYPE_MATCH;
4262 break;
4263 case '!': if (p[1] == '=')
4264 type = TYPE_NEQUAL;
4265 else if (p[1] == '~')
4266 type = TYPE_NOMATCH;
4267 break;
4268 case '>': if (p[1] != '=')
4269 {
4270 type = TYPE_GREATER;
4271 len = 1;
4272 }
4273 else
4274 type = TYPE_GEQUAL;
4275 break;
4276 case '<': if (p[1] != '=')
4277 {
4278 type = TYPE_SMALLER;
4279 len = 1;
4280 }
4281 else
4282 type = TYPE_SEQUAL;
4283 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004284 case 'i': if (p[1] == 's')
4285 {
4286 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4287 len = 5;
4288 if (!vim_isIDc(p[len]))
4289 {
4290 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4291 type_is = TRUE;
4292 }
4293 }
4294 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296
4297 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004298 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 */
4300 if (type != TYPE_UNKNOWN)
4301 {
4302 /* extra question mark appended: ignore case */
4303 if (p[len] == '?')
4304 {
4305 ic = TRUE;
4306 ++len;
4307 }
4308 /* extra '#' appended: match case */
4309 else if (p[len] == '#')
4310 {
4311 ic = FALSE;
4312 ++len;
4313 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004314 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 else
4316 ic = p_ic;
4317
4318 /*
4319 * Get the second variable.
4320 */
4321 *arg = skipwhite(p + len);
4322 if (eval5(arg, &var2, evaluate) == FAIL)
4323 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 return FAIL;
4326 }
4327
4328 if (evaluate)
4329 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004330 if (type_is && rettv->v_type != var2.v_type)
4331 {
4332 /* For "is" a different type always means FALSE, for "notis"
4333 * it means TRUE. */
4334 n1 = (type == TYPE_NEQUAL);
4335 }
4336 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4337 {
4338 if (type_is)
4339 {
4340 n1 = (rettv->v_type == var2.v_type
4341 && rettv->vval.v_list == var2.vval.v_list);
4342 if (type == TYPE_NEQUAL)
4343 n1 = !n1;
4344 }
4345 else if (rettv->v_type != var2.v_type
4346 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4347 {
4348 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004349 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004350 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004351 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004352 clear_tv(rettv);
4353 clear_tv(&var2);
4354 return FAIL;
4355 }
4356 else
4357 {
4358 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004359 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4360 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004361 if (type == TYPE_NEQUAL)
4362 n1 = !n1;
4363 }
4364 }
4365
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004366 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4367 {
4368 if (type_is)
4369 {
4370 n1 = (rettv->v_type == var2.v_type
4371 && rettv->vval.v_dict == var2.vval.v_dict);
4372 if (type == TYPE_NEQUAL)
4373 n1 = !n1;
4374 }
4375 else if (rettv->v_type != var2.v_type
4376 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4377 {
4378 if (rettv->v_type != var2.v_type)
4379 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4380 else
4381 EMSG(_("E736: Invalid operation for Dictionary"));
4382 clear_tv(rettv);
4383 clear_tv(&var2);
4384 return FAIL;
4385 }
4386 else
4387 {
4388 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004389 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4390 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004391 if (type == TYPE_NEQUAL)
4392 n1 = !n1;
4393 }
4394 }
4395
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004396 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4397 {
4398 if (rettv->v_type != var2.v_type
4399 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4400 {
4401 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004402 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004403 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004404 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004405 clear_tv(rettv);
4406 clear_tv(&var2);
4407 return FAIL;
4408 }
4409 else
4410 {
4411 /* Compare two Funcrefs for being equal or unequal. */
4412 if (rettv->vval.v_string == NULL
4413 || var2.vval.v_string == NULL)
4414 n1 = FALSE;
4415 else
4416 n1 = STRCMP(rettv->vval.v_string,
4417 var2.vval.v_string) == 0;
4418 if (type == TYPE_NEQUAL)
4419 n1 = !n1;
4420 }
4421 }
4422
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004423#ifdef FEAT_FLOAT
4424 /*
4425 * If one of the two variables is a float, compare as a float.
4426 * When using "=~" or "!~", always compare as string.
4427 */
4428 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4429 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4430 {
4431 float_T f1, f2;
4432
4433 if (rettv->v_type == VAR_FLOAT)
4434 f1 = rettv->vval.v_float;
4435 else
4436 f1 = get_tv_number(rettv);
4437 if (var2.v_type == VAR_FLOAT)
4438 f2 = var2.vval.v_float;
4439 else
4440 f2 = get_tv_number(&var2);
4441 n1 = FALSE;
4442 switch (type)
4443 {
4444 case TYPE_EQUAL: n1 = (f1 == f2); break;
4445 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4446 case TYPE_GREATER: n1 = (f1 > f2); break;
4447 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4448 case TYPE_SMALLER: n1 = (f1 < f2); break;
4449 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4450 case TYPE_UNKNOWN:
4451 case TYPE_MATCH:
4452 case TYPE_NOMATCH: break; /* avoid gcc warning */
4453 }
4454 }
4455#endif
4456
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 /*
4458 * If one of the two variables is a number, compare as a number.
4459 * When using "=~" or "!~", always compare as string.
4460 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004461 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4463 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004464 n1 = get_tv_number(rettv);
4465 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 switch (type)
4467 {
4468 case TYPE_EQUAL: n1 = (n1 == n2); break;
4469 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4470 case TYPE_GREATER: n1 = (n1 > n2); break;
4471 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4472 case TYPE_SMALLER: n1 = (n1 < n2); break;
4473 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4474 case TYPE_UNKNOWN:
4475 case TYPE_MATCH:
4476 case TYPE_NOMATCH: break; /* avoid gcc warning */
4477 }
4478 }
4479 else
4480 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004481 s1 = get_tv_string_buf(rettv, buf1);
4482 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4484 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4485 else
4486 i = 0;
4487 n1 = FALSE;
4488 switch (type)
4489 {
4490 case TYPE_EQUAL: n1 = (i == 0); break;
4491 case TYPE_NEQUAL: n1 = (i != 0); break;
4492 case TYPE_GREATER: n1 = (i > 0); break;
4493 case TYPE_GEQUAL: n1 = (i >= 0); break;
4494 case TYPE_SMALLER: n1 = (i < 0); break;
4495 case TYPE_SEQUAL: n1 = (i <= 0); break;
4496
4497 case TYPE_MATCH:
4498 case TYPE_NOMATCH:
4499 /* avoid 'l' flag in 'cpoptions' */
4500 save_cpo = p_cpo;
4501 p_cpo = (char_u *)"";
4502 regmatch.regprog = vim_regcomp(s2,
4503 RE_MAGIC + RE_STRING);
4504 regmatch.rm_ic = ic;
4505 if (regmatch.regprog != NULL)
4506 {
4507 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4508 vim_free(regmatch.regprog);
4509 if (type == TYPE_NOMATCH)
4510 n1 = !n1;
4511 }
4512 p_cpo = save_cpo;
4513 break;
4514
4515 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4516 }
4517 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004518 clear_tv(rettv);
4519 clear_tv(&var2);
4520 rettv->v_type = VAR_NUMBER;
4521 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 }
4523 }
4524
4525 return OK;
4526}
4527
4528/*
4529 * Handle fourth level expression:
4530 * + number addition
4531 * - number subtraction
4532 * . string concatenation
4533 *
4534 * "arg" must point to the first non-white of the expression.
4535 * "arg" is advanced to the next non-white after the recognized expression.
4536 *
4537 * Return OK or FAIL.
4538 */
4539 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004540eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 int evaluate;
4544{
Bram Moolenaar33570922005-01-25 22:26:29 +00004545 typval_T var2;
4546 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 int op;
4548 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004549#ifdef FEAT_FLOAT
4550 float_T f1 = 0, f2 = 0;
4551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 char_u *s1, *s2;
4553 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4554 char_u *p;
4555
4556 /*
4557 * Get the first variable.
4558 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004559 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 return FAIL;
4561
4562 /*
4563 * Repeat computing, until no '+', '-' or '.' is following.
4564 */
4565 for (;;)
4566 {
4567 op = **arg;
4568 if (op != '+' && op != '-' && op != '.')
4569 break;
4570
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004571 if ((op != '+' || rettv->v_type != VAR_LIST)
4572#ifdef FEAT_FLOAT
4573 && (op == '.' || rettv->v_type != VAR_FLOAT)
4574#endif
4575 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004576 {
4577 /* For "list + ...", an illegal use of the first operand as
4578 * a number cannot be determined before evaluating the 2nd
4579 * operand: if this is also a list, all is ok.
4580 * For "something . ...", "something - ..." or "non-list + ...",
4581 * we know that the first operand needs to be a string or number
4582 * without evaluating the 2nd operand. So check before to avoid
4583 * side effects after an error. */
4584 if (evaluate && get_tv_string_chk(rettv) == NULL)
4585 {
4586 clear_tv(rettv);
4587 return FAIL;
4588 }
4589 }
4590
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 /*
4592 * Get the second variable.
4593 */
4594 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004595 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004597 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 return FAIL;
4599 }
4600
4601 if (evaluate)
4602 {
4603 /*
4604 * Compute the result.
4605 */
4606 if (op == '.')
4607 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004608 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4609 s2 = get_tv_string_buf_chk(&var2, buf2);
4610 if (s2 == NULL) /* type error ? */
4611 {
4612 clear_tv(rettv);
4613 clear_tv(&var2);
4614 return FAIL;
4615 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004616 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004617 clear_tv(rettv);
4618 rettv->v_type = VAR_STRING;
4619 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004621 else if (op == '+' && rettv->v_type == VAR_LIST
4622 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004623 {
4624 /* concatenate Lists */
4625 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4626 &var3) == FAIL)
4627 {
4628 clear_tv(rettv);
4629 clear_tv(&var2);
4630 return FAIL;
4631 }
4632 clear_tv(rettv);
4633 *rettv = var3;
4634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 else
4636 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004637 int error = FALSE;
4638
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004639#ifdef FEAT_FLOAT
4640 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004641 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004642 f1 = rettv->vval.v_float;
4643 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004644 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004645 else
4646#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004647 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004648 n1 = get_tv_number_chk(rettv, &error);
4649 if (error)
4650 {
4651 /* This can only happen for "list + non-list". For
4652 * "non-list + ..." or "something - ...", we returned
4653 * before evaluating the 2nd operand. */
4654 clear_tv(rettv);
4655 return FAIL;
4656 }
4657#ifdef FEAT_FLOAT
4658 if (var2.v_type == VAR_FLOAT)
4659 f1 = n1;
4660#endif
4661 }
4662#ifdef FEAT_FLOAT
4663 if (var2.v_type == VAR_FLOAT)
4664 {
4665 f2 = var2.vval.v_float;
4666 n2 = 0;
4667 }
4668 else
4669#endif
4670 {
4671 n2 = get_tv_number_chk(&var2, &error);
4672 if (error)
4673 {
4674 clear_tv(rettv);
4675 clear_tv(&var2);
4676 return FAIL;
4677 }
4678#ifdef FEAT_FLOAT
4679 if (rettv->v_type == VAR_FLOAT)
4680 f2 = n2;
4681#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004682 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004683 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004684
4685#ifdef FEAT_FLOAT
4686 /* If there is a float on either side the result is a float. */
4687 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4688 {
4689 if (op == '+')
4690 f1 = f1 + f2;
4691 else
4692 f1 = f1 - f2;
4693 rettv->v_type = VAR_FLOAT;
4694 rettv->vval.v_float = f1;
4695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004697#endif
4698 {
4699 if (op == '+')
4700 n1 = n1 + n2;
4701 else
4702 n1 = n1 - n2;
4703 rettv->v_type = VAR_NUMBER;
4704 rettv->vval.v_number = n1;
4705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004707 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
4709 }
4710 return OK;
4711}
4712
4713/*
4714 * Handle fifth level expression:
4715 * * number multiplication
4716 * / number division
4717 * % number modulo
4718 *
4719 * "arg" must point to the first non-white of the expression.
4720 * "arg" is advanced to the next non-white after the recognized expression.
4721 *
4722 * Return OK or FAIL.
4723 */
4724 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004725eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004729 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
Bram Moolenaar33570922005-01-25 22:26:29 +00004731 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 int op;
4733 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004734#ifdef FEAT_FLOAT
4735 int use_float = FALSE;
4736 float_T f1 = 0, f2;
4737#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004738 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739
4740 /*
4741 * Get the first variable.
4742 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004743 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 return FAIL;
4745
4746 /*
4747 * Repeat computing, until no '*', '/' or '%' is following.
4748 */
4749 for (;;)
4750 {
4751 op = **arg;
4752 if (op != '*' && op != '/' && op != '%')
4753 break;
4754
4755 if (evaluate)
4756 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004757#ifdef FEAT_FLOAT
4758 if (rettv->v_type == VAR_FLOAT)
4759 {
4760 f1 = rettv->vval.v_float;
4761 use_float = TRUE;
4762 n1 = 0;
4763 }
4764 else
4765#endif
4766 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004767 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004768 if (error)
4769 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 }
4771 else
4772 n1 = 0;
4773
4774 /*
4775 * Get the second variable.
4776 */
4777 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004778 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 return FAIL;
4780
4781 if (evaluate)
4782 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004783#ifdef FEAT_FLOAT
4784 if (var2.v_type == VAR_FLOAT)
4785 {
4786 if (!use_float)
4787 {
4788 f1 = n1;
4789 use_float = TRUE;
4790 }
4791 f2 = var2.vval.v_float;
4792 n2 = 0;
4793 }
4794 else
4795#endif
4796 {
4797 n2 = get_tv_number_chk(&var2, &error);
4798 clear_tv(&var2);
4799 if (error)
4800 return FAIL;
4801#ifdef FEAT_FLOAT
4802 if (use_float)
4803 f2 = n2;
4804#endif
4805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806
4807 /*
4808 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004809 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004811#ifdef FEAT_FLOAT
4812 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004814 if (op == '*')
4815 f1 = f1 * f2;
4816 else if (op == '/')
4817 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004818# ifdef VMS
4819 /* VMS crashes on divide by zero, work around it */
4820 if (f2 == 0.0)
4821 {
4822 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004823 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004824 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004825 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004826 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004827 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004828 }
4829 else
4830 f1 = f1 / f2;
4831# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004832 /* We rely on the floating point library to handle divide
4833 * by zero to result in "inf" and not a crash. */
4834 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004835# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004838 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004839 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004840 return FAIL;
4841 }
4842 rettv->v_type = VAR_FLOAT;
4843 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 }
4845 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004848 if (op == '*')
4849 n1 = n1 * n2;
4850 else if (op == '/')
4851 {
4852 if (n2 == 0) /* give an error message? */
4853 {
4854 if (n1 == 0)
4855 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4856 else if (n1 < 0)
4857 n1 = -0x7fffffffL;
4858 else
4859 n1 = 0x7fffffffL;
4860 }
4861 else
4862 n1 = n1 / n2;
4863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004865 {
4866 if (n2 == 0) /* give an error message? */
4867 n1 = 0;
4868 else
4869 n1 = n1 % n2;
4870 }
4871 rettv->v_type = VAR_NUMBER;
4872 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 }
4875 }
4876
4877 return OK;
4878}
4879
4880/*
4881 * Handle sixth level expression:
4882 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004883 * "string" string constant
4884 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 * &option-name option value
4886 * @r register contents
4887 * identifier variable value
4888 * function() function call
4889 * $VAR environment variable
4890 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004891 * [expr, expr] List
4892 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 *
4894 * Also handle:
4895 * ! in front logical NOT
4896 * - in front unary minus
4897 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004898 * trailing [] subscript in String or List
4899 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 *
4901 * "arg" must point to the first non-white of the expression.
4902 * "arg" is advanced to the next non-white after the recognized expression.
4903 *
4904 * Return OK or FAIL.
4905 */
4906 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004907eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004911 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 long n;
4914 int len;
4915 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 char_u *start_leader, *end_leader;
4917 int ret = OK;
4918 char_u *alias;
4919
4920 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004921 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004922 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004924 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925
4926 /*
4927 * Skip '!' and '-' characters. They are handled later.
4928 */
4929 start_leader = *arg;
4930 while (**arg == '!' || **arg == '-' || **arg == '+')
4931 *arg = skipwhite(*arg + 1);
4932 end_leader = *arg;
4933
4934 switch (**arg)
4935 {
4936 /*
4937 * Number constant.
4938 */
4939 case '0':
4940 case '1':
4941 case '2':
4942 case '3':
4943 case '4':
4944 case '5':
4945 case '6':
4946 case '7':
4947 case '8':
4948 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004949 {
4950#ifdef FEAT_FLOAT
4951 char_u *p = skipdigits(*arg + 1);
4952 int get_float = FALSE;
4953
4954 /* We accept a float when the format matches
4955 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004956 * strict to avoid backwards compatibility problems.
4957 * Don't look for a float after the "." operator, so that
4958 * ":let vers = 1.2.3" doesn't fail. */
4959 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004961 get_float = TRUE;
4962 p = skipdigits(p + 2);
4963 if (*p == 'e' || *p == 'E')
4964 {
4965 ++p;
4966 if (*p == '-' || *p == '+')
4967 ++p;
4968 if (!vim_isdigit(*p))
4969 get_float = FALSE;
4970 else
4971 p = skipdigits(p + 1);
4972 }
4973 if (ASCII_ISALPHA(*p) || *p == '.')
4974 get_float = FALSE;
4975 }
4976 if (get_float)
4977 {
4978 float_T f;
4979
4980 *arg += string2float(*arg, &f);
4981 if (evaluate)
4982 {
4983 rettv->v_type = VAR_FLOAT;
4984 rettv->vval.v_float = f;
4985 }
4986 }
4987 else
4988#endif
4989 {
4990 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4991 *arg += len;
4992 if (evaluate)
4993 {
4994 rettv->v_type = VAR_NUMBER;
4995 rettv->vval.v_number = n;
4996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 }
4998 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000
5001 /*
5002 * String constant: "string".
5003 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005004 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 break;
5006
5007 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005008 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005010 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005011 break;
5012
5013 /*
5014 * List: [expr, expr]
5015 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005016 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 break;
5018
5019 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005020 * Dictionary: {key: val, key: val}
5021 */
5022 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5023 break;
5024
5025 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005026 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005028 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 break;
5030
5031 /*
5032 * Environment variable: $VAR.
5033 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005034 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 break;
5036
5037 /*
5038 * Register contents: @r.
5039 */
5040 case '@': ++*arg;
5041 if (evaluate)
5042 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005043 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005044 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
5046 if (**arg != NUL)
5047 ++*arg;
5048 break;
5049
5050 /*
5051 * nested expression: (expression).
5052 */
5053 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005054 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 if (**arg == ')')
5056 ++*arg;
5057 else if (ret == OK)
5058 {
5059 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005060 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 ret = FAIL;
5062 }
5063 break;
5064
Bram Moolenaar8c711452005-01-14 21:53:12 +00005065 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 break;
5067 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005068
5069 if (ret == NOTDONE)
5070 {
5071 /*
5072 * Must be a variable or function name.
5073 * Can also be a curly-braces kind of name: {expr}.
5074 */
5075 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005076 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 if (alias != NULL)
5078 s = alias;
5079
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005080 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 ret = FAIL;
5082 else
5083 {
5084 if (**arg == '(') /* recursive! */
5085 {
5086 /* If "s" is the name of a variable of type VAR_FUNC
5087 * use its contents. */
5088 s = deref_func_name(s, &len);
5089
5090 /* Invoke the function. */
5091 ret = get_func_tv(s, len, rettv, arg,
5092 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005093 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 /* Stop the expression evaluation when immediately
5095 * aborting on error, or when an interrupt occurred or
5096 * an exception was thrown but not caught. */
5097 if (aborting())
5098 {
5099 if (ret == OK)
5100 clear_tv(rettv);
5101 ret = FAIL;
5102 }
5103 }
5104 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005105 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005106 else
5107 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 }
5109
5110 if (alias != NULL)
5111 vim_free(alias);
5112 }
5113
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 *arg = skipwhite(*arg);
5115
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005116 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5117 * expr(expr). */
5118 if (ret == OK)
5119 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120
5121 /*
5122 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5123 */
5124 if (ret == OK && evaluate && end_leader > start_leader)
5125 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005126 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005127 int val = 0;
5128#ifdef FEAT_FLOAT
5129 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005130
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005131 if (rettv->v_type == VAR_FLOAT)
5132 f = rettv->vval.v_float;
5133 else
5134#endif
5135 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005136 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005138 clear_tv(rettv);
5139 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005141 else
5142 {
5143 while (end_leader > start_leader)
5144 {
5145 --end_leader;
5146 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005147 {
5148#ifdef FEAT_FLOAT
5149 if (rettv->v_type == VAR_FLOAT)
5150 f = !f;
5151 else
5152#endif
5153 val = !val;
5154 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005155 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005156 {
5157#ifdef FEAT_FLOAT
5158 if (rettv->v_type == VAR_FLOAT)
5159 f = -f;
5160 else
5161#endif
5162 val = -val;
5163 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005164 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005165#ifdef FEAT_FLOAT
5166 if (rettv->v_type == VAR_FLOAT)
5167 {
5168 clear_tv(rettv);
5169 rettv->vval.v_float = f;
5170 }
5171 else
5172#endif
5173 {
5174 clear_tv(rettv);
5175 rettv->v_type = VAR_NUMBER;
5176 rettv->vval.v_number = val;
5177 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
5180
5181 return ret;
5182}
5183
5184/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005185 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5186 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5188 */
5189 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005190eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005191 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005192 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005193 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005194 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005195{
5196 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005197 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005198 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199 long len = -1;
5200 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005201 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005203
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005204 if (rettv->v_type == VAR_FUNC
5205#ifdef FEAT_FLOAT
5206 || rettv->v_type == VAR_FLOAT
5207#endif
5208 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005209 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005210 if (verbose)
5211 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005212 return FAIL;
5213 }
5214
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005216 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217 /*
5218 * dict.name
5219 */
5220 key = *arg + 1;
5221 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5222 ;
5223 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005224 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226 }
5227 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 /*
5230 * something[idx]
5231 *
5232 * Get the (first) variable from inside the [].
5233 */
5234 *arg = skipwhite(*arg + 1);
5235 if (**arg == ':')
5236 empty1 = TRUE;
5237 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5238 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005239 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5240 {
5241 /* not a number or string */
5242 clear_tv(&var1);
5243 return FAIL;
5244 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245
5246 /*
5247 * Get the second variable from inside the [:].
5248 */
5249 if (**arg == ':')
5250 {
5251 range = TRUE;
5252 *arg = skipwhite(*arg + 1);
5253 if (**arg == ']')
5254 empty2 = TRUE;
5255 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005257 if (!empty1)
5258 clear_tv(&var1);
5259 return FAIL;
5260 }
5261 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5262 {
5263 /* not a number or string */
5264 if (!empty1)
5265 clear_tv(&var1);
5266 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 return FAIL;
5268 }
5269 }
5270
5271 /* Check for the ']'. */
5272 if (**arg != ']')
5273 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005274 if (verbose)
5275 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005276 clear_tv(&var1);
5277 if (range)
5278 clear_tv(&var2);
5279 return FAIL;
5280 }
5281 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 }
5283
5284 if (evaluate)
5285 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286 n1 = 0;
5287 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005289 n1 = get_tv_number(&var1);
5290 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 }
5292 if (range)
5293 {
5294 if (empty2)
5295 n2 = -1;
5296 else
5297 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005298 n2 = get_tv_number(&var2);
5299 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 }
5301 }
5302
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005303 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304 {
5305 case VAR_NUMBER:
5306 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005307 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308 len = (long)STRLEN(s);
5309 if (range)
5310 {
5311 /* The resulting variable is a substring. If the indexes
5312 * are out of range the result is empty. */
5313 if (n1 < 0)
5314 {
5315 n1 = len + n1;
5316 if (n1 < 0)
5317 n1 = 0;
5318 }
5319 if (n2 < 0)
5320 n2 = len + n2;
5321 else if (n2 >= len)
5322 n2 = len;
5323 if (n1 >= len || n2 < 0 || n1 > n2)
5324 s = NULL;
5325 else
5326 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5327 }
5328 else
5329 {
5330 /* The resulting variable is a string of a single
5331 * character. If the index is too big or negative the
5332 * result is empty. */
5333 if (n1 >= len || n1 < 0)
5334 s = NULL;
5335 else
5336 s = vim_strnsave(s + n1, 1);
5337 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005338 clear_tv(rettv);
5339 rettv->v_type = VAR_STRING;
5340 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 break;
5342
5343 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005344 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 if (n1 < 0)
5346 n1 = len + n1;
5347 if (!empty1 && (n1 < 0 || n1 >= len))
5348 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005349 /* For a range we allow invalid values and return an empty
5350 * list. A list index out of range is an error. */
5351 if (!range)
5352 {
5353 if (verbose)
5354 EMSGN(_(e_listidx), n1);
5355 return FAIL;
5356 }
5357 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 }
5359 if (range)
5360 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005361 list_T *l;
5362 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363
5364 if (n2 < 0)
5365 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005366 else if (n2 >= len)
5367 n2 = len - 1;
5368 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005369 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 l = list_alloc();
5371 if (l == NULL)
5372 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005373 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 n1 <= n2; ++n1)
5375 {
5376 if (list_append_tv(l, &item->li_tv) == FAIL)
5377 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005378 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 return FAIL;
5380 }
5381 item = item->li_next;
5382 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005383 clear_tv(rettv);
5384 rettv->v_type = VAR_LIST;
5385 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005386 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387 }
5388 else
5389 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005390 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005391 clear_tv(rettv);
5392 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 }
5394 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005395
5396 case VAR_DICT:
5397 if (range)
5398 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005399 if (verbose)
5400 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005401 if (len == -1)
5402 clear_tv(&var1);
5403 return FAIL;
5404 }
5405 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005406 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407
5408 if (len == -1)
5409 {
5410 key = get_tv_string(&var1);
5411 if (*key == NUL)
5412 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005413 if (verbose)
5414 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005415 clear_tv(&var1);
5416 return FAIL;
5417 }
5418 }
5419
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005420 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005421
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005422 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005423 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005424 if (len == -1)
5425 clear_tv(&var1);
5426 if (item == NULL)
5427 return FAIL;
5428
5429 copy_tv(&item->di_tv, &var1);
5430 clear_tv(rettv);
5431 *rettv = var1;
5432 }
5433 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 }
5435 }
5436
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 return OK;
5438}
5439
5440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 * Get an option value.
5442 * "arg" points to the '&' or '+' before the option name.
5443 * "arg" is advanced to character after the option name.
5444 * Return OK or FAIL.
5445 */
5446 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005447get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005449 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 int evaluate;
5451{
5452 char_u *option_end;
5453 long numval;
5454 char_u *stringval;
5455 int opt_type;
5456 int c;
5457 int working = (**arg == '+'); /* has("+option") */
5458 int ret = OK;
5459 int opt_flags;
5460
5461 /*
5462 * Isolate the option name and find its value.
5463 */
5464 option_end = find_option_end(arg, &opt_flags);
5465 if (option_end == NULL)
5466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005467 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 EMSG2(_("E112: Option name missing: %s"), *arg);
5469 return FAIL;
5470 }
5471
5472 if (!evaluate)
5473 {
5474 *arg = option_end;
5475 return OK;
5476 }
5477
5478 c = *option_end;
5479 *option_end = NUL;
5480 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005481 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
5483 if (opt_type == -3) /* invalid name */
5484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005485 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 EMSG2(_("E113: Unknown option: %s"), *arg);
5487 ret = FAIL;
5488 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005489 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 {
5491 if (opt_type == -2) /* hidden string option */
5492 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005493 rettv->v_type = VAR_STRING;
5494 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 }
5496 else if (opt_type == -1) /* hidden number option */
5497 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005498 rettv->v_type = VAR_NUMBER;
5499 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 }
5501 else if (opt_type == 1) /* number option */
5502 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005503 rettv->v_type = VAR_NUMBER;
5504 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 }
5506 else /* string option */
5507 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005508 rettv->v_type = VAR_STRING;
5509 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 }
5511 }
5512 else if (working && (opt_type == -2 || opt_type == -1))
5513 ret = FAIL;
5514
5515 *option_end = c; /* put back for error messages */
5516 *arg = option_end;
5517
5518 return ret;
5519}
5520
5521/*
5522 * Allocate a variable for a string constant.
5523 * Return OK or FAIL.
5524 */
5525 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005526get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 int evaluate;
5530{
5531 char_u *p;
5532 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 int extra = 0;
5534
5535 /*
5536 * Find the end of the string, skipping backslashed characters.
5537 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005538 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 {
5540 if (*p == '\\' && p[1] != NUL)
5541 {
5542 ++p;
5543 /* A "\<x>" form occupies at least 4 characters, and produces up
5544 * to 6 characters: reserve space for 2 extra */
5545 if (*p == '<')
5546 extra += 2;
5547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 }
5549
5550 if (*p != '"')
5551 {
5552 EMSG2(_("E114: Missing quote: %s"), *arg);
5553 return FAIL;
5554 }
5555
5556 /* If only parsing, set *arg and return here */
5557 if (!evaluate)
5558 {
5559 *arg = p + 1;
5560 return OK;
5561 }
5562
5563 /*
5564 * Copy the string into allocated memory, handling backslashed
5565 * characters.
5566 */
5567 name = alloc((unsigned)(p - *arg + extra));
5568 if (name == NULL)
5569 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005570 rettv->v_type = VAR_STRING;
5571 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
Bram Moolenaar8c711452005-01-14 21:53:12 +00005573 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 {
5575 if (*p == '\\')
5576 {
5577 switch (*++p)
5578 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005579 case 'b': *name++ = BS; ++p; break;
5580 case 'e': *name++ = ESC; ++p; break;
5581 case 'f': *name++ = FF; ++p; break;
5582 case 'n': *name++ = NL; ++p; break;
5583 case 'r': *name++ = CAR; ++p; break;
5584 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585
5586 case 'X': /* hex: "\x1", "\x12" */
5587 case 'x':
5588 case 'u': /* Unicode: "\u0023" */
5589 case 'U':
5590 if (vim_isxdigit(p[1]))
5591 {
5592 int n, nr;
5593 int c = toupper(*p);
5594
5595 if (c == 'X')
5596 n = 2;
5597 else
5598 n = 4;
5599 nr = 0;
5600 while (--n >= 0 && vim_isxdigit(p[1]))
5601 {
5602 ++p;
5603 nr = (nr << 4) + hex2nr(*p);
5604 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005605 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606#ifdef FEAT_MBYTE
5607 /* For "\u" store the number according to
5608 * 'encoding'. */
5609 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005610 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 else
5612#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005613 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 break;
5616
5617 /* octal: "\1", "\12", "\123" */
5618 case '0':
5619 case '1':
5620 case '2':
5621 case '3':
5622 case '4':
5623 case '5':
5624 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005625 case '7': *name = *p++ - '0';
5626 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005628 *name = (*name << 3) + *p++ - '0';
5629 if (*p >= '0' && *p <= '7')
5630 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005632 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 break;
5634
5635 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005636 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 if (extra != 0)
5638 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005639 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 break;
5641 }
5642 /* FALLTHROUGH */
5643
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 break;
5646 }
5647 }
5648 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005649 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005652 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 *arg = p + 1;
5654
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 return OK;
5656}
5657
5658/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 * Return OK or FAIL.
5661 */
5662 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005663get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 int evaluate;
5667{
5668 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005669 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005670 int reduce = 0;
5671
5672 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005673 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005674 */
5675 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005678 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005680 break;
5681 ++reduce;
5682 ++p;
5683 }
5684 }
5685
Bram Moolenaar8c711452005-01-14 21:53:12 +00005686 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005687 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005688 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005689 return FAIL;
5690 }
5691
Bram Moolenaar8c711452005-01-14 21:53:12 +00005692 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005693 if (!evaluate)
5694 {
5695 *arg = p + 1;
5696 return OK;
5697 }
5698
5699 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005700 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005701 */
5702 str = alloc((unsigned)((p - *arg) - reduce));
5703 if (str == NULL)
5704 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005705 rettv->v_type = VAR_STRING;
5706 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005707
Bram Moolenaar8c711452005-01-14 21:53:12 +00005708 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005709 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005710 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005711 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005712 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005713 break;
5714 ++p;
5715 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005718 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005719 *arg = p + 1;
5720
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005721 return OK;
5722}
5723
5724/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005725 * Allocate a variable for a List and fill it from "*arg".
5726 * Return OK or FAIL.
5727 */
5728 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005729get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005730 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005731 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005732 int evaluate;
5733{
Bram Moolenaar33570922005-01-25 22:26:29 +00005734 list_T *l = NULL;
5735 typval_T tv;
5736 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005737
5738 if (evaluate)
5739 {
5740 l = list_alloc();
5741 if (l == NULL)
5742 return FAIL;
5743 }
5744
5745 *arg = skipwhite(*arg + 1);
5746 while (**arg != ']' && **arg != NUL)
5747 {
5748 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5749 goto failret;
5750 if (evaluate)
5751 {
5752 item = listitem_alloc();
5753 if (item != NULL)
5754 {
5755 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005756 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005757 list_append(l, item);
5758 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005759 else
5760 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005761 }
5762
5763 if (**arg == ']')
5764 break;
5765 if (**arg != ',')
5766 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005768 goto failret;
5769 }
5770 *arg = skipwhite(*arg + 1);
5771 }
5772
5773 if (**arg != ']')
5774 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005776failret:
5777 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005778 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779 return FAIL;
5780 }
5781
5782 *arg = skipwhite(*arg + 1);
5783 if (evaluate)
5784 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005785 rettv->v_type = VAR_LIST;
5786 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005787 ++l->lv_refcount;
5788 }
5789
5790 return OK;
5791}
5792
5793/*
5794 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005795 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005796 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005797 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005798list_alloc()
5799{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005800 list_T *l;
5801
5802 l = (list_T *)alloc_clear(sizeof(list_T));
5803 if (l != NULL)
5804 {
5805 /* Prepend the list to the list of lists for garbage collection. */
5806 if (first_list != NULL)
5807 first_list->lv_used_prev = l;
5808 l->lv_used_prev = NULL;
5809 l->lv_used_next = first_list;
5810 first_list = l;
5811 }
5812 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813}
5814
5815/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005816 * Allocate an empty list for a return value.
5817 * Returns OK or FAIL.
5818 */
5819 static int
5820rettv_list_alloc(rettv)
5821 typval_T *rettv;
5822{
5823 list_T *l = list_alloc();
5824
5825 if (l == NULL)
5826 return FAIL;
5827
5828 rettv->vval.v_list = l;
5829 rettv->v_type = VAR_LIST;
5830 ++l->lv_refcount;
5831 return OK;
5832}
5833
5834/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835 * Unreference a list: decrement the reference count and free it when it
5836 * becomes zero.
5837 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005838 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005839list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005840 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005841{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005842 if (l != NULL && --l->lv_refcount <= 0)
5843 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005844}
5845
5846/*
5847 * Free a list, including all items it points to.
5848 * Ignores the reference count.
5849 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005850 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005851list_free(l, recurse)
5852 list_T *l;
5853 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854{
Bram Moolenaar33570922005-01-25 22:26:29 +00005855 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005856
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005857 /* Remove the list from the list of lists for garbage collection. */
5858 if (l->lv_used_prev == NULL)
5859 first_list = l->lv_used_next;
5860 else
5861 l->lv_used_prev->lv_used_next = l->lv_used_next;
5862 if (l->lv_used_next != NULL)
5863 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5864
Bram Moolenaard9fba312005-06-26 22:34:35 +00005865 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005866 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005867 /* Remove the item before deleting it. */
5868 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005869 if (recurse || (item->li_tv.v_type != VAR_LIST
5870 && item->li_tv.v_type != VAR_DICT))
5871 clear_tv(&item->li_tv);
5872 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873 }
5874 vim_free(l);
5875}
5876
5877/*
5878 * Allocate a list item.
5879 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005880 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881listitem_alloc()
5882{
Bram Moolenaar33570922005-01-25 22:26:29 +00005883 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884}
5885
5886/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005887 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888 */
5889 static void
5890listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005891 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005893 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894 vim_free(item);
5895}
5896
5897/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005898 * Remove a list item from a List and free it. Also clears the value.
5899 */
5900 static void
5901listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005902 list_T *l;
5903 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005904{
5905 list_remove(l, item, item);
5906 listitem_free(item);
5907}
5908
5909/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910 * Get the number of items in a list.
5911 */
5912 static long
5913list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005914 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916 if (l == NULL)
5917 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005918 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919}
5920
5921/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005922 * Return TRUE when two lists have exactly the same values.
5923 */
5924 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005925list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 list_T *l1;
5927 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005928 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005929 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005930{
Bram Moolenaar33570922005-01-25 22:26:29 +00005931 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005932
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005933 if (l1 == NULL || l2 == NULL)
5934 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005935 if (l1 == l2)
5936 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005937 if (list_len(l1) != list_len(l2))
5938 return FALSE;
5939
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005940 for (item1 = l1->lv_first, item2 = l2->lv_first;
5941 item1 != NULL && item2 != NULL;
5942 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005943 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005944 return FALSE;
5945 return item1 == NULL && item2 == NULL;
5946}
5947
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005948#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5949 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005950/*
5951 * Return the dictitem that an entry in a hashtable points to.
5952 */
5953 dictitem_T *
5954dict_lookup(hi)
5955 hashitem_T *hi;
5956{
5957 return HI2DI(hi);
5958}
5959#endif
5960
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005961/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005962 * Return TRUE when two dictionaries have exactly the same key/values.
5963 */
5964 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005965dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005966 dict_T *d1;
5967 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005968 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005969 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005970{
Bram Moolenaar33570922005-01-25 22:26:29 +00005971 hashitem_T *hi;
5972 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005973 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005974
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005975 if (d1 == NULL || d2 == NULL)
5976 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005977 if (d1 == d2)
5978 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005979 if (dict_len(d1) != dict_len(d2))
5980 return FALSE;
5981
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005982 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005983 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005984 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005985 if (!HASHITEM_EMPTY(hi))
5986 {
5987 item2 = dict_find(d2, hi->hi_key, -1);
5988 if (item2 == NULL)
5989 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005990 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005991 return FALSE;
5992 --todo;
5993 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005994 }
5995 return TRUE;
5996}
5997
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005998static int tv_equal_recurse_limit;
5999
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006000/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006001 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006002 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006003 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006004 */
6005 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006006tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006007 typval_T *tv1;
6008 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006009 int ic; /* ignore case */
6010 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006011{
6012 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006013 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006014 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006015 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006016
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006017 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006018 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006019
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006020 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006021 * recursiveness to a limit. We guess they are equal then.
6022 * A fixed limit has the problem of still taking an awful long time.
6023 * Reduce the limit every time running into it. That should work fine for
6024 * deeply linked structures that are not recursively linked and catch
6025 * recursiveness quickly. */
6026 if (!recursive)
6027 tv_equal_recurse_limit = 1000;
6028 if (recursive_cnt >= tv_equal_recurse_limit)
6029 {
6030 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006031 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006032 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006033
6034 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006035 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006036 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006037 ++recursive_cnt;
6038 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6039 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006040 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006041
6042 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006043 ++recursive_cnt;
6044 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6045 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006046 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006047
6048 case VAR_FUNC:
6049 return (tv1->vval.v_string != NULL
6050 && tv2->vval.v_string != NULL
6051 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6052
6053 case VAR_NUMBER:
6054 return tv1->vval.v_number == tv2->vval.v_number;
6055
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006056#ifdef FEAT_FLOAT
6057 case VAR_FLOAT:
6058 return tv1->vval.v_float == tv2->vval.v_float;
6059#endif
6060
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006061 case VAR_STRING:
6062 s1 = get_tv_string_buf(tv1, buf1);
6063 s2 = get_tv_string_buf(tv2, buf2);
6064 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006065 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006066
6067 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006068 return TRUE;
6069}
6070
6071/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072 * Locate item with index "n" in list "l" and return it.
6073 * A negative index is counted from the end; -1 is the last item.
6074 * Returns NULL when "n" is out of range.
6075 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006076 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006078 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006079 long n;
6080{
Bram Moolenaar33570922005-01-25 22:26:29 +00006081 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006082 long idx;
6083
6084 if (l == NULL)
6085 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006086
6087 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006089 n = l->lv_len + n;
6090
6091 /* Check for index out of range. */
6092 if (n < 0 || n >= l->lv_len)
6093 return NULL;
6094
6095 /* When there is a cached index may start search from there. */
6096 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006098 if (n < l->lv_idx / 2)
6099 {
6100 /* closest to the start of the list */
6101 item = l->lv_first;
6102 idx = 0;
6103 }
6104 else if (n > (l->lv_idx + l->lv_len) / 2)
6105 {
6106 /* closest to the end of the list */
6107 item = l->lv_last;
6108 idx = l->lv_len - 1;
6109 }
6110 else
6111 {
6112 /* closest to the cached index */
6113 item = l->lv_idx_item;
6114 idx = l->lv_idx;
6115 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006116 }
6117 else
6118 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006119 if (n < l->lv_len / 2)
6120 {
6121 /* closest to the start of the list */
6122 item = l->lv_first;
6123 idx = 0;
6124 }
6125 else
6126 {
6127 /* closest to the end of the list */
6128 item = l->lv_last;
6129 idx = l->lv_len - 1;
6130 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006131 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006132
6133 while (n > idx)
6134 {
6135 /* search forward */
6136 item = item->li_next;
6137 ++idx;
6138 }
6139 while (n < idx)
6140 {
6141 /* search backward */
6142 item = item->li_prev;
6143 --idx;
6144 }
6145
6146 /* cache the used index */
6147 l->lv_idx = idx;
6148 l->lv_idx_item = item;
6149
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006150 return item;
6151}
6152
6153/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006154 * Get list item "l[idx]" as a number.
6155 */
6156 static long
6157list_find_nr(l, idx, errorp)
6158 list_T *l;
6159 long idx;
6160 int *errorp; /* set to TRUE when something wrong */
6161{
6162 listitem_T *li;
6163
6164 li = list_find(l, idx);
6165 if (li == NULL)
6166 {
6167 if (errorp != NULL)
6168 *errorp = TRUE;
6169 return -1L;
6170 }
6171 return get_tv_number_chk(&li->li_tv, errorp);
6172}
6173
6174/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006175 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6176 */
6177 char_u *
6178list_find_str(l, idx)
6179 list_T *l;
6180 long idx;
6181{
6182 listitem_T *li;
6183
6184 li = list_find(l, idx - 1);
6185 if (li == NULL)
6186 {
6187 EMSGN(_(e_listidx), idx);
6188 return NULL;
6189 }
6190 return get_tv_string(&li->li_tv);
6191}
6192
6193/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006194 * Locate "item" list "l" and return its index.
6195 * Returns -1 when "item" is not in the list.
6196 */
6197 static long
6198list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006199 list_T *l;
6200 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006201{
6202 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006203 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006204
6205 if (l == NULL)
6206 return -1;
6207 idx = 0;
6208 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6209 ++idx;
6210 if (li == NULL)
6211 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006212 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006213}
6214
6215/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006216 * Append item "item" to the end of list "l".
6217 */
6218 static void
6219list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006220 list_T *l;
6221 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006222{
6223 if (l->lv_last == NULL)
6224 {
6225 /* empty list */
6226 l->lv_first = item;
6227 l->lv_last = item;
6228 item->li_prev = NULL;
6229 }
6230 else
6231 {
6232 l->lv_last->li_next = item;
6233 item->li_prev = l->lv_last;
6234 l->lv_last = item;
6235 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006236 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006237 item->li_next = NULL;
6238}
6239
6240/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006241 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006242 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006243 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006244 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006245list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006246 list_T *l;
6247 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006248{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006249 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006250
Bram Moolenaar05159a02005-02-26 23:04:13 +00006251 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006252 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006253 copy_tv(tv, &li->li_tv);
6254 list_append(l, li);
6255 return OK;
6256}
6257
6258/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006259 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006260 * Return FAIL when out of memory.
6261 */
6262 int
6263list_append_dict(list, dict)
6264 list_T *list;
6265 dict_T *dict;
6266{
6267 listitem_T *li = listitem_alloc();
6268
6269 if (li == NULL)
6270 return FAIL;
6271 li->li_tv.v_type = VAR_DICT;
6272 li->li_tv.v_lock = 0;
6273 li->li_tv.vval.v_dict = dict;
6274 list_append(list, li);
6275 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006276 return OK;
6277}
6278
6279/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006280 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006281 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006282 * Returns FAIL when out of memory.
6283 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006284 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006285list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006286 list_T *l;
6287 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006288 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006289{
6290 listitem_T *li = listitem_alloc();
6291
6292 if (li == NULL)
6293 return FAIL;
6294 list_append(l, li);
6295 li->li_tv.v_type = VAR_STRING;
6296 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006297 if (str == NULL)
6298 li->li_tv.vval.v_string = NULL;
6299 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006300 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006301 return FAIL;
6302 return OK;
6303}
6304
6305/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006306 * Append "n" to list "l".
6307 * Returns FAIL when out of memory.
6308 */
6309 static int
6310list_append_number(l, n)
6311 list_T *l;
6312 varnumber_T n;
6313{
6314 listitem_T *li;
6315
6316 li = listitem_alloc();
6317 if (li == NULL)
6318 return FAIL;
6319 li->li_tv.v_type = VAR_NUMBER;
6320 li->li_tv.v_lock = 0;
6321 li->li_tv.vval.v_number = n;
6322 list_append(l, li);
6323 return OK;
6324}
6325
6326/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006328 * If "item" is NULL append at the end.
6329 * Return FAIL when out of memory.
6330 */
6331 static int
6332list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006333 list_T *l;
6334 typval_T *tv;
6335 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006336{
Bram Moolenaar33570922005-01-25 22:26:29 +00006337 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006338
6339 if (ni == NULL)
6340 return FAIL;
6341 copy_tv(tv, &ni->li_tv);
6342 if (item == NULL)
6343 /* Append new item at end of list. */
6344 list_append(l, ni);
6345 else
6346 {
6347 /* Insert new item before existing item. */
6348 ni->li_prev = item->li_prev;
6349 ni->li_next = item;
6350 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006351 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006352 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006353 ++l->lv_idx;
6354 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006355 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006356 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006357 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006358 l->lv_idx_item = NULL;
6359 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006360 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006361 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006362 }
6363 return OK;
6364}
6365
6366/*
6367 * Extend "l1" with "l2".
6368 * If "bef" is NULL append at the end, otherwise insert before this item.
6369 * Returns FAIL when out of memory.
6370 */
6371 static int
6372list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 list_T *l1;
6374 list_T *l2;
6375 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006376{
Bram Moolenaar33570922005-01-25 22:26:29 +00006377 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006378 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006379
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006380 /* We also quit the loop when we have inserted the original item count of
6381 * the list, avoid a hang when we extend a list with itself. */
6382 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006383 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6384 return FAIL;
6385 return OK;
6386}
6387
6388/*
6389 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6390 * Return FAIL when out of memory.
6391 */
6392 static int
6393list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 list_T *l1;
6395 list_T *l2;
6396 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006397{
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006399
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006400 if (l1 == NULL || l2 == NULL)
6401 return FAIL;
6402
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006404 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006405 if (l == NULL)
6406 return FAIL;
6407 tv->v_type = VAR_LIST;
6408 tv->vval.v_list = l;
6409
6410 /* append all items from the second list */
6411 return list_extend(l, l2, NULL);
6412}
6413
6414/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006415 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006417 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418 * Returns NULL when out of memory.
6419 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006420 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006421list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006424 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006425{
Bram Moolenaar33570922005-01-25 22:26:29 +00006426 list_T *copy;
6427 listitem_T *item;
6428 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006429
6430 if (orig == NULL)
6431 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006432
6433 copy = list_alloc();
6434 if (copy != NULL)
6435 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006436 if (copyID != 0)
6437 {
6438 /* Do this before adding the items, because one of the items may
6439 * refer back to this list. */
6440 orig->lv_copyID = copyID;
6441 orig->lv_copylist = copy;
6442 }
6443 for (item = orig->lv_first; item != NULL && !got_int;
6444 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006445 {
6446 ni = listitem_alloc();
6447 if (ni == NULL)
6448 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006449 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006450 {
6451 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6452 {
6453 vim_free(ni);
6454 break;
6455 }
6456 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006457 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006458 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006459 list_append(copy, ni);
6460 }
6461 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006462 if (item != NULL)
6463 {
6464 list_unref(copy);
6465 copy = NULL;
6466 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006467 }
6468
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006469 return copy;
6470}
6471
6472/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006473 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006474 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006475 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006476 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006477list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006478 list_T *l;
6479 listitem_T *item;
6480 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006481{
Bram Moolenaar33570922005-01-25 22:26:29 +00006482 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006483
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006484 /* notify watchers */
6485 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006486 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006487 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006488 list_fix_watch(l, ip);
6489 if (ip == item2)
6490 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006491 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006492
6493 if (item2->li_next == NULL)
6494 l->lv_last = item->li_prev;
6495 else
6496 item2->li_next->li_prev = item->li_prev;
6497 if (item->li_prev == NULL)
6498 l->lv_first = item2->li_next;
6499 else
6500 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006501 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006502}
6503
6504/*
6505 * Return an allocated string with the string representation of a list.
6506 * May return NULL.
6507 */
6508 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006509list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006510 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006511 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006512{
6513 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006514
6515 if (tv->vval.v_list == NULL)
6516 return NULL;
6517 ga_init2(&ga, (int)sizeof(char), 80);
6518 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006519 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006520 {
6521 vim_free(ga.ga_data);
6522 return NULL;
6523 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524 ga_append(&ga, ']');
6525 ga_append(&ga, NUL);
6526 return (char_u *)ga.ga_data;
6527}
6528
6529/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006530 * Join list "l" into a string in "*gap", using separator "sep".
6531 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006532 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006533 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006534 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006535list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006536 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006537 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006538 char_u *sep;
6539 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006540 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006541{
6542 int first = TRUE;
6543 char_u *tofree;
6544 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006545 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006546 char_u *s;
6547
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006548 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006549 {
6550 if (first)
6551 first = FALSE;
6552 else
6553 ga_concat(gap, sep);
6554
6555 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006556 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006557 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006558 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006559 if (s != NULL)
6560 ga_concat(gap, s);
6561 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006562 if (s == NULL)
6563 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006564 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006565 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006566 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006567}
6568
6569/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006570 * Garbage collection for lists and dictionaries.
6571 *
6572 * We use reference counts to be able to free most items right away when they
6573 * are no longer used. But for composite items it's possible that it becomes
6574 * unused while the reference count is > 0: When there is a recursive
6575 * reference. Example:
6576 * :let l = [1, 2, 3]
6577 * :let d = {9: l}
6578 * :let l[1] = d
6579 *
6580 * Since this is quite unusual we handle this with garbage collection: every
6581 * once in a while find out which lists and dicts are not referenced from any
6582 * variable.
6583 *
6584 * Here is a good reference text about garbage collection (refers to Python
6585 * but it applies to all reference-counting mechanisms):
6586 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006587 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006588
6589/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006590 * Do garbage collection for lists and dicts.
6591 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006592 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006593 int
6594garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006595{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006596 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006597 buf_T *buf;
6598 win_T *wp;
6599 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006600 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006601 int did_free;
6602 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006603#ifdef FEAT_WINDOWS
6604 tabpage_T *tp;
6605#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006606
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006607 /* Only do this once. */
6608 want_garbage_collect = FALSE;
6609 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006610 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006611
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006612 /* We advance by two because we add one for items referenced through
6613 * previous_funccal. */
6614 current_copyID += COPYID_INC;
6615 copyID = current_copyID;
6616
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006617 /*
6618 * 1. Go through all accessible variables and mark all lists and dicts
6619 * with copyID.
6620 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006621
6622 /* Don't free variables in the previous_funccal list unless they are only
6623 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006624 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006625 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6626 {
6627 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6628 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6629 }
6630
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006631 /* script-local variables */
6632 for (i = 1; i <= ga_scripts.ga_len; ++i)
6633 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6634
6635 /* buffer-local variables */
6636 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6637 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6638
6639 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006640 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006641 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6642
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006643#ifdef FEAT_WINDOWS
6644 /* tabpage-local variables */
6645 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6646 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6647#endif
6648
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006649 /* global variables */
6650 set_ref_in_ht(&globvarht, copyID);
6651
6652 /* function-local variables */
6653 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6654 {
6655 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6656 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6657 }
6658
Bram Moolenaard812df62008-11-09 12:46:09 +00006659 /* v: vars */
6660 set_ref_in_ht(&vimvarht, copyID);
6661
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006662 /*
6663 * 2. Free lists and dictionaries that are not referenced.
6664 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006665 did_free = free_unref_items(copyID);
6666
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006667 /*
6668 * 3. Check if any funccal can be freed now.
6669 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006670 for (pfc = &previous_funccal; *pfc != NULL; )
6671 {
6672 if (can_free_funccal(*pfc, copyID))
6673 {
6674 fc = *pfc;
6675 *pfc = fc->caller;
6676 free_funccal(fc, TRUE);
6677 did_free = TRUE;
6678 did_free_funccal = TRUE;
6679 }
6680 else
6681 pfc = &(*pfc)->caller;
6682 }
6683 if (did_free_funccal)
6684 /* When a funccal was freed some more items might be garbage
6685 * collected, so run again. */
6686 (void)garbage_collect();
6687
6688 return did_free;
6689}
6690
6691/*
6692 * Free lists and dictionaries that are no longer referenced.
6693 */
6694 static int
6695free_unref_items(copyID)
6696 int copyID;
6697{
6698 dict_T *dd;
6699 list_T *ll;
6700 int did_free = FALSE;
6701
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006702 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006703 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006704 */
6705 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006706 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006707 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006708 /* Free the Dictionary and ordinary items it contains, but don't
6709 * recurse into Lists and Dictionaries, they will be in the list
6710 * of dicts or list of lists. */
6711 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006712 did_free = TRUE;
6713
6714 /* restart, next dict may also have been freed */
6715 dd = first_dict;
6716 }
6717 else
6718 dd = dd->dv_used_next;
6719
6720 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006721 * Go through the list of lists and free items without the copyID.
6722 * But don't free a list that has a watcher (used in a for loop), these
6723 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006724 */
6725 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006726 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6727 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006728 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006729 /* Free the List and ordinary items it contains, but don't recurse
6730 * into Lists and Dictionaries, they will be in the list of dicts
6731 * or list of lists. */
6732 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006733 did_free = TRUE;
6734
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006735 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006736 ll = first_list;
6737 }
6738 else
6739 ll = ll->lv_used_next;
6740
6741 return did_free;
6742}
6743
6744/*
6745 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6746 */
6747 static void
6748set_ref_in_ht(ht, copyID)
6749 hashtab_T *ht;
6750 int copyID;
6751{
6752 int todo;
6753 hashitem_T *hi;
6754
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006755 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006756 for (hi = ht->ht_array; todo > 0; ++hi)
6757 if (!HASHITEM_EMPTY(hi))
6758 {
6759 --todo;
6760 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6761 }
6762}
6763
6764/*
6765 * Mark all lists and dicts referenced through list "l" with "copyID".
6766 */
6767 static void
6768set_ref_in_list(l, copyID)
6769 list_T *l;
6770 int copyID;
6771{
6772 listitem_T *li;
6773
6774 for (li = l->lv_first; li != NULL; li = li->li_next)
6775 set_ref_in_item(&li->li_tv, copyID);
6776}
6777
6778/*
6779 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6780 */
6781 static void
6782set_ref_in_item(tv, copyID)
6783 typval_T *tv;
6784 int copyID;
6785{
6786 dict_T *dd;
6787 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006788
6789 switch (tv->v_type)
6790 {
6791 case VAR_DICT:
6792 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006793 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006794 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006795 /* Didn't see this dict yet. */
6796 dd->dv_copyID = copyID;
6797 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006798 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006799 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006800
6801 case VAR_LIST:
6802 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006803 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006804 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006805 /* Didn't see this list yet. */
6806 ll->lv_copyID = copyID;
6807 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006808 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006809 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006810 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006811 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006812}
6813
6814/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006815 * Allocate an empty header for a dictionary.
6816 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006817 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006818dict_alloc()
6819{
Bram Moolenaar33570922005-01-25 22:26:29 +00006820 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006821
Bram Moolenaar33570922005-01-25 22:26:29 +00006822 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006823 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006824 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006825 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006826 if (first_dict != NULL)
6827 first_dict->dv_used_prev = d;
6828 d->dv_used_next = first_dict;
6829 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006830 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006831
Bram Moolenaar33570922005-01-25 22:26:29 +00006832 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006833 d->dv_lock = 0;
6834 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006835 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006836 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006837 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006838}
6839
6840/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006841 * Allocate an empty dict for a return value.
6842 * Returns OK or FAIL.
6843 */
6844 static int
6845rettv_dict_alloc(rettv)
6846 typval_T *rettv;
6847{
6848 dict_T *d = dict_alloc();
6849
6850 if (d == NULL)
6851 return FAIL;
6852
6853 rettv->vval.v_dict = d;
6854 rettv->v_type = VAR_DICT;
6855 ++d->dv_refcount;
6856 return OK;
6857}
6858
6859
6860/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006861 * Unreference a Dictionary: decrement the reference count and free it when it
6862 * becomes zero.
6863 */
6864 static void
6865dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006866 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006867{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006868 if (d != NULL && --d->dv_refcount <= 0)
6869 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006870}
6871
6872/*
6873 * Free a Dictionary, including all items it contains.
6874 * Ignores the reference count.
6875 */
6876 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006877dict_free(d, recurse)
6878 dict_T *d;
6879 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006880{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006881 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006882 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006883 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006884
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885 /* Remove the dict from the list of dicts for garbage collection. */
6886 if (d->dv_used_prev == NULL)
6887 first_dict = d->dv_used_next;
6888 else
6889 d->dv_used_prev->dv_used_next = d->dv_used_next;
6890 if (d->dv_used_next != NULL)
6891 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6892
6893 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006894 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006895 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006896 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006897 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006898 if (!HASHITEM_EMPTY(hi))
6899 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006900 /* Remove the item before deleting it, just in case there is
6901 * something recursive causing trouble. */
6902 di = HI2DI(hi);
6903 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006904 if (recurse || (di->di_tv.v_type != VAR_LIST
6905 && di->di_tv.v_type != VAR_DICT))
6906 clear_tv(&di->di_tv);
6907 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006908 --todo;
6909 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006910 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006911 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006912 vim_free(d);
6913}
6914
6915/*
6916 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006917 * The "key" is copied to the new item.
6918 * Note that the value of the item "di_tv" still needs to be initialized!
6919 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006920 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006921 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006922dictitem_alloc(key)
6923 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006924{
Bram Moolenaar33570922005-01-25 22:26:29 +00006925 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006926
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006927 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006928 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006929 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006930 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006931 di->di_flags = 0;
6932 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006933 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006934}
6935
6936/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006937 * Make a copy of a Dictionary item.
6938 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006939 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006940dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006941 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006942{
Bram Moolenaar33570922005-01-25 22:26:29 +00006943 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006944
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006945 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6946 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006947 if (di != NULL)
6948 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006949 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006950 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006951 copy_tv(&org->di_tv, &di->di_tv);
6952 }
6953 return di;
6954}
6955
6956/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006957 * Remove item "item" from Dictionary "dict" and free it.
6958 */
6959 static void
6960dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006961 dict_T *dict;
6962 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006963{
Bram Moolenaar33570922005-01-25 22:26:29 +00006964 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006965
Bram Moolenaar33570922005-01-25 22:26:29 +00006966 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006967 if (HASHITEM_EMPTY(hi))
6968 EMSG2(_(e_intern2), "dictitem_remove()");
6969 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006970 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006971 dictitem_free(item);
6972}
6973
6974/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006975 * Free a dict item. Also clears the value.
6976 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006977 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006978dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006979 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006980{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006981 clear_tv(&item->di_tv);
6982 vim_free(item);
6983}
6984
6985/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006986 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6987 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006988 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006989 * Returns NULL when out of memory.
6990 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006991 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006992dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006993 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006994 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006995 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006996{
Bram Moolenaar33570922005-01-25 22:26:29 +00006997 dict_T *copy;
6998 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006999 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007000 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007001
7002 if (orig == NULL)
7003 return NULL;
7004
7005 copy = dict_alloc();
7006 if (copy != NULL)
7007 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007008 if (copyID != 0)
7009 {
7010 orig->dv_copyID = copyID;
7011 orig->dv_copydict = copy;
7012 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007013 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007014 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007015 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007016 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007017 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007018 --todo;
7019
7020 di = dictitem_alloc(hi->hi_key);
7021 if (di == NULL)
7022 break;
7023 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007024 {
7025 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7026 copyID) == FAIL)
7027 {
7028 vim_free(di);
7029 break;
7030 }
7031 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007032 else
7033 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7034 if (dict_add(copy, di) == FAIL)
7035 {
7036 dictitem_free(di);
7037 break;
7038 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007039 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007040 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007041
Bram Moolenaare9a41262005-01-15 22:18:47 +00007042 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007043 if (todo > 0)
7044 {
7045 dict_unref(copy);
7046 copy = NULL;
7047 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007048 }
7049
7050 return copy;
7051}
7052
7053/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007054 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007055 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007056 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007057 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007058dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007059 dict_T *d;
7060 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007061{
Bram Moolenaar33570922005-01-25 22:26:29 +00007062 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007063}
7064
Bram Moolenaar8c711452005-01-14 21:53:12 +00007065/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007066 * Add a number or string entry to dictionary "d".
7067 * When "str" is NULL use number "nr", otherwise use "str".
7068 * Returns FAIL when out of memory and when key already exists.
7069 */
7070 int
7071dict_add_nr_str(d, key, nr, str)
7072 dict_T *d;
7073 char *key;
7074 long nr;
7075 char_u *str;
7076{
7077 dictitem_T *item;
7078
7079 item = dictitem_alloc((char_u *)key);
7080 if (item == NULL)
7081 return FAIL;
7082 item->di_tv.v_lock = 0;
7083 if (str == NULL)
7084 {
7085 item->di_tv.v_type = VAR_NUMBER;
7086 item->di_tv.vval.v_number = nr;
7087 }
7088 else
7089 {
7090 item->di_tv.v_type = VAR_STRING;
7091 item->di_tv.vval.v_string = vim_strsave(str);
7092 }
7093 if (dict_add(d, item) == FAIL)
7094 {
7095 dictitem_free(item);
7096 return FAIL;
7097 }
7098 return OK;
7099}
7100
7101/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007102 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007103 * Returns FAIL when out of memory and when key already exists.
7104 */
7105 int
7106dict_add_list(d, key, list)
7107 dict_T *d;
7108 char *key;
7109 list_T *list;
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 item->di_tv.v_type = VAR_LIST;
7118 item->di_tv.vval.v_list = list;
7119 if (dict_add(d, item) == FAIL)
7120 {
7121 dictitem_free(item);
7122 return FAIL;
7123 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007124 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007125 return OK;
7126}
7127
7128/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007129 * Get the number of items in a Dictionary.
7130 */
7131 static long
7132dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007133 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007134{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007135 if (d == NULL)
7136 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007137 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007138}
7139
7140/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007141 * Find item "key[len]" in Dictionary "d".
7142 * If "len" is negative use strlen(key).
7143 * Returns NULL when not found.
7144 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007145 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007146dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007147 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007148 char_u *key;
7149 int len;
7150{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007151#define AKEYLEN 200
7152 char_u buf[AKEYLEN];
7153 char_u *akey;
7154 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007155 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007156
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007157 if (len < 0)
7158 akey = key;
7159 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007160 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007161 tofree = akey = vim_strnsave(key, len);
7162 if (akey == NULL)
7163 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007164 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007165 else
7166 {
7167 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007168 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007169 akey = buf;
7170 }
7171
Bram Moolenaar33570922005-01-25 22:26:29 +00007172 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007173 vim_free(tofree);
7174 if (HASHITEM_EMPTY(hi))
7175 return NULL;
7176 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007177}
7178
7179/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007180 * Get a string item from a dictionary.
7181 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007182 * Returns NULL if the entry doesn't exist or out of memory.
7183 */
7184 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007185get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007186 dict_T *d;
7187 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007188 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007189{
7190 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007191 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007192
7193 di = dict_find(d, key, -1);
7194 if (di == NULL)
7195 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007196 s = get_tv_string(&di->di_tv);
7197 if (save && s != NULL)
7198 s = vim_strsave(s);
7199 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007200}
7201
7202/*
7203 * Get a number item from a dictionary.
7204 * Returns 0 if the entry doesn't exist or out of memory.
7205 */
7206 long
7207get_dict_number(d, key)
7208 dict_T *d;
7209 char_u *key;
7210{
7211 dictitem_T *di;
7212
7213 di = dict_find(d, key, -1);
7214 if (di == NULL)
7215 return 0;
7216 return get_tv_number(&di->di_tv);
7217}
7218
7219/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220 * Return an allocated string with the string representation of a Dictionary.
7221 * May return NULL.
7222 */
7223 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007224dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007225 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007226 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227{
7228 garray_T ga;
7229 int first = TRUE;
7230 char_u *tofree;
7231 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007232 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007233 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007234 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007235 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007236
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007237 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238 return NULL;
7239 ga_init2(&ga, (int)sizeof(char), 80);
7240 ga_append(&ga, '{');
7241
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007242 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007243 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007245 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007247 --todo;
7248
7249 if (first)
7250 first = FALSE;
7251 else
7252 ga_concat(&ga, (char_u *)", ");
7253
7254 tofree = string_quote(hi->hi_key, FALSE);
7255 if (tofree != NULL)
7256 {
7257 ga_concat(&ga, tofree);
7258 vim_free(tofree);
7259 }
7260 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007261 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007262 if (s != NULL)
7263 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007264 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007265 if (s == NULL)
7266 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007269 if (todo > 0)
7270 {
7271 vim_free(ga.ga_data);
7272 return NULL;
7273 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007274
7275 ga_append(&ga, '}');
7276 ga_append(&ga, NUL);
7277 return (char_u *)ga.ga_data;
7278}
7279
7280/*
7281 * Allocate a variable for a Dictionary and fill it from "*arg".
7282 * Return OK or FAIL. Returns NOTDONE for {expr}.
7283 */
7284 static int
7285get_dict_tv(arg, rettv, evaluate)
7286 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007287 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288 int evaluate;
7289{
Bram Moolenaar33570922005-01-25 22:26:29 +00007290 dict_T *d = NULL;
7291 typval_T tvkey;
7292 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007293 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007294 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007295 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007296 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007297
7298 /*
7299 * First check if it's not a curly-braces thing: {expr}.
7300 * Must do this without evaluating, otherwise a function may be called
7301 * twice. Unfortunately this means we need to call eval1() twice for the
7302 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007303 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007305 if (*start != '}')
7306 {
7307 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7308 return FAIL;
7309 if (*start == '}')
7310 return NOTDONE;
7311 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007312
7313 if (evaluate)
7314 {
7315 d = dict_alloc();
7316 if (d == NULL)
7317 return FAIL;
7318 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319 tvkey.v_type = VAR_UNKNOWN;
7320 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007321
7322 *arg = skipwhite(*arg + 1);
7323 while (**arg != '}' && **arg != NUL)
7324 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007326 goto failret;
7327 if (**arg != ':')
7328 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007329 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331 goto failret;
7332 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007333 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007334 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007335 key = get_tv_string_buf_chk(&tvkey, buf);
7336 if (key == NULL || *key == NUL)
7337 {
7338 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7339 if (key != NULL)
7340 EMSG(_(e_emptykey));
7341 clear_tv(&tvkey);
7342 goto failret;
7343 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007344 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345
7346 *arg = skipwhite(*arg + 1);
7347 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7348 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007349 if (evaluate)
7350 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007351 goto failret;
7352 }
7353 if (evaluate)
7354 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007355 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007356 if (item != NULL)
7357 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007358 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007359 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007360 clear_tv(&tv);
7361 goto failret;
7362 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 item = dictitem_alloc(key);
7364 clear_tv(&tvkey);
7365 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007366 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007367 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007368 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007369 if (dict_add(d, item) == FAIL)
7370 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007371 }
7372 }
7373
7374 if (**arg == '}')
7375 break;
7376 if (**arg != ',')
7377 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007378 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007379 goto failret;
7380 }
7381 *arg = skipwhite(*arg + 1);
7382 }
7383
7384 if (**arg != '}')
7385 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007386 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007387failret:
7388 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007389 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390 return FAIL;
7391 }
7392
7393 *arg = skipwhite(*arg + 1);
7394 if (evaluate)
7395 {
7396 rettv->v_type = VAR_DICT;
7397 rettv->vval.v_dict = d;
7398 ++d->dv_refcount;
7399 }
7400
7401 return OK;
7402}
7403
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007405 * Return a string with the string representation of a variable.
7406 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007407 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007408 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007409 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007410 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007411 */
7412 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007413echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007414 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007415 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007416 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007417 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007418{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007419 static int recurse = 0;
7420 char_u *r = NULL;
7421
Bram Moolenaar33570922005-01-25 22:26:29 +00007422 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007423 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007424 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007425 *tofree = NULL;
7426 return NULL;
7427 }
7428 ++recurse;
7429
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007430 switch (tv->v_type)
7431 {
7432 case VAR_FUNC:
7433 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007434 r = tv->vval.v_string;
7435 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007436
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007437 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007438 if (tv->vval.v_list == NULL)
7439 {
7440 *tofree = NULL;
7441 r = NULL;
7442 }
7443 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7444 {
7445 *tofree = NULL;
7446 r = (char_u *)"[...]";
7447 }
7448 else
7449 {
7450 tv->vval.v_list->lv_copyID = copyID;
7451 *tofree = list2string(tv, copyID);
7452 r = *tofree;
7453 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007454 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007455
Bram Moolenaar8c711452005-01-14 21:53:12 +00007456 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007457 if (tv->vval.v_dict == NULL)
7458 {
7459 *tofree = NULL;
7460 r = NULL;
7461 }
7462 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7463 {
7464 *tofree = NULL;
7465 r = (char_u *)"{...}";
7466 }
7467 else
7468 {
7469 tv->vval.v_dict->dv_copyID = copyID;
7470 *tofree = dict2string(tv, copyID);
7471 r = *tofree;
7472 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007473 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007474
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007475 case VAR_STRING:
7476 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007477 *tofree = NULL;
7478 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007479 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007480
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007481#ifdef FEAT_FLOAT
7482 case VAR_FLOAT:
7483 *tofree = NULL;
7484 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7485 r = numbuf;
7486 break;
7487#endif
7488
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007489 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007490 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007491 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007492 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007493
7494 --recurse;
7495 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007496}
7497
7498/*
7499 * Return a string with the string representation of a variable.
7500 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7501 * "numbuf" is used for a number.
7502 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007503 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007504 */
7505 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007506tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007507 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007508 char_u **tofree;
7509 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007510 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007511{
7512 switch (tv->v_type)
7513 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007514 case VAR_FUNC:
7515 *tofree = string_quote(tv->vval.v_string, TRUE);
7516 return *tofree;
7517 case VAR_STRING:
7518 *tofree = string_quote(tv->vval.v_string, FALSE);
7519 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007520#ifdef FEAT_FLOAT
7521 case VAR_FLOAT:
7522 *tofree = NULL;
7523 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7524 return numbuf;
7525#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007526 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007527 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007529 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007530 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007531 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007532 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007533 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007534}
7535
7536/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007537 * Return string "str" in ' quotes, doubling ' characters.
7538 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007540 */
7541 static char_u *
7542string_quote(str, function)
7543 char_u *str;
7544 int function;
7545{
Bram Moolenaar33570922005-01-25 22:26:29 +00007546 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007547 char_u *p, *r, *s;
7548
Bram Moolenaar33570922005-01-25 22:26:29 +00007549 len = (function ? 13 : 3);
7550 if (str != NULL)
7551 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007552 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007553 for (p = str; *p != NUL; mb_ptr_adv(p))
7554 if (*p == '\'')
7555 ++len;
7556 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007557 s = r = alloc(len);
7558 if (r != NULL)
7559 {
7560 if (function)
7561 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007563 r += 10;
7564 }
7565 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007566 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007567 if (str != NULL)
7568 for (p = str; *p != NUL; )
7569 {
7570 if (*p == '\'')
7571 *r++ = '\'';
7572 MB_COPY_CHAR(p, r);
7573 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007575 if (function)
7576 *r++ = ')';
7577 *r++ = NUL;
7578 }
7579 return s;
7580}
7581
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007582#ifdef FEAT_FLOAT
7583/*
7584 * Convert the string "text" to a floating point number.
7585 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7586 * this always uses a decimal point.
7587 * Returns the length of the text that was consumed.
7588 */
7589 static int
7590string2float(text, value)
7591 char_u *text;
7592 float_T *value; /* result stored here */
7593{
7594 char *s = (char *)text;
7595 float_T f;
7596
7597 f = strtod(s, &s);
7598 *value = f;
7599 return (int)((char_u *)s - text);
7600}
7601#endif
7602
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 * Get the value of an environment variable.
7605 * "arg" is pointing to the '$'. It is advanced to after the name.
7606 * If the environment variable was not set, silently assume it is empty.
7607 * Always return OK.
7608 */
7609 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007610get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 int evaluate;
7614{
7615 char_u *string = NULL;
7616 int len;
7617 int cc;
7618 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007619 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620
7621 ++*arg;
7622 name = *arg;
7623 len = get_env_len(arg);
7624 if (evaluate)
7625 {
7626 if (len != 0)
7627 {
7628 cc = name[len];
7629 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007630 /* first try vim_getenv(), fast for normal environment vars */
7631 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007633 {
7634 if (!mustfree)
7635 string = vim_strsave(string);
7636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 else
7638 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007639 if (mustfree)
7640 vim_free(string);
7641
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 /* next try expanding things like $VIM and ${HOME} */
7643 string = expand_env_save(name - 1);
7644 if (string != NULL && *string == '$')
7645 {
7646 vim_free(string);
7647 string = NULL;
7648 }
7649 }
7650 name[len] = cc;
7651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007652 rettv->v_type = VAR_STRING;
7653 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 }
7655
7656 return OK;
7657}
7658
7659/*
7660 * Array with names and number of arguments of all internal functions
7661 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7662 */
7663static struct fst
7664{
7665 char *f_name; /* function name */
7666 char f_min_argc; /* minimal number of arguments */
7667 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007668 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007669 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670} functions[] =
7671{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007672#ifdef FEAT_FLOAT
7673 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007674 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007675#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007676 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 {"append", 2, 2, f_append},
7678 {"argc", 0, 0, f_argc},
7679 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007680 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007681#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007682 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007683 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007684 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007687 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 {"bufexists", 1, 1, f_bufexists},
7689 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7690 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7691 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7692 {"buflisted", 1, 1, f_buflisted},
7693 {"bufloaded", 1, 1, f_bufloaded},
7694 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007695 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 {"bufwinnr", 1, 1, f_bufwinnr},
7697 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007698 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007699 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007700#ifdef FEAT_FLOAT
7701 {"ceil", 1, 1, f_ceil},
7702#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007703 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 {"char2nr", 1, 1, f_char2nr},
7705 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007706 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007708#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007709 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007710 {"complete_add", 1, 1, f_complete_add},
7711 {"complete_check", 0, 0, f_complete_check},
7712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007714 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007715#ifdef FEAT_FLOAT
7716 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007717 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007718#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007719 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007721 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007722 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 {"delete", 1, 1, f_delete},
7724 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007725 {"diff_filler", 1, 1, f_diff_filler},
7726 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007727 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007729 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 {"eventhandler", 0, 0, f_eventhandler},
7731 {"executable", 1, 1, f_executable},
7732 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007733#ifdef FEAT_FLOAT
7734 {"exp", 1, 1, f_exp},
7735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007737 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007738 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7740 {"filereadable", 1, 1, f_filereadable},
7741 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007742 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007743 {"finddir", 1, 3, f_finddir},
7744 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007745#ifdef FEAT_FLOAT
7746 {"float2nr", 1, 1, f_float2nr},
7747 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007748 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007749#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007750 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 {"fnamemodify", 2, 2, f_fnamemodify},
7752 {"foldclosed", 1, 1, f_foldclosed},
7753 {"foldclosedend", 1, 1, f_foldclosedend},
7754 {"foldlevel", 1, 1, f_foldlevel},
7755 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007756 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007758 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007759 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007760 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007761 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 {"getbufvar", 2, 2, f_getbufvar},
7763 {"getchar", 0, 1, f_getchar},
7764 {"getcharmod", 0, 0, f_getcharmod},
7765 {"getcmdline", 0, 0, f_getcmdline},
7766 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007767 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007769 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007770 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 {"getfsize", 1, 1, f_getfsize},
7772 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007773 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007774 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007775 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007776 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007777 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007778 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007779 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007780 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007782 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007783 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 {"getwinposx", 0, 0, f_getwinposx},
7785 {"getwinposy", 0, 0, f_getwinposy},
7786 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007787 {"glob", 1, 2, f_glob},
7788 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007790 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007791 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007792 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7794 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7795 {"histadd", 2, 2, f_histadd},
7796 {"histdel", 1, 2, f_histdel},
7797 {"histget", 1, 2, f_histget},
7798 {"histnr", 1, 1, f_histnr},
7799 {"hlID", 1, 1, f_hlID},
7800 {"hlexists", 1, 1, f_hlexists},
7801 {"hostname", 0, 0, f_hostname},
7802 {"iconv", 3, 3, f_iconv},
7803 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007804 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007805 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007807 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 {"inputrestore", 0, 0, f_inputrestore},
7809 {"inputsave", 0, 0, f_inputsave},
7810 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007811 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007813 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007814 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007815 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007816 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007818 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 {"libcall", 3, 3, f_libcall},
7820 {"libcallnr", 3, 3, f_libcallnr},
7821 {"line", 1, 1, f_line},
7822 {"line2byte", 1, 1, f_line2byte},
7823 {"lispindent", 1, 1, f_lispindent},
7824 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007825#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007826 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007827 {"log10", 1, 1, f_log10},
7828#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007829 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007830 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007831 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007832 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007833 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007834 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007835 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007836 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007837 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007838 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007839 {"max", 1, 1, f_max},
7840 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007841#ifdef vim_mkdir
7842 {"mkdir", 1, 3, f_mkdir},
7843#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007844 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007845#ifdef FEAT_MZSCHEME
7846 {"mzeval", 1, 1, f_mzeval},
7847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 {"nextnonblank", 1, 1, f_nextnonblank},
7849 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007850 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007851#ifdef FEAT_FLOAT
7852 {"pow", 2, 2, f_pow},
7853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007855 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007856 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007857 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007858 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007859 {"reltime", 0, 2, f_reltime},
7860 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 {"remote_expr", 2, 3, f_remote_expr},
7862 {"remote_foreground", 1, 1, f_remote_foreground},
7863 {"remote_peek", 1, 2, f_remote_peek},
7864 {"remote_read", 1, 1, f_remote_read},
7865 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007866 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007868 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007870 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007871#ifdef FEAT_FLOAT
7872 {"round", 1, 1, f_round},
7873#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007874 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007875 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007876 {"searchpair", 3, 7, f_searchpair},
7877 {"searchpairpos", 3, 7, f_searchpairpos},
7878 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 {"server2client", 2, 2, f_server2client},
7880 {"serverlist", 0, 0, f_serverlist},
7881 {"setbufvar", 3, 3, f_setbufvar},
7882 {"setcmdpos", 1, 1, f_setcmdpos},
7883 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007884 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007885 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007886 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007887 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007889 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007890 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007892 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007894#ifdef FEAT_FLOAT
7895 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007896 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007897#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007898 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007899 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007900 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007901 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007902 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007903#ifdef FEAT_FLOAT
7904 {"sqrt", 1, 1, f_sqrt},
7905 {"str2float", 1, 1, f_str2float},
7906#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007907 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007908 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007909 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910#ifdef HAVE_STRFTIME
7911 {"strftime", 1, 2, f_strftime},
7912#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007913 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007914 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 {"strlen", 1, 1, f_strlen},
7916 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007917 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007919 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 {"submatch", 1, 1, f_submatch},
7921 {"substitute", 4, 4, f_substitute},
7922 {"synID", 3, 3, f_synID},
7923 {"synIDattr", 2, 3, f_synIDattr},
7924 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007925 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007926 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007927 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007928 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007929 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007930 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007931 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007932 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007933#ifdef FEAT_FLOAT
7934 {"tan", 1, 1, f_tan},
7935 {"tanh", 1, 1, f_tanh},
7936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007938 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {"tolower", 1, 1, f_tolower},
7940 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007941 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007942#ifdef FEAT_FLOAT
7943 {"trunc", 1, 1, f_trunc},
7944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007946 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007947 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007948 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {"virtcol", 1, 1, f_virtcol},
7950 {"visualmode", 0, 1, f_visualmode},
7951 {"winbufnr", 1, 1, f_winbufnr},
7952 {"wincol", 0, 0, f_wincol},
7953 {"winheight", 1, 1, f_winheight},
7954 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007955 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007957 {"winrestview", 1, 1, f_winrestview},
7958 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007960 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961};
7962
7963#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7964
7965/*
7966 * Function given to ExpandGeneric() to obtain the list of internal
7967 * or user defined function names.
7968 */
7969 char_u *
7970get_function_name(xp, idx)
7971 expand_T *xp;
7972 int idx;
7973{
7974 static int intidx = -1;
7975 char_u *name;
7976
7977 if (idx == 0)
7978 intidx = -1;
7979 if (intidx < 0)
7980 {
7981 name = get_user_func_name(xp, idx);
7982 if (name != NULL)
7983 return name;
7984 }
7985 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7986 {
7987 STRCPY(IObuff, functions[intidx].f_name);
7988 STRCAT(IObuff, "(");
7989 if (functions[intidx].f_max_argc == 0)
7990 STRCAT(IObuff, ")");
7991 return IObuff;
7992 }
7993
7994 return NULL;
7995}
7996
7997/*
7998 * Function given to ExpandGeneric() to obtain the list of internal or
7999 * user defined variable or function names.
8000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 char_u *
8002get_expr_name(xp, idx)
8003 expand_T *xp;
8004 int idx;
8005{
8006 static int intidx = -1;
8007 char_u *name;
8008
8009 if (idx == 0)
8010 intidx = -1;
8011 if (intidx < 0)
8012 {
8013 name = get_function_name(xp, idx);
8014 if (name != NULL)
8015 return name;
8016 }
8017 return get_user_var_name(xp, ++intidx);
8018}
8019
8020#endif /* FEAT_CMDL_COMPL */
8021
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008022#if defined(EBCDIC) || defined(PROTO)
8023/*
8024 * Compare struct fst by function name.
8025 */
8026 static int
8027compare_func_name(s1, s2)
8028 const void *s1;
8029 const void *s2;
8030{
8031 struct fst *p1 = (struct fst *)s1;
8032 struct fst *p2 = (struct fst *)s2;
8033
8034 return STRCMP(p1->f_name, p2->f_name);
8035}
8036
8037/*
8038 * Sort the function table by function name.
8039 * The sorting of the table above is ASCII dependant.
8040 * On machines using EBCDIC we have to sort it.
8041 */
8042 static void
8043sortFunctions()
8044{
8045 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8046
8047 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8048}
8049#endif
8050
8051
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052/*
8053 * Find internal function in table above.
8054 * Return index, or -1 if not found
8055 */
8056 static int
8057find_internal_func(name)
8058 char_u *name; /* name of the function */
8059{
8060 int first = 0;
8061 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8062 int cmp;
8063 int x;
8064
8065 /*
8066 * Find the function name in the table. Binary search.
8067 */
8068 while (first <= last)
8069 {
8070 x = first + ((unsigned)(last - first) >> 1);
8071 cmp = STRCMP(name, functions[x].f_name);
8072 if (cmp < 0)
8073 last = x - 1;
8074 else if (cmp > 0)
8075 first = x + 1;
8076 else
8077 return x;
8078 }
8079 return -1;
8080}
8081
8082/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008083 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8084 * name it contains, otherwise return "name".
8085 */
8086 static char_u *
8087deref_func_name(name, lenp)
8088 char_u *name;
8089 int *lenp;
8090{
Bram Moolenaar33570922005-01-25 22:26:29 +00008091 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008092 int cc;
8093
8094 cc = name[*lenp];
8095 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008096 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008097 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008098 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008099 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008100 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008101 {
8102 *lenp = 0;
8103 return (char_u *)""; /* just in case */
8104 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008105 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008106 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008107 }
8108
8109 return name;
8110}
8111
8112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 * Allocate a variable for the result of a function.
8114 * Return OK or FAIL.
8115 */
8116 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008117get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8118 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 char_u *name; /* name of the function */
8120 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 char_u **arg; /* argument, pointing to the '(' */
8123 linenr_T firstline; /* first line of range */
8124 linenr_T lastline; /* last line of range */
8125 int *doesrange; /* return: function handled range */
8126 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008127 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128{
8129 char_u *argp;
8130 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008131 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 int argcount = 0; /* number of arguments found */
8133
8134 /*
8135 * Get the arguments.
8136 */
8137 argp = *arg;
8138 while (argcount < MAX_FUNC_ARGS)
8139 {
8140 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8141 if (*argp == ')' || *argp == ',' || *argp == NUL)
8142 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8144 {
8145 ret = FAIL;
8146 break;
8147 }
8148 ++argcount;
8149 if (*argp != ',')
8150 break;
8151 }
8152 if (*argp == ')')
8153 ++argp;
8154 else
8155 ret = FAIL;
8156
8157 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008158 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008159 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008161 {
8162 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008163 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008164 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008165 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167
8168 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008169 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170
8171 *arg = skipwhite(argp);
8172 return ret;
8173}
8174
8175
8176/*
8177 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008178 * Return OK when the function can't be called, FAIL otherwise.
8179 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 */
8181 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008182call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008183 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008184 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008186 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008188 typval_T *argvars; /* vars for arguments, must have "argcount"
8189 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 linenr_T firstline; /* first line of range */
8191 linenr_T lastline; /* last line of range */
8192 int *doesrange; /* return: function handled range */
8193 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008194 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195{
8196 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197#define ERROR_UNKNOWN 0
8198#define ERROR_TOOMANY 1
8199#define ERROR_TOOFEW 2
8200#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008201#define ERROR_DICT 4
8202#define ERROR_NONE 5
8203#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 int error = ERROR_NONE;
8205 int i;
8206 int llen;
8207 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208#define FLEN_FIXED 40
8209 char_u fname_buf[FLEN_FIXED + 1];
8210 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008211 char_u *name;
8212
8213 /* Make a copy of the name, if it comes from a funcref variable it could
8214 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008215 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008216 if (name == NULL)
8217 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218
8219 /*
8220 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8221 * Change <SNR>123_name() to K_SNR 123_name().
8222 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 llen = eval_fname_script(name);
8225 if (llen > 0)
8226 {
8227 fname_buf[0] = K_SPECIAL;
8228 fname_buf[1] = KS_EXTRA;
8229 fname_buf[2] = (int)KE_SNR;
8230 i = 3;
8231 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8232 {
8233 if (current_SID <= 0)
8234 error = ERROR_SCRIPT;
8235 else
8236 {
8237 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8238 i = (int)STRLEN(fname_buf);
8239 }
8240 }
8241 if (i + STRLEN(name + llen) < FLEN_FIXED)
8242 {
8243 STRCPY(fname_buf + i, name + llen);
8244 fname = fname_buf;
8245 }
8246 else
8247 {
8248 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8249 if (fname == NULL)
8250 error = ERROR_OTHER;
8251 else
8252 {
8253 mch_memmove(fname, fname_buf, (size_t)i);
8254 STRCPY(fname + i, name + llen);
8255 }
8256 }
8257 }
8258 else
8259 fname = name;
8260
8261 *doesrange = FALSE;
8262
8263
8264 /* execute the function if no errors detected and executing */
8265 if (evaluate && error == ERROR_NONE)
8266 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008267 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8268 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 error = ERROR_UNKNOWN;
8270
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008271 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {
8273 /*
8274 * User defined function.
8275 */
8276 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008277
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008279 /* Trigger FuncUndefined event, may load the function. */
8280 if (fp == NULL
8281 && apply_autocmds(EVENT_FUNCUNDEFINED,
8282 fname, fname, TRUE, NULL)
8283 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008285 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 fp = find_func(fname);
8287 }
8288#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008289 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008290 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008291 {
8292 /* loaded a package, search for the function again */
8293 fp = find_func(fname);
8294 }
8295
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 if (fp != NULL)
8297 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008298 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008300 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008302 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008304 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008305 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 else
8307 {
8308 /*
8309 * Call the user function.
8310 * Save and restore search patterns, script variables and
8311 * redo buffer.
8312 */
8313 save_search_patterns();
8314 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008315 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008316 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008317 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008318 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8319 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8320 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008321 /* Function was unreferenced while being used, free it
8322 * now. */
8323 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 restoreRedobuff();
8325 restore_search_patterns();
8326 error = ERROR_NONE;
8327 }
8328 }
8329 }
8330 else
8331 {
8332 /*
8333 * Find the function name in the table, call its implementation.
8334 */
8335 i = find_internal_func(fname);
8336 if (i >= 0)
8337 {
8338 if (argcount < functions[i].f_min_argc)
8339 error = ERROR_TOOFEW;
8340 else if (argcount > functions[i].f_max_argc)
8341 error = ERROR_TOOMANY;
8342 else
8343 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008344 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008345 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 error = ERROR_NONE;
8347 }
8348 }
8349 }
8350 /*
8351 * The function call (or "FuncUndefined" autocommand sequence) might
8352 * have been aborted by an error, an interrupt, or an explicitly thrown
8353 * exception that has not been caught so far. This situation can be
8354 * tested for by calling aborting(). For an error in an internal
8355 * function or for the "E132" error in call_user_func(), however, the
8356 * throw point at which the "force_abort" flag (temporarily reset by
8357 * emsg()) is normally updated has not been reached yet. We need to
8358 * update that flag first to make aborting() reliable.
8359 */
8360 update_force_abort();
8361 }
8362 if (error == ERROR_NONE)
8363 ret = OK;
8364
8365 /*
8366 * Report an error unless the argument evaluation or function call has been
8367 * cancelled due to an aborting error, an interrupt, or an exception.
8368 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008369 if (!aborting())
8370 {
8371 switch (error)
8372 {
8373 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008374 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008375 break;
8376 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008377 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008378 break;
8379 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008380 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008381 name);
8382 break;
8383 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008384 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008385 name);
8386 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008387 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008388 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008389 name);
8390 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008391 }
8392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 if (fname != name && fname != fname_buf)
8395 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008396 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397
8398 return ret;
8399}
8400
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008401/*
8402 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008403 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008404 */
8405 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008406emsg_funcname(ermsg, name)
8407 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008408 char_u *name;
8409{
8410 char_u *p;
8411
8412 if (*name == K_SPECIAL)
8413 p = concat_str((char_u *)"<SNR>", name + 3);
8414 else
8415 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008416 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008417 if (p != name)
8418 vim_free(p);
8419}
8420
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008421/*
8422 * Return TRUE for a non-zero Number and a non-empty String.
8423 */
8424 static int
8425non_zero_arg(argvars)
8426 typval_T *argvars;
8427{
8428 return ((argvars[0].v_type == VAR_NUMBER
8429 && argvars[0].vval.v_number != 0)
8430 || (argvars[0].v_type == VAR_STRING
8431 && argvars[0].vval.v_string != NULL
8432 && *argvars[0].vval.v_string != NUL));
8433}
8434
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435/*********************************************
8436 * Implementation of the built-in functions
8437 */
8438
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008439#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008440static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8441
8442/*
8443 * Get the float value of "argvars[0]" into "f".
8444 * Returns FAIL when the argument is not a Number or Float.
8445 */
8446 static int
8447get_float_arg(argvars, f)
8448 typval_T *argvars;
8449 float_T *f;
8450{
8451 if (argvars[0].v_type == VAR_FLOAT)
8452 {
8453 *f = argvars[0].vval.v_float;
8454 return OK;
8455 }
8456 if (argvars[0].v_type == VAR_NUMBER)
8457 {
8458 *f = (float_T)argvars[0].vval.v_number;
8459 return OK;
8460 }
8461 EMSG(_("E808: Number or Float required"));
8462 return FAIL;
8463}
8464
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008465/*
8466 * "abs(expr)" function
8467 */
8468 static void
8469f_abs(argvars, rettv)
8470 typval_T *argvars;
8471 typval_T *rettv;
8472{
8473 if (argvars[0].v_type == VAR_FLOAT)
8474 {
8475 rettv->v_type = VAR_FLOAT;
8476 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8477 }
8478 else
8479 {
8480 varnumber_T n;
8481 int error = FALSE;
8482
8483 n = get_tv_number_chk(&argvars[0], &error);
8484 if (error)
8485 rettv->vval.v_number = -1;
8486 else if (n > 0)
8487 rettv->vval.v_number = n;
8488 else
8489 rettv->vval.v_number = -n;
8490 }
8491}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008492
8493/*
8494 * "acos()" function
8495 */
8496 static void
8497f_acos(argvars, rettv)
8498 typval_T *argvars;
8499 typval_T *rettv;
8500{
8501 float_T f;
8502
8503 rettv->v_type = VAR_FLOAT;
8504 if (get_float_arg(argvars, &f) == OK)
8505 rettv->vval.v_float = acos(f);
8506 else
8507 rettv->vval.v_float = 0.0;
8508}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008509#endif
8510
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008512 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 */
8514 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008515f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008516 typval_T *argvars;
8517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518{
Bram Moolenaar33570922005-01-25 22:26:29 +00008519 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008521 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008522 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008524 if ((l = argvars[0].vval.v_list) != NULL
8525 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8526 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008527 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008528 }
8529 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008530 EMSG(_(e_listreq));
8531}
8532
8533/*
8534 * "append(lnum, string/list)" function
8535 */
8536 static void
8537f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008538 typval_T *argvars;
8539 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008540{
8541 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008542 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008543 list_T *l = NULL;
8544 listitem_T *li = NULL;
8545 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008546 long added = 0;
8547
Bram Moolenaar0d660222005-01-07 21:51:51 +00008548 lnum = get_tv_lnum(argvars);
8549 if (lnum >= 0
8550 && lnum <= curbuf->b_ml.ml_line_count
8551 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008552 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008553 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008554 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008555 l = argvars[1].vval.v_list;
8556 if (l == NULL)
8557 return;
8558 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008559 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008560 for (;;)
8561 {
8562 if (l == NULL)
8563 tv = &argvars[1]; /* append a string */
8564 else if (li == NULL)
8565 break; /* end of list */
8566 else
8567 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008568 line = get_tv_string_chk(tv);
8569 if (line == NULL) /* type error */
8570 {
8571 rettv->vval.v_number = 1; /* Failed */
8572 break;
8573 }
8574 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008575 ++added;
8576 if (l == NULL)
8577 break;
8578 li = li->li_next;
8579 }
8580
8581 appended_lines_mark(lnum, added);
8582 if (curwin->w_cursor.lnum > lnum)
8583 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008585 else
8586 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587}
8588
8589/*
8590 * "argc()" function
8591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008593f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008594 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008597 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598}
8599
8600/*
8601 * "argidx()" function
8602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008604f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008605 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008608 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609}
8610
8611/*
8612 * "argv(nr)" function
8613 */
8614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008615f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008616 typval_T *argvars;
8617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618{
8619 int idx;
8620
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008621 if (argvars[0].v_type != VAR_UNKNOWN)
8622 {
8623 idx = get_tv_number_chk(&argvars[0], NULL);
8624 if (idx >= 0 && idx < ARGCOUNT)
8625 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8626 else
8627 rettv->vval.v_string = NULL;
8628 rettv->v_type = VAR_STRING;
8629 }
8630 else if (rettv_list_alloc(rettv) == OK)
8631 for (idx = 0; idx < ARGCOUNT; ++idx)
8632 list_append_string(rettv->vval.v_list,
8633 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634}
8635
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008636#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008637/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008638 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008639 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008640 static void
8641f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008642 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008643 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008644{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008645 float_T f;
8646
8647 rettv->v_type = VAR_FLOAT;
8648 if (get_float_arg(argvars, &f) == OK)
8649 rettv->vval.v_float = asin(f);
8650 else
8651 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008652}
8653
8654/*
8655 * "atan()" function
8656 */
8657 static void
8658f_atan(argvars, rettv)
8659 typval_T *argvars;
8660 typval_T *rettv;
8661{
8662 float_T f;
8663
8664 rettv->v_type = VAR_FLOAT;
8665 if (get_float_arg(argvars, &f) == OK)
8666 rettv->vval.v_float = atan(f);
8667 else
8668 rettv->vval.v_float = 0.0;
8669}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008670
8671/*
8672 * "atan2()" function
8673 */
8674 static void
8675f_atan2(argvars, rettv)
8676 typval_T *argvars;
8677 typval_T *rettv;
8678{
8679 float_T fx, fy;
8680
8681 rettv->v_type = VAR_FLOAT;
8682 if (get_float_arg(argvars, &fx) == OK
8683 && get_float_arg(&argvars[1], &fy) == OK)
8684 rettv->vval.v_float = atan2(fx, fy);
8685 else
8686 rettv->vval.v_float = 0.0;
8687}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008688#endif
8689
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690/*
8691 * "browse(save, title, initdir, default)" function
8692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008694f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008695 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697{
8698#ifdef FEAT_BROWSE
8699 int save;
8700 char_u *title;
8701 char_u *initdir;
8702 char_u *defname;
8703 char_u buf[NUMBUFLEN];
8704 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008705 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008707 save = get_tv_number_chk(&argvars[0], &error);
8708 title = get_tv_string_chk(&argvars[1]);
8709 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8710 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008712 if (error || title == NULL || initdir == NULL || defname == NULL)
8713 rettv->vval.v_string = NULL;
8714 else
8715 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008716 do_browse(save ? BROWSE_SAVE : 0,
8717 title, defname, NULL, initdir, NULL, curbuf);
8718#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008719 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008720#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008721 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008722}
8723
8724/*
8725 * "browsedir(title, initdir)" function
8726 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008728f_browsedir(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 Moolenaar7b0294c2004-10-11 10:16:09 +00008731{
8732#ifdef FEAT_BROWSE
8733 char_u *title;
8734 char_u *initdir;
8735 char_u buf[NUMBUFLEN];
8736
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008737 title = get_tv_string_chk(&argvars[0]);
8738 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008740 if (title == NULL || initdir == NULL)
8741 rettv->vval.v_string = NULL;
8742 else
8743 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008744 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008746 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008748 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749}
8750
Bram Moolenaar33570922005-01-25 22:26:29 +00008751static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008752
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753/*
8754 * Find a buffer by number or exact name.
8755 */
8756 static buf_T *
8757find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008758 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759{
8760 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008762 if (avar->v_type == VAR_NUMBER)
8763 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008764 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008766 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008767 if (buf == NULL)
8768 {
8769 /* No full path name match, try a match with a URL or a "nofile"
8770 * buffer, these don't use the full path. */
8771 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8772 if (buf->b_fname != NULL
8773 && (path_with_url(buf->b_fname)
8774#ifdef FEAT_QUICKFIX
8775 || bt_nofile(buf)
8776#endif
8777 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008778 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008779 break;
8780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 }
8782 return buf;
8783}
8784
8785/*
8786 * "bufexists(expr)" function
8787 */
8788 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008789f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008790 typval_T *argvars;
8791 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008793 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794}
8795
8796/*
8797 * "buflisted(expr)" function
8798 */
8799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008800f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008801 typval_T *argvars;
8802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803{
8804 buf_T *buf;
8805
8806 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008807 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808}
8809
8810/*
8811 * "bufloaded(expr)" function
8812 */
8813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008814f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008815 typval_T *argvars;
8816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817{
8818 buf_T *buf;
8819
8820 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008821 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822}
8823
Bram Moolenaar33570922005-01-25 22:26:29 +00008824static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008825
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826/*
8827 * Get buffer by number or pattern.
8828 */
8829 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008830get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008831 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008833 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 int save_magic;
8835 char_u *save_cpo;
8836 buf_T *buf;
8837
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008838 if (tv->v_type == VAR_NUMBER)
8839 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008840 if (tv->v_type != VAR_STRING)
8841 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 if (name == NULL || *name == NUL)
8843 return curbuf;
8844 if (name[0] == '$' && name[1] == NUL)
8845 return lastbuf;
8846
8847 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8848 save_magic = p_magic;
8849 p_magic = TRUE;
8850 save_cpo = p_cpo;
8851 p_cpo = (char_u *)"";
8852
8853 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8854 TRUE, FALSE));
8855
8856 p_magic = save_magic;
8857 p_cpo = save_cpo;
8858
8859 /* If not found, try expanding the name, like done for bufexists(). */
8860 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008861 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862
8863 return buf;
8864}
8865
8866/*
8867 * "bufname(expr)" function
8868 */
8869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008870f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008871 typval_T *argvars;
8872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873{
8874 buf_T *buf;
8875
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008876 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008878 buf = get_buf_tv(&argvars[0]);
8879 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008881 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008883 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 --emsg_off;
8885}
8886
8887/*
8888 * "bufnr(expr)" function
8889 */
8890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008891f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008892 typval_T *argvars;
8893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894{
8895 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008896 int error = FALSE;
8897 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008899 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008901 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008902 --emsg_off;
8903
8904 /* If the buffer isn't found and the second argument is not zero create a
8905 * new buffer. */
8906 if (buf == NULL
8907 && argvars[1].v_type != VAR_UNKNOWN
8908 && get_tv_number_chk(&argvars[1], &error) != 0
8909 && !error
8910 && (name = get_tv_string_chk(&argvars[0])) != NULL
8911 && !error)
8912 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8913
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008915 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008917 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918}
8919
8920/*
8921 * "bufwinnr(nr)" function
8922 */
8923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008924f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008925 typval_T *argvars;
8926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927{
8928#ifdef FEAT_WINDOWS
8929 win_T *wp;
8930 int winnr = 0;
8931#endif
8932 buf_T *buf;
8933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008934 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008936 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937#ifdef FEAT_WINDOWS
8938 for (wp = firstwin; wp; wp = wp->w_next)
8939 {
8940 ++winnr;
8941 if (wp->w_buffer == buf)
8942 break;
8943 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008946 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947#endif
8948 --emsg_off;
8949}
8950
8951/*
8952 * "byte2line(byte)" function
8953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008955f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008956 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958{
8959#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008960 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961#else
8962 long boff = 0;
8963
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008964 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008968 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 (linenr_T)0, &boff);
8970#endif
8971}
8972
8973/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008974 * "byteidx()" function
8975 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008977f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008978 typval_T *argvars;
8979 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008980{
8981#ifdef FEAT_MBYTE
8982 char_u *t;
8983#endif
8984 char_u *str;
8985 long idx;
8986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008987 str = get_tv_string_chk(&argvars[0]);
8988 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008989 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008991 return;
8992
8993#ifdef FEAT_MBYTE
8994 t = str;
8995 for ( ; idx > 0; idx--)
8996 {
8997 if (*t == NUL) /* EOL reached */
8998 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008999 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009000 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009001 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009002#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009003 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009004 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009005#endif
9006}
9007
9008/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009009 * "call(func, arglist)" function
9010 */
9011 static void
9012f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009013 typval_T *argvars;
9014 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009015{
9016 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009017 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009018 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00009019 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009020 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00009021 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009022
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009023 if (argvars[1].v_type != VAR_LIST)
9024 {
9025 EMSG(_(e_listreq));
9026 return;
9027 }
9028 if (argvars[1].vval.v_list == NULL)
9029 return;
9030
9031 if (argvars[0].v_type == VAR_FUNC)
9032 func = argvars[0].vval.v_string;
9033 else
9034 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009035 if (*func == NUL)
9036 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009037
Bram Moolenaare9a41262005-01-15 22:18:47 +00009038 if (argvars[2].v_type != VAR_UNKNOWN)
9039 {
9040 if (argvars[2].v_type != VAR_DICT)
9041 {
9042 EMSG(_(e_dictreq));
9043 return;
9044 }
9045 selfdict = argvars[2].vval.v_dict;
9046 }
9047
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009048 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9049 item = item->li_next)
9050 {
9051 if (argc == MAX_FUNC_ARGS)
9052 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009053 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009054 break;
9055 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009056 /* Make a copy of each argument. This is needed to be able to set
9057 * v_lock to VAR_FIXED in the copy without changing the original list.
9058 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009059 copy_tv(&item->li_tv, &argv[argc++]);
9060 }
9061
9062 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009063 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009064 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9065 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009066
9067 /* Free the arguments. */
9068 while (argc > 0)
9069 clear_tv(&argv[--argc]);
9070}
9071
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009072#ifdef FEAT_FLOAT
9073/*
9074 * "ceil({float})" function
9075 */
9076 static void
9077f_ceil(argvars, rettv)
9078 typval_T *argvars;
9079 typval_T *rettv;
9080{
9081 float_T f;
9082
9083 rettv->v_type = VAR_FLOAT;
9084 if (get_float_arg(argvars, &f) == OK)
9085 rettv->vval.v_float = ceil(f);
9086 else
9087 rettv->vval.v_float = 0.0;
9088}
9089#endif
9090
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009091/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009092 * "changenr()" function
9093 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009094 static void
9095f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009096 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009097 typval_T *rettv;
9098{
9099 rettv->vval.v_number = curbuf->b_u_seq_cur;
9100}
9101
9102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 * "char2nr(string)" function
9104 */
9105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009106f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009107 typval_T *argvars;
9108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109{
9110#ifdef FEAT_MBYTE
9111 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009112 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 else
9114#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116}
9117
9118/*
9119 * "cindent(lnum)" function
9120 */
9121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009123 typval_T *argvars;
9124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125{
9126#ifdef FEAT_CINDENT
9127 pos_T pos;
9128 linenr_T lnum;
9129
9130 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009131 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9133 {
9134 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009135 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 curwin->w_cursor = pos;
9137 }
9138 else
9139#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009140 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141}
9142
9143/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009144 * "clearmatches()" function
9145 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009146 static void
9147f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009148 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009149 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009150{
9151#ifdef FEAT_SEARCH_EXTRA
9152 clear_matches(curwin);
9153#endif
9154}
9155
9156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 * "col(string)" function
9158 */
9159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009160f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009161 typval_T *argvars;
9162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163{
9164 colnr_T col = 0;
9165 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009166 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009168 fp = var2fpos(&argvars[0], FALSE, &fnum);
9169 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 {
9171 if (fp->col == MAXCOL)
9172 {
9173 /* '> can be MAXCOL, get the length of the line then */
9174 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009175 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 else
9177 col = MAXCOL;
9178 }
9179 else
9180 {
9181 col = fp->col + 1;
9182#ifdef FEAT_VIRTUALEDIT
9183 /* col(".") when the cursor is on the NUL at the end of the line
9184 * because of "coladd" can be seen as an extra column. */
9185 if (virtual_active() && fp == &curwin->w_cursor)
9186 {
9187 char_u *p = ml_get_cursor();
9188
9189 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9190 curwin->w_virtcol - curwin->w_cursor.coladd))
9191 {
9192# ifdef FEAT_MBYTE
9193 int l;
9194
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009195 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 col += l;
9197# else
9198 if (*p != NUL && p[1] == NUL)
9199 ++col;
9200# endif
9201 }
9202 }
9203#endif
9204 }
9205 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009206 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207}
9208
Bram Moolenaar572cb562005-08-05 21:35:02 +00009209#if defined(FEAT_INS_EXPAND)
9210/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009211 * "complete()" function
9212 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009213 static void
9214f_complete(argvars, rettv)
9215 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009216 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009217{
9218 int startcol;
9219
9220 if ((State & INSERT) == 0)
9221 {
9222 EMSG(_("E785: complete() can only be used in Insert mode"));
9223 return;
9224 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009225
9226 /* Check for undo allowed here, because if something was already inserted
9227 * the line was already saved for undo and this check isn't done. */
9228 if (!undo_allowed())
9229 return;
9230
Bram Moolenaarade00832006-03-10 21:46:58 +00009231 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9232 {
9233 EMSG(_(e_invarg));
9234 return;
9235 }
9236
9237 startcol = get_tv_number_chk(&argvars[0], NULL);
9238 if (startcol <= 0)
9239 return;
9240
9241 set_completion(startcol - 1, argvars[1].vval.v_list);
9242}
9243
9244/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009245 * "complete_add()" function
9246 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009247 static void
9248f_complete_add(argvars, rettv)
9249 typval_T *argvars;
9250 typval_T *rettv;
9251{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009252 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009253}
9254
9255/*
9256 * "complete_check()" function
9257 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009258 static void
9259f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009260 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009261 typval_T *rettv;
9262{
9263 int saved = RedrawingDisabled;
9264
9265 RedrawingDisabled = 0;
9266 ins_compl_check_keys(0);
9267 rettv->vval.v_number = compl_interrupted;
9268 RedrawingDisabled = saved;
9269}
9270#endif
9271
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272/*
9273 * "confirm(message, buttons[, default [, type]])" function
9274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009276f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009277 typval_T *argvars UNUSED;
9278 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279{
9280#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9281 char_u *message;
9282 char_u *buttons = NULL;
9283 char_u buf[NUMBUFLEN];
9284 char_u buf2[NUMBUFLEN];
9285 int def = 1;
9286 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009287 char_u *typestr;
9288 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009290 message = get_tv_string_chk(&argvars[0]);
9291 if (message == NULL)
9292 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009293 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009295 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9296 if (buttons == NULL)
9297 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009298 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009300 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009301 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009303 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9304 if (typestr == NULL)
9305 error = TRUE;
9306 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009308 switch (TOUPPER_ASC(*typestr))
9309 {
9310 case 'E': type = VIM_ERROR; break;
9311 case 'Q': type = VIM_QUESTION; break;
9312 case 'I': type = VIM_INFO; break;
9313 case 'W': type = VIM_WARNING; break;
9314 case 'G': type = VIM_GENERIC; break;
9315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 }
9317 }
9318 }
9319 }
9320
9321 if (buttons == NULL || *buttons == NUL)
9322 buttons = (char_u *)_("&Ok");
9323
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009324 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009325 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009326 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327#endif
9328}
9329
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009330/*
9331 * "copy()" function
9332 */
9333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009334f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009335 typval_T *argvars;
9336 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009337{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009338 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009339}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009341#ifdef FEAT_FLOAT
9342/*
9343 * "cos()" function
9344 */
9345 static void
9346f_cos(argvars, rettv)
9347 typval_T *argvars;
9348 typval_T *rettv;
9349{
9350 float_T f;
9351
9352 rettv->v_type = VAR_FLOAT;
9353 if (get_float_arg(argvars, &f) == OK)
9354 rettv->vval.v_float = cos(f);
9355 else
9356 rettv->vval.v_float = 0.0;
9357}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009358
9359/*
9360 * "cosh()" function
9361 */
9362 static void
9363f_cosh(argvars, rettv)
9364 typval_T *argvars;
9365 typval_T *rettv;
9366{
9367 float_T f;
9368
9369 rettv->v_type = VAR_FLOAT;
9370 if (get_float_arg(argvars, &f) == OK)
9371 rettv->vval.v_float = cosh(f);
9372 else
9373 rettv->vval.v_float = 0.0;
9374}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009375#endif
9376
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009378 * "count()" function
9379 */
9380 static void
9381f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009382 typval_T *argvars;
9383 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009384{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009385 long n = 0;
9386 int ic = FALSE;
9387
Bram Moolenaare9a41262005-01-15 22:18:47 +00009388 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009389 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009390 listitem_T *li;
9391 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009392 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009393
Bram Moolenaare9a41262005-01-15 22:18:47 +00009394 if ((l = argvars[0].vval.v_list) != NULL)
9395 {
9396 li = l->lv_first;
9397 if (argvars[2].v_type != VAR_UNKNOWN)
9398 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009399 int error = FALSE;
9400
9401 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009402 if (argvars[3].v_type != VAR_UNKNOWN)
9403 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009404 idx = get_tv_number_chk(&argvars[3], &error);
9405 if (!error)
9406 {
9407 li = list_find(l, idx);
9408 if (li == NULL)
9409 EMSGN(_(e_listidx), idx);
9410 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009411 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009412 if (error)
9413 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009414 }
9415
9416 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009417 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009418 ++n;
9419 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009420 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009421 else if (argvars[0].v_type == VAR_DICT)
9422 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009423 int todo;
9424 dict_T *d;
9425 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009426
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009427 if ((d = argvars[0].vval.v_dict) != NULL)
9428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009429 int error = FALSE;
9430
Bram Moolenaare9a41262005-01-15 22:18:47 +00009431 if (argvars[2].v_type != VAR_UNKNOWN)
9432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009433 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009434 if (argvars[3].v_type != VAR_UNKNOWN)
9435 EMSG(_(e_invarg));
9436 }
9437
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009438 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009439 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009440 {
9441 if (!HASHITEM_EMPTY(hi))
9442 {
9443 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009444 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009445 ++n;
9446 }
9447 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009448 }
9449 }
9450 else
9451 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009452 rettv->vval.v_number = n;
9453}
9454
9455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9457 *
9458 * Checks the existence of a cscope connection.
9459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009461f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009462 typval_T *argvars UNUSED;
9463 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464{
9465#ifdef FEAT_CSCOPE
9466 int num = 0;
9467 char_u *dbpath = NULL;
9468 char_u *prepend = NULL;
9469 char_u buf[NUMBUFLEN];
9470
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009471 if (argvars[0].v_type != VAR_UNKNOWN
9472 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009474 num = (int)get_tv_number(&argvars[0]);
9475 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009476 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009477 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 }
9479
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009480 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481#endif
9482}
9483
9484/*
9485 * "cursor(lnum, col)" function
9486 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009487 * Moves the cursor to the specified line and column.
9488 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009491f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009492 typval_T *argvars;
9493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494{
9495 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009496#ifdef FEAT_VIRTUALEDIT
9497 long coladd = 0;
9498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009500 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009501 if (argvars[1].v_type == VAR_UNKNOWN)
9502 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009503 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009504
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009505 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009506 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009507 line = pos.lnum;
9508 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009509#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009510 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009511#endif
9512 }
9513 else
9514 {
9515 line = get_tv_lnum(argvars);
9516 col = get_tv_number_chk(&argvars[1], NULL);
9517#ifdef FEAT_VIRTUALEDIT
9518 if (argvars[2].v_type != VAR_UNKNOWN)
9519 coladd = get_tv_number_chk(&argvars[2], NULL);
9520#endif
9521 }
9522 if (line < 0 || col < 0
9523#ifdef FEAT_VIRTUALEDIT
9524 || coladd < 0
9525#endif
9526 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009527 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 if (line > 0)
9529 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 if (col > 0)
9531 curwin->w_cursor.col = col - 1;
9532#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009533 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534#endif
9535
9536 /* Make sure the cursor is in a valid position. */
9537 check_cursor();
9538#ifdef FEAT_MBYTE
9539 /* Correct cursor for multi-byte character. */
9540 if (has_mbyte)
9541 mb_adjust_cursor();
9542#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009543
9544 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009545 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546}
9547
9548/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009549 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 */
9551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009553 typval_T *argvars;
9554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009556 int noref = 0;
9557
9558 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009559 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009560 if (noref < 0 || noref > 1)
9561 EMSG(_(e_invarg));
9562 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009563 {
9564 current_copyID += COPYID_INC;
9565 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567}
9568
9569/*
9570 * "delete()" function
9571 */
9572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009573f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009574 typval_T *argvars;
9575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576{
9577 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009578 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009580 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581}
9582
9583/*
9584 * "did_filetype()" function
9585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009587f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009588 typval_T *argvars UNUSED;
9589 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590{
9591#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009592 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593#endif
9594}
9595
9596/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009597 * "diff_filler()" function
9598 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009600f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009601 typval_T *argvars UNUSED;
9602 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009603{
9604#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009605 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009606#endif
9607}
9608
9609/*
9610 * "diff_hlID()" function
9611 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009613f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009614 typval_T *argvars UNUSED;
9615 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009616{
9617#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009618 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009619 static linenr_T prev_lnum = 0;
9620 static int changedtick = 0;
9621 static int fnum = 0;
9622 static int change_start = 0;
9623 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009624 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009625 int filler_lines;
9626 int col;
9627
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009628 if (lnum < 0) /* ignore type error in {lnum} arg */
9629 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009630 if (lnum != prev_lnum
9631 || changedtick != curbuf->b_changedtick
9632 || fnum != curbuf->b_fnum)
9633 {
9634 /* New line, buffer, change: need to get the values. */
9635 filler_lines = diff_check(curwin, lnum);
9636 if (filler_lines < 0)
9637 {
9638 if (filler_lines == -1)
9639 {
9640 change_start = MAXCOL;
9641 change_end = -1;
9642 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9643 hlID = HLF_ADD; /* added line */
9644 else
9645 hlID = HLF_CHD; /* changed line */
9646 }
9647 else
9648 hlID = HLF_ADD; /* added line */
9649 }
9650 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009651 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009652 prev_lnum = lnum;
9653 changedtick = curbuf->b_changedtick;
9654 fnum = curbuf->b_fnum;
9655 }
9656
9657 if (hlID == HLF_CHD || hlID == HLF_TXD)
9658 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009659 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009660 if (col >= change_start && col <= change_end)
9661 hlID = HLF_TXD; /* changed text */
9662 else
9663 hlID = HLF_CHD; /* changed line */
9664 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009665 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009666#endif
9667}
9668
9669/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009670 * "empty({expr})" function
9671 */
9672 static void
9673f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009674 typval_T *argvars;
9675 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009676{
9677 int n;
9678
9679 switch (argvars[0].v_type)
9680 {
9681 case VAR_STRING:
9682 case VAR_FUNC:
9683 n = argvars[0].vval.v_string == NULL
9684 || *argvars[0].vval.v_string == NUL;
9685 break;
9686 case VAR_NUMBER:
9687 n = argvars[0].vval.v_number == 0;
9688 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009689#ifdef FEAT_FLOAT
9690 case VAR_FLOAT:
9691 n = argvars[0].vval.v_float == 0.0;
9692 break;
9693#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009694 case VAR_LIST:
9695 n = argvars[0].vval.v_list == NULL
9696 || argvars[0].vval.v_list->lv_first == NULL;
9697 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009698 case VAR_DICT:
9699 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009700 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009701 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009702 default:
9703 EMSG2(_(e_intern2), "f_empty()");
9704 n = 0;
9705 }
9706
9707 rettv->vval.v_number = n;
9708}
9709
9710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 * "escape({string}, {chars})" function
9712 */
9713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009714f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009715 typval_T *argvars;
9716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717{
9718 char_u buf[NUMBUFLEN];
9719
Bram Moolenaar758711c2005-02-02 23:11:38 +00009720 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9721 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009722 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723}
9724
9725/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009726 * "eval()" function
9727 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009728 static void
9729f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009730 typval_T *argvars;
9731 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009732{
9733 char_u *s;
9734
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009735 s = get_tv_string_chk(&argvars[0]);
9736 if (s != NULL)
9737 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009739 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9740 {
9741 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009742 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009743 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009744 else if (*s != NUL)
9745 EMSG(_(e_trailing));
9746}
9747
9748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 * "eventhandler()" function
9750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009752f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009753 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757}
9758
9759/*
9760 * "executable()" function
9761 */
9762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009763f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009764 typval_T *argvars;
9765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009767 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768}
9769
9770/*
9771 * "exists()" function
9772 */
9773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009775 typval_T *argvars;
9776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777{
9778 char_u *p;
9779 char_u *name;
9780 int n = FALSE;
9781 int len = 0;
9782
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009783 no_autoload = TRUE;
9784
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009785 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 if (*p == '$') /* environment variable */
9787 {
9788 /* first try "normal" environment variables (fast) */
9789 if (mch_getenv(p + 1) != NULL)
9790 n = TRUE;
9791 else
9792 {
9793 /* try expanding things like $VIM and ${HOME} */
9794 p = expand_env_save(p);
9795 if (p != NULL && *p != '$')
9796 n = TRUE;
9797 vim_free(p);
9798 }
9799 }
9800 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009801 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009802 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009803 if (*skipwhite(p) != NUL)
9804 n = FALSE; /* trailing garbage */
9805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 else if (*p == '*') /* internal or user defined function */
9807 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009808 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 }
9810 else if (*p == ':')
9811 {
9812 n = cmd_exists(p + 1);
9813 }
9814 else if (*p == '#')
9815 {
9816#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009817 if (p[1] == '#')
9818 n = autocmd_supported(p + 2);
9819 else
9820 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821#endif
9822 }
9823 else /* internal variable */
9824 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009825 char_u *tofree;
9826 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009828 /* get_name_len() takes care of expanding curly braces */
9829 name = p;
9830 len = get_name_len(&p, &tofree, TRUE, FALSE);
9831 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009833 if (tofree != NULL)
9834 name = tofree;
9835 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9836 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009838 /* handle d.key, l[idx], f(expr) */
9839 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9840 if (n)
9841 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 }
9843 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009844 if (*p != NUL)
9845 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009847 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 }
9849
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009850 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009851
9852 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853}
9854
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009855#ifdef FEAT_FLOAT
9856/*
9857 * "exp()" function
9858 */
9859 static void
9860f_exp(argvars, rettv)
9861 typval_T *argvars;
9862 typval_T *rettv;
9863{
9864 float_T f;
9865
9866 rettv->v_type = VAR_FLOAT;
9867 if (get_float_arg(argvars, &f) == OK)
9868 rettv->vval.v_float = exp(f);
9869 else
9870 rettv->vval.v_float = 0.0;
9871}
9872#endif
9873
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874/*
9875 * "expand()" function
9876 */
9877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009878f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009879 typval_T *argvars;
9880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881{
9882 char_u *s;
9883 int len;
9884 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009885 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009887 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009889 rettv->v_type = VAR_STRING;
9890 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 if (*s == '%' || *s == '#' || *s == '<')
9892 {
9893 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009894 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 --emsg_off;
9896 }
9897 else
9898 {
9899 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009900 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009901 if (argvars[1].v_type != VAR_UNKNOWN
9902 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009903 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009904 if (!error)
9905 {
9906 ExpandInit(&xpc);
9907 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009908 if (p_wic)
9909 options += WILD_ICASE;
9910 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, options, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009911 }
9912 else
9913 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 }
9915}
9916
9917/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009918 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009919 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009920 */
9921 static void
9922f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009923 typval_T *argvars;
9924 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009925{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009926 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009927 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009928 list_T *l1, *l2;
9929 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009930 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009931 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009932
Bram Moolenaare9a41262005-01-15 22:18:47 +00009933 l1 = argvars[0].vval.v_list;
9934 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009935 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9936 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009937 {
9938 if (argvars[2].v_type != VAR_UNKNOWN)
9939 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009940 before = get_tv_number_chk(&argvars[2], &error);
9941 if (error)
9942 return; /* type error; errmsg already given */
9943
Bram Moolenaar758711c2005-02-02 23:11:38 +00009944 if (before == l1->lv_len)
9945 item = NULL;
9946 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009947 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009948 item = list_find(l1, before);
9949 if (item == NULL)
9950 {
9951 EMSGN(_(e_listidx), before);
9952 return;
9953 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009954 }
9955 }
9956 else
9957 item = NULL;
9958 list_extend(l1, l2, item);
9959
Bram Moolenaare9a41262005-01-15 22:18:47 +00009960 copy_tv(&argvars[0], rettv);
9961 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009962 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009963 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9964 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009965 dict_T *d1, *d2;
9966 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009967 char_u *action;
9968 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009969 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009970 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009971
9972 d1 = argvars[0].vval.v_dict;
9973 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009974 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9975 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009976 {
9977 /* Check the third argument. */
9978 if (argvars[2].v_type != VAR_UNKNOWN)
9979 {
9980 static char *(av[]) = {"keep", "force", "error"};
9981
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009982 action = get_tv_string_chk(&argvars[2]);
9983 if (action == NULL)
9984 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009985 for (i = 0; i < 3; ++i)
9986 if (STRCMP(action, av[i]) == 0)
9987 break;
9988 if (i == 3)
9989 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009990 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009991 return;
9992 }
9993 }
9994 else
9995 action = (char_u *)"force";
9996
9997 /* Go over all entries in the second dict and add them to the
9998 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009999 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010000 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010001 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010002 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010003 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010004 --todo;
10005 di1 = dict_find(d1, hi2->hi_key, -1);
10006 if (di1 == NULL)
10007 {
10008 di1 = dictitem_copy(HI2DI(hi2));
10009 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10010 dictitem_free(di1);
10011 }
10012 else if (*action == 'e')
10013 {
10014 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10015 break;
10016 }
10017 else if (*action == 'f')
10018 {
10019 clear_tv(&di1->di_tv);
10020 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10021 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010022 }
10023 }
10024
Bram Moolenaare9a41262005-01-15 22:18:47 +000010025 copy_tv(&argvars[0], rettv);
10026 }
10027 }
10028 else
10029 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010030}
10031
10032/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010033 * "feedkeys()" function
10034 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010035 static void
10036f_feedkeys(argvars, rettv)
10037 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010038 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010039{
10040 int remap = TRUE;
10041 char_u *keys, *flags;
10042 char_u nbuf[NUMBUFLEN];
10043 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010044 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010045
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010046 /* This is not allowed in the sandbox. If the commands would still be
10047 * executed in the sandbox it would be OK, but it probably happens later,
10048 * when "sandbox" is no longer set. */
10049 if (check_secure())
10050 return;
10051
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010052 keys = get_tv_string(&argvars[0]);
10053 if (*keys != NUL)
10054 {
10055 if (argvars[1].v_type != VAR_UNKNOWN)
10056 {
10057 flags = get_tv_string_buf(&argvars[1], nbuf);
10058 for ( ; *flags != NUL; ++flags)
10059 {
10060 switch (*flags)
10061 {
10062 case 'n': remap = FALSE; break;
10063 case 'm': remap = TRUE; break;
10064 case 't': typed = TRUE; break;
10065 }
10066 }
10067 }
10068
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010069 /* Need to escape K_SPECIAL and CSI before putting the string in the
10070 * typeahead buffer. */
10071 keys_esc = vim_strsave_escape_csi(keys);
10072 if (keys_esc != NULL)
10073 {
10074 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010075 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010076 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010077 if (vgetc_busy)
10078 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010079 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010080 }
10081}
10082
10083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 * "filereadable()" function
10085 */
10086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010087f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010088 typval_T *argvars;
10089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010091 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 char_u *p;
10093 int n;
10094
Bram Moolenaarc236c162008-07-13 17:41:49 +000010095#ifndef O_NONBLOCK
10096# define O_NONBLOCK 0
10097#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010098 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010099 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10100 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 {
10102 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010103 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 }
10105 else
10106 n = FALSE;
10107
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010108 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109}
10110
10111/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010112 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 * rights to write into.
10114 */
10115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010116f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010117 typval_T *argvars;
10118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010120 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121}
10122
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010123static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010124
10125 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010126findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010127 typval_T *argvars;
10128 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010129 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010130{
10131#ifdef FEAT_SEARCHPATH
10132 char_u *fname;
10133 char_u *fresult = NULL;
10134 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10135 char_u *p;
10136 char_u pathbuf[NUMBUFLEN];
10137 int count = 1;
10138 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010139 int error = FALSE;
10140#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010141
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010142 rettv->vval.v_string = NULL;
10143 rettv->v_type = VAR_STRING;
10144
10145#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010146 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010147
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010148 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010149 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010150 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10151 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010152 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010153 else
10154 {
10155 if (*p != NUL)
10156 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010157
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010158 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010159 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010160 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010161 }
10162
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010163 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10164 error = TRUE;
10165
10166 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010168 do
10169 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010170 if (rettv->v_type == VAR_STRING)
10171 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010172 fresult = find_file_in_path_option(first ? fname : NULL,
10173 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010174 0, first, path,
10175 find_what,
10176 curbuf->b_ffname,
10177 find_what == FINDFILE_DIR
10178 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010179 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010180
10181 if (fresult != NULL && rettv->v_type == VAR_LIST)
10182 list_append_string(rettv->vval.v_list, fresult, -1);
10183
10184 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010185 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010186
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010187 if (rettv->v_type == VAR_STRING)
10188 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010189#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010190}
10191
Bram Moolenaar33570922005-01-25 22:26:29 +000010192static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10193static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010194
10195/*
10196 * Implementation of map() and filter().
10197 */
10198 static void
10199filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010200 typval_T *argvars;
10201 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010202 int map;
10203{
10204 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010205 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010206 listitem_T *li, *nli;
10207 list_T *l = NULL;
10208 dictitem_T *di;
10209 hashtab_T *ht;
10210 hashitem_T *hi;
10211 dict_T *d = NULL;
10212 typval_T save_val;
10213 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010214 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010215 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010216 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010217 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010218 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010219
Bram Moolenaare9a41262005-01-15 22:18:47 +000010220 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010221 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010222 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010223 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010224 return;
10225 }
10226 else if (argvars[0].v_type == VAR_DICT)
10227 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010228 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010229 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010230 return;
10231 }
10232 else
10233 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010234 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010235 return;
10236 }
10237
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010238 expr = get_tv_string_buf_chk(&argvars[1], buf);
10239 /* On type errors, the preceding call has already displayed an error
10240 * message. Avoid a misleading error message for an empty string that
10241 * was not passed as argument. */
10242 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010244 prepare_vimvar(VV_VAL, &save_val);
10245 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010246
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010247 /* We reset "did_emsg" to be able to detect whether an error
10248 * occurred during evaluation of the expression. */
10249 save_did_emsg = did_emsg;
10250 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010251
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010252 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010253 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010255 vimvars[VV_KEY].vv_type = VAR_STRING;
10256
10257 ht = &d->dv_hashtab;
10258 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010259 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010260 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010262 if (!HASHITEM_EMPTY(hi))
10263 {
10264 --todo;
10265 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010266 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010267 break;
10268 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010269 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010270 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010271 break;
10272 if (!map && rem)
10273 dictitem_remove(d, di);
10274 clear_tv(&vimvars[VV_KEY].vv_tv);
10275 }
10276 }
10277 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010278 }
10279 else
10280 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010281 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10282
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010283 for (li = l->lv_first; li != NULL; li = nli)
10284 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010285 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010286 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010287 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010288 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010289 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010290 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010291 break;
10292 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010293 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010294 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010295 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010296 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010297
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010298 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010299 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010300
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010301 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010302 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010303
10304 copy_tv(&argvars[0], rettv);
10305}
10306
10307 static int
10308filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010309 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010310 char_u *expr;
10311 int map;
10312 int *remp;
10313{
Bram Moolenaar33570922005-01-25 22:26:29 +000010314 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010315 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010316 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010317
Bram Moolenaar33570922005-01-25 22:26:29 +000010318 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010319 s = expr;
10320 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010321 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010322 if (*s != NUL) /* check for trailing chars after expr */
10323 {
10324 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010325 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010326 }
10327 if (map)
10328 {
10329 /* map(): replace the list item value */
10330 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010331 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010332 *tv = rettv;
10333 }
10334 else
10335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010336 int error = FALSE;
10337
Bram Moolenaare9a41262005-01-15 22:18:47 +000010338 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010339 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010340 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010341 /* On type error, nothing has been removed; return FAIL to stop the
10342 * loop. The error message was given by get_tv_number_chk(). */
10343 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010344 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010345 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010346 retval = OK;
10347theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010348 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010349 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010350}
10351
10352/*
10353 * "filter()" function
10354 */
10355 static void
10356f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010357 typval_T *argvars;
10358 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010359{
10360 filter_map(argvars, rettv, FALSE);
10361}
10362
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010363/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010364 * "finddir({fname}[, {path}[, {count}]])" function
10365 */
10366 static void
10367f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010368 typval_T *argvars;
10369 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010370{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010371 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010372}
10373
10374/*
10375 * "findfile({fname}[, {path}[, {count}]])" function
10376 */
10377 static void
10378f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010379 typval_T *argvars;
10380 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010381{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010382 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010383}
10384
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010385#ifdef FEAT_FLOAT
10386/*
10387 * "float2nr({float})" function
10388 */
10389 static void
10390f_float2nr(argvars, rettv)
10391 typval_T *argvars;
10392 typval_T *rettv;
10393{
10394 float_T f;
10395
10396 if (get_float_arg(argvars, &f) == OK)
10397 {
10398 if (f < -0x7fffffff)
10399 rettv->vval.v_number = -0x7fffffff;
10400 else if (f > 0x7fffffff)
10401 rettv->vval.v_number = 0x7fffffff;
10402 else
10403 rettv->vval.v_number = (varnumber_T)f;
10404 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010405}
10406
10407/*
10408 * "floor({float})" function
10409 */
10410 static void
10411f_floor(argvars, rettv)
10412 typval_T *argvars;
10413 typval_T *rettv;
10414{
10415 float_T f;
10416
10417 rettv->v_type = VAR_FLOAT;
10418 if (get_float_arg(argvars, &f) == OK)
10419 rettv->vval.v_float = floor(f);
10420 else
10421 rettv->vval.v_float = 0.0;
10422}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010423
10424/*
10425 * "fmod()" function
10426 */
10427 static void
10428f_fmod(argvars, rettv)
10429 typval_T *argvars;
10430 typval_T *rettv;
10431{
10432 float_T fx, fy;
10433
10434 rettv->v_type = VAR_FLOAT;
10435 if (get_float_arg(argvars, &fx) == OK
10436 && get_float_arg(&argvars[1], &fy) == OK)
10437 rettv->vval.v_float = fmod(fx, fy);
10438 else
10439 rettv->vval.v_float = 0.0;
10440}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010441#endif
10442
Bram Moolenaar0d660222005-01-07 21:51:51 +000010443/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010444 * "fnameescape({string})" function
10445 */
10446 static void
10447f_fnameescape(argvars, rettv)
10448 typval_T *argvars;
10449 typval_T *rettv;
10450{
10451 rettv->vval.v_string = vim_strsave_fnameescape(
10452 get_tv_string(&argvars[0]), FALSE);
10453 rettv->v_type = VAR_STRING;
10454}
10455
10456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 * "fnamemodify({fname}, {mods})" function
10458 */
10459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010460f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010461 typval_T *argvars;
10462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463{
10464 char_u *fname;
10465 char_u *mods;
10466 int usedlen = 0;
10467 int len;
10468 char_u *fbuf = NULL;
10469 char_u buf[NUMBUFLEN];
10470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010471 fname = get_tv_string_chk(&argvars[0]);
10472 mods = get_tv_string_buf_chk(&argvars[1], buf);
10473 if (fname == NULL || mods == NULL)
10474 fname = NULL;
10475 else
10476 {
10477 len = (int)STRLEN(fname);
10478 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010481 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010483 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010485 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 vim_free(fbuf);
10487}
10488
Bram Moolenaar33570922005-01-25 22:26:29 +000010489static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490
10491/*
10492 * "foldclosed()" function
10493 */
10494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010495foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010496 typval_T *argvars;
10497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 int end;
10499{
10500#ifdef FEAT_FOLDING
10501 linenr_T lnum;
10502 linenr_T first, last;
10503
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010504 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10506 {
10507 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10508 {
10509 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010510 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010512 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 return;
10514 }
10515 }
10516#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010517 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518}
10519
10520/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010521 * "foldclosed()" function
10522 */
10523 static void
10524f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010525 typval_T *argvars;
10526 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010527{
10528 foldclosed_both(argvars, rettv, FALSE);
10529}
10530
10531/*
10532 * "foldclosedend()" function
10533 */
10534 static void
10535f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010536 typval_T *argvars;
10537 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010538{
10539 foldclosed_both(argvars, rettv, TRUE);
10540}
10541
10542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 * "foldlevel()" function
10544 */
10545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010546f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010547 typval_T *argvars;
10548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549{
10550#ifdef FEAT_FOLDING
10551 linenr_T lnum;
10552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010553 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010555 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557}
10558
10559/*
10560 * "foldtext()" function
10561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010563f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010564 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566{
10567#ifdef FEAT_FOLDING
10568 linenr_T lnum;
10569 char_u *s;
10570 char_u *r;
10571 int len;
10572 char *txt;
10573#endif
10574
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010575 rettv->v_type = VAR_STRING;
10576 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010578 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10579 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10580 <= curbuf->b_ml.ml_line_count
10581 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582 {
10583 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010584 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10585 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 {
10587 if (!linewhite(lnum))
10588 break;
10589 ++lnum;
10590 }
10591
10592 /* Find interesting text in this line. */
10593 s = skipwhite(ml_get(lnum));
10594 /* skip C comment-start */
10595 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010596 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010598 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010599 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010600 {
10601 s = skipwhite(ml_get(lnum + 1));
10602 if (*s == '*')
10603 s = skipwhite(s + 1);
10604 }
10605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606 txt = _("+-%s%3ld lines: ");
10607 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010608 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 + 20 /* for %3ld */
10610 + STRLEN(s))); /* concatenated */
10611 if (r != NULL)
10612 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010613 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10614 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10615 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 len = (int)STRLEN(r);
10617 STRCAT(r, s);
10618 /* remove 'foldmarker' and 'commentstring' */
10619 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010620 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 }
10622 }
10623#endif
10624}
10625
10626/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010627 * "foldtextresult(lnum)" function
10628 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010630f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010631 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010632 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010633{
10634#ifdef FEAT_FOLDING
10635 linenr_T lnum;
10636 char_u *text;
10637 char_u buf[51];
10638 foldinfo_T foldinfo;
10639 int fold_count;
10640#endif
10641
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010642 rettv->v_type = VAR_STRING;
10643 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010644#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010645 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010646 /* treat illegal types and illegal string values for {lnum} the same */
10647 if (lnum < 0)
10648 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010649 fold_count = foldedCount(curwin, lnum, &foldinfo);
10650 if (fold_count > 0)
10651 {
10652 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10653 &foldinfo, buf);
10654 if (text == buf)
10655 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010656 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010657 }
10658#endif
10659}
10660
10661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 * "foreground()" function
10663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010665f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010666 typval_T *argvars UNUSED;
10667 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669#ifdef FEAT_GUI
10670 if (gui.in_use)
10671 gui_mch_set_foreground();
10672#else
10673# ifdef WIN32
10674 win32_set_foreground();
10675# endif
10676#endif
10677}
10678
10679/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010680 * "function()" function
10681 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010683f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010684 typval_T *argvars;
10685 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010686{
10687 char_u *s;
10688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010689 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010690 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010691 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010692 /* Don't check an autoload name for existence here. */
10693 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010694 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010695 else
10696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010697 rettv->vval.v_string = vim_strsave(s);
10698 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010699 }
10700}
10701
10702/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010703 * "garbagecollect()" function
10704 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010705 static void
10706f_garbagecollect(argvars, rettv)
10707 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010708 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010709{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010710 /* This is postponed until we are back at the toplevel, because we may be
10711 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10712 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010713
10714 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10715 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010716}
10717
10718/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010719 * "get()" function
10720 */
10721 static void
10722f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010723 typval_T *argvars;
10724 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010725{
Bram Moolenaar33570922005-01-25 22:26:29 +000010726 listitem_T *li;
10727 list_T *l;
10728 dictitem_T *di;
10729 dict_T *d;
10730 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010731
Bram Moolenaare9a41262005-01-15 22:18:47 +000010732 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010733 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010734 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010735 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010736 int error = FALSE;
10737
10738 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10739 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010740 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010741 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010742 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010743 else if (argvars[0].v_type == VAR_DICT)
10744 {
10745 if ((d = argvars[0].vval.v_dict) != NULL)
10746 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010747 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010748 if (di != NULL)
10749 tv = &di->di_tv;
10750 }
10751 }
10752 else
10753 EMSG2(_(e_listdictarg), "get()");
10754
10755 if (tv == NULL)
10756 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010757 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010758 copy_tv(&argvars[2], rettv);
10759 }
10760 else
10761 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010762}
10763
Bram Moolenaar342337a2005-07-21 21:11:17 +000010764static 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 +000010765
10766/*
10767 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010768 * Return a range (from start to end) of lines in rettv from the specified
10769 * buffer.
10770 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010771 */
10772 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010773get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010774 buf_T *buf;
10775 linenr_T start;
10776 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010777 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010778 typval_T *rettv;
10779{
10780 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010781
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010782 if (retlist && rettv_list_alloc(rettv) == FAIL)
10783 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010784
10785 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10786 return;
10787
10788 if (!retlist)
10789 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010790 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10791 p = ml_get_buf(buf, start, FALSE);
10792 else
10793 p = (char_u *)"";
10794
10795 rettv->v_type = VAR_STRING;
10796 rettv->vval.v_string = vim_strsave(p);
10797 }
10798 else
10799 {
10800 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010801 return;
10802
10803 if (start < 1)
10804 start = 1;
10805 if (end > buf->b_ml.ml_line_count)
10806 end = buf->b_ml.ml_line_count;
10807 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010808 if (list_append_string(rettv->vval.v_list,
10809 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010810 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010811 }
10812}
10813
10814/*
10815 * "getbufline()" function
10816 */
10817 static void
10818f_getbufline(argvars, rettv)
10819 typval_T *argvars;
10820 typval_T *rettv;
10821{
10822 linenr_T lnum;
10823 linenr_T end;
10824 buf_T *buf;
10825
10826 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10827 ++emsg_off;
10828 buf = get_buf_tv(&argvars[0]);
10829 --emsg_off;
10830
Bram Moolenaar661b1822005-07-28 22:36:45 +000010831 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010832 if (argvars[2].v_type == VAR_UNKNOWN)
10833 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010834 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010835 end = get_tv_lnum_buf(&argvars[2], buf);
10836
Bram Moolenaar342337a2005-07-21 21:11:17 +000010837 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010838}
10839
Bram Moolenaar0d660222005-01-07 21:51:51 +000010840/*
10841 * "getbufvar()" function
10842 */
10843 static void
10844f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010845 typval_T *argvars;
10846 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010847{
10848 buf_T *buf;
10849 buf_T *save_curbuf;
10850 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010851 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010852
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010853 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10854 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010855 ++emsg_off;
10856 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010857
10858 rettv->v_type = VAR_STRING;
10859 rettv->vval.v_string = NULL;
10860
10861 if (buf != NULL && varname != NULL)
10862 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010863 /* set curbuf to be our buf, temporarily */
10864 save_curbuf = curbuf;
10865 curbuf = buf;
10866
Bram Moolenaar0d660222005-01-07 21:51:51 +000010867 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010868 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010010869 else if (STRCMP(varname, "changedtick") == 0)
10870 {
10871 rettv->v_type = VAR_NUMBER;
10872 rettv->vval.v_number = curbuf->b_changedtick;
10873 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010874 else
10875 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010876 if (*varname == NUL)
10877 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10878 * scope prefix before the NUL byte is required by
10879 * find_var_in_ht(). */
10880 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010881 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010882 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010883 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010884 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010885 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010886
10887 /* restore previous notion of curbuf */
10888 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010889 }
10890
10891 --emsg_off;
10892}
10893
10894/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 * "getchar()" function
10896 */
10897 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010898f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010899 typval_T *argvars;
10900 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901{
10902 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010903 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010905 /* Position the cursor. Needed after a message that ends in a space. */
10906 windgoto(msg_row, msg_col);
10907
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 ++no_mapping;
10909 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010910 for (;;)
10911 {
10912 if (argvars[0].v_type == VAR_UNKNOWN)
10913 /* getchar(): blocking wait. */
10914 n = safe_vgetc();
10915 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10916 /* getchar(1): only check if char avail */
10917 n = vpeekc();
10918 else if (error || vpeekc() == NUL)
10919 /* illegal argument or getchar(0) and no char avail: return zero */
10920 n = 0;
10921 else
10922 /* getchar(0) and char avail: return char */
10923 n = safe_vgetc();
10924 if (n == K_IGNORE)
10925 continue;
10926 break;
10927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 --no_mapping;
10929 --allow_keys;
10930
Bram Moolenaar219b8702006-11-01 14:32:36 +000010931 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10932 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10933 vimvars[VV_MOUSE_COL].vv_nr = 0;
10934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010935 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 if (IS_SPECIAL(n) || mod_mask != 0)
10937 {
10938 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10939 int i = 0;
10940
10941 /* Turn a special key into three bytes, plus modifier. */
10942 if (mod_mask != 0)
10943 {
10944 temp[i++] = K_SPECIAL;
10945 temp[i++] = KS_MODIFIER;
10946 temp[i++] = mod_mask;
10947 }
10948 if (IS_SPECIAL(n))
10949 {
10950 temp[i++] = K_SPECIAL;
10951 temp[i++] = K_SECOND(n);
10952 temp[i++] = K_THIRD(n);
10953 }
10954#ifdef FEAT_MBYTE
10955 else if (has_mbyte)
10956 i += (*mb_char2bytes)(n, temp + i);
10957#endif
10958 else
10959 temp[i++] = n;
10960 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010961 rettv->v_type = VAR_STRING;
10962 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010963
10964#ifdef FEAT_MOUSE
10965 if (n == K_LEFTMOUSE
10966 || n == K_LEFTMOUSE_NM
10967 || n == K_LEFTDRAG
10968 || n == K_LEFTRELEASE
10969 || n == K_LEFTRELEASE_NM
10970 || n == K_MIDDLEMOUSE
10971 || n == K_MIDDLEDRAG
10972 || n == K_MIDDLERELEASE
10973 || n == K_RIGHTMOUSE
10974 || n == K_RIGHTDRAG
10975 || n == K_RIGHTRELEASE
10976 || n == K_X1MOUSE
10977 || n == K_X1DRAG
10978 || n == K_X1RELEASE
10979 || n == K_X2MOUSE
10980 || n == K_X2DRAG
10981 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020010982 || n == K_MOUSELEFT
10983 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000010984 || n == K_MOUSEDOWN
10985 || n == K_MOUSEUP)
10986 {
10987 int row = mouse_row;
10988 int col = mouse_col;
10989 win_T *win;
10990 linenr_T lnum;
10991# ifdef FEAT_WINDOWS
10992 win_T *wp;
10993# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010994 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010995
10996 if (row >= 0 && col >= 0)
10997 {
10998 /* Find the window at the mouse coordinates and compute the
10999 * text position. */
11000 win = mouse_find_win(&row, &col);
11001 (void)mouse_comp_pos(win, &row, &col, &lnum);
11002# ifdef FEAT_WINDOWS
11003 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011004 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011005# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011006 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011007 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11008 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11009 }
11010 }
11011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 }
11013}
11014
11015/*
11016 * "getcharmod()" function
11017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011019f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011020 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011023 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024}
11025
11026/*
11027 * "getcmdline()" function
11028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011030f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011031 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011034 rettv->v_type = VAR_STRING;
11035 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036}
11037
11038/*
11039 * "getcmdpos()" function
11040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011042f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011043 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011046 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047}
11048
11049/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011050 * "getcmdtype()" function
11051 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011052 static void
11053f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011054 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011055 typval_T *rettv;
11056{
11057 rettv->v_type = VAR_STRING;
11058 rettv->vval.v_string = alloc(2);
11059 if (rettv->vval.v_string != NULL)
11060 {
11061 rettv->vval.v_string[0] = get_cmdline_type();
11062 rettv->vval.v_string[1] = NUL;
11063 }
11064}
11065
11066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 * "getcwd()" function
11068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011070f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011071 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073{
11074 char_u cwd[MAXPATHL];
11075
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011076 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011078 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 else
11080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011081 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000011083 if (rettv->vval.v_string != NULL)
11084 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085#endif
11086 }
11087}
11088
11089/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011090 * "getfontname()" function
11091 */
11092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011093f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011094 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011095 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011096{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011097 rettv->v_type = VAR_STRING;
11098 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011099#ifdef FEAT_GUI
11100 if (gui.in_use)
11101 {
11102 GuiFont font;
11103 char_u *name = NULL;
11104
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011105 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011106 {
11107 /* Get the "Normal" font. Either the name saved by
11108 * hl_set_font_name() or from the font ID. */
11109 font = gui.norm_font;
11110 name = hl_get_font_name();
11111 }
11112 else
11113 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011114 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011115 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11116 return;
11117 font = gui_mch_get_font(name, FALSE);
11118 if (font == NOFONT)
11119 return; /* Invalid font name, return empty string. */
11120 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011121 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011122 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011123 gui_mch_free_font(font);
11124 }
11125#endif
11126}
11127
11128/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011129 * "getfperm({fname})" function
11130 */
11131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011132f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011133 typval_T *argvars;
11134 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011135{
11136 char_u *fname;
11137 struct stat st;
11138 char_u *perm = NULL;
11139 char_u flags[] = "rwx";
11140 int i;
11141
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011142 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011143
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011144 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011145 if (mch_stat((char *)fname, &st) >= 0)
11146 {
11147 perm = vim_strsave((char_u *)"---------");
11148 if (perm != NULL)
11149 {
11150 for (i = 0; i < 9; i++)
11151 {
11152 if (st.st_mode & (1 << (8 - i)))
11153 perm[i] = flags[i % 3];
11154 }
11155 }
11156 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011157 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011158}
11159
11160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 * "getfsize({fname})" function
11162 */
11163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011165 typval_T *argvars;
11166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167{
11168 char_u *fname;
11169 struct stat st;
11170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011171 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011173 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174
11175 if (mch_stat((char *)fname, &st) >= 0)
11176 {
11177 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011178 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011180 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011181 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011182
11183 /* non-perfect check for overflow */
11184 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11185 rettv->vval.v_number = -2;
11186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187 }
11188 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011189 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190}
11191
11192/*
11193 * "getftime({fname})" function
11194 */
11195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011196f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011197 typval_T *argvars;
11198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199{
11200 char_u *fname;
11201 struct stat st;
11202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011203 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204
11205 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011206 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011208 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209}
11210
11211/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011212 * "getftype({fname})" function
11213 */
11214 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011215f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011216 typval_T *argvars;
11217 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011218{
11219 char_u *fname;
11220 struct stat st;
11221 char_u *type = NULL;
11222 char *t;
11223
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011224 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011227 if (mch_lstat((char *)fname, &st) >= 0)
11228 {
11229#ifdef S_ISREG
11230 if (S_ISREG(st.st_mode))
11231 t = "file";
11232 else if (S_ISDIR(st.st_mode))
11233 t = "dir";
11234# ifdef S_ISLNK
11235 else if (S_ISLNK(st.st_mode))
11236 t = "link";
11237# endif
11238# ifdef S_ISBLK
11239 else if (S_ISBLK(st.st_mode))
11240 t = "bdev";
11241# endif
11242# ifdef S_ISCHR
11243 else if (S_ISCHR(st.st_mode))
11244 t = "cdev";
11245# endif
11246# ifdef S_ISFIFO
11247 else if (S_ISFIFO(st.st_mode))
11248 t = "fifo";
11249# endif
11250# ifdef S_ISSOCK
11251 else if (S_ISSOCK(st.st_mode))
11252 t = "fifo";
11253# endif
11254 else
11255 t = "other";
11256#else
11257# ifdef S_IFMT
11258 switch (st.st_mode & S_IFMT)
11259 {
11260 case S_IFREG: t = "file"; break;
11261 case S_IFDIR: t = "dir"; break;
11262# ifdef S_IFLNK
11263 case S_IFLNK: t = "link"; break;
11264# endif
11265# ifdef S_IFBLK
11266 case S_IFBLK: t = "bdev"; break;
11267# endif
11268# ifdef S_IFCHR
11269 case S_IFCHR: t = "cdev"; break;
11270# endif
11271# ifdef S_IFIFO
11272 case S_IFIFO: t = "fifo"; break;
11273# endif
11274# ifdef S_IFSOCK
11275 case S_IFSOCK: t = "socket"; break;
11276# endif
11277 default: t = "other";
11278 }
11279# else
11280 if (mch_isdir(fname))
11281 t = "dir";
11282 else
11283 t = "file";
11284# endif
11285#endif
11286 type = vim_strsave((char_u *)t);
11287 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011288 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011289}
11290
11291/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011292 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011293 */
11294 static void
11295f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011296 typval_T *argvars;
11297 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011298{
11299 linenr_T lnum;
11300 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011301 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011302
11303 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011304 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011305 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011306 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011307 retlist = FALSE;
11308 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011309 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011310 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011311 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011312 retlist = TRUE;
11313 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011314
Bram Moolenaar342337a2005-07-21 21:11:17 +000011315 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011316}
11317
11318/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011319 * "getmatches()" function
11320 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011321 static void
11322f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011323 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011324 typval_T *rettv;
11325{
11326#ifdef FEAT_SEARCH_EXTRA
11327 dict_T *dict;
11328 matchitem_T *cur = curwin->w_match_head;
11329
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011330 if (rettv_list_alloc(rettv) == OK)
11331 {
11332 while (cur != NULL)
11333 {
11334 dict = dict_alloc();
11335 if (dict == NULL)
11336 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011337 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11338 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11339 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11340 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11341 list_append_dict(rettv->vval.v_list, dict);
11342 cur = cur->next;
11343 }
11344 }
11345#endif
11346}
11347
11348/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011349 * "getpid()" function
11350 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011351 static void
11352f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011353 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011354 typval_T *rettv;
11355{
11356 rettv->vval.v_number = mch_get_pid();
11357}
11358
11359/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011360 * "getpos(string)" function
11361 */
11362 static void
11363f_getpos(argvars, rettv)
11364 typval_T *argvars;
11365 typval_T *rettv;
11366{
11367 pos_T *fp;
11368 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011369 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011370
11371 if (rettv_list_alloc(rettv) == OK)
11372 {
11373 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011374 fp = var2fpos(&argvars[0], TRUE, &fnum);
11375 if (fnum != -1)
11376 list_append_number(l, (varnumber_T)fnum);
11377 else
11378 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011379 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11380 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011381 list_append_number(l, (fp != NULL)
11382 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011383 : (varnumber_T)0);
11384 list_append_number(l,
11385#ifdef FEAT_VIRTUALEDIT
11386 (fp != NULL) ? (varnumber_T)fp->coladd :
11387#endif
11388 (varnumber_T)0);
11389 }
11390 else
11391 rettv->vval.v_number = FALSE;
11392}
11393
11394/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011395 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011396 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011397 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011398f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011399 typval_T *argvars UNUSED;
11400 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011401{
11402#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011403 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011404#endif
11405
Bram Moolenaar2641f772005-03-25 21:58:17 +000011406#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011407 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011408 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011409 wp = NULL;
11410 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11411 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011412 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011413 if (wp == NULL)
11414 return;
11415 }
11416
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011417 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011418 }
11419#endif
11420}
11421
11422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423 * "getreg()" function
11424 */
11425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011426f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011427 typval_T *argvars;
11428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429{
11430 char_u *strregname;
11431 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011432 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011433 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011435 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011437 strregname = get_tv_string_chk(&argvars[0]);
11438 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011439 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011440 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011443 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444 regname = (strregname == NULL ? '"' : *strregname);
11445 if (regname == 0)
11446 regname = '"';
11447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011449 rettv->vval.v_string = error ? NULL :
11450 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451}
11452
11453/*
11454 * "getregtype()" function
11455 */
11456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011457f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011458 typval_T *argvars;
11459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460{
11461 char_u *strregname;
11462 int regname;
11463 char_u buf[NUMBUFLEN + 2];
11464 long reglen = 0;
11465
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011466 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011467 {
11468 strregname = get_tv_string_chk(&argvars[0]);
11469 if (strregname == NULL) /* type error; errmsg already given */
11470 {
11471 rettv->v_type = VAR_STRING;
11472 rettv->vval.v_string = NULL;
11473 return;
11474 }
11475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476 else
11477 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011478 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479
11480 regname = (strregname == NULL ? '"' : *strregname);
11481 if (regname == 0)
11482 regname = '"';
11483
11484 buf[0] = NUL;
11485 buf[1] = NUL;
11486 switch (get_reg_type(regname, &reglen))
11487 {
11488 case MLINE: buf[0] = 'V'; break;
11489 case MCHAR: buf[0] = 'v'; break;
11490#ifdef FEAT_VISUAL
11491 case MBLOCK:
11492 buf[0] = Ctrl_V;
11493 sprintf((char *)buf + 1, "%ld", reglen + 1);
11494 break;
11495#endif
11496 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011497 rettv->v_type = VAR_STRING;
11498 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499}
11500
11501/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011502 * "gettabvar()" function
11503 */
11504 static void
11505f_gettabvar(argvars, rettv)
11506 typval_T *argvars;
11507 typval_T *rettv;
11508{
11509 tabpage_T *tp;
11510 dictitem_T *v;
11511 char_u *varname;
11512
11513 rettv->v_type = VAR_STRING;
11514 rettv->vval.v_string = NULL;
11515
11516 varname = get_tv_string_chk(&argvars[1]);
11517 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11518 if (tp != NULL && varname != NULL)
11519 {
11520 /* look up the variable */
11521 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11522 if (v != NULL)
11523 copy_tv(&v->di_tv, rettv);
11524 }
11525}
11526
11527/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011528 * "gettabwinvar()" function
11529 */
11530 static void
11531f_gettabwinvar(argvars, rettv)
11532 typval_T *argvars;
11533 typval_T *rettv;
11534{
11535 getwinvar(argvars, rettv, 1);
11536}
11537
11538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011539 * "getwinposx()" function
11540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011542f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011543 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011546 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011547#ifdef FEAT_GUI
11548 if (gui.in_use)
11549 {
11550 int x, y;
11551
11552 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011553 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554 }
11555#endif
11556}
11557
11558/*
11559 * "getwinposy()" function
11560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011562f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011563 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011566 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567#ifdef FEAT_GUI
11568 if (gui.in_use)
11569 {
11570 int x, y;
11571
11572 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011573 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574 }
11575#endif
11576}
11577
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011578/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011579 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011580 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011581 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011582find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011583 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011584 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011585{
11586#ifdef FEAT_WINDOWS
11587 win_T *wp;
11588#endif
11589 int nr;
11590
11591 nr = get_tv_number_chk(vp, NULL);
11592
11593#ifdef FEAT_WINDOWS
11594 if (nr < 0)
11595 return NULL;
11596 if (nr == 0)
11597 return curwin;
11598
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011599 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11600 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011601 if (--nr <= 0)
11602 break;
11603 return wp;
11604#else
11605 if (nr == 0 || nr == 1)
11606 return curwin;
11607 return NULL;
11608#endif
11609}
11610
Bram Moolenaar071d4272004-06-13 20:20:40 +000011611/*
11612 * "getwinvar()" function
11613 */
11614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011615f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011616 typval_T *argvars;
11617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011619 getwinvar(argvars, rettv, 0);
11620}
11621
11622/*
11623 * getwinvar() and gettabwinvar()
11624 */
11625 static void
11626getwinvar(argvars, rettv, off)
11627 typval_T *argvars;
11628 typval_T *rettv;
11629 int off; /* 1 for gettabwinvar() */
11630{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631 win_T *win, *oldcurwin;
11632 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011633 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011634 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011636#ifdef FEAT_WINDOWS
11637 if (off == 1)
11638 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11639 else
11640 tp = curtab;
11641#endif
11642 win = find_win_by_nr(&argvars[off], tp);
11643 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011644 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011646 rettv->v_type = VAR_STRING;
11647 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648
11649 if (win != NULL && varname != NULL)
11650 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011651 /* Set curwin to be our win, temporarily. Also set curbuf, so
11652 * that we can get buffer-local options. */
11653 oldcurwin = curwin;
11654 curwin = win;
11655 curbuf = win->w_buffer;
11656
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011658 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659 else
11660 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011661 if (*varname == NUL)
11662 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11663 * scope prefix before the NUL byte is required by
11664 * find_var_in_ht(). */
11665 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011667 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011669 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011671
11672 /* restore previous notion of curwin */
11673 curwin = oldcurwin;
11674 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675 }
11676
11677 --emsg_off;
11678}
11679
11680/*
11681 * "glob()" function
11682 */
11683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011684f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011685 typval_T *argvars;
11686 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011688 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011690 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011692 /* When the optional second argument is non-zero, don't remove matches
11693 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11694 if (argvars[1].v_type != VAR_UNKNOWN
11695 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011696 options |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011697 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011698 if (!error)
11699 {
11700 ExpandInit(&xpc);
11701 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011702 if (p_wic)
11703 options += WILD_ICASE;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011704 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011705 NULL, options, WILD_ALL);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011706 }
11707 else
11708 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709}
11710
11711/*
11712 * "globpath()" function
11713 */
11714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011715f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011716 typval_T *argvars;
11717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011719 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011721 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011722 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011723
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011724 /* When the optional second argument is non-zero, don't remove matches
11725 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11726 if (argvars[2].v_type != VAR_UNKNOWN
11727 && get_tv_number_chk(&argvars[2], &error))
11728 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011729 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011730 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011731 rettv->vval.v_string = NULL;
11732 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011733 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11734 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735}
11736
11737/*
11738 * "has()" function
11739 */
11740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011741f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011742 typval_T *argvars;
11743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744{
11745 int i;
11746 char_u *name;
11747 int n = FALSE;
11748 static char *(has_list[]) =
11749 {
11750#ifdef AMIGA
11751 "amiga",
11752# ifdef FEAT_ARP
11753 "arp",
11754# endif
11755#endif
11756#ifdef __BEOS__
11757 "beos",
11758#endif
11759#ifdef MSDOS
11760# ifdef DJGPP
11761 "dos32",
11762# else
11763 "dos16",
11764# endif
11765#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011766#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767 "mac",
11768#endif
11769#if defined(MACOS_X_UNIX)
11770 "macunix",
11771#endif
11772#ifdef OS2
11773 "os2",
11774#endif
11775#ifdef __QNX__
11776 "qnx",
11777#endif
11778#ifdef RISCOS
11779 "riscos",
11780#endif
11781#ifdef UNIX
11782 "unix",
11783#endif
11784#ifdef VMS
11785 "vms",
11786#endif
11787#ifdef WIN16
11788 "win16",
11789#endif
11790#ifdef WIN32
11791 "win32",
11792#endif
11793#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11794 "win32unix",
11795#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011796#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797 "win64",
11798#endif
11799#ifdef EBCDIC
11800 "ebcdic",
11801#endif
11802#ifndef CASE_INSENSITIVE_FILENAME
11803 "fname_case",
11804#endif
11805#ifdef FEAT_ARABIC
11806 "arabic",
11807#endif
11808#ifdef FEAT_AUTOCMD
11809 "autocmd",
11810#endif
11811#ifdef FEAT_BEVAL
11812 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011813# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11814 "balloon_multiline",
11815# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011816#endif
11817#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11818 "builtin_terms",
11819# ifdef ALL_BUILTIN_TCAPS
11820 "all_builtin_terms",
11821# endif
11822#endif
11823#ifdef FEAT_BYTEOFF
11824 "byte_offset",
11825#endif
11826#ifdef FEAT_CINDENT
11827 "cindent",
11828#endif
11829#ifdef FEAT_CLIENTSERVER
11830 "clientserver",
11831#endif
11832#ifdef FEAT_CLIPBOARD
11833 "clipboard",
11834#endif
11835#ifdef FEAT_CMDL_COMPL
11836 "cmdline_compl",
11837#endif
11838#ifdef FEAT_CMDHIST
11839 "cmdline_hist",
11840#endif
11841#ifdef FEAT_COMMENTS
11842 "comments",
11843#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011844#ifdef FEAT_CONCEAL
11845 "conceal",
11846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847#ifdef FEAT_CRYPT
11848 "cryptv",
11849#endif
11850#ifdef FEAT_CSCOPE
11851 "cscope",
11852#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011853#ifdef FEAT_CURSORBIND
11854 "cursorbind",
11855#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011856#ifdef CURSOR_SHAPE
11857 "cursorshape",
11858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859#ifdef DEBUG
11860 "debug",
11861#endif
11862#ifdef FEAT_CON_DIALOG
11863 "dialog_con",
11864#endif
11865#ifdef FEAT_GUI_DIALOG
11866 "dialog_gui",
11867#endif
11868#ifdef FEAT_DIFF
11869 "diff",
11870#endif
11871#ifdef FEAT_DIGRAPHS
11872 "digraphs",
11873#endif
11874#ifdef FEAT_DND
11875 "dnd",
11876#endif
11877#ifdef FEAT_EMACS_TAGS
11878 "emacs_tags",
11879#endif
11880 "eval", /* always present, of course! */
11881#ifdef FEAT_EX_EXTRA
11882 "ex_extra",
11883#endif
11884#ifdef FEAT_SEARCH_EXTRA
11885 "extra_search",
11886#endif
11887#ifdef FEAT_FKMAP
11888 "farsi",
11889#endif
11890#ifdef FEAT_SEARCHPATH
11891 "file_in_path",
11892#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011893#if defined(UNIX) && !defined(USE_SYSTEM)
11894 "filterpipe",
11895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896#ifdef FEAT_FIND_ID
11897 "find_in_path",
11898#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011899#ifdef FEAT_FLOAT
11900 "float",
11901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902#ifdef FEAT_FOLDING
11903 "folding",
11904#endif
11905#ifdef FEAT_FOOTER
11906 "footer",
11907#endif
11908#if !defined(USE_SYSTEM) && defined(UNIX)
11909 "fork",
11910#endif
11911#ifdef FEAT_GETTEXT
11912 "gettext",
11913#endif
11914#ifdef FEAT_GUI
11915 "gui",
11916#endif
11917#ifdef FEAT_GUI_ATHENA
11918# ifdef FEAT_GUI_NEXTAW
11919 "gui_neXtaw",
11920# else
11921 "gui_athena",
11922# endif
11923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924#ifdef FEAT_GUI_GTK
11925 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011928#ifdef FEAT_GUI_GNOME
11929 "gui_gnome",
11930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931#ifdef FEAT_GUI_MAC
11932 "gui_mac",
11933#endif
11934#ifdef FEAT_GUI_MOTIF
11935 "gui_motif",
11936#endif
11937#ifdef FEAT_GUI_PHOTON
11938 "gui_photon",
11939#endif
11940#ifdef FEAT_GUI_W16
11941 "gui_win16",
11942#endif
11943#ifdef FEAT_GUI_W32
11944 "gui_win32",
11945#endif
11946#ifdef FEAT_HANGULIN
11947 "hangul_input",
11948#endif
11949#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11950 "iconv",
11951#endif
11952#ifdef FEAT_INS_EXPAND
11953 "insert_expand",
11954#endif
11955#ifdef FEAT_JUMPLIST
11956 "jumplist",
11957#endif
11958#ifdef FEAT_KEYMAP
11959 "keymap",
11960#endif
11961#ifdef FEAT_LANGMAP
11962 "langmap",
11963#endif
11964#ifdef FEAT_LIBCALL
11965 "libcall",
11966#endif
11967#ifdef FEAT_LINEBREAK
11968 "linebreak",
11969#endif
11970#ifdef FEAT_LISP
11971 "lispindent",
11972#endif
11973#ifdef FEAT_LISTCMDS
11974 "listcmds",
11975#endif
11976#ifdef FEAT_LOCALMAP
11977 "localmap",
11978#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020011979#ifdef FEAT_LUA
11980# ifndef DYNAMIC_LUA
11981 "lua",
11982# endif
11983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984#ifdef FEAT_MENU
11985 "menu",
11986#endif
11987#ifdef FEAT_SESSION
11988 "mksession",
11989#endif
11990#ifdef FEAT_MODIFY_FNAME
11991 "modify_fname",
11992#endif
11993#ifdef FEAT_MOUSE
11994 "mouse",
11995#endif
11996#ifdef FEAT_MOUSESHAPE
11997 "mouseshape",
11998#endif
11999#if defined(UNIX) || defined(VMS)
12000# ifdef FEAT_MOUSE_DEC
12001 "mouse_dec",
12002# endif
12003# ifdef FEAT_MOUSE_GPM
12004 "mouse_gpm",
12005# endif
12006# ifdef FEAT_MOUSE_JSB
12007 "mouse_jsbterm",
12008# endif
12009# ifdef FEAT_MOUSE_NET
12010 "mouse_netterm",
12011# endif
12012# ifdef FEAT_MOUSE_PTERM
12013 "mouse_pterm",
12014# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012015# ifdef FEAT_SYSMOUSE
12016 "mouse_sysmouse",
12017# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018# ifdef FEAT_MOUSE_XTERM
12019 "mouse_xterm",
12020# endif
12021#endif
12022#ifdef FEAT_MBYTE
12023 "multi_byte",
12024#endif
12025#ifdef FEAT_MBYTE_IME
12026 "multi_byte_ime",
12027#endif
12028#ifdef FEAT_MULTI_LANG
12029 "multi_lang",
12030#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012031#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012032#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012033 "mzscheme",
12034#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036#ifdef FEAT_OLE
12037 "ole",
12038#endif
12039#ifdef FEAT_OSFILETYPE
12040 "osfiletype",
12041#endif
12042#ifdef FEAT_PATH_EXTRA
12043 "path_extra",
12044#endif
12045#ifdef FEAT_PERL
12046#ifndef DYNAMIC_PERL
12047 "perl",
12048#endif
12049#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012050#ifdef FEAT_PERSISTENT_UNDO
12051 "persistent_undo",
12052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053#ifdef FEAT_PYTHON
12054#ifndef DYNAMIC_PYTHON
12055 "python",
12056#endif
12057#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012058#ifdef FEAT_PYTHON3
12059#ifndef DYNAMIC_PYTHON3
12060 "python3",
12061#endif
12062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063#ifdef FEAT_POSTSCRIPT
12064 "postscript",
12065#endif
12066#ifdef FEAT_PRINTER
12067 "printer",
12068#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012069#ifdef FEAT_PROFILE
12070 "profile",
12071#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012072#ifdef FEAT_RELTIME
12073 "reltime",
12074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075#ifdef FEAT_QUICKFIX
12076 "quickfix",
12077#endif
12078#ifdef FEAT_RIGHTLEFT
12079 "rightleft",
12080#endif
12081#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12082 "ruby",
12083#endif
12084#ifdef FEAT_SCROLLBIND
12085 "scrollbind",
12086#endif
12087#ifdef FEAT_CMDL_INFO
12088 "showcmd",
12089 "cmdline_info",
12090#endif
12091#ifdef FEAT_SIGNS
12092 "signs",
12093#endif
12094#ifdef FEAT_SMARTINDENT
12095 "smartindent",
12096#endif
12097#ifdef FEAT_SNIFF
12098 "sniff",
12099#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012100#ifdef STARTUPTIME
12101 "startuptime",
12102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103#ifdef FEAT_STL_OPT
12104 "statusline",
12105#endif
12106#ifdef FEAT_SUN_WORKSHOP
12107 "sun_workshop",
12108#endif
12109#ifdef FEAT_NETBEANS_INTG
12110 "netbeans_intg",
12111#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012112#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012113 "spell",
12114#endif
12115#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116 "syntax",
12117#endif
12118#if defined(USE_SYSTEM) || !defined(UNIX)
12119 "system",
12120#endif
12121#ifdef FEAT_TAG_BINS
12122 "tag_binary",
12123#endif
12124#ifdef FEAT_TAG_OLDSTATIC
12125 "tag_old_static",
12126#endif
12127#ifdef FEAT_TAG_ANYWHITE
12128 "tag_any_white",
12129#endif
12130#ifdef FEAT_TCL
12131# ifndef DYNAMIC_TCL
12132 "tcl",
12133# endif
12134#endif
12135#ifdef TERMINFO
12136 "terminfo",
12137#endif
12138#ifdef FEAT_TERMRESPONSE
12139 "termresponse",
12140#endif
12141#ifdef FEAT_TEXTOBJ
12142 "textobjects",
12143#endif
12144#ifdef HAVE_TGETENT
12145 "tgetent",
12146#endif
12147#ifdef FEAT_TITLE
12148 "title",
12149#endif
12150#ifdef FEAT_TOOLBAR
12151 "toolbar",
12152#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012153#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12154 "unnamedplus",
12155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156#ifdef FEAT_USR_CMDS
12157 "user-commands", /* was accidentally included in 5.4 */
12158 "user_commands",
12159#endif
12160#ifdef FEAT_VIMINFO
12161 "viminfo",
12162#endif
12163#ifdef FEAT_VERTSPLIT
12164 "vertsplit",
12165#endif
12166#ifdef FEAT_VIRTUALEDIT
12167 "virtualedit",
12168#endif
12169#ifdef FEAT_VISUAL
12170 "visual",
12171#endif
12172#ifdef FEAT_VISUALEXTRA
12173 "visualextra",
12174#endif
12175#ifdef FEAT_VREPLACE
12176 "vreplace",
12177#endif
12178#ifdef FEAT_WILDIGN
12179 "wildignore",
12180#endif
12181#ifdef FEAT_WILDMENU
12182 "wildmenu",
12183#endif
12184#ifdef FEAT_WINDOWS
12185 "windows",
12186#endif
12187#ifdef FEAT_WAK
12188 "winaltkeys",
12189#endif
12190#ifdef FEAT_WRITEBACKUP
12191 "writebackup",
12192#endif
12193#ifdef FEAT_XIM
12194 "xim",
12195#endif
12196#ifdef FEAT_XFONTSET
12197 "xfontset",
12198#endif
12199#ifdef USE_XSMP
12200 "xsmp",
12201#endif
12202#ifdef USE_XSMP_INTERACT
12203 "xsmp_interact",
12204#endif
12205#ifdef FEAT_XCLIPBOARD
12206 "xterm_clipboard",
12207#endif
12208#ifdef FEAT_XTERM_SAVE
12209 "xterm_save",
12210#endif
12211#if defined(UNIX) && defined(FEAT_X11)
12212 "X11",
12213#endif
12214 NULL
12215 };
12216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012217 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218 for (i = 0; has_list[i] != NULL; ++i)
12219 if (STRICMP(name, has_list[i]) == 0)
12220 {
12221 n = TRUE;
12222 break;
12223 }
12224
12225 if (n == FALSE)
12226 {
12227 if (STRNICMP(name, "patch", 5) == 0)
12228 n = has_patch(atoi((char *)name + 5));
12229 else if (STRICMP(name, "vim_starting") == 0)
12230 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012231#ifdef FEAT_MBYTE
12232 else if (STRICMP(name, "multi_byte_encoding") == 0)
12233 n = has_mbyte;
12234#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012235#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12236 else if (STRICMP(name, "balloon_multiline") == 0)
12237 n = multiline_balloon_available();
12238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239#ifdef DYNAMIC_TCL
12240 else if (STRICMP(name, "tcl") == 0)
12241 n = tcl_enabled(FALSE);
12242#endif
12243#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12244 else if (STRICMP(name, "iconv") == 0)
12245 n = iconv_enabled(FALSE);
12246#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012247#ifdef DYNAMIC_LUA
12248 else if (STRICMP(name, "lua") == 0)
12249 n = lua_enabled(FALSE);
12250#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012251#ifdef DYNAMIC_MZSCHEME
12252 else if (STRICMP(name, "mzscheme") == 0)
12253 n = mzscheme_enabled(FALSE);
12254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255#ifdef DYNAMIC_RUBY
12256 else if (STRICMP(name, "ruby") == 0)
12257 n = ruby_enabled(FALSE);
12258#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012259#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260#ifdef DYNAMIC_PYTHON
12261 else if (STRICMP(name, "python") == 0)
12262 n = python_enabled(FALSE);
12263#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012264#endif
12265#ifdef FEAT_PYTHON3
12266#ifdef DYNAMIC_PYTHON3
12267 else if (STRICMP(name, "python3") == 0)
12268 n = python3_enabled(FALSE);
12269#endif
12270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271#ifdef DYNAMIC_PERL
12272 else if (STRICMP(name, "perl") == 0)
12273 n = perl_enabled(FALSE);
12274#endif
12275#ifdef FEAT_GUI
12276 else if (STRICMP(name, "gui_running") == 0)
12277 n = (gui.in_use || gui.starting);
12278# ifdef FEAT_GUI_W32
12279 else if (STRICMP(name, "gui_win32s") == 0)
12280 n = gui_is_win32s();
12281# endif
12282# ifdef FEAT_BROWSE
12283 else if (STRICMP(name, "browse") == 0)
12284 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12285# endif
12286#endif
12287#ifdef FEAT_SYN_HL
12288 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012289 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290#endif
12291#if defined(WIN3264)
12292 else if (STRICMP(name, "win95") == 0)
12293 n = mch_windows95();
12294#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012295#ifdef FEAT_NETBEANS_INTG
12296 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012297 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299 }
12300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012301 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302}
12303
12304/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012305 * "has_key()" function
12306 */
12307 static void
12308f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012309 typval_T *argvars;
12310 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012311{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012312 if (argvars[0].v_type != VAR_DICT)
12313 {
12314 EMSG(_(e_dictreq));
12315 return;
12316 }
12317 if (argvars[0].vval.v_dict == NULL)
12318 return;
12319
12320 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012321 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012322}
12323
12324/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012325 * "haslocaldir()" function
12326 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012327 static void
12328f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012329 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012330 typval_T *rettv;
12331{
12332 rettv->vval.v_number = (curwin->w_localdir != NULL);
12333}
12334
12335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336 * "hasmapto()" function
12337 */
12338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012339f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012340 typval_T *argvars;
12341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342{
12343 char_u *name;
12344 char_u *mode;
12345 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012346 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012348 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012349 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350 mode = (char_u *)"nvo";
12351 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012353 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012354 if (argvars[2].v_type != VAR_UNKNOWN)
12355 abbr = get_tv_number(&argvars[2]);
12356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357
Bram Moolenaar2c932302006-03-18 21:42:09 +000012358 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012359 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012361 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362}
12363
12364/*
12365 * "histadd()" function
12366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012368f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012369 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371{
12372#ifdef FEAT_CMDHIST
12373 int histype;
12374 char_u *str;
12375 char_u buf[NUMBUFLEN];
12376#endif
12377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012378 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379 if (check_restricted() || check_secure())
12380 return;
12381#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012382 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12383 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384 if (histype >= 0)
12385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012386 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387 if (*str != NUL)
12388 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012389 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012391 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392 return;
12393 }
12394 }
12395#endif
12396}
12397
12398/*
12399 * "histdel()" function
12400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012402f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012403 typval_T *argvars UNUSED;
12404 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405{
12406#ifdef FEAT_CMDHIST
12407 int n;
12408 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012409 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012411 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12412 if (str == NULL)
12413 n = 0;
12414 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012416 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012417 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012419 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012420 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421 else
12422 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012423 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012424 get_tv_string_buf(&argvars[1], buf));
12425 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012426#endif
12427}
12428
12429/*
12430 * "histget()" function
12431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012433f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012434 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012435 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436{
12437#ifdef FEAT_CMDHIST
12438 int type;
12439 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012440 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012442 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12443 if (str == NULL)
12444 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012446 {
12447 type = get_histtype(str);
12448 if (argvars[1].v_type == VAR_UNKNOWN)
12449 idx = get_history_idx(type);
12450 else
12451 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12452 /* -1 on type error */
12453 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012456 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012458 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459}
12460
12461/*
12462 * "histnr()" function
12463 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012465f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012466 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012468{
12469 int i;
12470
12471#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012472 char_u *history = get_tv_string_chk(&argvars[0]);
12473
12474 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475 if (i >= HIST_CMD && i < HIST_COUNT)
12476 i = get_history_idx(i);
12477 else
12478#endif
12479 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012480 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012481}
12482
12483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484 * "highlightID(name)" function
12485 */
12486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012487f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012488 typval_T *argvars;
12489 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012491 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492}
12493
12494/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012495 * "highlight_exists()" function
12496 */
12497 static void
12498f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012499 typval_T *argvars;
12500 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012501{
12502 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12503}
12504
12505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506 * "hostname()" function
12507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012509f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012510 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512{
12513 char_u hostname[256];
12514
12515 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012516 rettv->v_type = VAR_STRING;
12517 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012518}
12519
12520/*
12521 * iconv() function
12522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012524f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012525 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012526 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527{
12528#ifdef FEAT_MBYTE
12529 char_u buf1[NUMBUFLEN];
12530 char_u buf2[NUMBUFLEN];
12531 char_u *from, *to, *str;
12532 vimconv_T vimconv;
12533#endif
12534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012535 rettv->v_type = VAR_STRING;
12536 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537
12538#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012539 str = get_tv_string(&argvars[0]);
12540 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12541 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542 vimconv.vc_type = CONV_NONE;
12543 convert_setup(&vimconv, from, to);
12544
12545 /* If the encodings are equal, no conversion needed. */
12546 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012547 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012549 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012550
12551 convert_setup(&vimconv, NULL, NULL);
12552 vim_free(from);
12553 vim_free(to);
12554#endif
12555}
12556
12557/*
12558 * "indent()" function
12559 */
12560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012561f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012562 typval_T *argvars;
12563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012564{
12565 linenr_T lnum;
12566
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012567 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012568 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012569 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012570 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012571 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572}
12573
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012574/*
12575 * "index()" function
12576 */
12577 static void
12578f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012579 typval_T *argvars;
12580 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012581{
Bram Moolenaar33570922005-01-25 22:26:29 +000012582 list_T *l;
12583 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012584 long idx = 0;
12585 int ic = FALSE;
12586
12587 rettv->vval.v_number = -1;
12588 if (argvars[0].v_type != VAR_LIST)
12589 {
12590 EMSG(_(e_listreq));
12591 return;
12592 }
12593 l = argvars[0].vval.v_list;
12594 if (l != NULL)
12595 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012596 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012597 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012598 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012599 int error = FALSE;
12600
Bram Moolenaar758711c2005-02-02 23:11:38 +000012601 /* Start at specified item. Use the cached index that list_find()
12602 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012603 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012604 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012605 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012606 ic = get_tv_number_chk(&argvars[3], &error);
12607 if (error)
12608 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012609 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012610
Bram Moolenaar758711c2005-02-02 23:11:38 +000012611 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012612 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012613 {
12614 rettv->vval.v_number = idx;
12615 break;
12616 }
12617 }
12618}
12619
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620static int inputsecret_flag = 0;
12621
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012622static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12623
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012625 * This function is used by f_input() and f_inputdialog() functions. The third
12626 * argument to f_input() specifies the type of completion to use at the
12627 * prompt. The third argument to f_inputdialog() specifies the value to return
12628 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012629 */
12630 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012631get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012632 typval_T *argvars;
12633 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012634 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012636 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637 char_u *p = NULL;
12638 int c;
12639 char_u buf[NUMBUFLEN];
12640 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012641 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012642 int xp_type = EXPAND_NOTHING;
12643 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012645 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012646 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647
12648#ifdef NO_CONSOLE_INPUT
12649 /* While starting up, there is no place to enter text. */
12650 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012651 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652#endif
12653
12654 cmd_silent = FALSE; /* Want to see the prompt. */
12655 if (prompt != NULL)
12656 {
12657 /* Only the part of the message after the last NL is considered as
12658 * prompt for the command line */
12659 p = vim_strrchr(prompt, '\n');
12660 if (p == NULL)
12661 p = prompt;
12662 else
12663 {
12664 ++p;
12665 c = *p;
12666 *p = NUL;
12667 msg_start();
12668 msg_clr_eos();
12669 msg_puts_attr(prompt, echo_attr);
12670 msg_didout = FALSE;
12671 msg_starthere();
12672 *p = c;
12673 }
12674 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012675
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012676 if (argvars[1].v_type != VAR_UNKNOWN)
12677 {
12678 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12679 if (defstr != NULL)
12680 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012682 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012683 {
12684 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012685 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012686 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012687
Bram Moolenaar4463f292005-09-25 22:20:24 +000012688 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012689
Bram Moolenaar4463f292005-09-25 22:20:24 +000012690 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12691 if (xp_name == NULL)
12692 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012693
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012694 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012695
Bram Moolenaar4463f292005-09-25 22:20:24 +000012696 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12697 &xp_arg) == FAIL)
12698 return;
12699 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012700 }
12701
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012702 if (defstr != NULL)
12703 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012704 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12705 xp_type, xp_arg);
12706
12707 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012709 /* since the user typed this, no need to wait for return */
12710 need_wait_return = FALSE;
12711 msg_didout = FALSE;
12712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713 cmd_silent = cmd_silent_save;
12714}
12715
12716/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012717 * "input()" function
12718 * Also handles inputsecret() when inputsecret is set.
12719 */
12720 static void
12721f_input(argvars, rettv)
12722 typval_T *argvars;
12723 typval_T *rettv;
12724{
12725 get_user_input(argvars, rettv, FALSE);
12726}
12727
12728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729 * "inputdialog()" function
12730 */
12731 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012732f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012733 typval_T *argvars;
12734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735{
12736#if defined(FEAT_GUI_TEXTDIALOG)
12737 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12738 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12739 {
12740 char_u *message;
12741 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012742 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012744 message = get_tv_string_chk(&argvars[0]);
12745 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012746 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012747 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748 else
12749 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012750 if (message != NULL && defstr != NULL
12751 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010012752 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012753 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754 else
12755 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012756 if (message != NULL && defstr != NULL
12757 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012758 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012759 rettv->vval.v_string = vim_strsave(
12760 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012764 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765 }
12766 else
12767#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012768 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769}
12770
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012771/*
12772 * "inputlist()" function
12773 */
12774 static void
12775f_inputlist(argvars, rettv)
12776 typval_T *argvars;
12777 typval_T *rettv;
12778{
12779 listitem_T *li;
12780 int selected;
12781 int mouse_used;
12782
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012783#ifdef NO_CONSOLE_INPUT
12784 /* While starting up, there is no place to enter text. */
12785 if (no_console_input())
12786 return;
12787#endif
12788 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12789 {
12790 EMSG2(_(e_listarg), "inputlist()");
12791 return;
12792 }
12793
12794 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012795 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012796 lines_left = Rows; /* avoid more prompt */
12797 msg_scroll = TRUE;
12798 msg_clr_eos();
12799
12800 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12801 {
12802 msg_puts(get_tv_string(&li->li_tv));
12803 msg_putchar('\n');
12804 }
12805
12806 /* Ask for choice. */
12807 selected = prompt_for_number(&mouse_used);
12808 if (mouse_used)
12809 selected -= lines_left;
12810
12811 rettv->vval.v_number = selected;
12812}
12813
12814
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12816
12817/*
12818 * "inputrestore()" function
12819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012821f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012822 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824{
12825 if (ga_userinput.ga_len > 0)
12826 {
12827 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12829 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012830 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831 }
12832 else if (p_verbose > 1)
12833 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012834 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012835 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836 }
12837}
12838
12839/*
12840 * "inputsave()" function
12841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012843f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012844 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012846{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012847 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 if (ga_grow(&ga_userinput, 1) == OK)
12849 {
12850 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12851 + ga_userinput.ga_len);
12852 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012853 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854 }
12855 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012856 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012857}
12858
12859/*
12860 * "inputsecret()" function
12861 */
12862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012863f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012864 typval_T *argvars;
12865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866{
12867 ++cmdline_star;
12868 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012869 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870 --cmdline_star;
12871 --inputsecret_flag;
12872}
12873
12874/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012875 * "insert()" function
12876 */
12877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012878f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012879 typval_T *argvars;
12880 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012881{
12882 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012883 listitem_T *item;
12884 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012885 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012886
12887 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012888 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012889 else if ((l = argvars[0].vval.v_list) != NULL
12890 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012891 {
12892 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012893 before = get_tv_number_chk(&argvars[2], &error);
12894 if (error)
12895 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012896
Bram Moolenaar758711c2005-02-02 23:11:38 +000012897 if (before == l->lv_len)
12898 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012899 else
12900 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012901 item = list_find(l, before);
12902 if (item == NULL)
12903 {
12904 EMSGN(_(e_listidx), before);
12905 l = NULL;
12906 }
12907 }
12908 if (l != NULL)
12909 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012910 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012911 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012912 }
12913 }
12914}
12915
12916/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917 * "isdirectory()" function
12918 */
12919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012920f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012921 typval_T *argvars;
12922 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012924 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925}
12926
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012927/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012928 * "islocked()" function
12929 */
12930 static void
12931f_islocked(argvars, rettv)
12932 typval_T *argvars;
12933 typval_T *rettv;
12934{
12935 lval_T lv;
12936 char_u *end;
12937 dictitem_T *di;
12938
12939 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012940 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12941 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012942 if (end != NULL && lv.ll_name != NULL)
12943 {
12944 if (*end != NUL)
12945 EMSG(_(e_trailing));
12946 else
12947 {
12948 if (lv.ll_tv == NULL)
12949 {
12950 if (check_changedtick(lv.ll_name))
12951 rettv->vval.v_number = 1; /* always locked */
12952 else
12953 {
12954 di = find_var(lv.ll_name, NULL);
12955 if (di != NULL)
12956 {
12957 /* Consider a variable locked when:
12958 * 1. the variable itself is locked
12959 * 2. the value of the variable is locked.
12960 * 3. the List or Dict value is locked.
12961 */
12962 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12963 || tv_islocked(&di->di_tv));
12964 }
12965 }
12966 }
12967 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012968 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012969 else if (lv.ll_newkey != NULL)
12970 EMSG2(_(e_dictkey), lv.ll_newkey);
12971 else if (lv.ll_list != NULL)
12972 /* List item. */
12973 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12974 else
12975 /* Dictionary item. */
12976 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12977 }
12978 }
12979
12980 clear_lval(&lv);
12981}
12982
Bram Moolenaar33570922005-01-25 22:26:29 +000012983static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012984
12985/*
12986 * Turn a dict into a list:
12987 * "what" == 0: list of keys
12988 * "what" == 1: list of values
12989 * "what" == 2: list of items
12990 */
12991 static void
12992dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012993 typval_T *argvars;
12994 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012995 int what;
12996{
Bram Moolenaar33570922005-01-25 22:26:29 +000012997 list_T *l2;
12998 dictitem_T *di;
12999 hashitem_T *hi;
13000 listitem_T *li;
13001 listitem_T *li2;
13002 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013003 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013004
Bram Moolenaar8c711452005-01-14 21:53:12 +000013005 if (argvars[0].v_type != VAR_DICT)
13006 {
13007 EMSG(_(e_dictreq));
13008 return;
13009 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013010 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013011 return;
13012
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013013 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013014 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013015
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013016 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013017 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013018 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013019 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013020 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013021 --todo;
13022 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013023
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013024 li = listitem_alloc();
13025 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013026 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013027 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013028
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013029 if (what == 0)
13030 {
13031 /* keys() */
13032 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013033 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013034 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13035 }
13036 else if (what == 1)
13037 {
13038 /* values() */
13039 copy_tv(&di->di_tv, &li->li_tv);
13040 }
13041 else
13042 {
13043 /* items() */
13044 l2 = list_alloc();
13045 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013046 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013047 li->li_tv.vval.v_list = l2;
13048 if (l2 == NULL)
13049 break;
13050 ++l2->lv_refcount;
13051
13052 li2 = listitem_alloc();
13053 if (li2 == NULL)
13054 break;
13055 list_append(l2, li2);
13056 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013057 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013058 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13059
13060 li2 = listitem_alloc();
13061 if (li2 == NULL)
13062 break;
13063 list_append(l2, li2);
13064 copy_tv(&di->di_tv, &li2->li_tv);
13065 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013066 }
13067 }
13068}
13069
13070/*
13071 * "items(dict)" function
13072 */
13073 static void
13074f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013075 typval_T *argvars;
13076 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013077{
13078 dict_list(argvars, rettv, 2);
13079}
13080
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013082 * "join()" function
13083 */
13084 static void
13085f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013086 typval_T *argvars;
13087 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013088{
13089 garray_T ga;
13090 char_u *sep;
13091
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013092 if (argvars[0].v_type != VAR_LIST)
13093 {
13094 EMSG(_(e_listreq));
13095 return;
13096 }
13097 if (argvars[0].vval.v_list == NULL)
13098 return;
13099 if (argvars[1].v_type == VAR_UNKNOWN)
13100 sep = (char_u *)" ";
13101 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013102 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013103
13104 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013105
13106 if (sep != NULL)
13107 {
13108 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013109 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013110 ga_append(&ga, NUL);
13111 rettv->vval.v_string = (char_u *)ga.ga_data;
13112 }
13113 else
13114 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013115}
13116
13117/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013118 * "keys()" function
13119 */
13120 static void
13121f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013122 typval_T *argvars;
13123 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013124{
13125 dict_list(argvars, rettv, 0);
13126}
13127
13128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129 * "last_buffer_nr()" function.
13130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013132f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013133 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135{
13136 int n = 0;
13137 buf_T *buf;
13138
13139 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13140 if (n < buf->b_fnum)
13141 n = buf->b_fnum;
13142
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013143 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013144}
13145
13146/*
13147 * "len()" function
13148 */
13149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013150f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013151 typval_T *argvars;
13152 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013153{
13154 switch (argvars[0].v_type)
13155 {
13156 case VAR_STRING:
13157 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013158 rettv->vval.v_number = (varnumber_T)STRLEN(
13159 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013160 break;
13161 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013162 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013163 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013164 case VAR_DICT:
13165 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13166 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013167 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013168 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013169 break;
13170 }
13171}
13172
Bram Moolenaar33570922005-01-25 22:26:29 +000013173static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013174
13175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013176libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013177 typval_T *argvars;
13178 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013179 int type;
13180{
13181#ifdef FEAT_LIBCALL
13182 char_u *string_in;
13183 char_u **string_result;
13184 int nr_result;
13185#endif
13186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013187 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013188 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013189 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013190
13191 if (check_restricted() || check_secure())
13192 return;
13193
13194#ifdef FEAT_LIBCALL
13195 /* The first two args must be strings, otherwise its meaningless */
13196 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13197 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013198 string_in = NULL;
13199 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013200 string_in = argvars[2].vval.v_string;
13201 if (type == VAR_NUMBER)
13202 string_result = NULL;
13203 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013204 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013205 if (mch_libcall(argvars[0].vval.v_string,
13206 argvars[1].vval.v_string,
13207 string_in,
13208 argvars[2].vval.v_number,
13209 string_result,
13210 &nr_result) == OK
13211 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013212 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013213 }
13214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215}
13216
13217/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013218 * "libcall()" function
13219 */
13220 static void
13221f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013222 typval_T *argvars;
13223 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013224{
13225 libcall_common(argvars, rettv, VAR_STRING);
13226}
13227
13228/*
13229 * "libcallnr()" function
13230 */
13231 static void
13232f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013233 typval_T *argvars;
13234 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013235{
13236 libcall_common(argvars, rettv, VAR_NUMBER);
13237}
13238
13239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240 * "line(string)" function
13241 */
13242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013243f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013244 typval_T *argvars;
13245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246{
13247 linenr_T lnum = 0;
13248 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013249 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013251 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252 if (fp != NULL)
13253 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013254 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255}
13256
13257/*
13258 * "line2byte(lnum)" function
13259 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013261f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013262 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264{
13265#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013266 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013267#else
13268 linenr_T lnum;
13269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013270 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013272 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013274 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13275 if (rettv->vval.v_number >= 0)
13276 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277#endif
13278}
13279
13280/*
13281 * "lispindent(lnum)" function
13282 */
13283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013284f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013285 typval_T *argvars;
13286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287{
13288#ifdef FEAT_LISP
13289 pos_T pos;
13290 linenr_T lnum;
13291
13292 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013293 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13295 {
13296 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013297 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298 curwin->w_cursor = pos;
13299 }
13300 else
13301#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013302 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303}
13304
13305/*
13306 * "localtime()" function
13307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013309f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013310 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013313 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314}
13315
Bram Moolenaar33570922005-01-25 22:26:29 +000013316static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317
13318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013319get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013320 typval_T *argvars;
13321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322 int exact;
13323{
13324 char_u *keys;
13325 char_u *which;
13326 char_u buf[NUMBUFLEN];
13327 char_u *keys_buf = NULL;
13328 char_u *rhs;
13329 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013330 int abbr = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013331 int get_dict = FALSE;
13332 mapblock_T *mp;
13333 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013334
13335 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013336 rettv->v_type = VAR_STRING;
13337 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013339 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340 if (*keys == NUL)
13341 return;
13342
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013343 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013345 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013346 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013347 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013348 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013349 if (argvars[3].v_type != VAR_UNKNOWN)
13350 get_dict = get_tv_number(&argvars[3]);
13351 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353 else
13354 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013355 if (which == NULL)
13356 return;
13357
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358 mode = get_map_mode(&which, 0);
13359
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013360 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013361 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013363
13364 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013366 /* Return a string. */
13367 if (rhs != NULL)
13368 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369
Bram Moolenaarbd743252010-10-20 21:23:33 +020013370 }
13371 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13372 {
13373 /* Return a dictionary. */
13374 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13375 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13376 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377
Bram Moolenaarbd743252010-10-20 21:23:33 +020013378 dict_add_nr_str(dict, "lhs", 0L, lhs);
13379 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13380 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13381 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13382 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13383 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13384 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13385 dict_add_nr_str(dict, "mode", 0L, mapmode);
13386
13387 vim_free(lhs);
13388 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389 }
13390}
13391
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013392#ifdef FEAT_FLOAT
13393/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013394 * "log()" function
13395 */
13396 static void
13397f_log(argvars, rettv)
13398 typval_T *argvars;
13399 typval_T *rettv;
13400{
13401 float_T f;
13402
13403 rettv->v_type = VAR_FLOAT;
13404 if (get_float_arg(argvars, &f) == OK)
13405 rettv->vval.v_float = log(f);
13406 else
13407 rettv->vval.v_float = 0.0;
13408}
13409
13410/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013411 * "log10()" function
13412 */
13413 static void
13414f_log10(argvars, rettv)
13415 typval_T *argvars;
13416 typval_T *rettv;
13417{
13418 float_T f;
13419
13420 rettv->v_type = VAR_FLOAT;
13421 if (get_float_arg(argvars, &f) == OK)
13422 rettv->vval.v_float = log10(f);
13423 else
13424 rettv->vval.v_float = 0.0;
13425}
13426#endif
13427
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013429 * "map()" function
13430 */
13431 static void
13432f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013433 typval_T *argvars;
13434 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013435{
13436 filter_map(argvars, rettv, TRUE);
13437}
13438
13439/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013440 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441 */
13442 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013443f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013444 typval_T *argvars;
13445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013447 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448}
13449
13450/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013451 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452 */
13453 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013454f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013455 typval_T *argvars;
13456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013458 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459}
13460
Bram Moolenaar33570922005-01-25 22:26:29 +000013461static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462
13463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013464find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013465 typval_T *argvars;
13466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467 int type;
13468{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013469 char_u *str = NULL;
13470 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471 char_u *pat;
13472 regmatch_T regmatch;
13473 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013474 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475 char_u *save_cpo;
13476 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013477 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013478 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013479 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013480 list_T *l = NULL;
13481 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013482 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013483 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484
13485 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13486 save_cpo = p_cpo;
13487 p_cpo = (char_u *)"";
13488
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013489 rettv->vval.v_number = -1;
13490 if (type == 3)
13491 {
13492 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013493 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013494 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013495 }
13496 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013498 rettv->v_type = VAR_STRING;
13499 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013502 if (argvars[0].v_type == VAR_LIST)
13503 {
13504 if ((l = argvars[0].vval.v_list) == NULL)
13505 goto theend;
13506 li = l->lv_first;
13507 }
13508 else
13509 expr = str = get_tv_string(&argvars[0]);
13510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013511 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13512 if (pat == NULL)
13513 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013514
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013515 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013517 int error = FALSE;
13518
13519 start = get_tv_number_chk(&argvars[2], &error);
13520 if (error)
13521 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013522 if (l != NULL)
13523 {
13524 li = list_find(l, start);
13525 if (li == NULL)
13526 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013527 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013528 }
13529 else
13530 {
13531 if (start < 0)
13532 start = 0;
13533 if (start > (long)STRLEN(str))
13534 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013535 /* When "count" argument is there ignore matches before "start",
13536 * otherwise skip part of the string. Differs when pattern is "^"
13537 * or "\<". */
13538 if (argvars[3].v_type != VAR_UNKNOWN)
13539 startcol = start;
13540 else
13541 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013542 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013543
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013544 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013545 nth = get_tv_number_chk(&argvars[3], &error);
13546 if (error)
13547 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548 }
13549
13550 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13551 if (regmatch.regprog != NULL)
13552 {
13553 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013554
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013555 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013556 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013557 if (l != NULL)
13558 {
13559 if (li == NULL)
13560 {
13561 match = FALSE;
13562 break;
13563 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013564 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013565 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013566 if (str == NULL)
13567 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013568 }
13569
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013570 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013571
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013572 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013573 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013574 if (l == NULL && !match)
13575 break;
13576
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013577 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013578 if (l != NULL)
13579 {
13580 li = li->li_next;
13581 ++idx;
13582 }
13583 else
13584 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013585#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013586 startcol = (colnr_T)(regmatch.startp[0]
13587 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013588#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013589 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013590#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013591 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013592 }
13593
13594 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013596 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013597 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013598 int i;
13599
13600 /* return list with matched string and submatches */
13601 for (i = 0; i < NSUBEXP; ++i)
13602 {
13603 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013604 {
13605 if (list_append_string(rettv->vval.v_list,
13606 (char_u *)"", 0) == FAIL)
13607 break;
13608 }
13609 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013610 regmatch.startp[i],
13611 (int)(regmatch.endp[i] - regmatch.startp[i]))
13612 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013613 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013614 }
13615 }
13616 else if (type == 2)
13617 {
13618 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013619 if (l != NULL)
13620 copy_tv(&li->li_tv, rettv);
13621 else
13622 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013624 }
13625 else if (l != NULL)
13626 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627 else
13628 {
13629 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013630 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631 (varnumber_T)(regmatch.startp[0] - str);
13632 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013633 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013635 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636 }
13637 }
13638 vim_free(regmatch.regprog);
13639 }
13640
13641theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013642 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643 p_cpo = save_cpo;
13644}
13645
13646/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013647 * "match()" function
13648 */
13649 static void
13650f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013651 typval_T *argvars;
13652 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013653{
13654 find_some_match(argvars, rettv, 1);
13655}
13656
13657/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013658 * "matchadd()" function
13659 */
13660 static void
13661f_matchadd(argvars, rettv)
13662 typval_T *argvars;
13663 typval_T *rettv;
13664{
13665#ifdef FEAT_SEARCH_EXTRA
13666 char_u buf[NUMBUFLEN];
13667 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13668 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13669 int prio = 10; /* default priority */
13670 int id = -1;
13671 int error = FALSE;
13672
13673 rettv->vval.v_number = -1;
13674
13675 if (grp == NULL || pat == NULL)
13676 return;
13677 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013678 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013679 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013680 if (argvars[3].v_type != VAR_UNKNOWN)
13681 id = get_tv_number_chk(&argvars[3], &error);
13682 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013683 if (error == TRUE)
13684 return;
13685 if (id >= 1 && id <= 3)
13686 {
13687 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13688 return;
13689 }
13690
13691 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13692#endif
13693}
13694
13695/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013696 * "matcharg()" function
13697 */
13698 static void
13699f_matcharg(argvars, rettv)
13700 typval_T *argvars;
13701 typval_T *rettv;
13702{
13703 if (rettv_list_alloc(rettv) == OK)
13704 {
13705#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013706 int id = get_tv_number(&argvars[0]);
13707 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013708
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013709 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013710 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013711 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13712 {
13713 list_append_string(rettv->vval.v_list,
13714 syn_id2name(m->hlg_id), -1);
13715 list_append_string(rettv->vval.v_list, m->pattern, -1);
13716 }
13717 else
13718 {
13719 list_append_string(rettv->vval.v_list, NUL, -1);
13720 list_append_string(rettv->vval.v_list, NUL, -1);
13721 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013722 }
13723#endif
13724 }
13725}
13726
13727/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013728 * "matchdelete()" function
13729 */
13730 static void
13731f_matchdelete(argvars, rettv)
13732 typval_T *argvars;
13733 typval_T *rettv;
13734{
13735#ifdef FEAT_SEARCH_EXTRA
13736 rettv->vval.v_number = match_delete(curwin,
13737 (int)get_tv_number(&argvars[0]), TRUE);
13738#endif
13739}
13740
13741/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013742 * "matchend()" function
13743 */
13744 static void
13745f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013746 typval_T *argvars;
13747 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013748{
13749 find_some_match(argvars, rettv, 0);
13750}
13751
13752/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013753 * "matchlist()" function
13754 */
13755 static void
13756f_matchlist(argvars, rettv)
13757 typval_T *argvars;
13758 typval_T *rettv;
13759{
13760 find_some_match(argvars, rettv, 3);
13761}
13762
13763/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013764 * "matchstr()" function
13765 */
13766 static void
13767f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013768 typval_T *argvars;
13769 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013770{
13771 find_some_match(argvars, rettv, 2);
13772}
13773
Bram Moolenaar33570922005-01-25 22:26:29 +000013774static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013775
13776 static void
13777max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013778 typval_T *argvars;
13779 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013780 int domax;
13781{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013782 long n = 0;
13783 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013784 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013785
13786 if (argvars[0].v_type == VAR_LIST)
13787 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013788 list_T *l;
13789 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013790
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013791 l = argvars[0].vval.v_list;
13792 if (l != NULL)
13793 {
13794 li = l->lv_first;
13795 if (li != NULL)
13796 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013797 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013798 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013799 {
13800 li = li->li_next;
13801 if (li == NULL)
13802 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013803 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013804 if (domax ? i > n : i < n)
13805 n = i;
13806 }
13807 }
13808 }
13809 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013810 else if (argvars[0].v_type == VAR_DICT)
13811 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013812 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013813 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013814 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013815 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013816
13817 d = argvars[0].vval.v_dict;
13818 if (d != NULL)
13819 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013820 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013821 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013822 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013823 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013824 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013825 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013826 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013827 if (first)
13828 {
13829 n = i;
13830 first = FALSE;
13831 }
13832 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013833 n = i;
13834 }
13835 }
13836 }
13837 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013838 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013839 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013840 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013841}
13842
13843/*
13844 * "max()" function
13845 */
13846 static void
13847f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013848 typval_T *argvars;
13849 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013850{
13851 max_min(argvars, rettv, TRUE);
13852}
13853
13854/*
13855 * "min()" function
13856 */
13857 static void
13858f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013859 typval_T *argvars;
13860 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013861{
13862 max_min(argvars, rettv, FALSE);
13863}
13864
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013865static int mkdir_recurse __ARGS((char_u *dir, int prot));
13866
13867/*
13868 * Create the directory in which "dir" is located, and higher levels when
13869 * needed.
13870 */
13871 static int
13872mkdir_recurse(dir, prot)
13873 char_u *dir;
13874 int prot;
13875{
13876 char_u *p;
13877 char_u *updir;
13878 int r = FAIL;
13879
13880 /* Get end of directory name in "dir".
13881 * We're done when it's "/" or "c:/". */
13882 p = gettail_sep(dir);
13883 if (p <= get_past_head(dir))
13884 return OK;
13885
13886 /* If the directory exists we're done. Otherwise: create it.*/
13887 updir = vim_strnsave(dir, (int)(p - dir));
13888 if (updir == NULL)
13889 return FAIL;
13890 if (mch_isdir(updir))
13891 r = OK;
13892 else if (mkdir_recurse(updir, prot) == OK)
13893 r = vim_mkdir_emsg(updir, prot);
13894 vim_free(updir);
13895 return r;
13896}
13897
13898#ifdef vim_mkdir
13899/*
13900 * "mkdir()" function
13901 */
13902 static void
13903f_mkdir(argvars, rettv)
13904 typval_T *argvars;
13905 typval_T *rettv;
13906{
13907 char_u *dir;
13908 char_u buf[NUMBUFLEN];
13909 int prot = 0755;
13910
13911 rettv->vval.v_number = FAIL;
13912 if (check_restricted() || check_secure())
13913 return;
13914
13915 dir = get_tv_string_buf(&argvars[0], buf);
13916 if (argvars[1].v_type != VAR_UNKNOWN)
13917 {
13918 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013919 prot = get_tv_number_chk(&argvars[2], NULL);
13920 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013921 mkdir_recurse(dir, prot);
13922 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013923 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013924}
13925#endif
13926
Bram Moolenaar0d660222005-01-07 21:51:51 +000013927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928 * "mode()" function
13929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013931f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013932 typval_T *argvars;
13933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013935 char_u buf[3];
13936
13937 buf[1] = NUL;
13938 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939
13940#ifdef FEAT_VISUAL
13941 if (VIsual_active)
13942 {
13943 if (VIsual_select)
13944 buf[0] = VIsual_mode + 's' - 'v';
13945 else
13946 buf[0] = VIsual_mode;
13947 }
13948 else
13949#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013950 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13951 || State == CONFIRM)
13952 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013954 if (State == ASKMORE)
13955 buf[1] = 'm';
13956 else if (State == CONFIRM)
13957 buf[1] = '?';
13958 }
13959 else if (State == EXTERNCMD)
13960 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961 else if (State & INSERT)
13962 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013963#ifdef FEAT_VREPLACE
13964 if (State & VREPLACE_FLAG)
13965 {
13966 buf[0] = 'R';
13967 buf[1] = 'v';
13968 }
13969 else
13970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971 if (State & REPLACE_FLAG)
13972 buf[0] = 'R';
13973 else
13974 buf[0] = 'i';
13975 }
13976 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013977 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013979 if (exmode_active)
13980 buf[1] = 'v';
13981 }
13982 else if (exmode_active)
13983 {
13984 buf[0] = 'c';
13985 buf[1] = 'e';
13986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013988 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013989 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013990 if (finish_op)
13991 buf[1] = 'o';
13992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013994 /* Clear out the minor mode when the argument is not a non-zero number or
13995 * non-empty string. */
13996 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013997 buf[1] = NUL;
13998
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013999 rettv->vval.v_string = vim_strsave(buf);
14000 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001}
14002
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014003#ifdef FEAT_MZSCHEME
14004/*
14005 * "mzeval()" function
14006 */
14007 static void
14008f_mzeval(argvars, rettv)
14009 typval_T *argvars;
14010 typval_T *rettv;
14011{
14012 char_u *str;
14013 char_u buf[NUMBUFLEN];
14014
14015 str = get_tv_string_buf(&argvars[0], buf);
14016 do_mzeval(str, rettv);
14017}
14018#endif
14019
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014021 * "nextnonblank()" function
14022 */
14023 static void
14024f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014025 typval_T *argvars;
14026 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014027{
14028 linenr_T lnum;
14029
14030 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14031 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014032 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014033 {
14034 lnum = 0;
14035 break;
14036 }
14037 if (*skipwhite(ml_get(lnum)) != NUL)
14038 break;
14039 }
14040 rettv->vval.v_number = lnum;
14041}
14042
14043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044 * "nr2char()" function
14045 */
14046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014047f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014048 typval_T *argvars;
14049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050{
14051 char_u buf[NUMBUFLEN];
14052
14053#ifdef FEAT_MBYTE
14054 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014055 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056 else
14057#endif
14058 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014059 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060 buf[1] = NUL;
14061 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014062 rettv->v_type = VAR_STRING;
14063 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014064}
14065
14066/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014067 * "pathshorten()" function
14068 */
14069 static void
14070f_pathshorten(argvars, rettv)
14071 typval_T *argvars;
14072 typval_T *rettv;
14073{
14074 char_u *p;
14075
14076 rettv->v_type = VAR_STRING;
14077 p = get_tv_string_chk(&argvars[0]);
14078 if (p == NULL)
14079 rettv->vval.v_string = NULL;
14080 else
14081 {
14082 p = vim_strsave(p);
14083 rettv->vval.v_string = p;
14084 if (p != NULL)
14085 shorten_dir(p);
14086 }
14087}
14088
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014089#ifdef FEAT_FLOAT
14090/*
14091 * "pow()" function
14092 */
14093 static void
14094f_pow(argvars, rettv)
14095 typval_T *argvars;
14096 typval_T *rettv;
14097{
14098 float_T fx, fy;
14099
14100 rettv->v_type = VAR_FLOAT;
14101 if (get_float_arg(argvars, &fx) == OK
14102 && get_float_arg(&argvars[1], &fy) == OK)
14103 rettv->vval.v_float = pow(fx, fy);
14104 else
14105 rettv->vval.v_float = 0.0;
14106}
14107#endif
14108
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014109/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014110 * "prevnonblank()" function
14111 */
14112 static void
14113f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014114 typval_T *argvars;
14115 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014116{
14117 linenr_T lnum;
14118
14119 lnum = get_tv_lnum(argvars);
14120 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14121 lnum = 0;
14122 else
14123 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14124 --lnum;
14125 rettv->vval.v_number = lnum;
14126}
14127
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014128#ifdef HAVE_STDARG_H
14129/* This dummy va_list is here because:
14130 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14131 * - locally in the function results in a "used before set" warning
14132 * - using va_start() to initialize it gives "function with fixed args" error */
14133static va_list ap;
14134#endif
14135
Bram Moolenaar8c711452005-01-14 21:53:12 +000014136/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014137 * "printf()" function
14138 */
14139 static void
14140f_printf(argvars, rettv)
14141 typval_T *argvars;
14142 typval_T *rettv;
14143{
14144 rettv->v_type = VAR_STRING;
14145 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014146#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014147 {
14148 char_u buf[NUMBUFLEN];
14149 int len;
14150 char_u *s;
14151 int saved_did_emsg = did_emsg;
14152 char *fmt;
14153
14154 /* Get the required length, allocate the buffer and do it for real. */
14155 did_emsg = FALSE;
14156 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014157 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014158 if (!did_emsg)
14159 {
14160 s = alloc(len + 1);
14161 if (s != NULL)
14162 {
14163 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014164 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014165 }
14166 }
14167 did_emsg |= saved_did_emsg;
14168 }
14169#endif
14170}
14171
14172/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014173 * "pumvisible()" function
14174 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014175 static void
14176f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014177 typval_T *argvars UNUSED;
14178 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014179{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014180#ifdef FEAT_INS_EXPAND
14181 if (pum_visible())
14182 rettv->vval.v_number = 1;
14183#endif
14184}
14185
14186/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014187 * "range()" function
14188 */
14189 static void
14190f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014191 typval_T *argvars;
14192 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014193{
14194 long start;
14195 long end;
14196 long stride = 1;
14197 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014198 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014199
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014200 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014201 if (argvars[1].v_type == VAR_UNKNOWN)
14202 {
14203 end = start - 1;
14204 start = 0;
14205 }
14206 else
14207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014208 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014209 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014210 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014211 }
14212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014213 if (error)
14214 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014215 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014216 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014217 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014218 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014219 else
14220 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014221 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014222 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014223 if (list_append_number(rettv->vval.v_list,
14224 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014225 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014226 }
14227}
14228
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014229/*
14230 * "readfile()" function
14231 */
14232 static void
14233f_readfile(argvars, rettv)
14234 typval_T *argvars;
14235 typval_T *rettv;
14236{
14237 int binary = FALSE;
14238 char_u *fname;
14239 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014240 listitem_T *li;
14241#define FREAD_SIZE 200 /* optimized for text lines */
14242 char_u buf[FREAD_SIZE];
14243 int readlen; /* size of last fread() */
14244 int buflen; /* nr of valid chars in buf[] */
14245 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14246 int tolist; /* first byte in buf[] still to be put in list */
14247 int chop; /* how many CR to chop off */
14248 char_u *prev = NULL; /* previously read bytes, if any */
14249 int prevlen = 0; /* length of "prev" if not NULL */
14250 char_u *s;
14251 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014252 long maxline = MAXLNUM;
14253 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014254
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014255 if (argvars[1].v_type != VAR_UNKNOWN)
14256 {
14257 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14258 binary = TRUE;
14259 if (argvars[2].v_type != VAR_UNKNOWN)
14260 maxline = get_tv_number(&argvars[2]);
14261 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014262
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014263 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014264 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014265
14266 /* Always open the file in binary mode, library functions have a mind of
14267 * their own about CR-LF conversion. */
14268 fname = get_tv_string(&argvars[0]);
14269 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14270 {
14271 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14272 return;
14273 }
14274
14275 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014276 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014277 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014278 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014279 buflen = filtd + readlen;
14280 tolist = 0;
14281 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14282 {
14283 if (buf[filtd] == '\n' || readlen <= 0)
14284 {
14285 /* Only when in binary mode add an empty list item when the
14286 * last line ends in a '\n'. */
14287 if (!binary && readlen == 0 && filtd == 0)
14288 break;
14289
14290 /* Found end-of-line or end-of-file: add a text line to the
14291 * list. */
14292 chop = 0;
14293 if (!binary)
14294 while (filtd - chop - 1 >= tolist
14295 && buf[filtd - chop - 1] == '\r')
14296 ++chop;
14297 len = filtd - tolist - chop;
14298 if (prev == NULL)
14299 s = vim_strnsave(buf + tolist, len);
14300 else
14301 {
14302 s = alloc((unsigned)(prevlen + len + 1));
14303 if (s != NULL)
14304 {
14305 mch_memmove(s, prev, prevlen);
14306 vim_free(prev);
14307 prev = NULL;
14308 mch_memmove(s + prevlen, buf + tolist, len);
14309 s[prevlen + len] = NUL;
14310 }
14311 }
14312 tolist = filtd + 1;
14313
14314 li = listitem_alloc();
14315 if (li == NULL)
14316 {
14317 vim_free(s);
14318 break;
14319 }
14320 li->li_tv.v_type = VAR_STRING;
14321 li->li_tv.v_lock = 0;
14322 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014323 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014324
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014325 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014326 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014327 if (readlen <= 0)
14328 break;
14329 }
14330 else if (buf[filtd] == NUL)
14331 buf[filtd] = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014332#ifdef FEAT_MBYTE
14333 else if (buf[filtd] == 0xef
14334 && enc_utf8
14335 && filtd + 2 < buflen
14336 && !binary
14337 && buf[filtd + 1] == 0xbb
14338 && buf[filtd + 2] == 0xbf)
14339 {
14340 /* remove utf-8 byte order mark */
14341 mch_memmove(buf + filtd, buf + filtd + 3, buflen - filtd - 3);
14342 --filtd;
14343 buflen -= 3;
14344 }
14345#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014346 }
14347 if (readlen <= 0)
14348 break;
14349
14350 if (tolist == 0)
14351 {
14352 /* "buf" is full, need to move text to an allocated buffer */
14353 if (prev == NULL)
14354 {
14355 prev = vim_strnsave(buf, buflen);
14356 prevlen = buflen;
14357 }
14358 else
14359 {
14360 s = alloc((unsigned)(prevlen + buflen));
14361 if (s != NULL)
14362 {
14363 mch_memmove(s, prev, prevlen);
14364 mch_memmove(s + prevlen, buf, buflen);
14365 vim_free(prev);
14366 prev = s;
14367 prevlen += buflen;
14368 }
14369 }
14370 filtd = 0;
14371 }
14372 else
14373 {
14374 mch_memmove(buf, buf + tolist, buflen - tolist);
14375 filtd -= tolist;
14376 }
14377 }
14378
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014379 /*
14380 * For a negative line count use only the lines at the end of the file,
14381 * free the rest.
14382 */
14383 if (maxline < 0)
14384 while (cnt > -maxline)
14385 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014386 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014387 --cnt;
14388 }
14389
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014390 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014391 fclose(fd);
14392}
14393
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014394#if defined(FEAT_RELTIME)
14395static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14396
14397/*
14398 * Convert a List to proftime_T.
14399 * Return FAIL when there is something wrong.
14400 */
14401 static int
14402list2proftime(arg, tm)
14403 typval_T *arg;
14404 proftime_T *tm;
14405{
14406 long n1, n2;
14407 int error = FALSE;
14408
14409 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14410 || arg->vval.v_list->lv_len != 2)
14411 return FAIL;
14412 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14413 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14414# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014415 tm->HighPart = n1;
14416 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014417# else
14418 tm->tv_sec = n1;
14419 tm->tv_usec = n2;
14420# endif
14421 return error ? FAIL : OK;
14422}
14423#endif /* FEAT_RELTIME */
14424
14425/*
14426 * "reltime()" function
14427 */
14428 static void
14429f_reltime(argvars, rettv)
14430 typval_T *argvars;
14431 typval_T *rettv;
14432{
14433#ifdef FEAT_RELTIME
14434 proftime_T res;
14435 proftime_T start;
14436
14437 if (argvars[0].v_type == VAR_UNKNOWN)
14438 {
14439 /* No arguments: get current time. */
14440 profile_start(&res);
14441 }
14442 else if (argvars[1].v_type == VAR_UNKNOWN)
14443 {
14444 if (list2proftime(&argvars[0], &res) == FAIL)
14445 return;
14446 profile_end(&res);
14447 }
14448 else
14449 {
14450 /* Two arguments: compute the difference. */
14451 if (list2proftime(&argvars[0], &start) == FAIL
14452 || list2proftime(&argvars[1], &res) == FAIL)
14453 return;
14454 profile_sub(&res, &start);
14455 }
14456
14457 if (rettv_list_alloc(rettv) == OK)
14458 {
14459 long n1, n2;
14460
14461# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014462 n1 = res.HighPart;
14463 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014464# else
14465 n1 = res.tv_sec;
14466 n2 = res.tv_usec;
14467# endif
14468 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14469 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14470 }
14471#endif
14472}
14473
14474/*
14475 * "reltimestr()" function
14476 */
14477 static void
14478f_reltimestr(argvars, rettv)
14479 typval_T *argvars;
14480 typval_T *rettv;
14481{
14482#ifdef FEAT_RELTIME
14483 proftime_T tm;
14484#endif
14485
14486 rettv->v_type = VAR_STRING;
14487 rettv->vval.v_string = NULL;
14488#ifdef FEAT_RELTIME
14489 if (list2proftime(&argvars[0], &tm) == OK)
14490 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14491#endif
14492}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014493
Bram Moolenaar0d660222005-01-07 21:51:51 +000014494#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14495static void make_connection __ARGS((void));
14496static int check_connection __ARGS((void));
14497
14498 static void
14499make_connection()
14500{
14501 if (X_DISPLAY == NULL
14502# ifdef FEAT_GUI
14503 && !gui.in_use
14504# endif
14505 )
14506 {
14507 x_force_connect = TRUE;
14508 setup_term_clip();
14509 x_force_connect = FALSE;
14510 }
14511}
14512
14513 static int
14514check_connection()
14515{
14516 make_connection();
14517 if (X_DISPLAY == NULL)
14518 {
14519 EMSG(_("E240: No connection to Vim server"));
14520 return FAIL;
14521 }
14522 return OK;
14523}
14524#endif
14525
14526#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014527static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014528
14529 static void
14530remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014531 typval_T *argvars;
14532 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014533 int expr;
14534{
14535 char_u *server_name;
14536 char_u *keys;
14537 char_u *r = NULL;
14538 char_u buf[NUMBUFLEN];
14539# ifdef WIN32
14540 HWND w;
14541# else
14542 Window w;
14543# endif
14544
14545 if (check_restricted() || check_secure())
14546 return;
14547
14548# ifdef FEAT_X11
14549 if (check_connection() == FAIL)
14550 return;
14551# endif
14552
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014553 server_name = get_tv_string_chk(&argvars[0]);
14554 if (server_name == NULL)
14555 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014556 keys = get_tv_string_buf(&argvars[1], buf);
14557# ifdef WIN32
14558 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14559# else
14560 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14561 < 0)
14562# endif
14563 {
14564 if (r != NULL)
14565 EMSG(r); /* sending worked but evaluation failed */
14566 else
14567 EMSG2(_("E241: Unable to send to %s"), server_name);
14568 return;
14569 }
14570
14571 rettv->vval.v_string = r;
14572
14573 if (argvars[2].v_type != VAR_UNKNOWN)
14574 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014575 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014576 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014577 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014578
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014579 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014580 v.di_tv.v_type = VAR_STRING;
14581 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014582 idvar = get_tv_string_chk(&argvars[2]);
14583 if (idvar != NULL)
14584 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014585 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014586 }
14587}
14588#endif
14589
14590/*
14591 * "remote_expr()" function
14592 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014593 static void
14594f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014595 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014596 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014597{
14598 rettv->v_type = VAR_STRING;
14599 rettv->vval.v_string = NULL;
14600#ifdef FEAT_CLIENTSERVER
14601 remote_common(argvars, rettv, TRUE);
14602#endif
14603}
14604
14605/*
14606 * "remote_foreground()" function
14607 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014608 static void
14609f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014610 typval_T *argvars UNUSED;
14611 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014612{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014613#ifdef FEAT_CLIENTSERVER
14614# ifdef WIN32
14615 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014616 {
14617 char_u *server_name = get_tv_string_chk(&argvars[0]);
14618
14619 if (server_name != NULL)
14620 serverForeground(server_name);
14621 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014622# else
14623 /* Send a foreground() expression to the server. */
14624 argvars[1].v_type = VAR_STRING;
14625 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14626 argvars[2].v_type = VAR_UNKNOWN;
14627 remote_common(argvars, rettv, TRUE);
14628 vim_free(argvars[1].vval.v_string);
14629# endif
14630#endif
14631}
14632
Bram Moolenaar0d660222005-01-07 21:51:51 +000014633 static void
14634f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014635 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014636 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014637{
14638#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014639 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014640 char_u *s = NULL;
14641# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014642 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014643# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014644 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014645
14646 if (check_restricted() || check_secure())
14647 {
14648 rettv->vval.v_number = -1;
14649 return;
14650 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014651 serverid = get_tv_string_chk(&argvars[0]);
14652 if (serverid == NULL)
14653 {
14654 rettv->vval.v_number = -1;
14655 return; /* type error; errmsg already given */
14656 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014657# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014658 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014659 if (n == 0)
14660 rettv->vval.v_number = -1;
14661 else
14662 {
14663 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14664 rettv->vval.v_number = (s != NULL);
14665 }
14666# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014667 if (check_connection() == FAIL)
14668 return;
14669
14670 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014671 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014672# endif
14673
14674 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14675 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014676 char_u *retvar;
14677
Bram Moolenaar33570922005-01-25 22:26:29 +000014678 v.di_tv.v_type = VAR_STRING;
14679 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014680 retvar = get_tv_string_chk(&argvars[1]);
14681 if (retvar != NULL)
14682 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014683 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014684 }
14685#else
14686 rettv->vval.v_number = -1;
14687#endif
14688}
14689
Bram Moolenaar0d660222005-01-07 21:51:51 +000014690 static void
14691f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014692 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014693 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014694{
14695 char_u *r = NULL;
14696
14697#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014698 char_u *serverid = get_tv_string_chk(&argvars[0]);
14699
14700 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014701 {
14702# ifdef WIN32
14703 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014704 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014705
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014706 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014707 if (n != 0)
14708 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14709 if (r == NULL)
14710# else
14711 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014712 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014713# endif
14714 EMSG(_("E277: Unable to read a server reply"));
14715 }
14716#endif
14717 rettv->v_type = VAR_STRING;
14718 rettv->vval.v_string = r;
14719}
14720
14721/*
14722 * "remote_send()" function
14723 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014724 static void
14725f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014726 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014727 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014728{
14729 rettv->v_type = VAR_STRING;
14730 rettv->vval.v_string = NULL;
14731#ifdef FEAT_CLIENTSERVER
14732 remote_common(argvars, rettv, FALSE);
14733#endif
14734}
14735
14736/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014737 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014738 */
14739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014740f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014741 typval_T *argvars;
14742 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014743{
Bram Moolenaar33570922005-01-25 22:26:29 +000014744 list_T *l;
14745 listitem_T *item, *item2;
14746 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014747 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014748 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014749 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014750 dict_T *d;
14751 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014752
Bram Moolenaar8c711452005-01-14 21:53:12 +000014753 if (argvars[0].v_type == VAR_DICT)
14754 {
14755 if (argvars[2].v_type != VAR_UNKNOWN)
14756 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014757 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014758 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014759 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014760 key = get_tv_string_chk(&argvars[1]);
14761 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014762 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014763 di = dict_find(d, key, -1);
14764 if (di == NULL)
14765 EMSG2(_(e_dictkey), key);
14766 else
14767 {
14768 *rettv = di->di_tv;
14769 init_tv(&di->di_tv);
14770 dictitem_remove(d, di);
14771 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014772 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014773 }
14774 }
14775 else if (argvars[0].v_type != VAR_LIST)
14776 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014777 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014778 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014780 int error = FALSE;
14781
14782 idx = get_tv_number_chk(&argvars[1], &error);
14783 if (error)
14784 ; /* type error: do nothing, errmsg already given */
14785 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014786 EMSGN(_(e_listidx), idx);
14787 else
14788 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014789 if (argvars[2].v_type == VAR_UNKNOWN)
14790 {
14791 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014792 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014793 *rettv = item->li_tv;
14794 vim_free(item);
14795 }
14796 else
14797 {
14798 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014799 end = get_tv_number_chk(&argvars[2], &error);
14800 if (error)
14801 ; /* type error: do nothing */
14802 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014803 EMSGN(_(e_listidx), end);
14804 else
14805 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014806 int cnt = 0;
14807
14808 for (li = item; li != NULL; li = li->li_next)
14809 {
14810 ++cnt;
14811 if (li == item2)
14812 break;
14813 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014814 if (li == NULL) /* didn't find "item2" after "item" */
14815 EMSG(_(e_invrange));
14816 else
14817 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014818 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014819 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014820 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014821 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014822 l->lv_first = item;
14823 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014824 item->li_prev = NULL;
14825 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014826 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014827 }
14828 }
14829 }
14830 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014831 }
14832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833}
14834
14835/*
14836 * "rename({from}, {to})" function
14837 */
14838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014839f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014840 typval_T *argvars;
14841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842{
14843 char_u buf[NUMBUFLEN];
14844
14845 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014846 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014848 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14849 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014850}
14851
14852/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014853 * "repeat()" function
14854 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014855 static void
14856f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014857 typval_T *argvars;
14858 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014859{
14860 char_u *p;
14861 int n;
14862 int slen;
14863 int len;
14864 char_u *r;
14865 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014866
14867 n = get_tv_number(&argvars[1]);
14868 if (argvars[0].v_type == VAR_LIST)
14869 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014870 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014871 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014872 if (list_extend(rettv->vval.v_list,
14873 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014874 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014875 }
14876 else
14877 {
14878 p = get_tv_string(&argvars[0]);
14879 rettv->v_type = VAR_STRING;
14880 rettv->vval.v_string = NULL;
14881
14882 slen = (int)STRLEN(p);
14883 len = slen * n;
14884 if (len <= 0)
14885 return;
14886
14887 r = alloc(len + 1);
14888 if (r != NULL)
14889 {
14890 for (i = 0; i < n; i++)
14891 mch_memmove(r + i * slen, p, (size_t)slen);
14892 r[len] = NUL;
14893 }
14894
14895 rettv->vval.v_string = r;
14896 }
14897}
14898
14899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014900 * "resolve()" function
14901 */
14902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014903f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014904 typval_T *argvars;
14905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014906{
14907 char_u *p;
14908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014909 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910#ifdef FEAT_SHORTCUT
14911 {
14912 char_u *v = NULL;
14913
14914 v = mch_resolve_shortcut(p);
14915 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014916 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014917 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014918 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919 }
14920#else
14921# ifdef HAVE_READLINK
14922 {
14923 char_u buf[MAXPATHL + 1];
14924 char_u *cpy;
14925 int len;
14926 char_u *remain = NULL;
14927 char_u *q;
14928 int is_relative_to_current = FALSE;
14929 int has_trailing_pathsep = FALSE;
14930 int limit = 100;
14931
14932 p = vim_strsave(p);
14933
14934 if (p[0] == '.' && (vim_ispathsep(p[1])
14935 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14936 is_relative_to_current = TRUE;
14937
14938 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014939 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014940 has_trailing_pathsep = TRUE;
14941
14942 q = getnextcomp(p);
14943 if (*q != NUL)
14944 {
14945 /* Separate the first path component in "p", and keep the
14946 * remainder (beginning with the path separator). */
14947 remain = vim_strsave(q - 1);
14948 q[-1] = NUL;
14949 }
14950
14951 for (;;)
14952 {
14953 for (;;)
14954 {
14955 len = readlink((char *)p, (char *)buf, MAXPATHL);
14956 if (len <= 0)
14957 break;
14958 buf[len] = NUL;
14959
14960 if (limit-- == 0)
14961 {
14962 vim_free(p);
14963 vim_free(remain);
14964 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014965 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966 goto fail;
14967 }
14968
14969 /* Ensure that the result will have a trailing path separator
14970 * if the argument has one. */
14971 if (remain == NULL && has_trailing_pathsep)
14972 add_pathsep(buf);
14973
14974 /* Separate the first path component in the link value and
14975 * concatenate the remainders. */
14976 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14977 if (*q != NUL)
14978 {
14979 if (remain == NULL)
14980 remain = vim_strsave(q - 1);
14981 else
14982 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014983 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984 if (cpy != NULL)
14985 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986 vim_free(remain);
14987 remain = cpy;
14988 }
14989 }
14990 q[-1] = NUL;
14991 }
14992
14993 q = gettail(p);
14994 if (q > p && *q == NUL)
14995 {
14996 /* Ignore trailing path separator. */
14997 q[-1] = NUL;
14998 q = gettail(p);
14999 }
15000 if (q > p && !mch_isFullName(buf))
15001 {
15002 /* symlink is relative to directory of argument */
15003 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15004 if (cpy != NULL)
15005 {
15006 STRCPY(cpy, p);
15007 STRCPY(gettail(cpy), buf);
15008 vim_free(p);
15009 p = cpy;
15010 }
15011 }
15012 else
15013 {
15014 vim_free(p);
15015 p = vim_strsave(buf);
15016 }
15017 }
15018
15019 if (remain == NULL)
15020 break;
15021
15022 /* Append the first path component of "remain" to "p". */
15023 q = getnextcomp(remain + 1);
15024 len = q - remain - (*q != NUL);
15025 cpy = vim_strnsave(p, STRLEN(p) + len);
15026 if (cpy != NULL)
15027 {
15028 STRNCAT(cpy, remain, len);
15029 vim_free(p);
15030 p = cpy;
15031 }
15032 /* Shorten "remain". */
15033 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015034 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035 else
15036 {
15037 vim_free(remain);
15038 remain = NULL;
15039 }
15040 }
15041
15042 /* If the result is a relative path name, make it explicitly relative to
15043 * the current directory if and only if the argument had this form. */
15044 if (!vim_ispathsep(*p))
15045 {
15046 if (is_relative_to_current
15047 && *p != NUL
15048 && !(p[0] == '.'
15049 && (p[1] == NUL
15050 || vim_ispathsep(p[1])
15051 || (p[1] == '.'
15052 && (p[2] == NUL
15053 || vim_ispathsep(p[2]))))))
15054 {
15055 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015056 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015057 if (cpy != NULL)
15058 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059 vim_free(p);
15060 p = cpy;
15061 }
15062 }
15063 else if (!is_relative_to_current)
15064 {
15065 /* Strip leading "./". */
15066 q = p;
15067 while (q[0] == '.' && vim_ispathsep(q[1]))
15068 q += 2;
15069 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015070 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015071 }
15072 }
15073
15074 /* Ensure that the result will have no trailing path separator
15075 * if the argument had none. But keep "/" or "//". */
15076 if (!has_trailing_pathsep)
15077 {
15078 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015079 if (after_pathsep(p, q))
15080 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015081 }
15082
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015083 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084 }
15085# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015086 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015087# endif
15088#endif
15089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015090 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015091
15092#ifdef HAVE_READLINK
15093fail:
15094#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015095 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015096}
15097
15098/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015099 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015100 */
15101 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015102f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015103 typval_T *argvars;
15104 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015105{
Bram Moolenaar33570922005-01-25 22:26:29 +000015106 list_T *l;
15107 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108
Bram Moolenaar0d660222005-01-07 21:51:51 +000015109 if (argvars[0].v_type != VAR_LIST)
15110 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015111 else if ((l = argvars[0].vval.v_list) != NULL
15112 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015113 {
15114 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015115 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015116 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015117 while (li != NULL)
15118 {
15119 ni = li->li_prev;
15120 list_append(l, li);
15121 li = ni;
15122 }
15123 rettv->vval.v_list = l;
15124 rettv->v_type = VAR_LIST;
15125 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015126 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015128}
15129
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015130#define SP_NOMOVE 0x01 /* don't move cursor */
15131#define SP_REPEAT 0x02 /* repeat to find outer pair */
15132#define SP_RETCOUNT 0x04 /* return matchcount */
15133#define SP_SETPCMARK 0x08 /* set previous context mark */
15134#define SP_START 0x10 /* accept match at start position */
15135#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15136#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015137
Bram Moolenaar33570922005-01-25 22:26:29 +000015138static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015139
15140/*
15141 * Get flags for a search function.
15142 * Possibly sets "p_ws".
15143 * Returns BACKWARD, FORWARD or zero (for an error).
15144 */
15145 static int
15146get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015147 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015148 int *flagsp;
15149{
15150 int dir = FORWARD;
15151 char_u *flags;
15152 char_u nbuf[NUMBUFLEN];
15153 int mask;
15154
15155 if (varp->v_type != VAR_UNKNOWN)
15156 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015157 flags = get_tv_string_buf_chk(varp, nbuf);
15158 if (flags == NULL)
15159 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015160 while (*flags != NUL)
15161 {
15162 switch (*flags)
15163 {
15164 case 'b': dir = BACKWARD; break;
15165 case 'w': p_ws = TRUE; break;
15166 case 'W': p_ws = FALSE; break;
15167 default: mask = 0;
15168 if (flagsp != NULL)
15169 switch (*flags)
15170 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015171 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015172 case 'e': mask = SP_END; break;
15173 case 'm': mask = SP_RETCOUNT; break;
15174 case 'n': mask = SP_NOMOVE; break;
15175 case 'p': mask = SP_SUBPAT; break;
15176 case 'r': mask = SP_REPEAT; break;
15177 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015178 }
15179 if (mask == 0)
15180 {
15181 EMSG2(_(e_invarg2), flags);
15182 dir = 0;
15183 }
15184 else
15185 *flagsp |= mask;
15186 }
15187 if (dir == 0)
15188 break;
15189 ++flags;
15190 }
15191 }
15192 return dir;
15193}
15194
Bram Moolenaar071d4272004-06-13 20:20:40 +000015195/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015196 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015197 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015198 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015199search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015200 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015201 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015202 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015203{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015204 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205 char_u *pat;
15206 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015207 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015208 int save_p_ws = p_ws;
15209 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015210 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015211 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015212 proftime_T tm;
15213#ifdef FEAT_RELTIME
15214 long time_limit = 0;
15215#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015216 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015217 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015218
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015219 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015220 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015221 if (dir == 0)
15222 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015223 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015224 if (flags & SP_START)
15225 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015226 if (flags & SP_END)
15227 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015228
Bram Moolenaar76929292008-01-06 19:07:36 +000015229 /* Optional arguments: line number to stop searching and timeout. */
15230 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015231 {
15232 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15233 if (lnum_stop < 0)
15234 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015235#ifdef FEAT_RELTIME
15236 if (argvars[3].v_type != VAR_UNKNOWN)
15237 {
15238 time_limit = get_tv_number_chk(&argvars[3], NULL);
15239 if (time_limit < 0)
15240 goto theend;
15241 }
15242#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015243 }
15244
Bram Moolenaar76929292008-01-06 19:07:36 +000015245#ifdef FEAT_RELTIME
15246 /* Set the time limit, if there is one. */
15247 profile_setlimit(time_limit, &tm);
15248#endif
15249
Bram Moolenaar231334e2005-07-25 20:46:57 +000015250 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015251 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015252 * Check to make sure only those flags are set.
15253 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15254 * flags cannot be set. Check for that condition also.
15255 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015256 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015257 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015258 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015259 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015260 goto theend;
15261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015262
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015263 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015264 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015265 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015266 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015268 if (flags & SP_SUBPAT)
15269 retval = subpatnum;
15270 else
15271 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015272 if (flags & SP_SETPCMARK)
15273 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015275 if (match_pos != NULL)
15276 {
15277 /* Store the match cursor position */
15278 match_pos->lnum = pos.lnum;
15279 match_pos->col = pos.col + 1;
15280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015281 /* "/$" will put the cursor after the end of the line, may need to
15282 * correct that here */
15283 check_cursor();
15284 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015285
15286 /* If 'n' flag is used: restore cursor position. */
15287 if (flags & SP_NOMOVE)
15288 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015289 else
15290 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015291theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015292 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015293
15294 return retval;
15295}
15296
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015297#ifdef FEAT_FLOAT
15298/*
15299 * "round({float})" function
15300 */
15301 static void
15302f_round(argvars, rettv)
15303 typval_T *argvars;
15304 typval_T *rettv;
15305{
15306 float_T f;
15307
15308 rettv->v_type = VAR_FLOAT;
15309 if (get_float_arg(argvars, &f) == OK)
15310 /* round() is not in C90, use ceil() or floor() instead. */
15311 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15312 else
15313 rettv->vval.v_float = 0.0;
15314}
15315#endif
15316
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015317/*
15318 * "search()" function
15319 */
15320 static void
15321f_search(argvars, rettv)
15322 typval_T *argvars;
15323 typval_T *rettv;
15324{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015325 int flags = 0;
15326
15327 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328}
15329
Bram Moolenaar071d4272004-06-13 20:20:40 +000015330/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015331 * "searchdecl()" function
15332 */
15333 static void
15334f_searchdecl(argvars, rettv)
15335 typval_T *argvars;
15336 typval_T *rettv;
15337{
15338 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015339 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015340 int error = FALSE;
15341 char_u *name;
15342
15343 rettv->vval.v_number = 1; /* default: FAIL */
15344
15345 name = get_tv_string_chk(&argvars[0]);
15346 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015347 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015348 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015349 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15350 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15351 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015352 if (!error && name != NULL)
15353 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015354 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015355}
15356
15357/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015358 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015360 static int
15361searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015362 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015363 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364{
15365 char_u *spat, *mpat, *epat;
15366 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015367 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368 int dir;
15369 int flags = 0;
15370 char_u nbuf1[NUMBUFLEN];
15371 char_u nbuf2[NUMBUFLEN];
15372 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015373 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015374 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015375 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376
Bram Moolenaar071d4272004-06-13 20:20:40 +000015377 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015378 spat = get_tv_string_chk(&argvars[0]);
15379 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15380 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15381 if (spat == NULL || mpat == NULL || epat == NULL)
15382 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384 /* Handle the optional fourth argument: flags */
15385 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015386 if (dir == 0)
15387 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015388
15389 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015390 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15391 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015392 if ((flags & (SP_END | SP_SUBPAT)) != 0
15393 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015394 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015395 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015396 goto theend;
15397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015399 /* Using 'r' implies 'W', otherwise it doesn't work. */
15400 if (flags & SP_REPEAT)
15401 p_ws = FALSE;
15402
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015403 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015404 if (argvars[3].v_type == VAR_UNKNOWN
15405 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406 skip = (char_u *)"";
15407 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015409 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015410 if (argvars[5].v_type != VAR_UNKNOWN)
15411 {
15412 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15413 if (lnum_stop < 0)
15414 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015415#ifdef FEAT_RELTIME
15416 if (argvars[6].v_type != VAR_UNKNOWN)
15417 {
15418 time_limit = get_tv_number_chk(&argvars[6], NULL);
15419 if (time_limit < 0)
15420 goto theend;
15421 }
15422#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015423 }
15424 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015425 if (skip == NULL)
15426 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015428 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015429 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015430
15431theend:
15432 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015433
15434 return retval;
15435}
15436
15437/*
15438 * "searchpair()" function
15439 */
15440 static void
15441f_searchpair(argvars, rettv)
15442 typval_T *argvars;
15443 typval_T *rettv;
15444{
15445 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15446}
15447
15448/*
15449 * "searchpairpos()" function
15450 */
15451 static void
15452f_searchpairpos(argvars, rettv)
15453 typval_T *argvars;
15454 typval_T *rettv;
15455{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015456 pos_T match_pos;
15457 int lnum = 0;
15458 int col = 0;
15459
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015460 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015461 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015462
15463 if (searchpair_cmn(argvars, &match_pos) > 0)
15464 {
15465 lnum = match_pos.lnum;
15466 col = match_pos.col;
15467 }
15468
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015469 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15470 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015471}
15472
15473/*
15474 * Search for a start/middle/end thing.
15475 * Used by searchpair(), see its documentation for the details.
15476 * Returns 0 or -1 for no match,
15477 */
15478 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015479do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15480 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015481 char_u *spat; /* start pattern */
15482 char_u *mpat; /* middle pattern */
15483 char_u *epat; /* end pattern */
15484 int dir; /* BACKWARD or FORWARD */
15485 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015486 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015487 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015488 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015489 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015490{
15491 char_u *save_cpo;
15492 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15493 long retval = 0;
15494 pos_T pos;
15495 pos_T firstpos;
15496 pos_T foundpos;
15497 pos_T save_cursor;
15498 pos_T save_pos;
15499 int n;
15500 int r;
15501 int nest = 1;
15502 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015503 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015504 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015505
15506 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15507 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015508 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015509
Bram Moolenaar76929292008-01-06 19:07:36 +000015510#ifdef FEAT_RELTIME
15511 /* Set the time limit, if there is one. */
15512 profile_setlimit(time_limit, &tm);
15513#endif
15514
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015515 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15516 * start/middle/end (pat3, for the top pair). */
15517 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15518 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15519 if (pat2 == NULL || pat3 == NULL)
15520 goto theend;
15521 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15522 if (*mpat == NUL)
15523 STRCPY(pat3, pat2);
15524 else
15525 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15526 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015527 if (flags & SP_START)
15528 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015529
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530 save_cursor = curwin->w_cursor;
15531 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015532 clearpos(&firstpos);
15533 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534 pat = pat3;
15535 for (;;)
15536 {
15537 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015538 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15540 /* didn't find it or found the first match again: FAIL */
15541 break;
15542
15543 if (firstpos.lnum == 0)
15544 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015545 if (equalpos(pos, foundpos))
15546 {
15547 /* Found the same position again. Can happen with a pattern that
15548 * has "\zs" at the end and searching backwards. Advance one
15549 * character and try again. */
15550 if (dir == BACKWARD)
15551 decl(&pos);
15552 else
15553 incl(&pos);
15554 }
15555 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015557 /* clear the start flag to avoid getting stuck here */
15558 options &= ~SEARCH_START;
15559
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560 /* If the skip pattern matches, ignore this match. */
15561 if (*skip != NUL)
15562 {
15563 save_pos = curwin->w_cursor;
15564 curwin->w_cursor = pos;
15565 r = eval_to_bool(skip, &err, NULL, FALSE);
15566 curwin->w_cursor = save_pos;
15567 if (err)
15568 {
15569 /* Evaluating {skip} caused an error, break here. */
15570 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015571 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572 break;
15573 }
15574 if (r)
15575 continue;
15576 }
15577
15578 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15579 {
15580 /* Found end when searching backwards or start when searching
15581 * forward: nested pair. */
15582 ++nest;
15583 pat = pat2; /* nested, don't search for middle */
15584 }
15585 else
15586 {
15587 /* Found end when searching forward or start when searching
15588 * backward: end of (nested) pair; or found middle in outer pair. */
15589 if (--nest == 1)
15590 pat = pat3; /* outer level, search for middle */
15591 }
15592
15593 if (nest == 0)
15594 {
15595 /* Found the match: return matchcount or line number. */
15596 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015597 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015599 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015600 if (flags & SP_SETPCMARK)
15601 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602 curwin->w_cursor = pos;
15603 if (!(flags & SP_REPEAT))
15604 break;
15605 nest = 1; /* search for next unmatched */
15606 }
15607 }
15608
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015609 if (match_pos != NULL)
15610 {
15611 /* Store the match cursor position */
15612 match_pos->lnum = curwin->w_cursor.lnum;
15613 match_pos->col = curwin->w_cursor.col + 1;
15614 }
15615
Bram Moolenaar071d4272004-06-13 20:20:40 +000015616 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015617 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618 curwin->w_cursor = save_cursor;
15619
15620theend:
15621 vim_free(pat2);
15622 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015623 if (p_cpo == empty_option)
15624 p_cpo = save_cpo;
15625 else
15626 /* Darn, evaluating the {skip} expression changed the value. */
15627 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015628
15629 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630}
15631
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015632/*
15633 * "searchpos()" function
15634 */
15635 static void
15636f_searchpos(argvars, rettv)
15637 typval_T *argvars;
15638 typval_T *rettv;
15639{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015640 pos_T match_pos;
15641 int lnum = 0;
15642 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015643 int n;
15644 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015645
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015646 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015647 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015648
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015649 n = search_cmn(argvars, &match_pos, &flags);
15650 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015651 {
15652 lnum = match_pos.lnum;
15653 col = match_pos.col;
15654 }
15655
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015656 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15657 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015658 if (flags & SP_SUBPAT)
15659 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015660}
15661
15662
Bram Moolenaar0d660222005-01-07 21:51:51 +000015663 static void
15664f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015665 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015668#ifdef FEAT_CLIENTSERVER
15669 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015670 char_u *server = get_tv_string_chk(&argvars[0]);
15671 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672
Bram Moolenaar0d660222005-01-07 21:51:51 +000015673 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015674 if (server == NULL || reply == NULL)
15675 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015676 if (check_restricted() || check_secure())
15677 return;
15678# ifdef FEAT_X11
15679 if (check_connection() == FAIL)
15680 return;
15681# endif
15682
15683 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015685 EMSG(_("E258: Unable to send to client"));
15686 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015688 rettv->vval.v_number = 0;
15689#else
15690 rettv->vval.v_number = -1;
15691#endif
15692}
15693
Bram Moolenaar0d660222005-01-07 21:51:51 +000015694 static void
15695f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015696 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015697 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015698{
15699 char_u *r = NULL;
15700
15701#ifdef FEAT_CLIENTSERVER
15702# ifdef WIN32
15703 r = serverGetVimNames();
15704# else
15705 make_connection();
15706 if (X_DISPLAY != NULL)
15707 r = serverGetVimNames(X_DISPLAY);
15708# endif
15709#endif
15710 rettv->v_type = VAR_STRING;
15711 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712}
15713
15714/*
15715 * "setbufvar()" function
15716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015718f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015719 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015720 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721{
15722 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015725 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015726 char_u nbuf[NUMBUFLEN];
15727
15728 if (check_restricted() || check_secure())
15729 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015730 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15731 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015732 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733 varp = &argvars[2];
15734
15735 if (buf != NULL && varname != NULL && varp != NULL)
15736 {
15737 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739
15740 if (*varname == '&')
15741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015742 long numval;
15743 char_u *strval;
15744 int error = FALSE;
15745
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015747 numval = get_tv_number_chk(varp, &error);
15748 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015749 if (!error && strval != NULL)
15750 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015751 }
15752 else
15753 {
15754 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15755 if (bufvarname != NULL)
15756 {
15757 STRCPY(bufvarname, "b:");
15758 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015759 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 vim_free(bufvarname);
15761 }
15762 }
15763
15764 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767}
15768
15769/*
15770 * "setcmdpos()" function
15771 */
15772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015773f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015774 typval_T *argvars;
15775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015777 int pos = (int)get_tv_number(&argvars[0]) - 1;
15778
15779 if (pos >= 0)
15780 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781}
15782
15783/*
15784 * "setline()" function
15785 */
15786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015787f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015788 typval_T *argvars;
15789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790{
15791 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015792 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015793 list_T *l = NULL;
15794 listitem_T *li = NULL;
15795 long added = 0;
15796 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015797
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015798 lnum = get_tv_lnum(&argvars[0]);
15799 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015800 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015801 l = argvars[1].vval.v_list;
15802 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015804 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015805 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015806
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015807 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015808 for (;;)
15809 {
15810 if (l != NULL)
15811 {
15812 /* list argument, get next string */
15813 if (li == NULL)
15814 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015815 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015816 li = li->li_next;
15817 }
15818
15819 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015820 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015821 break;
15822 if (lnum <= curbuf->b_ml.ml_line_count)
15823 {
15824 /* existing line, replace it */
15825 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15826 {
15827 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015828 if (lnum == curwin->w_cursor.lnum)
15829 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015830 rettv->vval.v_number = 0; /* OK */
15831 }
15832 }
15833 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15834 {
15835 /* lnum is one past the last line, append the line */
15836 ++added;
15837 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15838 rettv->vval.v_number = 0; /* OK */
15839 }
15840
15841 if (l == NULL) /* only one string argument */
15842 break;
15843 ++lnum;
15844 }
15845
15846 if (added > 0)
15847 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848}
15849
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015850static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15851
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015853 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015854 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015855 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015856set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015857 win_T *wp UNUSED;
15858 typval_T *list_arg UNUSED;
15859 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015860 typval_T *rettv;
15861{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015862#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015863 char_u *act;
15864 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015865#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015866
Bram Moolenaar2641f772005-03-25 21:58:17 +000015867 rettv->vval.v_number = -1;
15868
15869#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015870 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015871 EMSG(_(e_listreq));
15872 else
15873 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015874 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015875
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015876 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015877 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015878 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015879 if (act == NULL)
15880 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015881 if (*act == 'a' || *act == 'r')
15882 action = *act;
15883 }
15884
Bram Moolenaarbc226b62010-08-09 22:14:48 +020015885 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015886 rettv->vval.v_number = 0;
15887 }
15888#endif
15889}
15890
15891/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015892 * "setloclist()" function
15893 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015894 static void
15895f_setloclist(argvars, rettv)
15896 typval_T *argvars;
15897 typval_T *rettv;
15898{
15899 win_T *win;
15900
15901 rettv->vval.v_number = -1;
15902
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015903 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015904 if (win != NULL)
15905 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15906}
15907
15908/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015909 * "setmatches()" function
15910 */
15911 static void
15912f_setmatches(argvars, rettv)
15913 typval_T *argvars;
15914 typval_T *rettv;
15915{
15916#ifdef FEAT_SEARCH_EXTRA
15917 list_T *l;
15918 listitem_T *li;
15919 dict_T *d;
15920
15921 rettv->vval.v_number = -1;
15922 if (argvars[0].v_type != VAR_LIST)
15923 {
15924 EMSG(_(e_listreq));
15925 return;
15926 }
15927 if ((l = argvars[0].vval.v_list) != NULL)
15928 {
15929
15930 /* To some extent make sure that we are dealing with a list from
15931 * "getmatches()". */
15932 li = l->lv_first;
15933 while (li != NULL)
15934 {
15935 if (li->li_tv.v_type != VAR_DICT
15936 || (d = li->li_tv.vval.v_dict) == NULL)
15937 {
15938 EMSG(_(e_invarg));
15939 return;
15940 }
15941 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15942 && dict_find(d, (char_u *)"pattern", -1) != NULL
15943 && dict_find(d, (char_u *)"priority", -1) != NULL
15944 && dict_find(d, (char_u *)"id", -1) != NULL))
15945 {
15946 EMSG(_(e_invarg));
15947 return;
15948 }
15949 li = li->li_next;
15950 }
15951
15952 clear_matches(curwin);
15953 li = l->lv_first;
15954 while (li != NULL)
15955 {
15956 d = li->li_tv.vval.v_dict;
15957 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15958 get_dict_string(d, (char_u *)"pattern", FALSE),
15959 (int)get_dict_number(d, (char_u *)"priority"),
15960 (int)get_dict_number(d, (char_u *)"id"));
15961 li = li->li_next;
15962 }
15963 rettv->vval.v_number = 0;
15964 }
15965#endif
15966}
15967
15968/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015969 * "setpos()" function
15970 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015971 static void
15972f_setpos(argvars, rettv)
15973 typval_T *argvars;
15974 typval_T *rettv;
15975{
15976 pos_T pos;
15977 int fnum;
15978 char_u *name;
15979
Bram Moolenaar08250432008-02-13 11:42:46 +000015980 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015981 name = get_tv_string_chk(argvars);
15982 if (name != NULL)
15983 {
15984 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15985 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015986 if (--pos.col < 0)
15987 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015988 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015989 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015990 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015991 if (fnum == curbuf->b_fnum)
15992 {
15993 curwin->w_cursor = pos;
15994 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015995 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015996 }
15997 else
15998 EMSG(_(e_invarg));
15999 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016000 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16001 {
16002 /* set mark */
16003 if (setmark_pos(name[1], &pos, fnum) == OK)
16004 rettv->vval.v_number = 0;
16005 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016006 else
16007 EMSG(_(e_invarg));
16008 }
16009 }
16010}
16011
16012/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016013 * "setqflist()" function
16014 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016015 static void
16016f_setqflist(argvars, rettv)
16017 typval_T *argvars;
16018 typval_T *rettv;
16019{
16020 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16021}
16022
16023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024 * "setreg()" function
16025 */
16026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016027f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016028 typval_T *argvars;
16029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016030{
16031 int regname;
16032 char_u *strregname;
16033 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016034 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016035 int append;
16036 char_u yank_type;
16037 long block_len;
16038
16039 block_len = -1;
16040 yank_type = MAUTO;
16041 append = FALSE;
16042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016043 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016044 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016046 if (strregname == NULL)
16047 return; /* type error; errmsg already given */
16048 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049 if (regname == 0 || regname == '@')
16050 regname = '"';
16051 else if (regname == '=')
16052 return;
16053
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016054 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016056 stropt = get_tv_string_chk(&argvars[2]);
16057 if (stropt == NULL)
16058 return; /* type error */
16059 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016060 switch (*stropt)
16061 {
16062 case 'a': case 'A': /* append */
16063 append = TRUE;
16064 break;
16065 case 'v': case 'c': /* character-wise selection */
16066 yank_type = MCHAR;
16067 break;
16068 case 'V': case 'l': /* line-wise selection */
16069 yank_type = MLINE;
16070 break;
16071#ifdef FEAT_VISUAL
16072 case 'b': case Ctrl_V: /* block-wise selection */
16073 yank_type = MBLOCK;
16074 if (VIM_ISDIGIT(stropt[1]))
16075 {
16076 ++stropt;
16077 block_len = getdigits(&stropt) - 1;
16078 --stropt;
16079 }
16080 break;
16081#endif
16082 }
16083 }
16084
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016085 strval = get_tv_string_chk(&argvars[1]);
16086 if (strval != NULL)
16087 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016089 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016090}
16091
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016092/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016093 * "settabvar()" function
16094 */
16095 static void
16096f_settabvar(argvars, rettv)
16097 typval_T *argvars;
16098 typval_T *rettv;
16099{
16100 tabpage_T *save_curtab;
16101 char_u *varname, *tabvarname;
16102 typval_T *varp;
16103 tabpage_T *tp;
16104
16105 rettv->vval.v_number = 0;
16106
16107 if (check_restricted() || check_secure())
16108 return;
16109
16110 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16111 varname = get_tv_string_chk(&argvars[1]);
16112 varp = &argvars[2];
16113
16114 if (tp != NULL && varname != NULL && varp != NULL)
16115 {
16116 save_curtab = curtab;
16117 goto_tabpage_tp(tp);
16118
16119 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16120 if (tabvarname != NULL)
16121 {
16122 STRCPY(tabvarname, "t:");
16123 STRCPY(tabvarname + 2, varname);
16124 set_var(tabvarname, varp, TRUE);
16125 vim_free(tabvarname);
16126 }
16127
16128 /* Restore current tabpage */
16129 if (valid_tabpage(save_curtab))
16130 goto_tabpage_tp(save_curtab);
16131 }
16132}
16133
16134/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016135 * "settabwinvar()" function
16136 */
16137 static void
16138f_settabwinvar(argvars, rettv)
16139 typval_T *argvars;
16140 typval_T *rettv;
16141{
16142 setwinvar(argvars, rettv, 1);
16143}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016144
16145/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016146 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016149f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016150 typval_T *argvars;
16151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016152{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016153 setwinvar(argvars, rettv, 0);
16154}
16155
16156/*
16157 * "setwinvar()" and "settabwinvar()" functions
16158 */
16159 static void
16160setwinvar(argvars, rettv, off)
16161 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016162 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016163 int off;
16164{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016165 win_T *win;
16166#ifdef FEAT_WINDOWS
16167 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016168 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169#endif
16170 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016171 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016173 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174
16175 if (check_restricted() || check_secure())
16176 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016177
16178#ifdef FEAT_WINDOWS
16179 if (off == 1)
16180 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16181 else
16182 tp = curtab;
16183#endif
16184 win = find_win_by_nr(&argvars[off], tp);
16185 varname = get_tv_string_chk(&argvars[off + 1]);
16186 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187
16188 if (win != NULL && varname != NULL && varp != NULL)
16189 {
16190#ifdef FEAT_WINDOWS
16191 /* set curwin to be our win, temporarily */
16192 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016193 save_curtab = curtab;
16194 goto_tabpage_tp(tp);
16195 if (!win_valid(win))
16196 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016197 curwin = win;
16198 curbuf = curwin->w_buffer;
16199#endif
16200
16201 if (*varname == '&')
16202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016203 long numval;
16204 char_u *strval;
16205 int error = FALSE;
16206
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016208 numval = get_tv_number_chk(varp, &error);
16209 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016210 if (!error && strval != NULL)
16211 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212 }
16213 else
16214 {
16215 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16216 if (winvarname != NULL)
16217 {
16218 STRCPY(winvarname, "w:");
16219 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016220 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221 vim_free(winvarname);
16222 }
16223 }
16224
16225#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016226 /* Restore current tabpage and window, if still valid (autocomands can
16227 * make them invalid). */
16228 if (valid_tabpage(save_curtab))
16229 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230 if (win_valid(save_curwin))
16231 {
16232 curwin = save_curwin;
16233 curbuf = curwin->w_buffer;
16234 }
16235#endif
16236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237}
16238
16239/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016240 * "shellescape({string})" function
16241 */
16242 static void
16243f_shellescape(argvars, rettv)
16244 typval_T *argvars;
16245 typval_T *rettv;
16246{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016247 rettv->vval.v_string = vim_strsave_shellescape(
16248 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016249 rettv->v_type = VAR_STRING;
16250}
16251
16252/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016253 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254 */
16255 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016256f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016257 typval_T *argvars;
16258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016260 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261
Bram Moolenaar0d660222005-01-07 21:51:51 +000016262 p = get_tv_string(&argvars[0]);
16263 rettv->vval.v_string = vim_strsave(p);
16264 simplify_filename(rettv->vval.v_string); /* simplify in place */
16265 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266}
16267
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016268#ifdef FEAT_FLOAT
16269/*
16270 * "sin()" function
16271 */
16272 static void
16273f_sin(argvars, rettv)
16274 typval_T *argvars;
16275 typval_T *rettv;
16276{
16277 float_T f;
16278
16279 rettv->v_type = VAR_FLOAT;
16280 if (get_float_arg(argvars, &f) == OK)
16281 rettv->vval.v_float = sin(f);
16282 else
16283 rettv->vval.v_float = 0.0;
16284}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016285
16286/*
16287 * "sinh()" function
16288 */
16289 static void
16290f_sinh(argvars, rettv)
16291 typval_T *argvars;
16292 typval_T *rettv;
16293{
16294 float_T f;
16295
16296 rettv->v_type = VAR_FLOAT;
16297 if (get_float_arg(argvars, &f) == OK)
16298 rettv->vval.v_float = sinh(f);
16299 else
16300 rettv->vval.v_float = 0.0;
16301}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016302#endif
16303
Bram Moolenaar0d660222005-01-07 21:51:51 +000016304static int
16305#ifdef __BORLANDC__
16306 _RTLENTRYF
16307#endif
16308 item_compare __ARGS((const void *s1, const void *s2));
16309static int
16310#ifdef __BORLANDC__
16311 _RTLENTRYF
16312#endif
16313 item_compare2 __ARGS((const void *s1, const void *s2));
16314
16315static int item_compare_ic;
16316static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016317static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016318#define ITEM_COMPARE_FAIL 999
16319
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016321 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016323 static int
16324#ifdef __BORLANDC__
16325_RTLENTRYF
16326#endif
16327item_compare(s1, s2)
16328 const void *s1;
16329 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016331 char_u *p1, *p2;
16332 char_u *tofree1, *tofree2;
16333 int res;
16334 char_u numbuf1[NUMBUFLEN];
16335 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016337 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16338 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016339 if (p1 == NULL)
16340 p1 = (char_u *)"";
16341 if (p2 == NULL)
16342 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016343 if (item_compare_ic)
16344 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016345 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016346 res = STRCMP(p1, p2);
16347 vim_free(tofree1);
16348 vim_free(tofree2);
16349 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016350}
16351
16352 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016353#ifdef __BORLANDC__
16354_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016355#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016356item_compare2(s1, s2)
16357 const void *s1;
16358 const void *s2;
16359{
16360 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016361 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016362 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016363 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016365 /* shortcut after failure in previous call; compare all items equal */
16366 if (item_compare_func_err)
16367 return 0;
16368
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016369 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16370 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016371 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16372 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016373
16374 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016375 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016376 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016377 clear_tv(&argv[0]);
16378 clear_tv(&argv[1]);
16379
16380 if (res == FAIL)
16381 res = ITEM_COMPARE_FAIL;
16382 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016383 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16384 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016385 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016386 clear_tv(&rettv);
16387 return res;
16388}
16389
16390/*
16391 * "sort({list})" function
16392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016394f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016395 typval_T *argvars;
16396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016397{
Bram Moolenaar33570922005-01-25 22:26:29 +000016398 list_T *l;
16399 listitem_T *li;
16400 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016401 long len;
16402 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016403
Bram Moolenaar0d660222005-01-07 21:51:51 +000016404 if (argvars[0].v_type != VAR_LIST)
16405 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016406 else
16407 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016408 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016409 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016410 return;
16411 rettv->vval.v_list = l;
16412 rettv->v_type = VAR_LIST;
16413 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414
Bram Moolenaar0d660222005-01-07 21:51:51 +000016415 len = list_len(l);
16416 if (len <= 1)
16417 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418
Bram Moolenaar0d660222005-01-07 21:51:51 +000016419 item_compare_ic = FALSE;
16420 item_compare_func = NULL;
16421 if (argvars[1].v_type != VAR_UNKNOWN)
16422 {
16423 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016424 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016425 else
16426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016427 int error = FALSE;
16428
16429 i = get_tv_number_chk(&argvars[1], &error);
16430 if (error)
16431 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016432 if (i == 1)
16433 item_compare_ic = TRUE;
16434 else
16435 item_compare_func = get_tv_string(&argvars[1]);
16436 }
16437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438
Bram Moolenaar0d660222005-01-07 21:51:51 +000016439 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016440 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016441 if (ptrs == NULL)
16442 return;
16443 i = 0;
16444 for (li = l->lv_first; li != NULL; li = li->li_next)
16445 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016446
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016447 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016448 /* test the compare function */
16449 if (item_compare_func != NULL
16450 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16451 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016452 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016454 {
16455 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016456 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016457 item_compare_func == NULL ? item_compare : item_compare2);
16458
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016459 if (!item_compare_func_err)
16460 {
16461 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016462 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016463 l->lv_len = 0;
16464 for (i = 0; i < len; ++i)
16465 list_append(l, ptrs[i]);
16466 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016467 }
16468
16469 vim_free(ptrs);
16470 }
16471}
16472
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016473/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016474 * "soundfold({word})" function
16475 */
16476 static void
16477f_soundfold(argvars, rettv)
16478 typval_T *argvars;
16479 typval_T *rettv;
16480{
16481 char_u *s;
16482
16483 rettv->v_type = VAR_STRING;
16484 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016485#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016486 rettv->vval.v_string = eval_soundfold(s);
16487#else
16488 rettv->vval.v_string = vim_strsave(s);
16489#endif
16490}
16491
16492/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016493 * "spellbadword()" function
16494 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016495 static void
16496f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016497 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016498 typval_T *rettv;
16499{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016500 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016501 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016502 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016503
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016504 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016505 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016506
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016507#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016508 if (argvars[0].v_type == VAR_UNKNOWN)
16509 {
16510 /* Find the start and length of the badly spelled word. */
16511 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16512 if (len != 0)
16513 word = ml_get_cursor();
16514 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016515 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016516 {
16517 char_u *str = get_tv_string_chk(&argvars[0]);
16518 int capcol = -1;
16519
16520 if (str != NULL)
16521 {
16522 /* Check the argument for spelling. */
16523 while (*str != NUL)
16524 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016525 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016526 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016527 {
16528 word = str;
16529 break;
16530 }
16531 str += len;
16532 }
16533 }
16534 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016535#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016536
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016537 list_append_string(rettv->vval.v_list, word, len);
16538 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016539 attr == HLF_SPB ? "bad" :
16540 attr == HLF_SPR ? "rare" :
16541 attr == HLF_SPL ? "local" :
16542 attr == HLF_SPC ? "caps" :
16543 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016544}
16545
16546/*
16547 * "spellsuggest()" function
16548 */
16549 static void
16550f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016551 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016552 typval_T *rettv;
16553{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016554#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016555 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016556 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016557 int maxcount;
16558 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016559 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016560 listitem_T *li;
16561 int need_capital = FALSE;
16562#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016563
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016564 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016565 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016566
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016567#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016568 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016569 {
16570 str = get_tv_string(&argvars[0]);
16571 if (argvars[1].v_type != VAR_UNKNOWN)
16572 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016573 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016574 if (maxcount <= 0)
16575 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016576 if (argvars[2].v_type != VAR_UNKNOWN)
16577 {
16578 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16579 if (typeerr)
16580 return;
16581 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016582 }
16583 else
16584 maxcount = 25;
16585
Bram Moolenaar4770d092006-01-12 23:22:24 +000016586 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016587
16588 for (i = 0; i < ga.ga_len; ++i)
16589 {
16590 str = ((char_u **)ga.ga_data)[i];
16591
16592 li = listitem_alloc();
16593 if (li == NULL)
16594 vim_free(str);
16595 else
16596 {
16597 li->li_tv.v_type = VAR_STRING;
16598 li->li_tv.v_lock = 0;
16599 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016600 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016601 }
16602 }
16603 ga_clear(&ga);
16604 }
16605#endif
16606}
16607
Bram Moolenaar0d660222005-01-07 21:51:51 +000016608 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016609f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016610 typval_T *argvars;
16611 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016612{
16613 char_u *str;
16614 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016615 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016616 regmatch_T regmatch;
16617 char_u patbuf[NUMBUFLEN];
16618 char_u *save_cpo;
16619 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016620 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016621 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016622 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016623
16624 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16625 save_cpo = p_cpo;
16626 p_cpo = (char_u *)"";
16627
16628 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016629 if (argvars[1].v_type != VAR_UNKNOWN)
16630 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016631 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16632 if (pat == NULL)
16633 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016634 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016635 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016636 }
16637 if (pat == NULL || *pat == NUL)
16638 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016639
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016640 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016642 if (typeerr)
16643 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644
Bram Moolenaar0d660222005-01-07 21:51:51 +000016645 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16646 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016647 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016648 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016649 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016650 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016651 if (*str == NUL)
16652 match = FALSE; /* empty item at the end */
16653 else
16654 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016655 if (match)
16656 end = regmatch.startp[0];
16657 else
16658 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016659 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16660 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016661 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016662 if (list_append_string(rettv->vval.v_list, str,
16663 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016664 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016665 }
16666 if (!match)
16667 break;
16668 /* Advance to just after the match. */
16669 if (regmatch.endp[0] > str)
16670 col = 0;
16671 else
16672 {
16673 /* Don't get stuck at the same match. */
16674#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016675 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016676#else
16677 col = 1;
16678#endif
16679 }
16680 str = regmatch.endp[0];
16681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016682
Bram Moolenaar0d660222005-01-07 21:51:51 +000016683 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016685
Bram Moolenaar0d660222005-01-07 21:51:51 +000016686 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687}
16688
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016689#ifdef FEAT_FLOAT
16690/*
16691 * "sqrt()" function
16692 */
16693 static void
16694f_sqrt(argvars, rettv)
16695 typval_T *argvars;
16696 typval_T *rettv;
16697{
16698 float_T f;
16699
16700 rettv->v_type = VAR_FLOAT;
16701 if (get_float_arg(argvars, &f) == OK)
16702 rettv->vval.v_float = sqrt(f);
16703 else
16704 rettv->vval.v_float = 0.0;
16705}
16706
16707/*
16708 * "str2float()" function
16709 */
16710 static void
16711f_str2float(argvars, rettv)
16712 typval_T *argvars;
16713 typval_T *rettv;
16714{
16715 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16716
16717 if (*p == '+')
16718 p = skipwhite(p + 1);
16719 (void)string2float(p, &rettv->vval.v_float);
16720 rettv->v_type = VAR_FLOAT;
16721}
16722#endif
16723
Bram Moolenaar2c932302006-03-18 21:42:09 +000016724/*
16725 * "str2nr()" function
16726 */
16727 static void
16728f_str2nr(argvars, rettv)
16729 typval_T *argvars;
16730 typval_T *rettv;
16731{
16732 int base = 10;
16733 char_u *p;
16734 long n;
16735
16736 if (argvars[1].v_type != VAR_UNKNOWN)
16737 {
16738 base = get_tv_number(&argvars[1]);
16739 if (base != 8 && base != 10 && base != 16)
16740 {
16741 EMSG(_(e_invarg));
16742 return;
16743 }
16744 }
16745
16746 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016747 if (*p == '+')
16748 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016749 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16750 rettv->vval.v_number = n;
16751}
16752
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753#ifdef HAVE_STRFTIME
16754/*
16755 * "strftime({format}[, {time}])" function
16756 */
16757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016758f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016759 typval_T *argvars;
16760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761{
16762 char_u result_buf[256];
16763 struct tm *curtime;
16764 time_t seconds;
16765 char_u *p;
16766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016767 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016769 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016770 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771 seconds = time(NULL);
16772 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016773 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774 curtime = localtime(&seconds);
16775 /* MSVC returns NULL for an invalid value of seconds. */
16776 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016777 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778 else
16779 {
16780# ifdef FEAT_MBYTE
16781 vimconv_T conv;
16782 char_u *enc;
16783
16784 conv.vc_type = CONV_NONE;
16785 enc = enc_locale();
16786 convert_setup(&conv, p_enc, enc);
16787 if (conv.vc_type != CONV_NONE)
16788 p = string_convert(&conv, p, NULL);
16789# endif
16790 if (p != NULL)
16791 (void)strftime((char *)result_buf, sizeof(result_buf),
16792 (char *)p, curtime);
16793 else
16794 result_buf[0] = NUL;
16795
16796# ifdef FEAT_MBYTE
16797 if (conv.vc_type != CONV_NONE)
16798 vim_free(p);
16799 convert_setup(&conv, enc, p_enc);
16800 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016801 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802 else
16803# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016804 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016805
16806# ifdef FEAT_MBYTE
16807 /* Release conversion descriptors */
16808 convert_setup(&conv, NULL, NULL);
16809 vim_free(enc);
16810# endif
16811 }
16812}
16813#endif
16814
16815/*
16816 * "stridx()" function
16817 */
16818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016819f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016820 typval_T *argvars;
16821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016822{
16823 char_u buf[NUMBUFLEN];
16824 char_u *needle;
16825 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016826 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016828 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016829
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016830 needle = get_tv_string_chk(&argvars[1]);
16831 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016832 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016833 if (needle == NULL || haystack == NULL)
16834 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835
Bram Moolenaar33570922005-01-25 22:26:29 +000016836 if (argvars[2].v_type != VAR_UNKNOWN)
16837 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016838 int error = FALSE;
16839
16840 start_idx = get_tv_number_chk(&argvars[2], &error);
16841 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016842 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016843 if (start_idx >= 0)
16844 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016845 }
16846
16847 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16848 if (pos != NULL)
16849 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850}
16851
16852/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016853 * "string()" function
16854 */
16855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016856f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016857 typval_T *argvars;
16858 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016859{
16860 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016861 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016862
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016863 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016864 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016865 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016866 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016867 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868}
16869
16870/*
16871 * "strlen()" function
16872 */
16873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016874f_strlen(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016878 rettv->vval.v_number = (varnumber_T)(STRLEN(
16879 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880}
16881
16882/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016883 * "strchars()" function
16884 */
16885 static void
16886f_strchars(argvars, rettv)
16887 typval_T *argvars;
16888 typval_T *rettv;
16889{
16890 char_u *s = get_tv_string(&argvars[0]);
16891#ifdef FEAT_MBYTE
16892 varnumber_T len = 0;
16893
16894 while (*s != NUL)
16895 {
16896 mb_cptr2char_adv(&s);
16897 ++len;
16898 }
16899 rettv->vval.v_number = len;
16900#else
16901 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16902#endif
16903}
16904
16905/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016906 * "strdisplaywidth()" function
16907 */
16908 static void
16909f_strdisplaywidth(argvars, rettv)
16910 typval_T *argvars;
16911 typval_T *rettv;
16912{
16913 char_u *s = get_tv_string(&argvars[0]);
16914 int col = 0;
16915
16916 if (argvars[1].v_type != VAR_UNKNOWN)
16917 col = get_tv_number(&argvars[1]);
16918
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016919 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016920}
16921
16922/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016923 * "strwidth()" function
16924 */
16925 static void
16926f_strwidth(argvars, rettv)
16927 typval_T *argvars;
16928 typval_T *rettv;
16929{
16930 char_u *s = get_tv_string(&argvars[0]);
16931
16932 rettv->vval.v_number = (varnumber_T)(
16933#ifdef FEAT_MBYTE
16934 mb_string2cells(s, -1)
16935#else
16936 STRLEN(s)
16937#endif
16938 );
16939}
16940
16941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016942 * "strpart()" function
16943 */
16944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016945f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016946 typval_T *argvars;
16947 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948{
16949 char_u *p;
16950 int n;
16951 int len;
16952 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016953 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016955 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956 slen = (int)STRLEN(p);
16957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016958 n = get_tv_number_chk(&argvars[1], &error);
16959 if (error)
16960 len = 0;
16961 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016962 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963 else
16964 len = slen - n; /* default len: all bytes that are available. */
16965
16966 /*
16967 * Only return the overlap between the specified part and the actual
16968 * string.
16969 */
16970 if (n < 0)
16971 {
16972 len += n;
16973 n = 0;
16974 }
16975 else if (n > slen)
16976 n = slen;
16977 if (len < 0)
16978 len = 0;
16979 else if (n + len > slen)
16980 len = slen - n;
16981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016982 rettv->v_type = VAR_STRING;
16983 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984}
16985
16986/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016987 * "strridx()" function
16988 */
16989 static void
16990f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016991 typval_T *argvars;
16992 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016993{
16994 char_u buf[NUMBUFLEN];
16995 char_u *needle;
16996 char_u *haystack;
16997 char_u *rest;
16998 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016999 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017000
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017001 needle = get_tv_string_chk(&argvars[1]);
17002 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017003
17004 rettv->vval.v_number = -1;
17005 if (needle == NULL || haystack == NULL)
17006 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017007
17008 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017009 if (argvars[2].v_type != VAR_UNKNOWN)
17010 {
17011 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017012 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017013 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017014 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017015 }
17016 else
17017 end_idx = haystack_len;
17018
Bram Moolenaar0d660222005-01-07 21:51:51 +000017019 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017020 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017021 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017022 lastmatch = haystack + end_idx;
17023 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017024 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017025 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017026 for (rest = haystack; *rest != '\0'; ++rest)
17027 {
17028 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017029 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017030 break;
17031 lastmatch = rest;
17032 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017033 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017034
17035 if (lastmatch == NULL)
17036 rettv->vval.v_number = -1;
17037 else
17038 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17039}
17040
17041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 * "strtrans()" function
17043 */
17044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017045f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017046 typval_T *argvars;
17047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017049 rettv->v_type = VAR_STRING;
17050 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017051}
17052
17053/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017054 * "submatch()" function
17055 */
17056 static void
17057f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017058 typval_T *argvars;
17059 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017060{
17061 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017062 rettv->vval.v_string =
17063 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017064}
17065
17066/*
17067 * "substitute()" function
17068 */
17069 static void
17070f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017071 typval_T *argvars;
17072 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017073{
17074 char_u patbuf[NUMBUFLEN];
17075 char_u subbuf[NUMBUFLEN];
17076 char_u flagsbuf[NUMBUFLEN];
17077
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017078 char_u *str = get_tv_string_chk(&argvars[0]);
17079 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17080 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17081 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17082
Bram Moolenaar0d660222005-01-07 21:51:51 +000017083 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017084 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17085 rettv->vval.v_string = NULL;
17086 else
17087 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017088}
17089
17090/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017091 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017094f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017095 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017096 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097{
17098 int id = 0;
17099#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017100 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101 long col;
17102 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017103 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017105 lnum = get_tv_lnum(argvars); /* -1 on type error */
17106 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17107 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017108
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017109 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017110 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017111 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017112#endif
17113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017114 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115}
17116
17117/*
17118 * "synIDattr(id, what [, mode])" function
17119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017121f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017122 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124{
17125 char_u *p = NULL;
17126#ifdef FEAT_SYN_HL
17127 int id;
17128 char_u *what;
17129 char_u *mode;
17130 char_u modebuf[NUMBUFLEN];
17131 int modec;
17132
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017133 id = get_tv_number(&argvars[0]);
17134 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017135 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017136 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017137 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017138 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017139 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140 modec = 0; /* replace invalid with current */
17141 }
17142 else
17143 {
17144#ifdef FEAT_GUI
17145 if (gui.in_use)
17146 modec = 'g';
17147 else
17148#endif
17149 if (t_colors > 1)
17150 modec = 'c';
17151 else
17152 modec = 't';
17153 }
17154
17155
17156 switch (TOLOWER_ASC(what[0]))
17157 {
17158 case 'b':
17159 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17160 p = highlight_color(id, what, modec);
17161 else /* bold */
17162 p = highlight_has_attr(id, HL_BOLD, modec);
17163 break;
17164
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017165 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 p = highlight_color(id, what, modec);
17167 break;
17168
17169 case 'i':
17170 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17171 p = highlight_has_attr(id, HL_INVERSE, modec);
17172 else /* italic */
17173 p = highlight_has_attr(id, HL_ITALIC, modec);
17174 break;
17175
17176 case 'n': /* name */
17177 p = get_highlight_name(NULL, id - 1);
17178 break;
17179
17180 case 'r': /* reverse */
17181 p = highlight_has_attr(id, HL_INVERSE, modec);
17182 break;
17183
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017184 case 's':
17185 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17186 p = highlight_color(id, what, modec);
17187 else /* standout */
17188 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189 break;
17190
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017191 case 'u':
17192 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17193 /* underline */
17194 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17195 else
17196 /* undercurl */
17197 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017198 break;
17199 }
17200
17201 if (p != NULL)
17202 p = vim_strsave(p);
17203#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017204 rettv->v_type = VAR_STRING;
17205 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017206}
17207
17208/*
17209 * "synIDtrans(id)" function
17210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017212f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017213 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017214 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215{
17216 int id;
17217
17218#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017219 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220
17221 if (id > 0)
17222 id = syn_get_final_id(id);
17223 else
17224#endif
17225 id = 0;
17226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017227 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228}
17229
17230/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017231 * "synconcealed(lnum, col)" function
17232 */
17233 static void
17234f_synconcealed(argvars, rettv)
17235 typval_T *argvars UNUSED;
17236 typval_T *rettv;
17237{
17238#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17239 long lnum;
17240 long col;
17241 int syntax_flags = 0;
17242 int cchar;
17243 int matchid = 0;
17244 char_u str[NUMBUFLEN];
17245#endif
17246
17247 rettv->v_type = VAR_LIST;
17248 rettv->vval.v_list = NULL;
17249
17250#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17251 lnum = get_tv_lnum(argvars); /* -1 on type error */
17252 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17253
17254 vim_memset(str, NUL, sizeof(str));
17255
17256 if (rettv_list_alloc(rettv) != FAIL)
17257 {
17258 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17259 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17260 && curwin->w_p_cole > 0)
17261 {
17262 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17263 syntax_flags = get_syntax_info(&matchid);
17264
17265 /* get the conceal character */
17266 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17267 {
17268 cchar = syn_get_sub_char();
17269 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17270 cchar = lcs_conceal;
17271 if (cchar != NUL)
17272 {
17273# ifdef FEAT_MBYTE
17274 if (has_mbyte)
17275 (*mb_char2bytes)(cchar, str);
17276 else
17277# endif
17278 str[0] = cchar;
17279 }
17280 }
17281 }
17282
17283 list_append_number(rettv->vval.v_list,
17284 (syntax_flags & HL_CONCEAL) != 0);
17285 /* -1 to auto-determine strlen */
17286 list_append_string(rettv->vval.v_list, str, -1);
17287 list_append_number(rettv->vval.v_list, matchid);
17288 }
17289#endif
17290}
17291
17292/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017293 * "synstack(lnum, col)" function
17294 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017295 static void
17296f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017297 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017298 typval_T *rettv;
17299{
17300#ifdef FEAT_SYN_HL
17301 long lnum;
17302 long col;
17303 int i;
17304 int id;
17305#endif
17306
17307 rettv->v_type = VAR_LIST;
17308 rettv->vval.v_list = NULL;
17309
17310#ifdef FEAT_SYN_HL
17311 lnum = get_tv_lnum(argvars); /* -1 on type error */
17312 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17313
17314 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017315 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017316 && rettv_list_alloc(rettv) != FAIL)
17317 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017318 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017319 for (i = 0; ; ++i)
17320 {
17321 id = syn_get_stack_item(i);
17322 if (id < 0)
17323 break;
17324 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17325 break;
17326 }
17327 }
17328#endif
17329}
17330
17331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 * "system()" function
17333 */
17334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017335f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017336 typval_T *argvars;
17337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017339 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017341 char_u *infile = NULL;
17342 char_u buf[NUMBUFLEN];
17343 int err = FALSE;
17344 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017346 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017347 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017348
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017349 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017350 {
17351 /*
17352 * Write the string to a temp file, to be used for input of the shell
17353 * command.
17354 */
17355 if ((infile = vim_tempname('i')) == NULL)
17356 {
17357 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017358 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017359 }
17360
17361 fd = mch_fopen((char *)infile, WRITEBIN);
17362 if (fd == NULL)
17363 {
17364 EMSG2(_(e_notopen), infile);
17365 goto done;
17366 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017367 p = get_tv_string_buf_chk(&argvars[1], buf);
17368 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017369 {
17370 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017371 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017372 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017373 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17374 err = TRUE;
17375 if (fclose(fd) != 0)
17376 err = TRUE;
17377 if (err)
17378 {
17379 EMSG(_("E677: Error writing temp file"));
17380 goto done;
17381 }
17382 }
17383
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017384 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17385 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017386
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387#ifdef USE_CR
17388 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017389 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 {
17391 char_u *s;
17392
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017393 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394 {
17395 if (*s == CAR)
17396 *s = NL;
17397 }
17398 }
17399#else
17400# ifdef USE_CRNL
17401 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017402 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 {
17404 char_u *s, *d;
17405
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017406 d = res;
17407 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408 {
17409 if (s[0] == CAR && s[1] == NL)
17410 ++s;
17411 *d++ = *s;
17412 }
17413 *d = NUL;
17414 }
17415# endif
17416#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017417
17418done:
17419 if (infile != NULL)
17420 {
17421 mch_remove(infile);
17422 vim_free(infile);
17423 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017424 rettv->v_type = VAR_STRING;
17425 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426}
17427
17428/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017429 * "tabpagebuflist()" function
17430 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017431 static void
17432f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017433 typval_T *argvars UNUSED;
17434 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017435{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017436#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017437 tabpage_T *tp;
17438 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017439
17440 if (argvars[0].v_type == VAR_UNKNOWN)
17441 wp = firstwin;
17442 else
17443 {
17444 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17445 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017446 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017447 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017448 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017449 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017450 for (; wp != NULL; wp = wp->w_next)
17451 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017452 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017453 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017454 }
17455#endif
17456}
17457
17458
17459/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017460 * "tabpagenr()" function
17461 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017462 static void
17463f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017464 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017465 typval_T *rettv;
17466{
17467 int nr = 1;
17468#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017469 char_u *arg;
17470
17471 if (argvars[0].v_type != VAR_UNKNOWN)
17472 {
17473 arg = get_tv_string_chk(&argvars[0]);
17474 nr = 0;
17475 if (arg != NULL)
17476 {
17477 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017478 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017479 else
17480 EMSG2(_(e_invexpr2), arg);
17481 }
17482 }
17483 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017484 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017485#endif
17486 rettv->vval.v_number = nr;
17487}
17488
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017489
17490#ifdef FEAT_WINDOWS
17491static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17492
17493/*
17494 * Common code for tabpagewinnr() and winnr().
17495 */
17496 static int
17497get_winnr(tp, argvar)
17498 tabpage_T *tp;
17499 typval_T *argvar;
17500{
17501 win_T *twin;
17502 int nr = 1;
17503 win_T *wp;
17504 char_u *arg;
17505
17506 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17507 if (argvar->v_type != VAR_UNKNOWN)
17508 {
17509 arg = get_tv_string_chk(argvar);
17510 if (arg == NULL)
17511 nr = 0; /* type error; errmsg already given */
17512 else if (STRCMP(arg, "$") == 0)
17513 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17514 else if (STRCMP(arg, "#") == 0)
17515 {
17516 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17517 if (twin == NULL)
17518 nr = 0;
17519 }
17520 else
17521 {
17522 EMSG2(_(e_invexpr2), arg);
17523 nr = 0;
17524 }
17525 }
17526
17527 if (nr > 0)
17528 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17529 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017530 {
17531 if (wp == NULL)
17532 {
17533 /* didn't find it in this tabpage */
17534 nr = 0;
17535 break;
17536 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017537 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017538 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017539 return nr;
17540}
17541#endif
17542
17543/*
17544 * "tabpagewinnr()" function
17545 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017546 static void
17547f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017548 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017549 typval_T *rettv;
17550{
17551 int nr = 1;
17552#ifdef FEAT_WINDOWS
17553 tabpage_T *tp;
17554
17555 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17556 if (tp == NULL)
17557 nr = 0;
17558 else
17559 nr = get_winnr(tp, &argvars[1]);
17560#endif
17561 rettv->vval.v_number = nr;
17562}
17563
17564
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017565/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017566 * "tagfiles()" function
17567 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017568 static void
17569f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017570 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017571 typval_T *rettv;
17572{
17573 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017574 tagname_T tn;
17575 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017576
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017577 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017578 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017579
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017580 for (first = TRUE; ; first = FALSE)
17581 if (get_tagfname(&tn, first, fname) == FAIL
17582 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017583 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017584 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017585}
17586
17587/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017588 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017589 */
17590 static void
17591f_taglist(argvars, rettv)
17592 typval_T *argvars;
17593 typval_T *rettv;
17594{
17595 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017596
17597 tag_pattern = get_tv_string(&argvars[0]);
17598
17599 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017600 if (*tag_pattern == NUL)
17601 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017602
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017603 if (rettv_list_alloc(rettv) == OK)
17604 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017605}
17606
17607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608 * "tempname()" function
17609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017611f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017612 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614{
17615 static int x = 'A';
17616
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017617 rettv->v_type = VAR_STRING;
17618 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619
17620 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17621 * names. Skip 'I' and 'O', they are used for shell redirection. */
17622 do
17623 {
17624 if (x == 'Z')
17625 x = '0';
17626 else if (x == '9')
17627 x = 'A';
17628 else
17629 {
17630#ifdef EBCDIC
17631 if (x == 'I')
17632 x = 'J';
17633 else if (x == 'R')
17634 x = 'S';
17635 else
17636#endif
17637 ++x;
17638 }
17639 } while (x == 'I' || x == 'O');
17640}
17641
17642/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017643 * "test(list)" function: Just checking the walls...
17644 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017645 static void
17646f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017647 typval_T *argvars UNUSED;
17648 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017649{
17650 /* Used for unit testing. Change the code below to your liking. */
17651#if 0
17652 listitem_T *li;
17653 list_T *l;
17654 char_u *bad, *good;
17655
17656 if (argvars[0].v_type != VAR_LIST)
17657 return;
17658 l = argvars[0].vval.v_list;
17659 if (l == NULL)
17660 return;
17661 li = l->lv_first;
17662 if (li == NULL)
17663 return;
17664 bad = get_tv_string(&li->li_tv);
17665 li = li->li_next;
17666 if (li == NULL)
17667 return;
17668 good = get_tv_string(&li->li_tv);
17669 rettv->vval.v_number = test_edit_score(bad, good);
17670#endif
17671}
17672
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017673#ifdef FEAT_FLOAT
17674/*
17675 * "tan()" function
17676 */
17677 static void
17678f_tan(argvars, rettv)
17679 typval_T *argvars;
17680 typval_T *rettv;
17681{
17682 float_T f;
17683
17684 rettv->v_type = VAR_FLOAT;
17685 if (get_float_arg(argvars, &f) == OK)
17686 rettv->vval.v_float = tan(f);
17687 else
17688 rettv->vval.v_float = 0.0;
17689}
17690
17691/*
17692 * "tanh()" function
17693 */
17694 static void
17695f_tanh(argvars, rettv)
17696 typval_T *argvars;
17697 typval_T *rettv;
17698{
17699 float_T f;
17700
17701 rettv->v_type = VAR_FLOAT;
17702 if (get_float_arg(argvars, &f) == OK)
17703 rettv->vval.v_float = tanh(f);
17704 else
17705 rettv->vval.v_float = 0.0;
17706}
17707#endif
17708
Bram Moolenaard52d9742005-08-21 22:20:28 +000017709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710 * "tolower(string)" function
17711 */
17712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017713f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017714 typval_T *argvars;
17715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716{
17717 char_u *p;
17718
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017719 p = vim_strsave(get_tv_string(&argvars[0]));
17720 rettv->v_type = VAR_STRING;
17721 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722
17723 if (p != NULL)
17724 while (*p != NUL)
17725 {
17726#ifdef FEAT_MBYTE
17727 int l;
17728
17729 if (enc_utf8)
17730 {
17731 int c, lc;
17732
17733 c = utf_ptr2char(p);
17734 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017735 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017736 /* TODO: reallocate string when byte count changes. */
17737 if (utf_char2len(lc) == l)
17738 utf_char2bytes(lc, p);
17739 p += l;
17740 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017741 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017742 p += l; /* skip multi-byte character */
17743 else
17744#endif
17745 {
17746 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17747 ++p;
17748 }
17749 }
17750}
17751
17752/*
17753 * "toupper(string)" function
17754 */
17755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017756f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017757 typval_T *argvars;
17758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017760 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017761 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762}
17763
17764/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017765 * "tr(string, fromstr, tostr)" function
17766 */
17767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017768f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017769 typval_T *argvars;
17770 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017771{
17772 char_u *instr;
17773 char_u *fromstr;
17774 char_u *tostr;
17775 char_u *p;
17776#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017777 int inlen;
17778 int fromlen;
17779 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017780 int idx;
17781 char_u *cpstr;
17782 int cplen;
17783 int first = TRUE;
17784#endif
17785 char_u buf[NUMBUFLEN];
17786 char_u buf2[NUMBUFLEN];
17787 garray_T ga;
17788
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017789 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017790 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17791 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017792
17793 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017794 rettv->v_type = VAR_STRING;
17795 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017796 if (fromstr == NULL || tostr == NULL)
17797 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017798 ga_init2(&ga, (int)sizeof(char), 80);
17799
17800#ifdef FEAT_MBYTE
17801 if (!has_mbyte)
17802#endif
17803 /* not multi-byte: fromstr and tostr must be the same length */
17804 if (STRLEN(fromstr) != STRLEN(tostr))
17805 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017806#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017807error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017808#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017809 EMSG2(_(e_invarg2), fromstr);
17810 ga_clear(&ga);
17811 return;
17812 }
17813
17814 /* fromstr and tostr have to contain the same number of chars */
17815 while (*instr != NUL)
17816 {
17817#ifdef FEAT_MBYTE
17818 if (has_mbyte)
17819 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017820 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017821 cpstr = instr;
17822 cplen = inlen;
17823 idx = 0;
17824 for (p = fromstr; *p != NUL; p += fromlen)
17825 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017826 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017827 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17828 {
17829 for (p = tostr; *p != NUL; p += tolen)
17830 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017831 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017832 if (idx-- == 0)
17833 {
17834 cplen = tolen;
17835 cpstr = p;
17836 break;
17837 }
17838 }
17839 if (*p == NUL) /* tostr is shorter than fromstr */
17840 goto error;
17841 break;
17842 }
17843 ++idx;
17844 }
17845
17846 if (first && cpstr == instr)
17847 {
17848 /* Check that fromstr and tostr have the same number of
17849 * (multi-byte) characters. Done only once when a character
17850 * of instr doesn't appear in fromstr. */
17851 first = FALSE;
17852 for (p = tostr; *p != NUL; p += tolen)
17853 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017854 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017855 --idx;
17856 }
17857 if (idx != 0)
17858 goto error;
17859 }
17860
17861 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017862 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017863 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017864
17865 instr += inlen;
17866 }
17867 else
17868#endif
17869 {
17870 /* When not using multi-byte chars we can do it faster. */
17871 p = vim_strchr(fromstr, *instr);
17872 if (p != NULL)
17873 ga_append(&ga, tostr[p - fromstr]);
17874 else
17875 ga_append(&ga, *instr);
17876 ++instr;
17877 }
17878 }
17879
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017880 /* add a terminating NUL */
17881 ga_grow(&ga, 1);
17882 ga_append(&ga, NUL);
17883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017884 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017885}
17886
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017887#ifdef FEAT_FLOAT
17888/*
17889 * "trunc({float})" function
17890 */
17891 static void
17892f_trunc(argvars, rettv)
17893 typval_T *argvars;
17894 typval_T *rettv;
17895{
17896 float_T f;
17897
17898 rettv->v_type = VAR_FLOAT;
17899 if (get_float_arg(argvars, &f) == OK)
17900 /* trunc() is not in C90, use floor() or ceil() instead. */
17901 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17902 else
17903 rettv->vval.v_float = 0.0;
17904}
17905#endif
17906
Bram Moolenaar8299df92004-07-10 09:47:34 +000017907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908 * "type(expr)" function
17909 */
17910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017911f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017912 typval_T *argvars;
17913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017914{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017915 int n;
17916
17917 switch (argvars[0].v_type)
17918 {
17919 case VAR_NUMBER: n = 0; break;
17920 case VAR_STRING: n = 1; break;
17921 case VAR_FUNC: n = 2; break;
17922 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017923 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017924#ifdef FEAT_FLOAT
17925 case VAR_FLOAT: n = 5; break;
17926#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017927 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17928 }
17929 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930}
17931
17932/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017933 * "undofile(name)" function
17934 */
17935 static void
17936f_undofile(argvars, rettv)
17937 typval_T *argvars;
17938 typval_T *rettv;
17939{
17940 rettv->v_type = VAR_STRING;
17941#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017942 {
17943 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17944
17945 if (ffname != NULL)
17946 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17947 vim_free(ffname);
17948 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017949#else
17950 rettv->vval.v_string = NULL;
17951#endif
17952}
17953
17954/*
Bram Moolenaara800b422010-06-27 01:15:55 +020017955 * "undotree()" function
17956 */
17957 static void
17958f_undotree(argvars, rettv)
17959 typval_T *argvars UNUSED;
17960 typval_T *rettv;
17961{
17962 if (rettv_dict_alloc(rettv) == OK)
17963 {
17964 dict_T *dict = rettv->vval.v_dict;
17965 list_T *list;
17966
Bram Moolenaar730cde92010-06-27 05:18:54 +020017967 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017968 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017969 dict_add_nr_str(dict, "save_last",
17970 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017971 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
17972 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017973 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017974
17975 list = list_alloc();
17976 if (list != NULL)
17977 {
17978 u_eval_tree(curbuf->b_u_oldhead, list);
17979 dict_add_list(dict, "entries", list);
17980 }
17981 }
17982}
17983
17984/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017985 * "values(dict)" function
17986 */
17987 static void
17988f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017989 typval_T *argvars;
17990 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017991{
17992 dict_list(argvars, rettv, 1);
17993}
17994
17995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996 * "virtcol(string)" function
17997 */
17998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017999f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018000 typval_T *argvars;
18001 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002{
18003 colnr_T vcol = 0;
18004 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018005 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018006
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018007 fp = var2fpos(&argvars[0], FALSE, &fnum);
18008 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18009 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010 {
18011 getvvcol(curwin, fp, NULL, NULL, &vcol);
18012 ++vcol;
18013 }
18014
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018015 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016}
18017
18018/*
18019 * "visualmode()" function
18020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018022f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018023 typval_T *argvars UNUSED;
18024 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025{
18026#ifdef FEAT_VISUAL
18027 char_u str[2];
18028
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018029 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030 str[0] = curbuf->b_visual_mode_eval;
18031 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018032 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033
18034 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018035 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018037#endif
18038}
18039
18040/*
18041 * "winbufnr(nr)" function
18042 */
18043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018044f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018045 typval_T *argvars;
18046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047{
18048 win_T *wp;
18049
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018050 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018052 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018054 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055}
18056
18057/*
18058 * "wincol()" function
18059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018061f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018062 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064{
18065 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018066 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067}
18068
18069/*
18070 * "winheight(nr)" function
18071 */
18072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018073f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018074 typval_T *argvars;
18075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076{
18077 win_T *wp;
18078
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018079 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018081 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018083 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084}
18085
18086/*
18087 * "winline()" function
18088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018090f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018091 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093{
18094 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018095 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096}
18097
18098/*
18099 * "winnr()" function
18100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018102f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018103 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018104 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105{
18106 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018107
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018109 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018111 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112}
18113
18114/*
18115 * "winrestcmd()" function
18116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018118f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018119 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121{
18122#ifdef FEAT_WINDOWS
18123 win_T *wp;
18124 int winnr = 1;
18125 garray_T ga;
18126 char_u buf[50];
18127
18128 ga_init2(&ga, (int)sizeof(char), 70);
18129 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18130 {
18131 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18132 ga_concat(&ga, buf);
18133# ifdef FEAT_VERTSPLIT
18134 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18135 ga_concat(&ga, buf);
18136# endif
18137 ++winnr;
18138 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018139 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018141 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018143 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018144#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018145 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146}
18147
18148/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018149 * "winrestview()" function
18150 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018151 static void
18152f_winrestview(argvars, rettv)
18153 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018154 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018155{
18156 dict_T *dict;
18157
18158 if (argvars[0].v_type != VAR_DICT
18159 || (dict = argvars[0].vval.v_dict) == NULL)
18160 EMSG(_(e_invarg));
18161 else
18162 {
18163 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18164 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18165#ifdef FEAT_VIRTUALEDIT
18166 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18167#endif
18168 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018169 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018170
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018171 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018172#ifdef FEAT_DIFF
18173 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18174#endif
18175 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18176 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18177
18178 check_cursor();
18179 changed_cline_bef_curs();
18180 invalidate_botline();
18181 redraw_later(VALID);
18182
18183 if (curwin->w_topline == 0)
18184 curwin->w_topline = 1;
18185 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18186 curwin->w_topline = curbuf->b_ml.ml_line_count;
18187#ifdef FEAT_DIFF
18188 check_topfill(curwin, TRUE);
18189#endif
18190 }
18191}
18192
18193/*
18194 * "winsaveview()" function
18195 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018196 static void
18197f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018198 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018199 typval_T *rettv;
18200{
18201 dict_T *dict;
18202
Bram Moolenaara800b422010-06-27 01:15:55 +020018203 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018204 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018205 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018206
18207 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18208 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18209#ifdef FEAT_VIRTUALEDIT
18210 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18211#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018212 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018213 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18214
18215 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18216#ifdef FEAT_DIFF
18217 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18218#endif
18219 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18220 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18221}
18222
18223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018224 * "winwidth(nr)" function
18225 */
18226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018227f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018228 typval_T *argvars;
18229 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230{
18231 win_T *wp;
18232
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018233 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018234 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018235 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018236 else
18237#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018238 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018240 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018241#endif
18242}
18243
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018245 * "writefile()" function
18246 */
18247 static void
18248f_writefile(argvars, rettv)
18249 typval_T *argvars;
18250 typval_T *rettv;
18251{
18252 int binary = FALSE;
18253 char_u *fname;
18254 FILE *fd;
18255 listitem_T *li;
18256 char_u *s;
18257 int ret = 0;
18258 int c;
18259
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018260 if (check_restricted() || check_secure())
18261 return;
18262
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018263 if (argvars[0].v_type != VAR_LIST)
18264 {
18265 EMSG2(_(e_listarg), "writefile()");
18266 return;
18267 }
18268 if (argvars[0].vval.v_list == NULL)
18269 return;
18270
18271 if (argvars[2].v_type != VAR_UNKNOWN
18272 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18273 binary = TRUE;
18274
18275 /* Always open the file in binary mode, library functions have a mind of
18276 * their own about CR-LF conversion. */
18277 fname = get_tv_string(&argvars[1]);
18278 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18279 {
18280 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18281 ret = -1;
18282 }
18283 else
18284 {
18285 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18286 li = li->li_next)
18287 {
18288 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18289 {
18290 if (*s == '\n')
18291 c = putc(NUL, fd);
18292 else
18293 c = putc(*s, fd);
18294 if (c == EOF)
18295 {
18296 ret = -1;
18297 break;
18298 }
18299 }
18300 if (!binary || li->li_next != NULL)
18301 if (putc('\n', fd) == EOF)
18302 {
18303 ret = -1;
18304 break;
18305 }
18306 if (ret < 0)
18307 {
18308 EMSG(_(e_write));
18309 break;
18310 }
18311 }
18312 fclose(fd);
18313 }
18314
18315 rettv->vval.v_number = ret;
18316}
18317
18318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018320 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321 */
18322 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018323var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018324 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018325 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018326 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018328 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018329 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018330 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018331
Bram Moolenaara5525202006-03-02 22:52:09 +000018332 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018333 if (varp->v_type == VAR_LIST)
18334 {
18335 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018336 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018337 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018338 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018339
18340 l = varp->vval.v_list;
18341 if (l == NULL)
18342 return NULL;
18343
18344 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018345 pos.lnum = list_find_nr(l, 0L, &error);
18346 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018347 return NULL; /* invalid line number */
18348
18349 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018350 pos.col = list_find_nr(l, 1L, &error);
18351 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018352 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018353 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018354
18355 /* We accept "$" for the column number: last column. */
18356 li = list_find(l, 1L);
18357 if (li != NULL && li->li_tv.v_type == VAR_STRING
18358 && li->li_tv.vval.v_string != NULL
18359 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18360 pos.col = len + 1;
18361
Bram Moolenaara5525202006-03-02 22:52:09 +000018362 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018363 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018364 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018365 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018366
Bram Moolenaara5525202006-03-02 22:52:09 +000018367#ifdef FEAT_VIRTUALEDIT
18368 /* Get the virtual offset. Defaults to zero. */
18369 pos.coladd = list_find_nr(l, 2L, &error);
18370 if (error)
18371 pos.coladd = 0;
18372#endif
18373
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018374 return &pos;
18375 }
18376
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018377 name = get_tv_string_chk(varp);
18378 if (name == NULL)
18379 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018380 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018381 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018382#ifdef FEAT_VISUAL
18383 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18384 {
18385 if (VIsual_active)
18386 return &VIsual;
18387 return &curwin->w_cursor;
18388 }
18389#endif
18390 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018392 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18394 return NULL;
18395 return pp;
18396 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018397
18398#ifdef FEAT_VIRTUALEDIT
18399 pos.coladd = 0;
18400#endif
18401
Bram Moolenaar477933c2007-07-17 14:32:23 +000018402 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018403 {
18404 pos.col = 0;
18405 if (name[1] == '0') /* "w0": first visible line */
18406 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018407 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018408 pos.lnum = curwin->w_topline;
18409 return &pos;
18410 }
18411 else if (name[1] == '$') /* "w$": last visible line */
18412 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018413 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018414 pos.lnum = curwin->w_botline - 1;
18415 return &pos;
18416 }
18417 }
18418 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018420 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018421 {
18422 pos.lnum = curbuf->b_ml.ml_line_count;
18423 pos.col = 0;
18424 }
18425 else
18426 {
18427 pos.lnum = curwin->w_cursor.lnum;
18428 pos.col = (colnr_T)STRLEN(ml_get_curline());
18429 }
18430 return &pos;
18431 }
18432 return NULL;
18433}
18434
18435/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018436 * Convert list in "arg" into a position and optional file number.
18437 * When "fnump" is NULL there is no file number, only 3 items.
18438 * Note that the column is passed on as-is, the caller may want to decrement
18439 * it to use 1 for the first column.
18440 * Return FAIL when conversion is not possible, doesn't check the position for
18441 * validity.
18442 */
18443 static int
18444list2fpos(arg, posp, fnump)
18445 typval_T *arg;
18446 pos_T *posp;
18447 int *fnump;
18448{
18449 list_T *l = arg->vval.v_list;
18450 long i = 0;
18451 long n;
18452
Bram Moolenaarbde35262006-07-23 20:12:24 +000018453 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18454 * when "fnump" isn't NULL and "coladd" is optional. */
18455 if (arg->v_type != VAR_LIST
18456 || l == NULL
18457 || l->lv_len < (fnump == NULL ? 2 : 3)
18458 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018459 return FAIL;
18460
18461 if (fnump != NULL)
18462 {
18463 n = list_find_nr(l, i++, NULL); /* fnum */
18464 if (n < 0)
18465 return FAIL;
18466 if (n == 0)
18467 n = curbuf->b_fnum; /* current buffer */
18468 *fnump = n;
18469 }
18470
18471 n = list_find_nr(l, i++, NULL); /* lnum */
18472 if (n < 0)
18473 return FAIL;
18474 posp->lnum = n;
18475
18476 n = list_find_nr(l, i++, NULL); /* col */
18477 if (n < 0)
18478 return FAIL;
18479 posp->col = n;
18480
18481#ifdef FEAT_VIRTUALEDIT
18482 n = list_find_nr(l, i, NULL);
18483 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018484 posp->coladd = 0;
18485 else
18486 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018487#endif
18488
18489 return OK;
18490}
18491
18492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493 * Get the length of an environment variable name.
18494 * Advance "arg" to the first character after the name.
18495 * Return 0 for error.
18496 */
18497 static int
18498get_env_len(arg)
18499 char_u **arg;
18500{
18501 char_u *p;
18502 int len;
18503
18504 for (p = *arg; vim_isIDc(*p); ++p)
18505 ;
18506 if (p == *arg) /* no name found */
18507 return 0;
18508
18509 len = (int)(p - *arg);
18510 *arg = p;
18511 return len;
18512}
18513
18514/*
18515 * Get the length of the name of a function or internal variable.
18516 * "arg" is advanced to the first non-white character after the name.
18517 * Return 0 if something is wrong.
18518 */
18519 static int
18520get_id_len(arg)
18521 char_u **arg;
18522{
18523 char_u *p;
18524 int len;
18525
18526 /* Find the end of the name. */
18527 for (p = *arg; eval_isnamec(*p); ++p)
18528 ;
18529 if (p == *arg) /* no name found */
18530 return 0;
18531
18532 len = (int)(p - *arg);
18533 *arg = skipwhite(p);
18534
18535 return len;
18536}
18537
18538/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018539 * Get the length of the name of a variable or function.
18540 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018542 * Return -1 if curly braces expansion failed.
18543 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544 * If the name contains 'magic' {}'s, expand them and return the
18545 * expanded name in an allocated string via 'alias' - caller must free.
18546 */
18547 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018548get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549 char_u **arg;
18550 char_u **alias;
18551 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018552 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553{
18554 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555 char_u *p;
18556 char_u *expr_start;
18557 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558
18559 *alias = NULL; /* default to no alias */
18560
18561 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18562 && (*arg)[2] == (int)KE_SNR)
18563 {
18564 /* hard coded <SNR>, already translated */
18565 *arg += 3;
18566 return get_id_len(arg) + 3;
18567 }
18568 len = eval_fname_script(*arg);
18569 if (len > 0)
18570 {
18571 /* literal "<SID>", "s:" or "<SNR>" */
18572 *arg += len;
18573 }
18574
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018576 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018577 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018578 p = find_name_end(*arg, &expr_start, &expr_end,
18579 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580 if (expr_start != NULL)
18581 {
18582 char_u *temp_string;
18583
18584 if (!evaluate)
18585 {
18586 len += (int)(p - *arg);
18587 *arg = skipwhite(p);
18588 return len;
18589 }
18590
18591 /*
18592 * Include any <SID> etc in the expanded string:
18593 * Thus the -len here.
18594 */
18595 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18596 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018597 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598 *alias = temp_string;
18599 *arg = skipwhite(p);
18600 return (int)STRLEN(temp_string);
18601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602
18603 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018604 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605 EMSG2(_(e_invexpr2), *arg);
18606
18607 return len;
18608}
18609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018610/*
18611 * Find the end of a variable or function name, taking care of magic braces.
18612 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18613 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018614 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018615 * Return a pointer to just after the name. Equal to "arg" if there is no
18616 * valid name.
18617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018619find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620 char_u *arg;
18621 char_u **expr_start;
18622 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018623 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018625 int mb_nest = 0;
18626 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627 char_u *p;
18628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018629 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018631 *expr_start = NULL;
18632 *expr_end = NULL;
18633 }
18634
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018635 /* Quick check for valid starting character. */
18636 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18637 return arg;
18638
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018639 for (p = arg; *p != NUL
18640 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018641 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018642 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018643 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018644 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018645 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018646 if (*p == '\'')
18647 {
18648 /* skip over 'string' to avoid counting [ and ] inside it. */
18649 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18650 ;
18651 if (*p == NUL)
18652 break;
18653 }
18654 else if (*p == '"')
18655 {
18656 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18657 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18658 if (*p == '\\' && p[1] != NUL)
18659 ++p;
18660 if (*p == NUL)
18661 break;
18662 }
18663
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018664 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018666 if (*p == '[')
18667 ++br_nest;
18668 else if (*p == ']')
18669 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018672 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018674 if (*p == '{')
18675 {
18676 mb_nest++;
18677 if (expr_start != NULL && *expr_start == NULL)
18678 *expr_start = p;
18679 }
18680 else if (*p == '}')
18681 {
18682 mb_nest--;
18683 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18684 *expr_end = p;
18685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018687 }
18688
18689 return p;
18690}
18691
18692/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018693 * Expands out the 'magic' {}'s in a variable/function name.
18694 * Note that this can call itself recursively, to deal with
18695 * constructs like foo{bar}{baz}{bam}
18696 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18697 * "in_start" ^
18698 * "expr_start" ^
18699 * "expr_end" ^
18700 * "in_end" ^
18701 *
18702 * Returns a new allocated string, which the caller must free.
18703 * Returns NULL for failure.
18704 */
18705 static char_u *
18706make_expanded_name(in_start, expr_start, expr_end, in_end)
18707 char_u *in_start;
18708 char_u *expr_start;
18709 char_u *expr_end;
18710 char_u *in_end;
18711{
18712 char_u c1;
18713 char_u *retval = NULL;
18714 char_u *temp_result;
18715 char_u *nextcmd = NULL;
18716
18717 if (expr_end == NULL || in_end == NULL)
18718 return NULL;
18719 *expr_start = NUL;
18720 *expr_end = NUL;
18721 c1 = *in_end;
18722 *in_end = NUL;
18723
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018724 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018725 if (temp_result != NULL && nextcmd == NULL)
18726 {
18727 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18728 + (in_end - expr_end) + 1));
18729 if (retval != NULL)
18730 {
18731 STRCPY(retval, in_start);
18732 STRCAT(retval, temp_result);
18733 STRCAT(retval, expr_end + 1);
18734 }
18735 }
18736 vim_free(temp_result);
18737
18738 *in_end = c1; /* put char back for error messages */
18739 *expr_start = '{';
18740 *expr_end = '}';
18741
18742 if (retval != NULL)
18743 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018744 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018745 if (expr_start != NULL)
18746 {
18747 /* Further expansion! */
18748 temp_result = make_expanded_name(retval, expr_start,
18749 expr_end, temp_result);
18750 vim_free(retval);
18751 retval = temp_result;
18752 }
18753 }
18754
18755 return retval;
18756}
18757
18758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018760 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761 */
18762 static int
18763eval_isnamec(c)
18764 int c;
18765{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018766 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18767}
18768
18769/*
18770 * Return TRUE if character "c" can be used as the first character in a
18771 * variable or function name (excluding '{' and '}').
18772 */
18773 static int
18774eval_isnamec1(c)
18775 int c;
18776{
18777 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018778}
18779
18780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781 * Set number v: variable to "val".
18782 */
18783 void
18784set_vim_var_nr(idx, val)
18785 int idx;
18786 long val;
18787{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018788 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789}
18790
18791/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018792 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793 */
18794 long
18795get_vim_var_nr(idx)
18796 int idx;
18797{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018798 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799}
18800
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018801/*
18802 * Get string v: variable value. Uses a static buffer, can only be used once.
18803 */
18804 char_u *
18805get_vim_var_str(idx)
18806 int idx;
18807{
18808 return get_tv_string(&vimvars[idx].vv_tv);
18809}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018810
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018812 * Get List v: variable value. Caller must take care of reference count when
18813 * needed.
18814 */
18815 list_T *
18816get_vim_var_list(idx)
18817 int idx;
18818{
18819 return vimvars[idx].vv_list;
18820}
18821
18822/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018823 * Set v:char to character "c".
18824 */
18825 void
18826set_vim_var_char(c)
18827 int c;
18828{
18829#ifdef FEAT_MBYTE
18830 char_u buf[MB_MAXBYTES];
18831#else
18832 char_u buf[2];
18833#endif
18834
18835#ifdef FEAT_MBYTE
18836 if (has_mbyte)
18837 buf[(*mb_char2bytes)(c, buf)] = NUL;
18838 else
18839#endif
18840 {
18841 buf[0] = c;
18842 buf[1] = NUL;
18843 }
18844 set_vim_var_string(VV_CHAR, buf, -1);
18845}
18846
18847/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018848 * Set v:count to "count" and v:count1 to "count1".
18849 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850 */
18851 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018852set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018853 long count;
18854 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018855 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018857 if (set_prevcount)
18858 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018859 vimvars[VV_COUNT].vv_nr = count;
18860 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861}
18862
18863/*
18864 * Set string v: variable to a copy of "val".
18865 */
18866 void
18867set_vim_var_string(idx, val, len)
18868 int idx;
18869 char_u *val;
18870 int len; /* length of "val" to use or -1 (whole string) */
18871{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018872 /* Need to do this (at least) once, since we can't initialize a union.
18873 * Will always be invoked when "v:progname" is set. */
18874 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18875
Bram Moolenaare9a41262005-01-15 22:18:47 +000018876 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018878 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018880 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018882 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883}
18884
18885/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018886 * Set List v: variable to "val".
18887 */
18888 void
18889set_vim_var_list(idx, val)
18890 int idx;
18891 list_T *val;
18892{
18893 list_unref(vimvars[idx].vv_list);
18894 vimvars[idx].vv_list = val;
18895 if (val != NULL)
18896 ++val->lv_refcount;
18897}
18898
18899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018900 * Set v:register if needed.
18901 */
18902 void
18903set_reg_var(c)
18904 int c;
18905{
18906 char_u regname;
18907
18908 if (c == 0 || c == ' ')
18909 regname = '"';
18910 else
18911 regname = c;
18912 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018913 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018914 set_vim_var_string(VV_REG, &regname, 1);
18915}
18916
18917/*
18918 * Get or set v:exception. If "oldval" == NULL, return the current value.
18919 * Otherwise, restore the value to "oldval" and return NULL.
18920 * Must always be called in pairs to save and restore v:exception! Does not
18921 * take care of memory allocations.
18922 */
18923 char_u *
18924v_exception(oldval)
18925 char_u *oldval;
18926{
18927 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018928 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929
Bram Moolenaare9a41262005-01-15 22:18:47 +000018930 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931 return NULL;
18932}
18933
18934/*
18935 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18936 * Otherwise, restore the value to "oldval" and return NULL.
18937 * Must always be called in pairs to save and restore v:throwpoint! Does not
18938 * take care of memory allocations.
18939 */
18940 char_u *
18941v_throwpoint(oldval)
18942 char_u *oldval;
18943{
18944 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018945 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946
Bram Moolenaare9a41262005-01-15 22:18:47 +000018947 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948 return NULL;
18949}
18950
18951#if defined(FEAT_AUTOCMD) || defined(PROTO)
18952/*
18953 * Set v:cmdarg.
18954 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18955 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18956 * Must always be called in pairs!
18957 */
18958 char_u *
18959set_cmdarg(eap, oldarg)
18960 exarg_T *eap;
18961 char_u *oldarg;
18962{
18963 char_u *oldval;
18964 char_u *newval;
18965 unsigned len;
18966
Bram Moolenaare9a41262005-01-15 22:18:47 +000018967 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018968 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018970 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018971 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018972 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973 }
18974
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018975 if (eap->force_bin == FORCE_BIN)
18976 len = 6;
18977 else if (eap->force_bin == FORCE_NOBIN)
18978 len = 8;
18979 else
18980 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018981
18982 if (eap->read_edit)
18983 len += 7;
18984
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018985 if (eap->force_ff != 0)
18986 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18987# ifdef FEAT_MBYTE
18988 if (eap->force_enc != 0)
18989 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018990 if (eap->bad_char != 0)
18991 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018992# endif
18993
18994 newval = alloc(len + 1);
18995 if (newval == NULL)
18996 return NULL;
18997
18998 if (eap->force_bin == FORCE_BIN)
18999 sprintf((char *)newval, " ++bin");
19000 else if (eap->force_bin == FORCE_NOBIN)
19001 sprintf((char *)newval, " ++nobin");
19002 else
19003 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019004
19005 if (eap->read_edit)
19006 STRCAT(newval, " ++edit");
19007
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019008 if (eap->force_ff != 0)
19009 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19010 eap->cmd + eap->force_ff);
19011# ifdef FEAT_MBYTE
19012 if (eap->force_enc != 0)
19013 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19014 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019015 if (eap->bad_char == BAD_KEEP)
19016 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19017 else if (eap->bad_char == BAD_DROP)
19018 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19019 else if (eap->bad_char != 0)
19020 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019021# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019022 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019023 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024}
19025#endif
19026
19027/*
19028 * Get the value of internal variable "name".
19029 * Return OK or FAIL.
19030 */
19031 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019032get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019033 char_u *name;
19034 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019035 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019036 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037{
19038 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019039 typval_T *tv = NULL;
19040 typval_T atv;
19041 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043
19044 /* truncate the name, so that we can use strcmp() */
19045 cc = name[len];
19046 name[len] = NUL;
19047
19048 /*
19049 * Check for "b:changedtick".
19050 */
19051 if (STRCMP(name, "b:changedtick") == 0)
19052 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019053 atv.v_type = VAR_NUMBER;
19054 atv.vval.v_number = curbuf->b_changedtick;
19055 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019056 }
19057
19058 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019059 * Check for user-defined variables.
19060 */
19061 else
19062 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019063 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019065 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019066 }
19067
Bram Moolenaare9a41262005-01-15 22:18:47 +000019068 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019070 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019071 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072 ret = FAIL;
19073 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019074 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019075 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076
19077 name[len] = cc;
19078
19079 return ret;
19080}
19081
19082/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019083 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19084 * Also handle function call with Funcref variable: func(expr)
19085 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19086 */
19087 static int
19088handle_subscript(arg, rettv, evaluate, verbose)
19089 char_u **arg;
19090 typval_T *rettv;
19091 int evaluate; /* do more than finding the end */
19092 int verbose; /* give error messages */
19093{
19094 int ret = OK;
19095 dict_T *selfdict = NULL;
19096 char_u *s;
19097 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019098 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019099
19100 while (ret == OK
19101 && (**arg == '['
19102 || (**arg == '.' && rettv->v_type == VAR_DICT)
19103 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19104 && !vim_iswhite(*(*arg - 1)))
19105 {
19106 if (**arg == '(')
19107 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019108 /* need to copy the funcref so that we can clear rettv */
19109 functv = *rettv;
19110 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019111
19112 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019113 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019114 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019115 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19116 &len, evaluate, selfdict);
19117
19118 /* Clear the funcref afterwards, so that deleting it while
19119 * evaluating the arguments is possible (see test55). */
19120 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019121
19122 /* Stop the expression evaluation when immediately aborting on
19123 * error, or when an interrupt occurred or an exception was thrown
19124 * but not caught. */
19125 if (aborting())
19126 {
19127 if (ret == OK)
19128 clear_tv(rettv);
19129 ret = FAIL;
19130 }
19131 dict_unref(selfdict);
19132 selfdict = NULL;
19133 }
19134 else /* **arg == '[' || **arg == '.' */
19135 {
19136 dict_unref(selfdict);
19137 if (rettv->v_type == VAR_DICT)
19138 {
19139 selfdict = rettv->vval.v_dict;
19140 if (selfdict != NULL)
19141 ++selfdict->dv_refcount;
19142 }
19143 else
19144 selfdict = NULL;
19145 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19146 {
19147 clear_tv(rettv);
19148 ret = FAIL;
19149 }
19150 }
19151 }
19152 dict_unref(selfdict);
19153 return ret;
19154}
19155
19156/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019157 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019158 * value).
19159 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019160 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019161alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019162{
Bram Moolenaar33570922005-01-25 22:26:29 +000019163 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019164}
19165
19166/*
19167 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168 * The string "s" must have been allocated, it is consumed.
19169 * Return NULL for out of memory, the variable otherwise.
19170 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019171 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019172alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173 char_u *s;
19174{
Bram Moolenaar33570922005-01-25 22:26:29 +000019175 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019177 rettv = alloc_tv();
19178 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019180 rettv->v_type = VAR_STRING;
19181 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182 }
19183 else
19184 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019185 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186}
19187
19188/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019189 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019190 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019191 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019192free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019193 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019194{
19195 if (varp != NULL)
19196 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019197 switch (varp->v_type)
19198 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019199 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019200 func_unref(varp->vval.v_string);
19201 /*FALLTHROUGH*/
19202 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019203 vim_free(varp->vval.v_string);
19204 break;
19205 case VAR_LIST:
19206 list_unref(varp->vval.v_list);
19207 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019208 case VAR_DICT:
19209 dict_unref(varp->vval.v_dict);
19210 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019211 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019212#ifdef FEAT_FLOAT
19213 case VAR_FLOAT:
19214#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019215 case VAR_UNKNOWN:
19216 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019217 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019218 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019219 break;
19220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019221 vim_free(varp);
19222 }
19223}
19224
19225/*
19226 * Free the memory for a variable value and set the value to NULL or 0.
19227 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019228 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019229clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019230 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231{
19232 if (varp != NULL)
19233 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019234 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019236 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019237 func_unref(varp->vval.v_string);
19238 /*FALLTHROUGH*/
19239 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019240 vim_free(varp->vval.v_string);
19241 varp->vval.v_string = NULL;
19242 break;
19243 case VAR_LIST:
19244 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019245 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019246 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019247 case VAR_DICT:
19248 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019249 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019250 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019251 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019252 varp->vval.v_number = 0;
19253 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019254#ifdef FEAT_FLOAT
19255 case VAR_FLOAT:
19256 varp->vval.v_float = 0.0;
19257 break;
19258#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019259 case VAR_UNKNOWN:
19260 break;
19261 default:
19262 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019263 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019264 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265 }
19266}
19267
19268/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019269 * Set the value of a variable to NULL without freeing items.
19270 */
19271 static void
19272init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019273 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019274{
19275 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019276 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019277}
19278
19279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019280 * Get the number value of a variable.
19281 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019282 * For incompatible types, return 0.
19283 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19284 * caller of incompatible types: it sets *denote to TRUE if "denote"
19285 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019286 */
19287 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019288get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019289 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019291 int error = FALSE;
19292
19293 return get_tv_number_chk(varp, &error); /* return 0L on error */
19294}
19295
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019296 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019297get_tv_number_chk(varp, denote)
19298 typval_T *varp;
19299 int *denote;
19300{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019301 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019302
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019303 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019305 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019306 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019307#ifdef FEAT_FLOAT
19308 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019309 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019310 break;
19311#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019312 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019313 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019314 break;
19315 case VAR_STRING:
19316 if (varp->vval.v_string != NULL)
19317 vim_str2nr(varp->vval.v_string, NULL, NULL,
19318 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019319 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019320 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019321 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019322 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019323 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019324 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019325 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019326 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019327 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019328 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019330 if (denote == NULL) /* useful for values that must be unsigned */
19331 n = -1;
19332 else
19333 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019334 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019335}
19336
19337/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019338 * Get the lnum from the first argument.
19339 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019340 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341 */
19342 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019343get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019344 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345{
Bram Moolenaar33570922005-01-25 22:26:29 +000019346 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019347 linenr_T lnum;
19348
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019349 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350 if (lnum == 0) /* no valid number, try using line() */
19351 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019352 rettv.v_type = VAR_NUMBER;
19353 f_line(argvars, &rettv);
19354 lnum = rettv.vval.v_number;
19355 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019356 }
19357 return lnum;
19358}
19359
19360/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019361 * Get the lnum from the first argument.
19362 * Also accepts "$", then "buf" is used.
19363 * Returns 0 on error.
19364 */
19365 static linenr_T
19366get_tv_lnum_buf(argvars, buf)
19367 typval_T *argvars;
19368 buf_T *buf;
19369{
19370 if (argvars[0].v_type == VAR_STRING
19371 && argvars[0].vval.v_string != NULL
19372 && argvars[0].vval.v_string[0] == '$'
19373 && buf != NULL)
19374 return buf->b_ml.ml_line_count;
19375 return get_tv_number_chk(&argvars[0], NULL);
19376}
19377
19378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 * Get the string value of a variable.
19380 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019381 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19382 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383 * If the String variable has never been set, return an empty string.
19384 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019385 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19386 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387 */
19388 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019389get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019390 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391{
19392 static char_u mybuf[NUMBUFLEN];
19393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019394 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019395}
19396
19397 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019398get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019399 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019400 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019402 char_u *res = get_tv_string_buf_chk(varp, buf);
19403
19404 return res != NULL ? res : (char_u *)"";
19405}
19406
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019407 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019408get_tv_string_chk(varp)
19409 typval_T *varp;
19410{
19411 static char_u mybuf[NUMBUFLEN];
19412
19413 return get_tv_string_buf_chk(varp, mybuf);
19414}
19415
19416 static char_u *
19417get_tv_string_buf_chk(varp, buf)
19418 typval_T *varp;
19419 char_u *buf;
19420{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019421 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019423 case VAR_NUMBER:
19424 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19425 return buf;
19426 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019427 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019428 break;
19429 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019430 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019431 break;
19432 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019433 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019434 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019435#ifdef FEAT_FLOAT
19436 case VAR_FLOAT:
19437 EMSG(_("E806: using Float as a String"));
19438 break;
19439#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019440 case VAR_STRING:
19441 if (varp->vval.v_string != NULL)
19442 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019443 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019444 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019445 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019446 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019448 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449}
19450
19451/*
19452 * Find variable "name" in the list of variables.
19453 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019454 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019455 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019456 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019458 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019459find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019460 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019461 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019462{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019464 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465
Bram Moolenaara7043832005-01-21 11:56:39 +000019466 ht = find_var_ht(name, &varname);
19467 if (htp != NULL)
19468 *htp = ht;
19469 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019471 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472}
19473
19474/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019475 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019476 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019478 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019479find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019480 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019481 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019482 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019483{
Bram Moolenaar33570922005-01-25 22:26:29 +000019484 hashitem_T *hi;
19485
19486 if (*varname == NUL)
19487 {
19488 /* Must be something like "s:", otherwise "ht" would be NULL. */
19489 switch (varname[-2])
19490 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019491 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019492 case 'g': return &globvars_var;
19493 case 'v': return &vimvars_var;
19494 case 'b': return &curbuf->b_bufvar;
19495 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019496#ifdef FEAT_WINDOWS
19497 case 't': return &curtab->tp_winvar;
19498#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019499 case 'l': return current_funccal == NULL
19500 ? NULL : &current_funccal->l_vars_var;
19501 case 'a': return current_funccal == NULL
19502 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019503 }
19504 return NULL;
19505 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019506
19507 hi = hash_find(ht, varname);
19508 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019509 {
19510 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019511 * worked find the variable again. Don't auto-load a script if it was
19512 * loaded already, otherwise it would be loaded every time when
19513 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019514 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019515 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019516 hi = hash_find(ht, varname);
19517 if (HASHITEM_EMPTY(hi))
19518 return NULL;
19519 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019520 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019521}
19522
19523/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019524 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019525 * Set "varname" to the start of name without ':'.
19526 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019527 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019528find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529 char_u *name;
19530 char_u **varname;
19531{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019532 hashitem_T *hi;
19533
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534 if (name[1] != ':')
19535 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019536 /* The name must not start with a colon or #. */
19537 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019538 return NULL;
19539 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019540
19541 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019542 hi = hash_find(&compat_hashtab, name);
19543 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019544 return &compat_hashtab;
19545
Bram Moolenaar071d4272004-06-13 20:20:40 +000019546 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019547 return &globvarht; /* global variable */
19548 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019549 }
19550 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019551 if (*name == 'g') /* global variable */
19552 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019553 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19554 */
19555 if (vim_strchr(name + 2, ':') != NULL
19556 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019557 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019559 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019561 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019562#ifdef FEAT_WINDOWS
19563 if (*name == 't') /* tab page variable */
19564 return &curtab->tp_vars.dv_hashtab;
19565#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019566 if (*name == 'v') /* v: variable */
19567 return &vimvarht;
19568 if (*name == 'a' && current_funccal != NULL) /* function argument */
19569 return &current_funccal->l_avars.dv_hashtab;
19570 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19571 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572 if (*name == 's' /* script variable */
19573 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19574 return &SCRIPT_VARS(current_SID);
19575 return NULL;
19576}
19577
19578/*
19579 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019580 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019581 * Returns NULL when it doesn't exist.
19582 */
19583 char_u *
19584get_var_value(name)
19585 char_u *name;
19586{
Bram Moolenaar33570922005-01-25 22:26:29 +000019587 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019588
Bram Moolenaara7043832005-01-21 11:56:39 +000019589 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590 if (v == NULL)
19591 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019592 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593}
19594
19595/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019596 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597 * sourcing this script and when executing functions defined in the script.
19598 */
19599 void
19600new_script_vars(id)
19601 scid_T id;
19602{
Bram Moolenaara7043832005-01-21 11:56:39 +000019603 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019604 hashtab_T *ht;
19605 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019606
Bram Moolenaar071d4272004-06-13 20:20:40 +000019607 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19608 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019609 /* Re-allocating ga_data means that an ht_array pointing to
19610 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019611 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019612 for (i = 1; i <= ga_scripts.ga_len; ++i)
19613 {
19614 ht = &SCRIPT_VARS(i);
19615 if (ht->ht_mask == HT_INIT_SIZE - 1)
19616 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019617 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019618 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019619 }
19620
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 while (ga_scripts.ga_len < id)
19622 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019623 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019624 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019625 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627 }
19628 }
19629}
19630
19631/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019632 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19633 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634 */
19635 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019636init_var_dict(dict, dict_var)
19637 dict_T *dict;
19638 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639{
Bram Moolenaar33570922005-01-25 22:26:29 +000019640 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019641 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019642 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019643 dict_var->di_tv.vval.v_dict = dict;
19644 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019645 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019646 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19647 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648}
19649
19650/*
19651 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019652 * Frees all allocated variables and the value they contain.
19653 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019654 */
19655 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019656vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019657 hashtab_T *ht;
19658{
19659 vars_clear_ext(ht, TRUE);
19660}
19661
19662/*
19663 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19664 */
19665 static void
19666vars_clear_ext(ht, free_val)
19667 hashtab_T *ht;
19668 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669{
Bram Moolenaara7043832005-01-21 11:56:39 +000019670 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019671 hashitem_T *hi;
19672 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673
Bram Moolenaar33570922005-01-25 22:26:29 +000019674 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019675 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019676 for (hi = ht->ht_array; todo > 0; ++hi)
19677 {
19678 if (!HASHITEM_EMPTY(hi))
19679 {
19680 --todo;
19681
Bram Moolenaar33570922005-01-25 22:26:29 +000019682 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019683 * ht_array might change then. hash_clear() takes care of it
19684 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019685 v = HI2DI(hi);
19686 if (free_val)
19687 clear_tv(&v->di_tv);
19688 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19689 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019690 }
19691 }
19692 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019693 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019694}
19695
Bram Moolenaara7043832005-01-21 11:56:39 +000019696/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019697 * Delete a variable from hashtab "ht" at item "hi".
19698 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019701delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019702 hashtab_T *ht;
19703 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019704{
Bram Moolenaar33570922005-01-25 22:26:29 +000019705 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019706
19707 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019708 clear_tv(&di->di_tv);
19709 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019710}
19711
19712/*
19713 * List the value of one internal variable.
19714 */
19715 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019716list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019717 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019719 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019721 char_u *tofree;
19722 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019723 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019724
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019725 current_copyID += COPYID_INC;
19726 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019727 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019728 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019729 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730}
19731
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019733list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019734 char_u *prefix;
19735 char_u *name;
19736 int type;
19737 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019738 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019739{
Bram Moolenaar31859182007-08-14 20:41:13 +000019740 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19741 msg_start();
19742 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019743 if (name != NULL) /* "a:" vars don't have a name stored */
19744 msg_puts(name);
19745 msg_putchar(' ');
19746 msg_advance(22);
19747 if (type == VAR_NUMBER)
19748 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019749 else if (type == VAR_FUNC)
19750 msg_putchar('*');
19751 else if (type == VAR_LIST)
19752 {
19753 msg_putchar('[');
19754 if (*string == '[')
19755 ++string;
19756 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019757 else if (type == VAR_DICT)
19758 {
19759 msg_putchar('{');
19760 if (*string == '{')
19761 ++string;
19762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019763 else
19764 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019765
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019767
19768 if (type == VAR_FUNC)
19769 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019770 if (*first)
19771 {
19772 msg_clr_eos();
19773 *first = FALSE;
19774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775}
19776
19777/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019778 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779 * If the variable already exists, the value is updated.
19780 * Otherwise the variable is created.
19781 */
19782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019783set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019784 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019785 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019786 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019787{
Bram Moolenaar33570922005-01-25 22:26:29 +000019788 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019790 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019791 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019793 ht = find_var_ht(name, &varname);
19794 if (ht == NULL || *varname == NUL)
19795 {
19796 EMSG2(_(e_illvar), name);
19797 return;
19798 }
19799 v = find_var_in_ht(ht, varname, TRUE);
19800
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019801 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019802 {
19803 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19804 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19805 ? name[2] : name[0]))
19806 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019807 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019808 return;
19809 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019810 /* Don't allow hiding a function. When "v" is not NULL we migth be
19811 * assigning another function to the same var, the type is checked
19812 * below. */
19813 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019814 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019815 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019816 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019817 return;
19818 }
19819 }
19820
Bram Moolenaar33570922005-01-25 22:26:29 +000019821 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019823 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019824 if (var_check_ro(v->di_flags, name)
19825 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019826 return;
19827 if (v->di_tv.v_type != tv->v_type
19828 && !((v->di_tv.v_type == VAR_STRING
19829 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019830 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019831 || tv->v_type == VAR_NUMBER))
19832#ifdef FEAT_FLOAT
19833 && !((v->di_tv.v_type == VAR_NUMBER
19834 || v->di_tv.v_type == VAR_FLOAT)
19835 && (tv->v_type == VAR_NUMBER
19836 || tv->v_type == VAR_FLOAT))
19837#endif
19838 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019839 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019840 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019841 return;
19842 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019843
19844 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019845 * Handle setting internal v: variables separately: we don't change
19846 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019847 */
19848 if (ht == &vimvarht)
19849 {
19850 if (v->di_tv.v_type == VAR_STRING)
19851 {
19852 vim_free(v->di_tv.vval.v_string);
19853 if (copy || tv->v_type != VAR_STRING)
19854 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19855 else
19856 {
19857 /* Take over the string to avoid an extra alloc/free. */
19858 v->di_tv.vval.v_string = tv->vval.v_string;
19859 tv->vval.v_string = NULL;
19860 }
19861 }
19862 else if (v->di_tv.v_type != VAR_NUMBER)
19863 EMSG2(_(e_intern2), "set_var()");
19864 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019865 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019866 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019867 if (STRCMP(varname, "searchforward") == 0)
19868 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19869 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019870 return;
19871 }
19872
19873 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874 }
19875 else /* add a new variable */
19876 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019877 /* Can't add "v:" variable. */
19878 if (ht == &vimvarht)
19879 {
19880 EMSG2(_(e_illvar), name);
19881 return;
19882 }
19883
Bram Moolenaar92124a32005-06-17 22:03:40 +000019884 /* Make sure the variable name is valid. */
19885 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019886 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19887 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019888 {
19889 EMSG2(_(e_illvar), varname);
19890 return;
19891 }
19892
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019893 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19894 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019895 if (v == NULL)
19896 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019897 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019898 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019900 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901 return;
19902 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019903 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019905
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019906 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019907 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019908 else
19909 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019910 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019911 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019912 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914}
19915
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019916/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019917 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019918 * Also give an error message.
19919 */
19920 static int
19921var_check_ro(flags, name)
19922 int flags;
19923 char_u *name;
19924{
19925 if (flags & DI_FLAGS_RO)
19926 {
19927 EMSG2(_(e_readonlyvar), name);
19928 return TRUE;
19929 }
19930 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19931 {
19932 EMSG2(_(e_readonlysbx), name);
19933 return TRUE;
19934 }
19935 return FALSE;
19936}
19937
19938/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019939 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19940 * Also give an error message.
19941 */
19942 static int
19943var_check_fixed(flags, name)
19944 int flags;
19945 char_u *name;
19946{
19947 if (flags & DI_FLAGS_FIX)
19948 {
19949 EMSG2(_("E795: Cannot delete variable %s"), name);
19950 return TRUE;
19951 }
19952 return FALSE;
19953}
19954
19955/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019956 * Return TRUE if typeval "tv" is set to be locked (immutable).
19957 * Also give an error message, using "name".
19958 */
19959 static int
19960tv_check_lock(lock, name)
19961 int lock;
19962 char_u *name;
19963{
19964 if (lock & VAR_LOCKED)
19965 {
19966 EMSG2(_("E741: Value is locked: %s"),
19967 name == NULL ? (char_u *)_("Unknown") : name);
19968 return TRUE;
19969 }
19970 if (lock & VAR_FIXED)
19971 {
19972 EMSG2(_("E742: Cannot change value of %s"),
19973 name == NULL ? (char_u *)_("Unknown") : name);
19974 return TRUE;
19975 }
19976 return FALSE;
19977}
19978
19979/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019980 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019981 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019982 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019983 * It is OK for "from" and "to" to point to the same item. This is used to
19984 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019985 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019986 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019987copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019988 typval_T *from;
19989 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019991 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019992 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019993 switch (from->v_type)
19994 {
19995 case VAR_NUMBER:
19996 to->vval.v_number = from->vval.v_number;
19997 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019998#ifdef FEAT_FLOAT
19999 case VAR_FLOAT:
20000 to->vval.v_float = from->vval.v_float;
20001 break;
20002#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020003 case VAR_STRING:
20004 case VAR_FUNC:
20005 if (from->vval.v_string == NULL)
20006 to->vval.v_string = NULL;
20007 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020008 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020009 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020010 if (from->v_type == VAR_FUNC)
20011 func_ref(to->vval.v_string);
20012 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020013 break;
20014 case VAR_LIST:
20015 if (from->vval.v_list == NULL)
20016 to->vval.v_list = NULL;
20017 else
20018 {
20019 to->vval.v_list = from->vval.v_list;
20020 ++to->vval.v_list->lv_refcount;
20021 }
20022 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020023 case VAR_DICT:
20024 if (from->vval.v_dict == NULL)
20025 to->vval.v_dict = NULL;
20026 else
20027 {
20028 to->vval.v_dict = from->vval.v_dict;
20029 ++to->vval.v_dict->dv_refcount;
20030 }
20031 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020032 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020033 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020034 break;
20035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036}
20037
20038/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020039 * Make a copy of an item.
20040 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020041 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20042 * reference to an already copied list/dict can be used.
20043 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020044 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020045 static int
20046item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020047 typval_T *from;
20048 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020049 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020050 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020051{
20052 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020053 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020054
Bram Moolenaar33570922005-01-25 22:26:29 +000020055 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020056 {
20057 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020058 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020059 }
20060 ++recurse;
20061
20062 switch (from->v_type)
20063 {
20064 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020065#ifdef FEAT_FLOAT
20066 case VAR_FLOAT:
20067#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020068 case VAR_STRING:
20069 case VAR_FUNC:
20070 copy_tv(from, to);
20071 break;
20072 case VAR_LIST:
20073 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020074 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020075 if (from->vval.v_list == NULL)
20076 to->vval.v_list = NULL;
20077 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20078 {
20079 /* use the copy made earlier */
20080 to->vval.v_list = from->vval.v_list->lv_copylist;
20081 ++to->vval.v_list->lv_refcount;
20082 }
20083 else
20084 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20085 if (to->vval.v_list == NULL)
20086 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020087 break;
20088 case VAR_DICT:
20089 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020090 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020091 if (from->vval.v_dict == NULL)
20092 to->vval.v_dict = NULL;
20093 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20094 {
20095 /* use the copy made earlier */
20096 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20097 ++to->vval.v_dict->dv_refcount;
20098 }
20099 else
20100 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20101 if (to->vval.v_dict == NULL)
20102 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020103 break;
20104 default:
20105 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020106 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020107 }
20108 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020109 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020110}
20111
20112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113 * ":echo expr1 ..." print each argument separated with a space, add a
20114 * newline at the end.
20115 * ":echon expr1 ..." print each argument plain.
20116 */
20117 void
20118ex_echo(eap)
20119 exarg_T *eap;
20120{
20121 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020122 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020123 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124 char_u *p;
20125 int needclr = TRUE;
20126 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020127 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128
20129 if (eap->skip)
20130 ++emsg_skip;
20131 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20132 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020133 /* If eval1() causes an error message the text from the command may
20134 * still need to be cleared. E.g., "echo 22,44". */
20135 need_clr_eos = needclr;
20136
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020138 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020139 {
20140 /*
20141 * Report the invalid expression unless the expression evaluation
20142 * has been cancelled due to an aborting error, an interrupt, or an
20143 * exception.
20144 */
20145 if (!aborting())
20146 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020147 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148 break;
20149 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020150 need_clr_eos = FALSE;
20151
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152 if (!eap->skip)
20153 {
20154 if (atstart)
20155 {
20156 atstart = FALSE;
20157 /* Call msg_start() after eval1(), evaluating the expression
20158 * may cause a message to appear. */
20159 if (eap->cmdidx == CMD_echo)
20160 msg_start();
20161 }
20162 else if (eap->cmdidx == CMD_echo)
20163 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020164 current_copyID += COPYID_INC;
20165 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020166 if (p != NULL)
20167 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020169 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020171 if (*p != TAB && needclr)
20172 {
20173 /* remove any text still there from the command */
20174 msg_clr_eos();
20175 needclr = FALSE;
20176 }
20177 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178 }
20179 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020180 {
20181#ifdef FEAT_MBYTE
20182 if (has_mbyte)
20183 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020184 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020185
20186 (void)msg_outtrans_len_attr(p, i, echo_attr);
20187 p += i - 1;
20188 }
20189 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020190#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020191 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020193 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020194 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020195 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020196 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197 arg = skipwhite(arg);
20198 }
20199 eap->nextcmd = check_nextcmd(arg);
20200
20201 if (eap->skip)
20202 --emsg_skip;
20203 else
20204 {
20205 /* remove text that may still be there from the command */
20206 if (needclr)
20207 msg_clr_eos();
20208 if (eap->cmdidx == CMD_echo)
20209 msg_end();
20210 }
20211}
20212
20213/*
20214 * ":echohl {name}".
20215 */
20216 void
20217ex_echohl(eap)
20218 exarg_T *eap;
20219{
20220 int id;
20221
20222 id = syn_name2id(eap->arg);
20223 if (id == 0)
20224 echo_attr = 0;
20225 else
20226 echo_attr = syn_id2attr(id);
20227}
20228
20229/*
20230 * ":execute expr1 ..." execute the result of an expression.
20231 * ":echomsg expr1 ..." Print a message
20232 * ":echoerr expr1 ..." Print an error
20233 * Each gets spaces around each argument and a newline at the end for
20234 * echo commands
20235 */
20236 void
20237ex_execute(eap)
20238 exarg_T *eap;
20239{
20240 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020241 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 int ret = OK;
20243 char_u *p;
20244 garray_T ga;
20245 int len;
20246 int save_did_emsg;
20247
20248 ga_init2(&ga, 1, 80);
20249
20250 if (eap->skip)
20251 ++emsg_skip;
20252 while (*arg != NUL && *arg != '|' && *arg != '\n')
20253 {
20254 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020255 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256 {
20257 /*
20258 * Report the invalid expression unless the expression evaluation
20259 * has been cancelled due to an aborting error, an interrupt, or an
20260 * exception.
20261 */
20262 if (!aborting())
20263 EMSG2(_(e_invexpr2), p);
20264 ret = FAIL;
20265 break;
20266 }
20267
20268 if (!eap->skip)
20269 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020270 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271 len = (int)STRLEN(p);
20272 if (ga_grow(&ga, len + 2) == FAIL)
20273 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020274 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 ret = FAIL;
20276 break;
20277 }
20278 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020281 ga.ga_len += len;
20282 }
20283
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020284 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285 arg = skipwhite(arg);
20286 }
20287
20288 if (ret != FAIL && ga.ga_data != NULL)
20289 {
20290 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020293 out_flush();
20294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 else if (eap->cmdidx == CMD_echoerr)
20296 {
20297 /* We don't want to abort following commands, restore did_emsg. */
20298 save_did_emsg = did_emsg;
20299 EMSG((char_u *)ga.ga_data);
20300 if (!force_abort)
20301 did_emsg = save_did_emsg;
20302 }
20303 else if (eap->cmdidx == CMD_execute)
20304 do_cmdline((char_u *)ga.ga_data,
20305 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20306 }
20307
20308 ga_clear(&ga);
20309
20310 if (eap->skip)
20311 --emsg_skip;
20312
20313 eap->nextcmd = check_nextcmd(arg);
20314}
20315
20316/*
20317 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20318 * "arg" points to the "&" or '+' when called, to "option" when returning.
20319 * Returns NULL when no option name found. Otherwise pointer to the char
20320 * after the option name.
20321 */
20322 static char_u *
20323find_option_end(arg, opt_flags)
20324 char_u **arg;
20325 int *opt_flags;
20326{
20327 char_u *p = *arg;
20328
20329 ++p;
20330 if (*p == 'g' && p[1] == ':')
20331 {
20332 *opt_flags = OPT_GLOBAL;
20333 p += 2;
20334 }
20335 else if (*p == 'l' && p[1] == ':')
20336 {
20337 *opt_flags = OPT_LOCAL;
20338 p += 2;
20339 }
20340 else
20341 *opt_flags = 0;
20342
20343 if (!ASCII_ISALPHA(*p))
20344 return NULL;
20345 *arg = p;
20346
20347 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20348 p += 4; /* termcap option */
20349 else
20350 while (ASCII_ISALPHA(*p))
20351 ++p;
20352 return p;
20353}
20354
20355/*
20356 * ":function"
20357 */
20358 void
20359ex_function(eap)
20360 exarg_T *eap;
20361{
20362 char_u *theline;
20363 int j;
20364 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366 char_u *name = NULL;
20367 char_u *p;
20368 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020369 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370 garray_T newargs;
20371 garray_T newlines;
20372 int varargs = FALSE;
20373 int mustend = FALSE;
20374 int flags = 0;
20375 ufunc_T *fp;
20376 int indent;
20377 int nesting;
20378 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020379 dictitem_T *v;
20380 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020381 static int func_nr = 0; /* number for nameless function */
20382 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020383 hashtab_T *ht;
20384 int todo;
20385 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020386 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020387
20388 /*
20389 * ":function" without argument: list functions.
20390 */
20391 if (ends_excmd(*eap->arg))
20392 {
20393 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020394 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020395 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020396 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020397 {
20398 if (!HASHITEM_EMPTY(hi))
20399 {
20400 --todo;
20401 fp = HI2UF(hi);
20402 if (!isdigit(*fp->uf_name))
20403 list_func_head(fp, FALSE);
20404 }
20405 }
20406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 eap->nextcmd = check_nextcmd(eap->arg);
20408 return;
20409 }
20410
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020411 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020412 * ":function /pat": list functions matching pattern.
20413 */
20414 if (*eap->arg == '/')
20415 {
20416 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20417 if (!eap->skip)
20418 {
20419 regmatch_T regmatch;
20420
20421 c = *p;
20422 *p = NUL;
20423 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20424 *p = c;
20425 if (regmatch.regprog != NULL)
20426 {
20427 regmatch.rm_ic = p_ic;
20428
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020429 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020430 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20431 {
20432 if (!HASHITEM_EMPTY(hi))
20433 {
20434 --todo;
20435 fp = HI2UF(hi);
20436 if (!isdigit(*fp->uf_name)
20437 && vim_regexec(&regmatch, fp->uf_name, 0))
20438 list_func_head(fp, FALSE);
20439 }
20440 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020441 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020442 }
20443 }
20444 if (*p == '/')
20445 ++p;
20446 eap->nextcmd = check_nextcmd(p);
20447 return;
20448 }
20449
20450 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020451 * Get the function name. There are these situations:
20452 * func normal function name
20453 * "name" == func, "fudi.fd_dict" == NULL
20454 * dict.func new dictionary entry
20455 * "name" == NULL, "fudi.fd_dict" set,
20456 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20457 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020458 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020459 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20460 * dict.func existing dict entry that's not a Funcref
20461 * "name" == NULL, "fudi.fd_dict" set,
20462 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20463 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020464 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020465 name = trans_function_name(&p, eap->skip, 0, &fudi);
20466 paren = (vim_strchr(p, '(') != NULL);
20467 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020468 {
20469 /*
20470 * Return on an invalid expression in braces, unless the expression
20471 * evaluation has been cancelled due to an aborting error, an
20472 * interrupt, or an exception.
20473 */
20474 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020475 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020476 if (!eap->skip && fudi.fd_newkey != NULL)
20477 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020478 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481 else
20482 eap->skip = TRUE;
20483 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020484
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485 /* An error in a function call during evaluation of an expression in magic
20486 * braces should not cause the function not to be defined. */
20487 saved_did_emsg = did_emsg;
20488 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489
20490 /*
20491 * ":function func" with only function name: list function.
20492 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020493 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020494 {
20495 if (!ends_excmd(*skipwhite(p)))
20496 {
20497 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020498 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499 }
20500 eap->nextcmd = check_nextcmd(p);
20501 if (eap->nextcmd != NULL)
20502 *p = NUL;
20503 if (!eap->skip && !got_int)
20504 {
20505 fp = find_func(name);
20506 if (fp != NULL)
20507 {
20508 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020509 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020511 if (FUNCLINE(fp, j) == NULL)
20512 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020513 msg_putchar('\n');
20514 msg_outnum((long)(j + 1));
20515 if (j < 9)
20516 msg_putchar(' ');
20517 if (j < 99)
20518 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020519 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 out_flush(); /* show a line at a time */
20521 ui_breakcheck();
20522 }
20523 if (!got_int)
20524 {
20525 msg_putchar('\n');
20526 msg_puts((char_u *)" endfunction");
20527 }
20528 }
20529 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020530 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020532 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533 }
20534
20535 /*
20536 * ":function name(arg1, arg2)" Define function.
20537 */
20538 p = skipwhite(p);
20539 if (*p != '(')
20540 {
20541 if (!eap->skip)
20542 {
20543 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020544 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020545 }
20546 /* attempt to continue by skipping some text */
20547 if (vim_strchr(p, '(') != NULL)
20548 p = vim_strchr(p, '(');
20549 }
20550 p = skipwhite(p + 1);
20551
20552 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20553 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20554
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020555 if (!eap->skip)
20556 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020557 /* Check the name of the function. Unless it's a dictionary function
20558 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020559 if (name != NULL)
20560 arg = name;
20561 else
20562 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020563 if (arg != NULL && (fudi.fd_di == NULL
20564 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020565 {
20566 if (*arg == K_SPECIAL)
20567 j = 3;
20568 else
20569 j = 0;
20570 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20571 : eval_isnamec(arg[j])))
20572 ++j;
20573 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020574 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020575 }
20576 }
20577
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578 /*
20579 * Isolate the arguments: "arg1, arg2, ...)"
20580 */
20581 while (*p != ')')
20582 {
20583 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20584 {
20585 varargs = TRUE;
20586 p += 3;
20587 mustend = TRUE;
20588 }
20589 else
20590 {
20591 arg = p;
20592 while (ASCII_ISALNUM(*p) || *p == '_')
20593 ++p;
20594 if (arg == p || isdigit(*arg)
20595 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20596 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20597 {
20598 if (!eap->skip)
20599 EMSG2(_("E125: Illegal argument: %s"), arg);
20600 break;
20601 }
20602 if (ga_grow(&newargs, 1) == FAIL)
20603 goto erret;
20604 c = *p;
20605 *p = NUL;
20606 arg = vim_strsave(arg);
20607 if (arg == NULL)
20608 goto erret;
20609 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20610 *p = c;
20611 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020612 if (*p == ',')
20613 ++p;
20614 else
20615 mustend = TRUE;
20616 }
20617 p = skipwhite(p);
20618 if (mustend && *p != ')')
20619 {
20620 if (!eap->skip)
20621 EMSG2(_(e_invarg2), eap->arg);
20622 break;
20623 }
20624 }
20625 ++p; /* skip the ')' */
20626
Bram Moolenaare9a41262005-01-15 22:18:47 +000020627 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020628 for (;;)
20629 {
20630 p = skipwhite(p);
20631 if (STRNCMP(p, "range", 5) == 0)
20632 {
20633 flags |= FC_RANGE;
20634 p += 5;
20635 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020636 else if (STRNCMP(p, "dict", 4) == 0)
20637 {
20638 flags |= FC_DICT;
20639 p += 4;
20640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641 else if (STRNCMP(p, "abort", 5) == 0)
20642 {
20643 flags |= FC_ABORT;
20644 p += 5;
20645 }
20646 else
20647 break;
20648 }
20649
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020650 /* When there is a line break use what follows for the function body.
20651 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20652 if (*p == '\n')
20653 line_arg = p + 1;
20654 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020655 EMSG(_(e_trailing));
20656
20657 /*
20658 * Read the body of the function, until ":endfunction" is found.
20659 */
20660 if (KeyTyped)
20661 {
20662 /* Check if the function already exists, don't let the user type the
20663 * whole function before telling him it doesn't work! For a script we
20664 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020665 if (!eap->skip && !eap->forceit)
20666 {
20667 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20668 EMSG(_(e_funcdict));
20669 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020670 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020672
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020673 if (!eap->skip && did_emsg)
20674 goto erret;
20675
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676 msg_putchar('\n'); /* don't overwrite the function name */
20677 cmdline_row = msg_row;
20678 }
20679
20680 indent = 2;
20681 nesting = 0;
20682 for (;;)
20683 {
20684 msg_scroll = TRUE;
20685 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020686 sourcing_lnum_off = sourcing_lnum;
20687
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020688 if (line_arg != NULL)
20689 {
20690 /* Use eap->arg, split up in parts by line breaks. */
20691 theline = line_arg;
20692 p = vim_strchr(theline, '\n');
20693 if (p == NULL)
20694 line_arg += STRLEN(line_arg);
20695 else
20696 {
20697 *p = NUL;
20698 line_arg = p + 1;
20699 }
20700 }
20701 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702 theline = getcmdline(':', 0L, indent);
20703 else
20704 theline = eap->getline(':', eap->cookie, indent);
20705 if (KeyTyped)
20706 lines_left = Rows - 1;
20707 if (theline == NULL)
20708 {
20709 EMSG(_("E126: Missing :endfunction"));
20710 goto erret;
20711 }
20712
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020713 /* Detect line continuation: sourcing_lnum increased more than one. */
20714 if (sourcing_lnum > sourcing_lnum_off + 1)
20715 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20716 else
20717 sourcing_lnum_off = 0;
20718
Bram Moolenaar071d4272004-06-13 20:20:40 +000020719 if (skip_until != NULL)
20720 {
20721 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20722 * don't check for ":endfunc". */
20723 if (STRCMP(theline, skip_until) == 0)
20724 {
20725 vim_free(skip_until);
20726 skip_until = NULL;
20727 }
20728 }
20729 else
20730 {
20731 /* skip ':' and blanks*/
20732 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20733 ;
20734
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020735 /* Check for "endfunction". */
20736 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020738 if (line_arg == NULL)
20739 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740 break;
20741 }
20742
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020743 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020744 * at "end". */
20745 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20746 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020747 else if (STRNCMP(p, "if", 2) == 0
20748 || STRNCMP(p, "wh", 2) == 0
20749 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020750 || STRNCMP(p, "try", 3) == 0)
20751 indent += 2;
20752
20753 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020754 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020755 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020756 if (*p == '!')
20757 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758 p += eval_fname_script(p);
20759 if (ASCII_ISALPHA(*p))
20760 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020761 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762 if (*skipwhite(p) == '(')
20763 {
20764 ++nesting;
20765 indent += 2;
20766 }
20767 }
20768 }
20769
20770 /* Check for ":append" or ":insert". */
20771 p = skip_range(p, NULL);
20772 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20773 || (p[0] == 'i'
20774 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20775 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20776 skip_until = vim_strsave((char_u *)".");
20777
20778 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20779 arg = skipwhite(skiptowhite(p));
20780 if (arg[0] == '<' && arg[1] =='<'
20781 && ((p[0] == 'p' && p[1] == 'y'
20782 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20783 || (p[0] == 'p' && p[1] == 'e'
20784 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20785 || (p[0] == 't' && p[1] == 'c'
20786 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20787 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20788 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020789 || (p[0] == 'm' && p[1] == 'z'
20790 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020791 ))
20792 {
20793 /* ":python <<" continues until a dot, like ":append" */
20794 p = skipwhite(arg + 2);
20795 if (*p == NUL)
20796 skip_until = vim_strsave((char_u *)".");
20797 else
20798 skip_until = vim_strsave(p);
20799 }
20800 }
20801
20802 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020803 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020804 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020805 if (line_arg == NULL)
20806 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020808 }
20809
20810 /* Copy the line to newly allocated memory. get_one_sourceline()
20811 * allocates 250 bytes per line, this saves 80% on average. The cost
20812 * is an extra alloc/free. */
20813 p = vim_strsave(theline);
20814 if (p != NULL)
20815 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020816 if (line_arg == NULL)
20817 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020818 theline = p;
20819 }
20820
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020821 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20822
20823 /* Add NULL lines for continuation lines, so that the line count is
20824 * equal to the index in the growarray. */
20825 while (sourcing_lnum_off-- > 0)
20826 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020827
20828 /* Check for end of eap->arg. */
20829 if (line_arg != NULL && *line_arg == NUL)
20830 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831 }
20832
20833 /* Don't define the function when skipping commands or when an error was
20834 * detected. */
20835 if (eap->skip || did_emsg)
20836 goto erret;
20837
20838 /*
20839 * If there are no errors, add the function
20840 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020841 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020842 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020843 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020844 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020845 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020846 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020847 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020848 goto erret;
20849 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020850
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020851 fp = find_func(name);
20852 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020853 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020854 if (!eap->forceit)
20855 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020856 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020857 goto erret;
20858 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020859 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020860 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020861 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020862 name);
20863 goto erret;
20864 }
20865 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020866 ga_clear_strings(&(fp->uf_args));
20867 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020868 vim_free(name);
20869 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020871 }
20872 else
20873 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020874 char numbuf[20];
20875
20876 fp = NULL;
20877 if (fudi.fd_newkey == NULL && !eap->forceit)
20878 {
20879 EMSG(_(e_funcdict));
20880 goto erret;
20881 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020882 if (fudi.fd_di == NULL)
20883 {
20884 /* Can't add a function to a locked dictionary */
20885 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20886 goto erret;
20887 }
20888 /* Can't change an existing function if it is locked */
20889 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20890 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020891
20892 /* Give the function a sequential number. Can only be used with a
20893 * Funcref! */
20894 vim_free(name);
20895 sprintf(numbuf, "%d", ++func_nr);
20896 name = vim_strsave((char_u *)numbuf);
20897 if (name == NULL)
20898 goto erret;
20899 }
20900
20901 if (fp == NULL)
20902 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020903 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020904 {
20905 int slen, plen;
20906 char_u *scriptname;
20907
20908 /* Check that the autoload name matches the script name. */
20909 j = FAIL;
20910 if (sourcing_name != NULL)
20911 {
20912 scriptname = autoload_name(name);
20913 if (scriptname != NULL)
20914 {
20915 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020916 plen = (int)STRLEN(p);
20917 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020918 if (slen > plen && fnamecmp(p,
20919 sourcing_name + slen - plen) == 0)
20920 j = OK;
20921 vim_free(scriptname);
20922 }
20923 }
20924 if (j == FAIL)
20925 {
20926 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20927 goto erret;
20928 }
20929 }
20930
20931 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020932 if (fp == NULL)
20933 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020934
20935 if (fudi.fd_dict != NULL)
20936 {
20937 if (fudi.fd_di == NULL)
20938 {
20939 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020940 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020941 if (fudi.fd_di == NULL)
20942 {
20943 vim_free(fp);
20944 goto erret;
20945 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020946 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20947 {
20948 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020949 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020950 goto erret;
20951 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020952 }
20953 else
20954 /* overwrite existing dict entry */
20955 clear_tv(&fudi.fd_di->di_tv);
20956 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020957 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020958 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020959 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020960
20961 /* behave like "dict" was used */
20962 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020963 }
20964
Bram Moolenaar071d4272004-06-13 20:20:40 +000020965 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020966 STRCPY(fp->uf_name, name);
20967 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020969 fp->uf_args = newargs;
20970 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020971#ifdef FEAT_PROFILE
20972 fp->uf_tml_count = NULL;
20973 fp->uf_tml_total = NULL;
20974 fp->uf_tml_self = NULL;
20975 fp->uf_profiling = FALSE;
20976 if (prof_def_func())
20977 func_do_profile(fp);
20978#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020979 fp->uf_varargs = varargs;
20980 fp->uf_flags = flags;
20981 fp->uf_calls = 0;
20982 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020983 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020984
20985erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 ga_clear_strings(&newargs);
20987 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020988ret_free:
20989 vim_free(skip_until);
20990 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993}
20994
20995/*
20996 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020997 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020998 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020999 * flags:
21000 * TFN_INT: internal function name OK
21001 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002 * Advances "pp" to just after the function name (if no error).
21003 */
21004 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021005trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006 char_u **pp;
21007 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021008 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021009 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021011 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 char_u *start;
21013 char_u *end;
21014 int lead;
21015 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021017 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021018
21019 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021020 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021022
21023 /* Check for hard coded <SNR>: already translated function ID (from a user
21024 * command). */
21025 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21026 && (*pp)[2] == (int)KE_SNR)
21027 {
21028 *pp += 3;
21029 len = get_id_len(pp) + 3;
21030 return vim_strnsave(start, len);
21031 }
21032
21033 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21034 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021036 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021037 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021038
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021039 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21040 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021041 if (end == start)
21042 {
21043 if (!skip)
21044 EMSG(_("E129: Function name required"));
21045 goto theend;
21046 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021047 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021048 {
21049 /*
21050 * Report an invalid expression in braces, unless the expression
21051 * evaluation has been cancelled due to an aborting error, an
21052 * interrupt, or an exception.
21053 */
21054 if (!aborting())
21055 {
21056 if (end != NULL)
21057 EMSG2(_(e_invarg2), start);
21058 }
21059 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021060 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021061 goto theend;
21062 }
21063
21064 if (lv.ll_tv != NULL)
21065 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021066 if (fdp != NULL)
21067 {
21068 fdp->fd_dict = lv.ll_dict;
21069 fdp->fd_newkey = lv.ll_newkey;
21070 lv.ll_newkey = NULL;
21071 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021072 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021073 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21074 {
21075 name = vim_strsave(lv.ll_tv->vval.v_string);
21076 *pp = end;
21077 }
21078 else
21079 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021080 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21081 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021082 EMSG(_(e_funcref));
21083 else
21084 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021085 name = NULL;
21086 }
21087 goto theend;
21088 }
21089
21090 if (lv.ll_name == NULL)
21091 {
21092 /* Error found, but continue after the function name. */
21093 *pp = end;
21094 goto theend;
21095 }
21096
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021097 /* Check if the name is a Funcref. If so, use the value. */
21098 if (lv.ll_exp_name != NULL)
21099 {
21100 len = (int)STRLEN(lv.ll_exp_name);
21101 name = deref_func_name(lv.ll_exp_name, &len);
21102 if (name == lv.ll_exp_name)
21103 name = NULL;
21104 }
21105 else
21106 {
21107 len = (int)(end - *pp);
21108 name = deref_func_name(*pp, &len);
21109 if (name == *pp)
21110 name = NULL;
21111 }
21112 if (name != NULL)
21113 {
21114 name = vim_strsave(name);
21115 *pp = end;
21116 goto theend;
21117 }
21118
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021119 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021120 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021121 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021122 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21123 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21124 {
21125 /* When there was "s:" already or the name expanded to get a
21126 * leading "s:" then remove it. */
21127 lv.ll_name += 2;
21128 len -= 2;
21129 lead = 2;
21130 }
21131 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021132 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021133 {
21134 if (lead == 2) /* skip over "s:" */
21135 lv.ll_name += 2;
21136 len = (int)(end - lv.ll_name);
21137 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021138
21139 /*
21140 * Copy the function name to allocated memory.
21141 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21142 * Accept <SNR>123_name() outside a script.
21143 */
21144 if (skip)
21145 lead = 0; /* do nothing */
21146 else if (lead > 0)
21147 {
21148 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021149 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21150 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021151 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021152 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021153 if (current_SID <= 0)
21154 {
21155 EMSG(_(e_usingsid));
21156 goto theend;
21157 }
21158 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21159 lead += (int)STRLEN(sid_buf);
21160 }
21161 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021162 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021163 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021164 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021165 goto theend;
21166 }
21167 name = alloc((unsigned)(len + lead + 1));
21168 if (name != NULL)
21169 {
21170 if (lead > 0)
21171 {
21172 name[0] = K_SPECIAL;
21173 name[1] = KS_EXTRA;
21174 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021175 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021176 STRCPY(name + 3, sid_buf);
21177 }
21178 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21179 name[len + lead] = NUL;
21180 }
21181 *pp = end;
21182
21183theend:
21184 clear_lval(&lv);
21185 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186}
21187
21188/*
21189 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21190 * Return 2 if "p" starts with "s:".
21191 * Return 0 otherwise.
21192 */
21193 static int
21194eval_fname_script(p)
21195 char_u *p;
21196{
21197 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21198 || STRNICMP(p + 1, "SNR>", 4) == 0))
21199 return 5;
21200 if (p[0] == 's' && p[1] == ':')
21201 return 2;
21202 return 0;
21203}
21204
21205/*
21206 * Return TRUE if "p" starts with "<SID>" or "s:".
21207 * Only works if eval_fname_script() returned non-zero for "p"!
21208 */
21209 static int
21210eval_fname_sid(p)
21211 char_u *p;
21212{
21213 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21214}
21215
21216/*
21217 * List the head of the function: "name(arg1, arg2)".
21218 */
21219 static void
21220list_func_head(fp, indent)
21221 ufunc_T *fp;
21222 int indent;
21223{
21224 int j;
21225
21226 msg_start();
21227 if (indent)
21228 MSG_PUTS(" ");
21229 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021230 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021231 {
21232 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021233 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234 }
21235 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021236 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021238 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239 {
21240 if (j)
21241 MSG_PUTS(", ");
21242 msg_puts(FUNCARG(fp, j));
21243 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021244 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021245 {
21246 if (j)
21247 MSG_PUTS(", ");
21248 MSG_PUTS("...");
21249 }
21250 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021251 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021252 if (p_verbose > 0)
21253 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021254}
21255
21256/*
21257 * Find a function by name, return pointer to it in ufuncs.
21258 * Return NULL for unknown function.
21259 */
21260 static ufunc_T *
21261find_func(name)
21262 char_u *name;
21263{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021264 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021265
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021266 hi = hash_find(&func_hashtab, name);
21267 if (!HASHITEM_EMPTY(hi))
21268 return HI2UF(hi);
21269 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270}
21271
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021272#if defined(EXITFREE) || defined(PROTO)
21273 void
21274free_all_functions()
21275{
21276 hashitem_T *hi;
21277
21278 /* Need to start all over every time, because func_free() may change the
21279 * hash table. */
21280 while (func_hashtab.ht_used > 0)
21281 for (hi = func_hashtab.ht_array; ; ++hi)
21282 if (!HASHITEM_EMPTY(hi))
21283 {
21284 func_free(HI2UF(hi));
21285 break;
21286 }
21287}
21288#endif
21289
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021290/*
21291 * Return TRUE if a function "name" exists.
21292 */
21293 static int
21294function_exists(name)
21295 char_u *name;
21296{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021297 char_u *nm = name;
21298 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021299 int n = FALSE;
21300
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021301 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021302 nm = skipwhite(nm);
21303
21304 /* Only accept "funcname", "funcname ", "funcname (..." and
21305 * "funcname(...", not "funcname!...". */
21306 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021307 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021308 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021309 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021310 else
21311 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021312 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021313 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021314 return n;
21315}
21316
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021317/*
21318 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021319 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021320 */
21321 static int
21322builtin_function(name)
21323 char_u *name;
21324{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021325 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21326 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021327}
21328
Bram Moolenaar05159a02005-02-26 23:04:13 +000021329#if defined(FEAT_PROFILE) || defined(PROTO)
21330/*
21331 * Start profiling function "fp".
21332 */
21333 static void
21334func_do_profile(fp)
21335 ufunc_T *fp;
21336{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021337 int len = fp->uf_lines.ga_len;
21338
21339 if (len == 0)
21340 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021341 fp->uf_tm_count = 0;
21342 profile_zero(&fp->uf_tm_self);
21343 profile_zero(&fp->uf_tm_total);
21344 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021345 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021346 if (fp->uf_tml_total == NULL)
21347 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021348 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021349 if (fp->uf_tml_self == NULL)
21350 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021351 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021352 fp->uf_tml_idx = -1;
21353 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21354 || fp->uf_tml_self == NULL)
21355 return; /* out of memory */
21356
21357 fp->uf_profiling = TRUE;
21358}
21359
21360/*
21361 * Dump the profiling results for all functions in file "fd".
21362 */
21363 void
21364func_dump_profile(fd)
21365 FILE *fd;
21366{
21367 hashitem_T *hi;
21368 int todo;
21369 ufunc_T *fp;
21370 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021371 ufunc_T **sorttab;
21372 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021373
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021374 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021375 if (todo == 0)
21376 return; /* nothing to dump */
21377
Bram Moolenaar73830342005-02-28 22:48:19 +000021378 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21379
Bram Moolenaar05159a02005-02-26 23:04:13 +000021380 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21381 {
21382 if (!HASHITEM_EMPTY(hi))
21383 {
21384 --todo;
21385 fp = HI2UF(hi);
21386 if (fp->uf_profiling)
21387 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021388 if (sorttab != NULL)
21389 sorttab[st_len++] = fp;
21390
Bram Moolenaar05159a02005-02-26 23:04:13 +000021391 if (fp->uf_name[0] == K_SPECIAL)
21392 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21393 else
21394 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21395 if (fp->uf_tm_count == 1)
21396 fprintf(fd, "Called 1 time\n");
21397 else
21398 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21399 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21400 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21401 fprintf(fd, "\n");
21402 fprintf(fd, "count total (s) self (s)\n");
21403
21404 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21405 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021406 if (FUNCLINE(fp, i) == NULL)
21407 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021408 prof_func_line(fd, fp->uf_tml_count[i],
21409 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021410 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21411 }
21412 fprintf(fd, "\n");
21413 }
21414 }
21415 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021416
21417 if (sorttab != NULL && st_len > 0)
21418 {
21419 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21420 prof_total_cmp);
21421 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21422 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21423 prof_self_cmp);
21424 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21425 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021426
21427 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021428}
Bram Moolenaar73830342005-02-28 22:48:19 +000021429
21430 static void
21431prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21432 FILE *fd;
21433 ufunc_T **sorttab;
21434 int st_len;
21435 char *title;
21436 int prefer_self; /* when equal print only self time */
21437{
21438 int i;
21439 ufunc_T *fp;
21440
21441 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21442 fprintf(fd, "count total (s) self (s) function\n");
21443 for (i = 0; i < 20 && i < st_len; ++i)
21444 {
21445 fp = sorttab[i];
21446 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21447 prefer_self);
21448 if (fp->uf_name[0] == K_SPECIAL)
21449 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21450 else
21451 fprintf(fd, " %s()\n", fp->uf_name);
21452 }
21453 fprintf(fd, "\n");
21454}
21455
21456/*
21457 * Print the count and times for one function or function line.
21458 */
21459 static void
21460prof_func_line(fd, count, total, self, prefer_self)
21461 FILE *fd;
21462 int count;
21463 proftime_T *total;
21464 proftime_T *self;
21465 int prefer_self; /* when equal print only self time */
21466{
21467 if (count > 0)
21468 {
21469 fprintf(fd, "%5d ", count);
21470 if (prefer_self && profile_equal(total, self))
21471 fprintf(fd, " ");
21472 else
21473 fprintf(fd, "%s ", profile_msg(total));
21474 if (!prefer_self && profile_equal(total, self))
21475 fprintf(fd, " ");
21476 else
21477 fprintf(fd, "%s ", profile_msg(self));
21478 }
21479 else
21480 fprintf(fd, " ");
21481}
21482
21483/*
21484 * Compare function for total time sorting.
21485 */
21486 static int
21487#ifdef __BORLANDC__
21488_RTLENTRYF
21489#endif
21490prof_total_cmp(s1, s2)
21491 const void *s1;
21492 const void *s2;
21493{
21494 ufunc_T *p1, *p2;
21495
21496 p1 = *(ufunc_T **)s1;
21497 p2 = *(ufunc_T **)s2;
21498 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21499}
21500
21501/*
21502 * Compare function for self time sorting.
21503 */
21504 static int
21505#ifdef __BORLANDC__
21506_RTLENTRYF
21507#endif
21508prof_self_cmp(s1, s2)
21509 const void *s1;
21510 const void *s2;
21511{
21512 ufunc_T *p1, *p2;
21513
21514 p1 = *(ufunc_T **)s1;
21515 p2 = *(ufunc_T **)s2;
21516 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21517}
21518
Bram Moolenaar05159a02005-02-26 23:04:13 +000021519#endif
21520
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021521/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021522 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021523 * Return TRUE if a package was loaded.
21524 */
21525 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021526script_autoload(name, reload)
21527 char_u *name;
21528 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021529{
21530 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021531 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021532 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021533 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021534
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021535 /* Return quickly when autoload disabled. */
21536 if (no_autoload)
21537 return FALSE;
21538
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021539 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021540 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021541 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021542 return FALSE;
21543
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021544 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021545
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021546 /* Find the name in the list of previously loaded package names. Skip
21547 * "autoload/", it's always the same. */
21548 for (i = 0; i < ga_loaded.ga_len; ++i)
21549 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21550 break;
21551 if (!reload && i < ga_loaded.ga_len)
21552 ret = FALSE; /* was loaded already */
21553 else
21554 {
21555 /* Remember the name if it wasn't loaded already. */
21556 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21557 {
21558 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21559 tofree = NULL;
21560 }
21561
21562 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021563 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021564 ret = TRUE;
21565 }
21566
21567 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021568 return ret;
21569}
21570
21571/*
21572 * Return the autoload script name for a function or variable name.
21573 * Returns NULL when out of memory.
21574 */
21575 static char_u *
21576autoload_name(name)
21577 char_u *name;
21578{
21579 char_u *p;
21580 char_u *scriptname;
21581
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021582 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021583 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21584 if (scriptname == NULL)
21585 return FALSE;
21586 STRCPY(scriptname, "autoload/");
21587 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021588 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021589 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021590 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021591 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021592 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021593}
21594
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21596
21597/*
21598 * Function given to ExpandGeneric() to obtain the list of user defined
21599 * function names.
21600 */
21601 char_u *
21602get_user_func_name(xp, idx)
21603 expand_T *xp;
21604 int idx;
21605{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021606 static long_u done;
21607 static hashitem_T *hi;
21608 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609
21610 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021612 done = 0;
21613 hi = func_hashtab.ht_array;
21614 }
21615 if (done < func_hashtab.ht_used)
21616 {
21617 if (done++ > 0)
21618 ++hi;
21619 while (HASHITEM_EMPTY(hi))
21620 ++hi;
21621 fp = HI2UF(hi);
21622
21623 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21624 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625
21626 cat_func_name(IObuff, fp);
21627 if (xp->xp_context != EXPAND_USER_FUNC)
21628 {
21629 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021630 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631 STRCAT(IObuff, ")");
21632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633 return IObuff;
21634 }
21635 return NULL;
21636}
21637
21638#endif /* FEAT_CMDL_COMPL */
21639
21640/*
21641 * Copy the function name of "fp" to buffer "buf".
21642 * "buf" must be able to hold the function name plus three bytes.
21643 * Takes care of script-local function names.
21644 */
21645 static void
21646cat_func_name(buf, fp)
21647 char_u *buf;
21648 ufunc_T *fp;
21649{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021650 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651 {
21652 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021653 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 }
21655 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021656 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657}
21658
21659/*
21660 * ":delfunction {name}"
21661 */
21662 void
21663ex_delfunction(eap)
21664 exarg_T *eap;
21665{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021666 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667 char_u *p;
21668 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021669 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021670
21671 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021672 name = trans_function_name(&p, eap->skip, 0, &fudi);
21673 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021675 {
21676 if (fudi.fd_dict != NULL && !eap->skip)
21677 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021678 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680 if (!ends_excmd(*skipwhite(p)))
21681 {
21682 vim_free(name);
21683 EMSG(_(e_trailing));
21684 return;
21685 }
21686 eap->nextcmd = check_nextcmd(p);
21687 if (eap->nextcmd != NULL)
21688 *p = NUL;
21689
21690 if (!eap->skip)
21691 fp = find_func(name);
21692 vim_free(name);
21693
21694 if (!eap->skip)
21695 {
21696 if (fp == NULL)
21697 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021698 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021699 return;
21700 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021701 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702 {
21703 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21704 return;
21705 }
21706
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021707 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021709 /* Delete the dict item that refers to the function, it will
21710 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021711 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021712 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021713 else
21714 func_free(fp);
21715 }
21716}
21717
21718/*
21719 * Free a function and remove it from the list of functions.
21720 */
21721 static void
21722func_free(fp)
21723 ufunc_T *fp;
21724{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021725 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021726
21727 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021728 ga_clear_strings(&(fp->uf_args));
21729 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021730#ifdef FEAT_PROFILE
21731 vim_free(fp->uf_tml_count);
21732 vim_free(fp->uf_tml_total);
21733 vim_free(fp->uf_tml_self);
21734#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021735
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021736 /* remove the function from the function hashtable */
21737 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21738 if (HASHITEM_EMPTY(hi))
21739 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021740 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021741 hash_remove(&func_hashtab, hi);
21742
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021743 vim_free(fp);
21744}
21745
21746/*
21747 * Unreference a Function: decrement the reference count and free it when it
21748 * becomes zero. Only for numbered functions.
21749 */
21750 static void
21751func_unref(name)
21752 char_u *name;
21753{
21754 ufunc_T *fp;
21755
21756 if (name != NULL && isdigit(*name))
21757 {
21758 fp = find_func(name);
21759 if (fp == NULL)
21760 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021761 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021762 {
21763 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021764 * when "uf_calls" becomes zero. */
21765 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021766 func_free(fp);
21767 }
21768 }
21769}
21770
21771/*
21772 * Count a reference to a Function.
21773 */
21774 static void
21775func_ref(name)
21776 char_u *name;
21777{
21778 ufunc_T *fp;
21779
21780 if (name != NULL && isdigit(*name))
21781 {
21782 fp = find_func(name);
21783 if (fp == NULL)
21784 EMSG2(_(e_intern2), "func_ref()");
21785 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021786 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787 }
21788}
21789
21790/*
21791 * Call a user function.
21792 */
21793 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021794call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795 ufunc_T *fp; /* pointer to function */
21796 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021797 typval_T *argvars; /* arguments */
21798 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021799 linenr_T firstline; /* first line of range */
21800 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021801 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802{
Bram Moolenaar33570922005-01-25 22:26:29 +000021803 char_u *save_sourcing_name;
21804 linenr_T save_sourcing_lnum;
21805 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021806 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021807 int save_did_emsg;
21808 static int depth = 0;
21809 dictitem_T *v;
21810 int fixvar_idx = 0; /* index in fixvar[] */
21811 int i;
21812 int ai;
21813 char_u numbuf[NUMBUFLEN];
21814 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021815#ifdef FEAT_PROFILE
21816 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021817 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021819
21820 /* If depth of calling is getting too high, don't execute the function */
21821 if (depth >= p_mfd)
21822 {
21823 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021824 rettv->v_type = VAR_NUMBER;
21825 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021826 return;
21827 }
21828 ++depth;
21829
21830 line_breakcheck(); /* check for CTRL-C hit */
21831
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021832 fc = (funccall_T *)alloc(sizeof(funccall_T));
21833 fc->caller = current_funccal;
21834 current_funccal = fc;
21835 fc->func = fp;
21836 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021837 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021838 fc->linenr = 0;
21839 fc->returned = FALSE;
21840 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021842 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21843 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021844
Bram Moolenaar33570922005-01-25 22:26:29 +000021845 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021846 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021847 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21848 * each argument variable and saves a lot of time.
21849 */
21850 /*
21851 * Init l: variables.
21852 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021853 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021854 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021855 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021856 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21857 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021858 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021859 name = v->di_key;
21860 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021861 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021862 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021863 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021864 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021865 v->di_tv.vval.v_dict = selfdict;
21866 ++selfdict->dv_refcount;
21867 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021868
Bram Moolenaar33570922005-01-25 22:26:29 +000021869 /*
21870 * Init a: variables.
21871 * Set a:0 to "argcount".
21872 * Set a:000 to a list with room for the "..." arguments.
21873 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021874 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21875 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021876 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021877 /* Use "name" to avoid a warning from some compiler that checks the
21878 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021879 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021880 name = v->di_key;
21881 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021882 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021883 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021884 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021885 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021886 v->di_tv.vval.v_list = &fc->l_varlist;
21887 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21888 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21889 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021890
21891 /*
21892 * Set a:firstline to "firstline" and a:lastline to "lastline".
21893 * Set a:name to named arguments.
21894 * Set a:N to the "..." arguments.
21895 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021896 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021897 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021898 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021899 (varnumber_T)lastline);
21900 for (i = 0; i < argcount; ++i)
21901 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021902 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021903 if (ai < 0)
21904 /* named argument a:name */
21905 name = FUNCARG(fp, i);
21906 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021907 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021908 /* "..." argument a:1, a:2, etc. */
21909 sprintf((char *)numbuf, "%d", ai + 1);
21910 name = numbuf;
21911 }
21912 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21913 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021914 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021915 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21916 }
21917 else
21918 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021919 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21920 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021921 if (v == NULL)
21922 break;
21923 v->di_flags = DI_FLAGS_RO;
21924 }
21925 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021926 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021927
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021928 /* Note: the values are copied directly to avoid alloc/free.
21929 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021930 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021931 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021932
21933 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21934 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021935 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21936 fc->l_listitems[ai].li_tv = argvars[i];
21937 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021938 }
21939 }
21940
Bram Moolenaar071d4272004-06-13 20:20:40 +000021941 /* Don't redraw while executing the function. */
21942 ++RedrawingDisabled;
21943 save_sourcing_name = sourcing_name;
21944 save_sourcing_lnum = sourcing_lnum;
21945 sourcing_lnum = 1;
21946 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021947 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021948 if (sourcing_name != NULL)
21949 {
21950 if (save_sourcing_name != NULL
21951 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21952 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21953 else
21954 STRCPY(sourcing_name, "function ");
21955 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21956
21957 if (p_verbose >= 12)
21958 {
21959 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021960 verbose_enter_scroll();
21961
Bram Moolenaar555b2802005-05-19 21:08:39 +000021962 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021963 if (p_verbose >= 14)
21964 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021965 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021966 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021967 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021968 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021969
21970 msg_puts((char_u *)"(");
21971 for (i = 0; i < argcount; ++i)
21972 {
21973 if (i > 0)
21974 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021975 if (argvars[i].v_type == VAR_NUMBER)
21976 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977 else
21978 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021979 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21980 if (s != NULL)
21981 {
21982 trunc_string(s, buf, MSG_BUF_CLEN);
21983 msg_puts(buf);
21984 vim_free(tofree);
21985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021986 }
21987 }
21988 msg_puts((char_u *)")");
21989 }
21990 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021991
21992 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993 --no_wait_return;
21994 }
21995 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021996#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021997 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021998 {
21999 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22000 func_do_profile(fp);
22001 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022002 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022003 {
22004 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022005 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022006 profile_zero(&fp->uf_tm_children);
22007 }
22008 script_prof_save(&wait_start);
22009 }
22010#endif
22011
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022013 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014 save_did_emsg = did_emsg;
22015 did_emsg = FALSE;
22016
22017 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022018 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22020
22021 --RedrawingDisabled;
22022
22023 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022024 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022026 clear_tv(rettv);
22027 rettv->v_type = VAR_NUMBER;
22028 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029 }
22030
Bram Moolenaar05159a02005-02-26 23:04:13 +000022031#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022032 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022033 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022034 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022035 profile_end(&call_start);
22036 profile_sub_wait(&wait_start, &call_start);
22037 profile_add(&fp->uf_tm_total, &call_start);
22038 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022039 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022040 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022041 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22042 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022043 }
22044 }
22045#endif
22046
Bram Moolenaar071d4272004-06-13 20:20:40 +000022047 /* when being verbose, mention the return value */
22048 if (p_verbose >= 12)
22049 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022051 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052
Bram Moolenaar071d4272004-06-13 20:20:40 +000022053 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022054 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022055 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022056 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022057 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022058 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022059 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022060 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022061 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022062 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022063 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022064
Bram Moolenaar555b2802005-05-19 21:08:39 +000022065 /* The value may be very long. Skip the middle part, so that we
22066 * have some idea how it starts and ends. smsg() would always
22067 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022068 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022069 if (s != NULL)
22070 {
22071 trunc_string(s, buf, MSG_BUF_CLEN);
22072 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
22073 vim_free(tofree);
22074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075 }
22076 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022077
22078 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022079 --no_wait_return;
22080 }
22081
22082 vim_free(sourcing_name);
22083 sourcing_name = save_sourcing_name;
22084 sourcing_lnum = save_sourcing_lnum;
22085 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022086#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022087 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022088 script_prof_restore(&wait_start);
22089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022090
22091 if (p_verbose >= 12 && sourcing_name != NULL)
22092 {
22093 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022094 verbose_enter_scroll();
22095
Bram Moolenaar555b2802005-05-19 21:08:39 +000022096 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022098
22099 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100 --no_wait_return;
22101 }
22102
22103 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022104 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022106
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022107 /* If the a:000 list and the l: and a: dicts are not referenced we can
22108 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022109 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22110 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22111 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22112 {
22113 free_funccal(fc, FALSE);
22114 }
22115 else
22116 {
22117 hashitem_T *hi;
22118 listitem_T *li;
22119 int todo;
22120
22121 /* "fc" is still in use. This can happen when returning "a:000" or
22122 * assigning "l:" to a global variable.
22123 * Link "fc" in the list for garbage collection later. */
22124 fc->caller = previous_funccal;
22125 previous_funccal = fc;
22126
22127 /* Make a copy of the a: variables, since we didn't do that above. */
22128 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22129 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22130 {
22131 if (!HASHITEM_EMPTY(hi))
22132 {
22133 --todo;
22134 v = HI2DI(hi);
22135 copy_tv(&v->di_tv, &v->di_tv);
22136 }
22137 }
22138
22139 /* Make a copy of the a:000 items, since we didn't do that above. */
22140 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22141 copy_tv(&li->li_tv, &li->li_tv);
22142 }
22143}
22144
22145/*
22146 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022147 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022148 */
22149 static int
22150can_free_funccal(fc, copyID)
22151 funccall_T *fc;
22152 int copyID;
22153{
22154 return (fc->l_varlist.lv_copyID != copyID
22155 && fc->l_vars.dv_copyID != copyID
22156 && fc->l_avars.dv_copyID != copyID);
22157}
22158
22159/*
22160 * Free "fc" and what it contains.
22161 */
22162 static void
22163free_funccal(fc, free_val)
22164 funccall_T *fc;
22165 int free_val; /* a: vars were allocated */
22166{
22167 listitem_T *li;
22168
22169 /* The a: variables typevals may not have been allocated, only free the
22170 * allocated variables. */
22171 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22172
22173 /* free all l: variables */
22174 vars_clear(&fc->l_vars.dv_hashtab);
22175
22176 /* Free the a:000 variables if they were allocated. */
22177 if (free_val)
22178 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22179 clear_tv(&li->li_tv);
22180
22181 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182}
22183
22184/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022185 * Add a number variable "name" to dict "dp" with value "nr".
22186 */
22187 static void
22188add_nr_var(dp, v, name, nr)
22189 dict_T *dp;
22190 dictitem_T *v;
22191 char *name;
22192 varnumber_T nr;
22193{
22194 STRCPY(v->di_key, name);
22195 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22196 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22197 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022198 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022199 v->di_tv.vval.v_number = nr;
22200}
22201
22202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022203 * ":return [expr]"
22204 */
22205 void
22206ex_return(eap)
22207 exarg_T *eap;
22208{
22209 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022210 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022211 int returning = FALSE;
22212
22213 if (current_funccal == NULL)
22214 {
22215 EMSG(_("E133: :return not inside a function"));
22216 return;
22217 }
22218
22219 if (eap->skip)
22220 ++emsg_skip;
22221
22222 eap->nextcmd = NULL;
22223 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022224 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022225 {
22226 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022227 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022228 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022229 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022230 }
22231 /* It's safer to return also on error. */
22232 else if (!eap->skip)
22233 {
22234 /*
22235 * Return unless the expression evaluation has been cancelled due to an
22236 * aborting error, an interrupt, or an exception.
22237 */
22238 if (!aborting())
22239 returning = do_return(eap, FALSE, TRUE, NULL);
22240 }
22241
22242 /* When skipping or the return gets pending, advance to the next command
22243 * in this line (!returning). Otherwise, ignore the rest of the line.
22244 * Following lines will be ignored by get_func_line(). */
22245 if (returning)
22246 eap->nextcmd = NULL;
22247 else if (eap->nextcmd == NULL) /* no argument */
22248 eap->nextcmd = check_nextcmd(arg);
22249
22250 if (eap->skip)
22251 --emsg_skip;
22252}
22253
22254/*
22255 * Return from a function. Possibly makes the return pending. Also called
22256 * for a pending return at the ":endtry" or after returning from an extra
22257 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022258 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022259 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260 * FALSE when the return gets pending.
22261 */
22262 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022263do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022264 exarg_T *eap;
22265 int reanimate;
22266 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022267 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022268{
22269 int idx;
22270 struct condstack *cstack = eap->cstack;
22271
22272 if (reanimate)
22273 /* Undo the return. */
22274 current_funccal->returned = FALSE;
22275
22276 /*
22277 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22278 * not in its finally clause (which then is to be executed next) is found.
22279 * In this case, make the ":return" pending for execution at the ":endtry".
22280 * Otherwise, return normally.
22281 */
22282 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22283 if (idx >= 0)
22284 {
22285 cstack->cs_pending[idx] = CSTP_RETURN;
22286
22287 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022288 /* A pending return again gets pending. "rettv" points to an
22289 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022291 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292 else
22293 {
22294 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022295 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022297 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022299 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022300 {
22301 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022302 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022303 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022304 else
22305 EMSG(_(e_outofmem));
22306 }
22307 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022308 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022309
22310 if (reanimate)
22311 {
22312 /* The pending return value could be overwritten by a ":return"
22313 * without argument in a finally clause; reset the default
22314 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022315 current_funccal->rettv->v_type = VAR_NUMBER;
22316 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317 }
22318 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022319 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022320 }
22321 else
22322 {
22323 current_funccal->returned = TRUE;
22324
22325 /* If the return is carried out now, store the return value. For
22326 * a return immediately after reanimation, the value is already
22327 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022328 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022329 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022330 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022331 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022332 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022333 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022334 }
22335 }
22336
22337 return idx < 0;
22338}
22339
22340/*
22341 * Free the variable with a pending return value.
22342 */
22343 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022344discard_pending_return(rettv)
22345 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022346{
Bram Moolenaar33570922005-01-25 22:26:29 +000022347 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022348}
22349
22350/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022351 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022352 * is an allocated string. Used by report_pending() for verbose messages.
22353 */
22354 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022355get_return_cmd(rettv)
22356 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022357{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022358 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022359 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022360 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022361
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022362 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022363 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022364 if (s == NULL)
22365 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022366
22367 STRCPY(IObuff, ":return ");
22368 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22369 if (STRLEN(s) + 8 >= IOSIZE)
22370 STRCPY(IObuff + IOSIZE - 4, "...");
22371 vim_free(tofree);
22372 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022373}
22374
22375/*
22376 * Get next function line.
22377 * Called by do_cmdline() to get the next line.
22378 * Returns allocated string, or NULL for end of function.
22379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380 char_u *
22381get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022382 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022383 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022384 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385{
Bram Moolenaar33570922005-01-25 22:26:29 +000022386 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022387 ufunc_T *fp = fcp->func;
22388 char_u *retval;
22389 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390
22391 /* If breakpoints have been added/deleted need to check for it. */
22392 if (fcp->dbg_tick != debug_tick)
22393 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022394 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022395 sourcing_lnum);
22396 fcp->dbg_tick = debug_tick;
22397 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022398#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022399 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022400 func_line_end(cookie);
22401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022402
Bram Moolenaar05159a02005-02-26 23:04:13 +000022403 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022404 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22405 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 retval = NULL;
22407 else
22408 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022409 /* Skip NULL lines (continuation lines). */
22410 while (fcp->linenr < gap->ga_len
22411 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22412 ++fcp->linenr;
22413 if (fcp->linenr >= gap->ga_len)
22414 retval = NULL;
22415 else
22416 {
22417 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22418 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022419#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022420 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022421 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022422#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022424 }
22425
22426 /* Did we encounter a breakpoint? */
22427 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22428 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022429 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022430 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022431 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432 sourcing_lnum);
22433 fcp->dbg_tick = debug_tick;
22434 }
22435
22436 return retval;
22437}
22438
Bram Moolenaar05159a02005-02-26 23:04:13 +000022439#if defined(FEAT_PROFILE) || defined(PROTO)
22440/*
22441 * Called when starting to read a function line.
22442 * "sourcing_lnum" must be correct!
22443 * When skipping lines it may not actually be executed, but we won't find out
22444 * until later and we need to store the time now.
22445 */
22446 void
22447func_line_start(cookie)
22448 void *cookie;
22449{
22450 funccall_T *fcp = (funccall_T *)cookie;
22451 ufunc_T *fp = fcp->func;
22452
22453 if (fp->uf_profiling && sourcing_lnum >= 1
22454 && sourcing_lnum <= fp->uf_lines.ga_len)
22455 {
22456 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022457 /* Skip continuation lines. */
22458 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22459 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022460 fp->uf_tml_execed = FALSE;
22461 profile_start(&fp->uf_tml_start);
22462 profile_zero(&fp->uf_tml_children);
22463 profile_get_wait(&fp->uf_tml_wait);
22464 }
22465}
22466
22467/*
22468 * Called when actually executing a function line.
22469 */
22470 void
22471func_line_exec(cookie)
22472 void *cookie;
22473{
22474 funccall_T *fcp = (funccall_T *)cookie;
22475 ufunc_T *fp = fcp->func;
22476
22477 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22478 fp->uf_tml_execed = TRUE;
22479}
22480
22481/*
22482 * Called when done with a function line.
22483 */
22484 void
22485func_line_end(cookie)
22486 void *cookie;
22487{
22488 funccall_T *fcp = (funccall_T *)cookie;
22489 ufunc_T *fp = fcp->func;
22490
22491 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22492 {
22493 if (fp->uf_tml_execed)
22494 {
22495 ++fp->uf_tml_count[fp->uf_tml_idx];
22496 profile_end(&fp->uf_tml_start);
22497 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022498 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022499 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22500 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022501 }
22502 fp->uf_tml_idx = -1;
22503 }
22504}
22505#endif
22506
Bram Moolenaar071d4272004-06-13 20:20:40 +000022507/*
22508 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022509 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510 */
22511 int
22512func_has_ended(cookie)
22513 void *cookie;
22514{
Bram Moolenaar33570922005-01-25 22:26:29 +000022515 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022516
22517 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22518 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022519 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022520 || fcp->returned);
22521}
22522
22523/*
22524 * return TRUE if cookie indicates a function which "abort"s on errors.
22525 */
22526 int
22527func_has_abort(cookie)
22528 void *cookie;
22529{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022530 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531}
22532
22533#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22534typedef enum
22535{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022536 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22537 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22538 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022539} var_flavour_T;
22540
22541static var_flavour_T var_flavour __ARGS((char_u *varname));
22542
22543 static var_flavour_T
22544var_flavour(varname)
22545 char_u *varname;
22546{
22547 char_u *p = varname;
22548
22549 if (ASCII_ISUPPER(*p))
22550 {
22551 while (*(++p))
22552 if (ASCII_ISLOWER(*p))
22553 return VAR_FLAVOUR_SESSION;
22554 return VAR_FLAVOUR_VIMINFO;
22555 }
22556 else
22557 return VAR_FLAVOUR_DEFAULT;
22558}
22559#endif
22560
22561#if defined(FEAT_VIMINFO) || defined(PROTO)
22562/*
22563 * Restore global vars that start with a capital from the viminfo file
22564 */
22565 int
22566read_viminfo_varlist(virp, writing)
22567 vir_T *virp;
22568 int writing;
22569{
22570 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022571 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022572 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573
22574 if (!writing && (find_viminfo_parameter('!') != NULL))
22575 {
22576 tab = vim_strchr(virp->vir_line + 1, '\t');
22577 if (tab != NULL)
22578 {
22579 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022580 switch (*tab)
22581 {
22582 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022583#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022584 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022585#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022586 case 'D': type = VAR_DICT; break;
22587 case 'L': type = VAR_LIST; break;
22588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589
22590 tab = vim_strchr(tab, '\t');
22591 if (tab != NULL)
22592 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022593 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022594 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022595 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022597#ifdef FEAT_FLOAT
22598 else if (type == VAR_FLOAT)
22599 (void)string2float(tab + 1, &tv.vval.v_float);
22600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022602 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022603 if (type == VAR_DICT || type == VAR_LIST)
22604 {
22605 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
22606
22607 if (etv == NULL)
22608 /* Failed to parse back the dict or list, use it as a
22609 * string. */
22610 tv.v_type = VAR_STRING;
22611 else
22612 {
22613 vim_free(tv.vval.v_string);
22614 tv = *etv;
22615 }
22616 }
22617
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022618 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022619
22620 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022621 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022622 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
22623 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022624 }
22625 }
22626 }
22627
22628 return viminfo_readline(virp);
22629}
22630
22631/*
22632 * Write global vars that start with a capital to the viminfo file
22633 */
22634 void
22635write_viminfo_varlist(fp)
22636 FILE *fp;
22637{
Bram Moolenaar33570922005-01-25 22:26:29 +000022638 hashitem_T *hi;
22639 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022640 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022641 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022642 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022643 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022644 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645
22646 if (find_viminfo_parameter('!') == NULL)
22647 return;
22648
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022649 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022650
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022651 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022652 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022653 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022654 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022655 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022656 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022657 this_var = HI2DI(hi);
22658 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022659 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022660 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022661 {
22662 case VAR_STRING: s = "STR"; break;
22663 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022664#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022665 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022666#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022667 case VAR_DICT: s = "DIC"; break;
22668 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000022669 default: continue;
22670 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022671 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022672 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022673 if (p != NULL)
22674 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022675 vim_free(tofree);
22676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022677 }
22678 }
22679}
22680#endif
22681
22682#if defined(FEAT_SESSION) || defined(PROTO)
22683 int
22684store_session_globals(fd)
22685 FILE *fd;
22686{
Bram Moolenaar33570922005-01-25 22:26:29 +000022687 hashitem_T *hi;
22688 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022689 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690 char_u *p, *t;
22691
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022692 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022693 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022695 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022696 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022697 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022698 this_var = HI2DI(hi);
22699 if ((this_var->di_tv.v_type == VAR_NUMBER
22700 || this_var->di_tv.v_type == VAR_STRING)
22701 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022702 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022703 /* Escape special characters with a backslash. Turn a LF and
22704 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022705 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022706 (char_u *)"\\\"\n\r");
22707 if (p == NULL) /* out of memory */
22708 break;
22709 for (t = p; *t != NUL; ++t)
22710 if (*t == '\n')
22711 *t = 'n';
22712 else if (*t == '\r')
22713 *t = 'r';
22714 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022715 this_var->di_key,
22716 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22717 : ' ',
22718 p,
22719 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22720 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022721 || put_eol(fd) == FAIL)
22722 {
22723 vim_free(p);
22724 return FAIL;
22725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 vim_free(p);
22727 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022728#ifdef FEAT_FLOAT
22729 else if (this_var->di_tv.v_type == VAR_FLOAT
22730 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22731 {
22732 float_T f = this_var->di_tv.vval.v_float;
22733 int sign = ' ';
22734
22735 if (f < 0)
22736 {
22737 f = -f;
22738 sign = '-';
22739 }
22740 if ((fprintf(fd, "let %s = %c&%f",
22741 this_var->di_key, sign, f) < 0)
22742 || put_eol(fd) == FAIL)
22743 return FAIL;
22744 }
22745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746 }
22747 }
22748 return OK;
22749}
22750#endif
22751
Bram Moolenaar661b1822005-07-28 22:36:45 +000022752/*
22753 * Display script name where an item was last set.
22754 * Should only be invoked when 'verbose' is non-zero.
22755 */
22756 void
22757last_set_msg(scriptID)
22758 scid_T scriptID;
22759{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022760 char_u *p;
22761
Bram Moolenaar661b1822005-07-28 22:36:45 +000022762 if (scriptID != 0)
22763 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022764 p = home_replace_save(NULL, get_scriptname(scriptID));
22765 if (p != NULL)
22766 {
22767 verbose_enter();
22768 MSG_PUTS(_("\n\tLast set from "));
22769 MSG_PUTS(p);
22770 vim_free(p);
22771 verbose_leave();
22772 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022773 }
22774}
22775
Bram Moolenaard812df62008-11-09 12:46:09 +000022776/*
22777 * List v:oldfiles in a nice way.
22778 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022779 void
22780ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022781 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022782{
22783 list_T *l = vimvars[VV_OLDFILES].vv_list;
22784 listitem_T *li;
22785 int nr = 0;
22786
22787 if (l == NULL)
22788 msg((char_u *)_("No old files"));
22789 else
22790 {
22791 msg_start();
22792 msg_scroll = TRUE;
22793 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22794 {
22795 msg_outnum((long)++nr);
22796 MSG_PUTS(": ");
22797 msg_outtrans(get_tv_string(&li->li_tv));
22798 msg_putchar('\n');
22799 out_flush(); /* output one line at a time */
22800 ui_breakcheck();
22801 }
22802 /* Assume "got_int" was set to truncate the listing. */
22803 got_int = FALSE;
22804
22805#ifdef FEAT_BROWSE_CMD
22806 if (cmdmod.browse)
22807 {
22808 quit_more = FALSE;
22809 nr = prompt_for_number(FALSE);
22810 msg_starthere();
22811 if (nr > 0)
22812 {
22813 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22814 (long)nr);
22815
22816 if (p != NULL)
22817 {
22818 p = expand_env_save(p);
22819 eap->arg = p;
22820 eap->cmdidx = CMD_edit;
22821 cmdmod.browse = FALSE;
22822 do_exedit(eap, NULL);
22823 vim_free(p);
22824 }
22825 }
22826 }
22827#endif
22828 }
22829}
22830
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831#endif /* FEAT_EVAL */
22832
Bram Moolenaar071d4272004-06-13 20:20:40 +000022833
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022834#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022835
22836#ifdef WIN3264
22837/*
22838 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22839 */
22840static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22841static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22842static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22843
22844/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022845 * Get the short path (8.3) for the filename in "fnamep".
22846 * Only works for a valid file name.
22847 * When the path gets longer "fnamep" is changed and the allocated buffer
22848 * is put in "bufp".
22849 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22850 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851 */
22852 static int
22853get_short_pathname(fnamep, bufp, fnamelen)
22854 char_u **fnamep;
22855 char_u **bufp;
22856 int *fnamelen;
22857{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022858 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859 char_u *newbuf;
22860
22861 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862 l = GetShortPathName(*fnamep, *fnamep, len);
22863 if (l > len - 1)
22864 {
22865 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022866 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022867 newbuf = vim_strnsave(*fnamep, l);
22868 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022869 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022870
22871 vim_free(*bufp);
22872 *fnamep = *bufp = newbuf;
22873
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022874 /* Really should always succeed, as the buffer is big enough. */
22875 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022876 }
22877
22878 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022879 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880}
22881
22882/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022883 * Get the short path (8.3) for the filename in "fname". The converted
22884 * path is returned in "bufp".
22885 *
22886 * Some of the directories specified in "fname" may not exist. This function
22887 * will shorten the existing directories at the beginning of the path and then
22888 * append the remaining non-existing path.
22889 *
22890 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022891 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022892 * bufp - Pointer to an allocated buffer for the filename.
22893 * fnamelen - Length of the filename pointed to by fname
22894 *
22895 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022896 */
22897 static int
22898shortpath_for_invalid_fname(fname, bufp, fnamelen)
22899 char_u **fname;
22900 char_u **bufp;
22901 int *fnamelen;
22902{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022903 char_u *short_fname, *save_fname, *pbuf_unused;
22904 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022906 int old_len, len;
22907 int new_len, sfx_len;
22908 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909
22910 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022911 old_len = *fnamelen;
22912 save_fname = vim_strnsave(*fname, old_len);
22913 pbuf_unused = NULL;
22914 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022916 endp = save_fname + old_len - 1; /* Find the end of the copy */
22917 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022919 /*
22920 * Try shortening the supplied path till it succeeds by removing one
22921 * directory at a time from the tail of the path.
22922 */
22923 len = 0;
22924 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022925 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022926 /* go back one path-separator */
22927 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22928 --endp;
22929 if (endp <= save_fname)
22930 break; /* processed the complete path */
22931
22932 /*
22933 * Replace the path separator with a NUL and try to shorten the
22934 * resulting path.
22935 */
22936 ch = *endp;
22937 *endp = 0;
22938 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022939 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022940 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22941 {
22942 retval = FAIL;
22943 goto theend;
22944 }
22945 *endp = ch; /* preserve the string */
22946
22947 if (len > 0)
22948 break; /* successfully shortened the path */
22949
22950 /* failed to shorten the path. Skip the path separator */
22951 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952 }
22953
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022954 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022956 /*
22957 * Succeeded in shortening the path. Now concatenate the shortened
22958 * path with the remaining path at the tail.
22959 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022960
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022961 /* Compute the length of the new path. */
22962 sfx_len = (int)(save_endp - endp) + 1;
22963 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022965 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022966 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022967 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022969 /* There is not enough space in the currently allocated string,
22970 * copy it to a buffer big enough. */
22971 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022972 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022973 {
22974 retval = FAIL;
22975 goto theend;
22976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022977 }
22978 else
22979 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022980 /* Transfer short_fname to the main buffer (it's big enough),
22981 * unless get_short_pathname() did its work in-place. */
22982 *fname = *bufp = save_fname;
22983 if (short_fname != save_fname)
22984 vim_strncpy(save_fname, short_fname, len);
22985 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022986 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022987
22988 /* concat the not-shortened part of the path */
22989 vim_strncpy(*fname + len, endp, sfx_len);
22990 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022991 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022992
22993theend:
22994 vim_free(pbuf_unused);
22995 vim_free(save_fname);
22996
22997 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022998}
22999
23000/*
23001 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023002 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003 */
23004 static int
23005shortpath_for_partial(fnamep, bufp, fnamelen)
23006 char_u **fnamep;
23007 char_u **bufp;
23008 int *fnamelen;
23009{
23010 int sepcount, len, tflen;
23011 char_u *p;
23012 char_u *pbuf, *tfname;
23013 int hasTilde;
23014
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023015 /* Count up the path separators from the RHS.. so we know which part
23016 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023017 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023018 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023019 if (vim_ispathsep(*p))
23020 ++sepcount;
23021
23022 /* Need full path first (use expand_env() to remove a "~/") */
23023 hasTilde = (**fnamep == '~');
23024 if (hasTilde)
23025 pbuf = tfname = expand_env_save(*fnamep);
23026 else
23027 pbuf = tfname = FullName_save(*fnamep, FALSE);
23028
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023029 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023031 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23032 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023033
23034 if (len == 0)
23035 {
23036 /* Don't have a valid filename, so shorten the rest of the
23037 * path if we can. This CAN give us invalid 8.3 filenames, but
23038 * there's not a lot of point in guessing what it might be.
23039 */
23040 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023041 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23042 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023043 }
23044
23045 /* Count the paths backward to find the beginning of the desired string. */
23046 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023047 {
23048#ifdef FEAT_MBYTE
23049 if (has_mbyte)
23050 p -= mb_head_off(tfname, p);
23051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023052 if (vim_ispathsep(*p))
23053 {
23054 if (sepcount == 0 || (hasTilde && sepcount == 1))
23055 break;
23056 else
23057 sepcount --;
23058 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023060 if (hasTilde)
23061 {
23062 --p;
23063 if (p >= tfname)
23064 *p = '~';
23065 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023066 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067 }
23068 else
23069 ++p;
23070
23071 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23072 vim_free(*bufp);
23073 *fnamelen = (int)STRLEN(p);
23074 *bufp = pbuf;
23075 *fnamep = p;
23076
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023077 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023078}
23079#endif /* WIN3264 */
23080
23081/*
23082 * Adjust a filename, according to a string of modifiers.
23083 * *fnamep must be NUL terminated when called. When returning, the length is
23084 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023085 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023086 * When there is an error, *fnamep is set to NULL.
23087 */
23088 int
23089modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23090 char_u *src; /* string with modifiers */
23091 int *usedlen; /* characters after src that are used */
23092 char_u **fnamep; /* file name so far */
23093 char_u **bufp; /* buffer for allocated file name or NULL */
23094 int *fnamelen; /* length of fnamep */
23095{
23096 int valid = 0;
23097 char_u *tail;
23098 char_u *s, *p, *pbuf;
23099 char_u dirname[MAXPATHL];
23100 int c;
23101 int has_fullname = 0;
23102#ifdef WIN3264
23103 int has_shortname = 0;
23104#endif
23105
23106repeat:
23107 /* ":p" - full path/file_name */
23108 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23109 {
23110 has_fullname = 1;
23111
23112 valid |= VALID_PATH;
23113 *usedlen += 2;
23114
23115 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23116 if ((*fnamep)[0] == '~'
23117#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23118 && ((*fnamep)[1] == '/'
23119# ifdef BACKSLASH_IN_FILENAME
23120 || (*fnamep)[1] == '\\'
23121# endif
23122 || (*fnamep)[1] == NUL)
23123
23124#endif
23125 )
23126 {
23127 *fnamep = expand_env_save(*fnamep);
23128 vim_free(*bufp); /* free any allocated file name */
23129 *bufp = *fnamep;
23130 if (*fnamep == NULL)
23131 return -1;
23132 }
23133
23134 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023135 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023136 {
23137 if (vim_ispathsep(*p)
23138 && p[1] == '.'
23139 && (p[2] == NUL
23140 || vim_ispathsep(p[2])
23141 || (p[2] == '.'
23142 && (p[3] == NUL || vim_ispathsep(p[3])))))
23143 break;
23144 }
23145
23146 /* FullName_save() is slow, don't use it when not needed. */
23147 if (*p != NUL || !vim_isAbsName(*fnamep))
23148 {
23149 *fnamep = FullName_save(*fnamep, *p != NUL);
23150 vim_free(*bufp); /* free any allocated file name */
23151 *bufp = *fnamep;
23152 if (*fnamep == NULL)
23153 return -1;
23154 }
23155
23156 /* Append a path separator to a directory. */
23157 if (mch_isdir(*fnamep))
23158 {
23159 /* Make room for one or two extra characters. */
23160 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23161 vim_free(*bufp); /* free any allocated file name */
23162 *bufp = *fnamep;
23163 if (*fnamep == NULL)
23164 return -1;
23165 add_pathsep(*fnamep);
23166 }
23167 }
23168
23169 /* ":." - path relative to the current directory */
23170 /* ":~" - path relative to the home directory */
23171 /* ":8" - shortname path - postponed till after */
23172 while (src[*usedlen] == ':'
23173 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23174 {
23175 *usedlen += 2;
23176 if (c == '8')
23177 {
23178#ifdef WIN3264
23179 has_shortname = 1; /* Postpone this. */
23180#endif
23181 continue;
23182 }
23183 pbuf = NULL;
23184 /* Need full path first (use expand_env() to remove a "~/") */
23185 if (!has_fullname)
23186 {
23187 if (c == '.' && **fnamep == '~')
23188 p = pbuf = expand_env_save(*fnamep);
23189 else
23190 p = pbuf = FullName_save(*fnamep, FALSE);
23191 }
23192 else
23193 p = *fnamep;
23194
23195 has_fullname = 0;
23196
23197 if (p != NULL)
23198 {
23199 if (c == '.')
23200 {
23201 mch_dirname(dirname, MAXPATHL);
23202 s = shorten_fname(p, dirname);
23203 if (s != NULL)
23204 {
23205 *fnamep = s;
23206 if (pbuf != NULL)
23207 {
23208 vim_free(*bufp); /* free any allocated file name */
23209 *bufp = pbuf;
23210 pbuf = NULL;
23211 }
23212 }
23213 }
23214 else
23215 {
23216 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23217 /* Only replace it when it starts with '~' */
23218 if (*dirname == '~')
23219 {
23220 s = vim_strsave(dirname);
23221 if (s != NULL)
23222 {
23223 *fnamep = s;
23224 vim_free(*bufp);
23225 *bufp = s;
23226 }
23227 }
23228 }
23229 vim_free(pbuf);
23230 }
23231 }
23232
23233 tail = gettail(*fnamep);
23234 *fnamelen = (int)STRLEN(*fnamep);
23235
23236 /* ":h" - head, remove "/file_name", can be repeated */
23237 /* Don't remove the first "/" or "c:\" */
23238 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23239 {
23240 valid |= VALID_HEAD;
23241 *usedlen += 2;
23242 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023243 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023244 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023245 *fnamelen = (int)(tail - *fnamep);
23246#ifdef VMS
23247 if (*fnamelen > 0)
23248 *fnamelen += 1; /* the path separator is part of the path */
23249#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023250 if (*fnamelen == 0)
23251 {
23252 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23253 p = vim_strsave((char_u *)".");
23254 if (p == NULL)
23255 return -1;
23256 vim_free(*bufp);
23257 *bufp = *fnamep = tail = p;
23258 *fnamelen = 1;
23259 }
23260 else
23261 {
23262 while (tail > s && !after_pathsep(s, tail))
23263 mb_ptr_back(*fnamep, tail);
23264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023265 }
23266
23267 /* ":8" - shortname */
23268 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23269 {
23270 *usedlen += 2;
23271#ifdef WIN3264
23272 has_shortname = 1;
23273#endif
23274 }
23275
23276#ifdef WIN3264
23277 /* Check shortname after we have done 'heads' and before we do 'tails'
23278 */
23279 if (has_shortname)
23280 {
23281 pbuf = NULL;
23282 /* Copy the string if it is shortened by :h */
23283 if (*fnamelen < (int)STRLEN(*fnamep))
23284 {
23285 p = vim_strnsave(*fnamep, *fnamelen);
23286 if (p == 0)
23287 return -1;
23288 vim_free(*bufp);
23289 *bufp = *fnamep = p;
23290 }
23291
23292 /* Split into two implementations - makes it easier. First is where
23293 * there isn't a full name already, second is where there is.
23294 */
23295 if (!has_fullname && !vim_isAbsName(*fnamep))
23296 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023297 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023298 return -1;
23299 }
23300 else
23301 {
23302 int l;
23303
23304 /* Simple case, already have the full-name
23305 * Nearly always shorter, so try first time. */
23306 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023307 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023308 return -1;
23309
23310 if (l == 0)
23311 {
23312 /* Couldn't find the filename.. search the paths.
23313 */
23314 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023315 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023316 return -1;
23317 }
23318 *fnamelen = l;
23319 }
23320 }
23321#endif /* WIN3264 */
23322
23323 /* ":t" - tail, just the basename */
23324 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23325 {
23326 *usedlen += 2;
23327 *fnamelen -= (int)(tail - *fnamep);
23328 *fnamep = tail;
23329 }
23330
23331 /* ":e" - extension, can be repeated */
23332 /* ":r" - root, without extension, can be repeated */
23333 while (src[*usedlen] == ':'
23334 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23335 {
23336 /* find a '.' in the tail:
23337 * - for second :e: before the current fname
23338 * - otherwise: The last '.'
23339 */
23340 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23341 s = *fnamep - 2;
23342 else
23343 s = *fnamep + *fnamelen - 1;
23344 for ( ; s > tail; --s)
23345 if (s[0] == '.')
23346 break;
23347 if (src[*usedlen + 1] == 'e') /* :e */
23348 {
23349 if (s > tail)
23350 {
23351 *fnamelen += (int)(*fnamep - (s + 1));
23352 *fnamep = s + 1;
23353#ifdef VMS
23354 /* cut version from the extension */
23355 s = *fnamep + *fnamelen - 1;
23356 for ( ; s > *fnamep; --s)
23357 if (s[0] == ';')
23358 break;
23359 if (s > *fnamep)
23360 *fnamelen = s - *fnamep;
23361#endif
23362 }
23363 else if (*fnamep <= tail)
23364 *fnamelen = 0;
23365 }
23366 else /* :r */
23367 {
23368 if (s > tail) /* remove one extension */
23369 *fnamelen = (int)(s - *fnamep);
23370 }
23371 *usedlen += 2;
23372 }
23373
23374 /* ":s?pat?foo?" - substitute */
23375 /* ":gs?pat?foo?" - global substitute */
23376 if (src[*usedlen] == ':'
23377 && (src[*usedlen + 1] == 's'
23378 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23379 {
23380 char_u *str;
23381 char_u *pat;
23382 char_u *sub;
23383 int sep;
23384 char_u *flags;
23385 int didit = FALSE;
23386
23387 flags = (char_u *)"";
23388 s = src + *usedlen + 2;
23389 if (src[*usedlen + 1] == 'g')
23390 {
23391 flags = (char_u *)"g";
23392 ++s;
23393 }
23394
23395 sep = *s++;
23396 if (sep)
23397 {
23398 /* find end of pattern */
23399 p = vim_strchr(s, sep);
23400 if (p != NULL)
23401 {
23402 pat = vim_strnsave(s, (int)(p - s));
23403 if (pat != NULL)
23404 {
23405 s = p + 1;
23406 /* find end of substitution */
23407 p = vim_strchr(s, sep);
23408 if (p != NULL)
23409 {
23410 sub = vim_strnsave(s, (int)(p - s));
23411 str = vim_strnsave(*fnamep, *fnamelen);
23412 if (sub != NULL && str != NULL)
23413 {
23414 *usedlen = (int)(p + 1 - src);
23415 s = do_string_sub(str, pat, sub, flags);
23416 if (s != NULL)
23417 {
23418 *fnamep = s;
23419 *fnamelen = (int)STRLEN(s);
23420 vim_free(*bufp);
23421 *bufp = s;
23422 didit = TRUE;
23423 }
23424 }
23425 vim_free(sub);
23426 vim_free(str);
23427 }
23428 vim_free(pat);
23429 }
23430 }
23431 /* after using ":s", repeat all the modifiers */
23432 if (didit)
23433 goto repeat;
23434 }
23435 }
23436
23437 return valid;
23438}
23439
23440/*
23441 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23442 * "flags" can be "g" to do a global substitute.
23443 * Returns an allocated string, NULL for error.
23444 */
23445 char_u *
23446do_string_sub(str, pat, sub, flags)
23447 char_u *str;
23448 char_u *pat;
23449 char_u *sub;
23450 char_u *flags;
23451{
23452 int sublen;
23453 regmatch_T regmatch;
23454 int i;
23455 int do_all;
23456 char_u *tail;
23457 garray_T ga;
23458 char_u *ret;
23459 char_u *save_cpo;
23460
23461 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23462 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023463 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023464
23465 ga_init2(&ga, 1, 200);
23466
23467 do_all = (flags[0] == 'g');
23468
23469 regmatch.rm_ic = p_ic;
23470 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23471 if (regmatch.regprog != NULL)
23472 {
23473 tail = str;
23474 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23475 {
23476 /*
23477 * Get some space for a temporary buffer to do the substitution
23478 * into. It will contain:
23479 * - The text up to where the match is.
23480 * - The substituted text.
23481 * - The text after the match.
23482 */
23483 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23484 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23485 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23486 {
23487 ga_clear(&ga);
23488 break;
23489 }
23490
23491 /* copy the text up to where the match is */
23492 i = (int)(regmatch.startp[0] - tail);
23493 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23494 /* add the substituted text */
23495 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23496 + ga.ga_len + i, TRUE, TRUE, FALSE);
23497 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023498 /* avoid getting stuck on a match with an empty string */
23499 if (tail == regmatch.endp[0])
23500 {
23501 if (*tail == NUL)
23502 break;
23503 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23504 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023505 }
23506 else
23507 {
23508 tail = regmatch.endp[0];
23509 if (*tail == NUL)
23510 break;
23511 }
23512 if (!do_all)
23513 break;
23514 }
23515
23516 if (ga.ga_data != NULL)
23517 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23518
23519 vim_free(regmatch.regprog);
23520 }
23521
23522 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23523 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023524 if (p_cpo == empty_option)
23525 p_cpo = save_cpo;
23526 else
23527 /* Darn, evaluating {sub} expression changed the value. */
23528 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023529
23530 return ret;
23531}
23532
23533#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */