blob: 45d3cbf68071035e7e68111854b23fcb14fe726d [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000115
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000116/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000117 * All user-defined global variables are stored in dictionary "globvardict".
118 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120static dict_T globvardict;
121static dictitem_T globvars_var;
122#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123
124/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000125 * Old Vim variables such as "v:version" are also available without the "v:".
126 * Also in functions. We need a special hashtable for them.
127 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000128static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000129
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200130/* When using exists() don't auto-load a script. */
131static int no_autoload = FALSE;
132
Bram Moolenaar532c7802005-01-27 14:44:31 +0000133/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000134 * When recursively copying lists and dicts we need to remember which ones we
135 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000136 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137 */
138static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000139#define COPYID_INC 2
140#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000141
142/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000143 * Array to hold the hashtab with variables local to each sourced script.
144 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000146typedef struct
147{
148 dictitem_T sv_var;
149 dict_T sv_dict;
150} scriptvar_T;
151
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200152static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
153#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
154#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
156static int echo_attr = 0; /* attributes used for ":echo" */
157
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000158/* Values for trans_function_name() argument: */
159#define TFN_INT 1 /* internal function name OK */
160#define TFN_QUIET 2 /* no error messages */
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000355 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000361 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200362 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000363};
364
365/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_type vv_di.di_tv.v_type
367#define vv_nr vv_di.di_tv.vval.v_number
368#define vv_float vv_di.di_tv.vval.v_float
369#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000370#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000371#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000372
373/*
374 * The v: variables are stored in dictionary "vimvardict".
375 * "vimvars_var" is the variable that is used for the "l:" scope.
376 */
377static dict_T vimvardict;
378static dictitem_T vimvars_var;
379#define vimvarht vimvardict.dv_hashtab
380
Bram Moolenaara40058a2005-07-11 22:42:07 +0000381static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
382static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
383#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
384static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
385#endif
386static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
387static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
388static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000389static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
390static void list_glob_vars __ARGS((int *first));
391static void list_buf_vars __ARGS((int *first));
392static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000393#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000394static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000395#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000396static void list_vim_vars __ARGS((int *first));
397static void list_script_vars __ARGS((int *first));
398static void list_func_vars __ARGS((int *first));
399static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000400static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
401static int check_changedtick __ARGS((char_u *arg));
402static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
403static void clear_lval __ARGS((lval_T *lp));
404static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
405static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
406static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
407static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
408static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
409static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
410static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
411static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
412static void item_lock __ARGS((typval_T *tv, int deep, int lock));
413static int tv_islocked __ARGS((typval_T *tv));
414
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
416static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000421static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
422static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000423
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000424static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
427static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
428static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000429static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static listitem_T *listitem_alloc __ARGS((void));
431static void listitem_free __ARGS((listitem_T *item));
432static void listitem_remove __ARGS((list_T *l, listitem_T *item));
433static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100434static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
435static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
436static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000438static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000441static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
443static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
444static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000445static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000446static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000447static char_u *list2string __ARGS((typval_T *tv, int copyID));
448static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000449static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000450static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
451static void set_ref_in_list __ARGS((list_T *l, int copyID));
452static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200453static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000454static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000455static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
457static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000458static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000460static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000461static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000462static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
463static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000464static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#ifdef FEAT_FLOAT
466static int string2float __ARGS((char_u *text, float_T *value));
467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
469static int find_internal_func __ARGS((char_u *name));
470static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
471static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200472static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000473static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000474static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000475
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#ifdef FEAT_FLOAT
477static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200478static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200486static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000487static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200488static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000489#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000490static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000501#ifdef FEAT_FLOAT
502static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
503#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000504static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000507static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000508static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000509#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000510static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000511static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
513#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000514static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000516#ifdef FEAT_FLOAT
517static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200518static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000519#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
523static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200534#ifdef FEAT_FLOAT
535static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
536#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000539static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000545#ifdef FEAT_FLOAT
546static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000549#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000550static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000559static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000561static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000567static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000575static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000576static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000577static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000578static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200581static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000582static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000590static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000604static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000610static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000622#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200623static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000624static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
625#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000630static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000631static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000632static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000634static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000638#ifdef vim_mkdir
639static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100642#ifdef FEAT_MZSCHEME
643static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000647static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000648#ifdef FEAT_FLOAT
649static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000652static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000653static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000655static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000656static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
669static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
670#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000672static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000674static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000681static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000682static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000683static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000684static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200686static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000687static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000689static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#ifdef FEAT_FLOAT
692static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200693static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000696static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000697static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000700#ifdef FEAT_FLOAT
701static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
703#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000704static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200705static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706#ifdef HAVE_STRFTIME
707static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
708#endif
709static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200715static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200716static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000722static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200723static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000725static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000726static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000727static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000728static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000729static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000731static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200732#ifdef FEAT_FLOAT
733static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
735#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000739#ifdef FEAT_FLOAT
740static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
741#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200743static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200744static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000754static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000756static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000757static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000759static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000760static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000761static int get_env_len __ARGS((char_u **arg));
762static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000763static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000764static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
765#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
766#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
767 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000768static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000770static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
772static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static typval_T *alloc_tv __ARGS((void));
774static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static void init_tv __ARGS((typval_T *varp));
776static long get_tv_number __ARGS((typval_T *varp));
777static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000778static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000779static char_u *get_tv_string __ARGS((typval_T *varp));
780static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000781static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000782static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000783static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
785static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
786static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000787static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
788static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000789static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
790static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000791static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200792static int var_check_func_name __ARGS((char_u *name, int new_var));
793static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000794static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000795static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000796static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
797static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
798static int eval_fname_script __ARGS((char_u *p));
799static int eval_fname_sid __ARGS((char_u *p));
800static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000801static ufunc_T *find_func __ARGS((char_u *name));
802static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000803static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000804#ifdef FEAT_PROFILE
805static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000806static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
807static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
808static int
809# ifdef __BORLANDC__
810 _RTLENTRYF
811# endif
812 prof_total_cmp __ARGS((const void *s1, const void *s2));
813static int
814# ifdef __BORLANDC__
815 _RTLENTRYF
816# endif
817 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000819static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000820static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000821static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822static void func_free __ARGS((ufunc_T *fp));
823static void func_unref __ARGS((char_u *name));
824static void func_ref __ARGS((char_u *name));
825static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000826static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
827static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000828static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000829static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
830static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000831static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000832static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000833static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200835
836#ifdef EBCDIC
837static int compare_func_name __ARGS((const void *s1, const void *s2));
838static void sortFunctions __ARGS(());
839#endif
840
841
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000842/* Character used as separated in autoload function/variable names. */
843#define AUTOLOAD_CHAR '#'
844
Bram Moolenaar33570922005-01-25 22:26:29 +0000845/*
846 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000847 */
848 void
849eval_init()
850{
Bram Moolenaar33570922005-01-25 22:26:29 +0000851 int i;
852 struct vimvar *p;
853
854 init_var_dict(&globvardict, &globvars_var);
855 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200856 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000857 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000858 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000859
860 for (i = 0; i < VV_LEN; ++i)
861 {
862 p = &vimvars[i];
863 STRCPY(p->vv_di.di_key, p->vv_name);
864 if (p->vv_flags & VV_RO)
865 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
866 else if (p->vv_flags & VV_RO_SBX)
867 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
868 else
869 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000870
871 /* add to v: scope dict, unless the value is not always available */
872 if (p->vv_type != VAR_UNKNOWN)
873 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000874 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000875 /* add to compat scope dict */
876 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000877 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000878 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200879
880#ifdef EBCDIC
881 /*
882 * Sort the function table, to enable binary sort.
883 */
884 sortFunctions();
885#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000886}
887
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000888#if defined(EXITFREE) || defined(PROTO)
889 void
890eval_clear()
891{
892 int i;
893 struct vimvar *p;
894
895 for (i = 0; i < VV_LEN; ++i)
896 {
897 p = &vimvars[i];
898 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000899 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000900 vim_free(p->vv_str);
901 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000902 }
903 else if (p->vv_di.di_tv.v_type == VAR_LIST)
904 {
905 list_unref(p->vv_list);
906 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908 }
909 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000910 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911 hash_clear(&compat_hashtab);
912
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000914
915 /* global variables */
916 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000917
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000918 /* autoloaded script names */
919 ga_clear_strings(&ga_loaded);
920
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200921 /* script-local variables */
922 for (i = 1; i <= ga_scripts.ga_len; ++i)
923 {
924 vars_clear(&SCRIPT_VARS(i));
925 vim_free(SCRIPT_SV(i));
926 }
927 ga_clear(&ga_scripts);
928
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000929 /* unreferenced lists and dicts */
930 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000931
932 /* functions */
933 free_all_functions();
934 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000935}
936#endif
937
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 * Return the name of the executed function.
940 */
941 char_u *
942func_name(cookie)
943 void *cookie;
944{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000945 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946}
947
948/*
949 * Return the address holding the next breakpoint line for a funccall cookie.
950 */
951 linenr_T *
952func_breakpoint(cookie)
953 void *cookie;
954{
Bram Moolenaar33570922005-01-25 22:26:29 +0000955 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
958/*
959 * Return the address holding the debug tick for a funccall cookie.
960 */
961 int *
962func_dbg_tick(cookie)
963 void *cookie;
964{
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967
968/*
969 * Return the nesting level for a funccall cookie.
970 */
971 int
972func_level(cookie)
973 void *cookie;
974{
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000979funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000981/* pointer to list of previously used funccal, still around because some
982 * item in it is still being used. */
983funccall_T *previous_funccal = NULL;
984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985/*
986 * Return TRUE when a function was ended by a ":return" command.
987 */
988 int
989current_func_returned()
990{
991 return current_funccal->returned;
992}
993
994
995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 * Set an internal variable to a string value. Creates the variable if it does
997 * not already exist.
998 */
999 void
1000set_internal_string_var(name, value)
1001 char_u *name;
1002 char_u *value;
1003{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001004 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001005 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
1007 val = vim_strsave(value);
1008 if (val != NULL)
1009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001010 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001011 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001013 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001014 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 }
1016 }
1017}
1018
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001019static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001020static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001021static char_u *redir_endp = NULL;
1022static char_u *redir_varname = NULL;
1023
1024/*
1025 * Start recording command output to a variable
1026 * Returns OK if successfully completed the setup. FAIL otherwise.
1027 */
1028 int
1029var_redir_start(name, append)
1030 char_u *name;
1031 int append; /* append to an existing variable */
1032{
1033 int save_emsg;
1034 int err;
1035 typval_T tv;
1036
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001037 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001038 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001039 {
1040 EMSG(_(e_invarg));
1041 return FAIL;
1042 }
1043
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001044 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001045 redir_varname = vim_strsave(name);
1046 if (redir_varname == NULL)
1047 return FAIL;
1048
1049 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1050 if (redir_lval == NULL)
1051 {
1052 var_redir_stop();
1053 return FAIL;
1054 }
1055
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001056 /* The output is stored in growarray "redir_ga" until redirection ends. */
1057 ga_init2(&redir_ga, (int)sizeof(char), 500);
1058
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001060 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1061 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1063 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001064 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001065 if (redir_endp != NULL && *redir_endp != NUL)
1066 /* Trailing characters are present after the variable name */
1067 EMSG(_(e_trailing));
1068 else
1069 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001070 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001071 var_redir_stop();
1072 return FAIL;
1073 }
1074
1075 /* check if we can write to the variable: set it to or append an empty
1076 * string */
1077 save_emsg = did_emsg;
1078 did_emsg = FALSE;
1079 tv.v_type = VAR_STRING;
1080 tv.vval.v_string = (char_u *)"";
1081 if (append)
1082 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1083 else
1084 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001085 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001087 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 if (err)
1089 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001090 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091 var_redir_stop();
1092 return FAIL;
1093 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094
1095 return OK;
1096}
1097
1098/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001099 * Append "value[value_len]" to the variable set by var_redir_start().
1100 * The actual appending is postponed until redirection ends, because the value
1101 * appended may in fact be the string we write to, changing it may cause freed
1102 * memory to be used:
1103 * :redir => foo
1104 * :let foo
1105 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 */
1107 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001108var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001110 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001112 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113
1114 if (redir_lval == NULL)
1115 return;
1116
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001117 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001118 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001123 {
1124 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001125 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001126 }
1127 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129}
1130
1131/*
1132 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001133 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 */
1135 void
1136var_redir_stop()
1137{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001138 typval_T tv;
1139
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 if (redir_lval != NULL)
1141 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001142 /* If there was no error: assign the text to the variable. */
1143 if (redir_endp != NULL)
1144 {
1145 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1146 tv.v_type = VAR_STRING;
1147 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001148 /* Call get_lval() again, if it's inside a Dict or List it may
1149 * have changed. */
1150 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1151 FALSE, FALSE, FALSE, FNE_CHECK_START);
1152 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1153 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1154 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001155 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001156
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001157 /* free the collected output */
1158 vim_free(redir_ga.ga_data);
1159 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001160
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 vim_free(redir_lval);
1162 redir_lval = NULL;
1163 }
1164 vim_free(redir_varname);
1165 redir_varname = NULL;
1166}
1167
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168# if defined(FEAT_MBYTE) || defined(PROTO)
1169 int
1170eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1171 char_u *enc_from;
1172 char_u *enc_to;
1173 char_u *fname_from;
1174 char_u *fname_to;
1175{
1176 int err = FALSE;
1177
1178 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1179 set_vim_var_string(VV_CC_TO, enc_to, -1);
1180 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1181 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1182 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1183 err = TRUE;
1184 set_vim_var_string(VV_CC_FROM, NULL, -1);
1185 set_vim_var_string(VV_CC_TO, NULL, -1);
1186 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1187 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1188
1189 if (err)
1190 return FAIL;
1191 return OK;
1192}
1193# endif
1194
1195# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1196 int
1197eval_printexpr(fname, args)
1198 char_u *fname;
1199 char_u *args;
1200{
1201 int err = FALSE;
1202
1203 set_vim_var_string(VV_FNAME_IN, fname, -1);
1204 set_vim_var_string(VV_CMDARG, args, -1);
1205 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1206 err = TRUE;
1207 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1208 set_vim_var_string(VV_CMDARG, NULL, -1);
1209
1210 if (err)
1211 {
1212 mch_remove(fname);
1213 return FAIL;
1214 }
1215 return OK;
1216}
1217# endif
1218
1219# if defined(FEAT_DIFF) || defined(PROTO)
1220 void
1221eval_diff(origfile, newfile, outfile)
1222 char_u *origfile;
1223 char_u *newfile;
1224 char_u *outfile;
1225{
1226 int err = FALSE;
1227
1228 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1229 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1230 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1231 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1232 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1233 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1234 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1235}
1236
1237 void
1238eval_patch(origfile, difffile, outfile)
1239 char_u *origfile;
1240 char_u *difffile;
1241 char_u *outfile;
1242{
1243 int err;
1244
1245 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1246 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1247 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1248 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1249 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1250 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1251 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1252}
1253# endif
1254
1255/*
1256 * Top level evaluation function, returning a boolean.
1257 * Sets "error" to TRUE if there was an error.
1258 * Return TRUE or FALSE.
1259 */
1260 int
1261eval_to_bool(arg, error, nextcmd, skip)
1262 char_u *arg;
1263 int *error;
1264 char_u **nextcmd;
1265 int skip; /* only parse, don't execute */
1266{
Bram Moolenaar33570922005-01-25 22:26:29 +00001267 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 int retval = FALSE;
1269
1270 if (skip)
1271 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001272 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 else
1275 {
1276 *error = FALSE;
1277 if (!skip)
1278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001279 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001280 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 }
1282 }
1283 if (skip)
1284 --emsg_skip;
1285
1286 return retval;
1287}
1288
1289/*
1290 * Top level evaluation function, returning a string. If "skip" is TRUE,
1291 * only parsing to "nextcmd" is done, without reporting errors. Return
1292 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1293 */
1294 char_u *
1295eval_to_string_skip(arg, nextcmd, skip)
1296 char_u *arg;
1297 char_u **nextcmd;
1298 int skip; /* only parse, don't execute */
1299{
Bram Moolenaar33570922005-01-25 22:26:29 +00001300 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 char_u *retval;
1302
1303 if (skip)
1304 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 retval = NULL;
1307 else
1308 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001309 retval = vim_strsave(get_tv_string(&tv));
1310 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312 if (skip)
1313 --emsg_skip;
1314
1315 return retval;
1316}
1317
1318/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001319 * Skip over an expression at "*pp".
1320 * Return FAIL for an error, OK otherwise.
1321 */
1322 int
1323skip_expr(pp)
1324 char_u **pp;
1325{
Bram Moolenaar33570922005-01-25 22:26:29 +00001326 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001327
1328 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001330}
1331
1332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001334 * When "convert" is TRUE convert a List into a sequence of lines and convert
1335 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 * Return pointer to allocated memory, or NULL for failure.
1337 */
1338 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001339eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 char_u *arg;
1341 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001342 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
Bram Moolenaar33570922005-01-25 22:26:29 +00001344 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001346 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001347#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001348 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001351 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 retval = NULL;
1353 else
1354 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001355 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 {
1357 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001358 if (tv.vval.v_list != NULL)
1359 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001360 ga_append(&ga, NUL);
1361 retval = (char_u *)ga.ga_data;
1362 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001363#ifdef FEAT_FLOAT
1364 else if (convert && tv.v_type == VAR_FLOAT)
1365 {
1366 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1367 retval = vim_strsave(numbuf);
1368 }
1369#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001370 else
1371 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001372 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 }
1374
1375 return retval;
1376}
1377
1378/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001379 * Call eval_to_string() without using current local variables and using
1380 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 */
1382 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001383eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 char_u *arg;
1385 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001386 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387{
1388 char_u *retval;
1389 void *save_funccalp;
1390
1391 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001392 if (use_sandbox)
1393 ++sandbox;
1394 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001395 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001396 if (use_sandbox)
1397 --sandbox;
1398 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 restore_funccal(save_funccalp);
1400 return retval;
1401}
1402
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403/*
1404 * Top level evaluation function, returning a number.
1405 * Evaluates "expr" silently.
1406 * Returns -1 for an error.
1407 */
1408 int
1409eval_to_number(expr)
1410 char_u *expr;
1411{
Bram Moolenaar33570922005-01-25 22:26:29 +00001412 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001414 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
1416 ++emsg_off;
1417
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001418 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 retval = -1;
1420 else
1421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001422 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001423 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 }
1425 --emsg_off;
1426
1427 return retval;
1428}
1429
Bram Moolenaara40058a2005-07-11 22:42:07 +00001430/*
1431 * Prepare v: variable "idx" to be used.
1432 * Save the current typeval in "save_tv".
1433 * When not used yet add the variable to the v: hashtable.
1434 */
1435 static void
1436prepare_vimvar(idx, save_tv)
1437 int idx;
1438 typval_T *save_tv;
1439{
1440 *save_tv = vimvars[idx].vv_tv;
1441 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1442 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1443}
1444
1445/*
1446 * Restore v: variable "idx" to typeval "save_tv".
1447 * When no longer defined, remove the variable from the v: hashtable.
1448 */
1449 static void
1450restore_vimvar(idx, save_tv)
1451 int idx;
1452 typval_T *save_tv;
1453{
1454 hashitem_T *hi;
1455
Bram Moolenaara40058a2005-07-11 22:42:07 +00001456 vimvars[idx].vv_tv = *save_tv;
1457 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1458 {
1459 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1460 if (HASHITEM_EMPTY(hi))
1461 EMSG2(_(e_intern2), "restore_vimvar()");
1462 else
1463 hash_remove(&vimvarht, hi);
1464 }
1465}
1466
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001467#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001468/*
1469 * Evaluate an expression to a list with suggestions.
1470 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001471 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001472 */
1473 list_T *
1474eval_spell_expr(badword, expr)
1475 char_u *badword;
1476 char_u *expr;
1477{
1478 typval_T save_val;
1479 typval_T rettv;
1480 list_T *list = NULL;
1481 char_u *p = skipwhite(expr);
1482
1483 /* Set "v:val" to the bad word. */
1484 prepare_vimvar(VV_VAL, &save_val);
1485 vimvars[VV_VAL].vv_type = VAR_STRING;
1486 vimvars[VV_VAL].vv_str = badword;
1487 if (p_verbose == 0)
1488 ++emsg_off;
1489
1490 if (eval1(&p, &rettv, TRUE) == OK)
1491 {
1492 if (rettv.v_type != VAR_LIST)
1493 clear_tv(&rettv);
1494 else
1495 list = rettv.vval.v_list;
1496 }
1497
1498 if (p_verbose == 0)
1499 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001500 restore_vimvar(VV_VAL, &save_val);
1501
1502 return list;
1503}
1504
1505/*
1506 * "list" is supposed to contain two items: a word and a number. Return the
1507 * word in "pp" and the number as the return value.
1508 * Return -1 if anything isn't right.
1509 * Used to get the good word and score from the eval_spell_expr() result.
1510 */
1511 int
1512get_spellword(list, pp)
1513 list_T *list;
1514 char_u **pp;
1515{
1516 listitem_T *li;
1517
1518 li = list->lv_first;
1519 if (li == NULL)
1520 return -1;
1521 *pp = get_tv_string(&li->li_tv);
1522
1523 li = li->li_next;
1524 if (li == NULL)
1525 return -1;
1526 return get_tv_number(&li->li_tv);
1527}
1528#endif
1529
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001530/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001531 * Top level evaluation function.
1532 * Returns an allocated typval_T with the result.
1533 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001534 */
1535 typval_T *
1536eval_expr(arg, nextcmd)
1537 char_u *arg;
1538 char_u **nextcmd;
1539{
1540 typval_T *tv;
1541
1542 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001543 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001544 {
1545 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001546 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001547 }
1548
1549 return tv;
1550}
1551
1552
Bram Moolenaar4f688582007-07-24 12:34:30 +00001553#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1554 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001556 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001557 * Uses argv[argc] for the function arguments. Only Number and String
1558 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001559 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001561 static int
1562call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 char_u *func;
1564 int argc;
1565 char_u **argv;
1566 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
Bram Moolenaar33570922005-01-25 22:26:29 +00001569 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 long n;
1571 int len;
1572 int i;
1573 int doesrange;
1574 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001575 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001577 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001579 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
1581 for (i = 0; i < argc; i++)
1582 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001583 /* Pass a NULL or empty argument as an empty string */
1584 if (argv[i] == NULL || *argv[i] == NUL)
1585 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001586 argvars[i].v_type = VAR_STRING;
1587 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001588 continue;
1589 }
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 /* Recognize a number argument, the others must be strings. */
1592 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1593 if (len != 0 && len == (int)STRLEN(argv[i]))
1594 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001595 argvars[i].v_type = VAR_NUMBER;
1596 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
1598 else
1599 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001600 argvars[i].v_type = VAR_STRING;
1601 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 }
1603 }
1604
1605 if (safe)
1606 {
1607 save_funccalp = save_funccal();
1608 ++sandbox;
1609 }
1610
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1612 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (safe)
1616 {
1617 --sandbox;
1618 restore_funccal(save_funccalp);
1619 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 vim_free(argvars);
1621
1622 if (ret == FAIL)
1623 clear_tv(rettv);
1624
1625 return ret;
1626}
1627
Bram Moolenaar4f688582007-07-24 12:34:30 +00001628# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001629/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001630 * Call vimL function "func" and return the result as a string.
1631 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001632 * Uses argv[argc] for the function arguments.
1633 */
1634 void *
1635call_func_retstr(func, argc, argv, safe)
1636 char_u *func;
1637 int argc;
1638 char_u **argv;
1639 int safe; /* use the sandbox */
1640{
1641 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001642 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643
1644 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1645 return NULL;
1646
1647 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001648 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 return retval;
1650}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001651# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652
Bram Moolenaar4f688582007-07-24 12:34:30 +00001653# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001654/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001655 * Call vimL function "func" and return the result as a number.
1656 * Returns -1 when calling the function fails.
1657 * Uses argv[argc] for the function arguments.
1658 */
1659 long
1660call_func_retnr(func, argc, argv, safe)
1661 char_u *func;
1662 int argc;
1663 char_u **argv;
1664 int safe; /* use the sandbox */
1665{
1666 typval_T rettv;
1667 long retval;
1668
1669 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1670 return -1;
1671
1672 retval = get_tv_number_chk(&rettv, NULL);
1673 clear_tv(&rettv);
1674 return retval;
1675}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001676# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001677
1678/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001679 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001681 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 */
1683 void *
1684call_func_retlist(func, argc, argv, safe)
1685 char_u *func;
1686 int argc;
1687 char_u **argv;
1688 int safe; /* use the sandbox */
1689{
1690 typval_T rettv;
1691
1692 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1693 return NULL;
1694
1695 if (rettv.v_type != VAR_LIST)
1696 {
1697 clear_tv(&rettv);
1698 return NULL;
1699 }
1700
1701 return rettv.vval.v_list;
1702}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703#endif
1704
Bram Moolenaar4f688582007-07-24 12:34:30 +00001705
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706/*
1707 * Save the current function call pointer, and set it to NULL.
1708 * Used when executing autocommands and for ":source".
1709 */
1710 void *
1711save_funccal()
1712{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001713 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 current_funccal = NULL;
1716 return (void *)fc;
1717}
1718
1719 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001720restore_funccal(vfc)
1721 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001723 funccall_T *fc = (funccall_T *)vfc;
1724
1725 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726}
1727
Bram Moolenaar05159a02005-02-26 23:04:13 +00001728#if defined(FEAT_PROFILE) || defined(PROTO)
1729/*
1730 * Prepare profiling for entering a child or something else that is not
1731 * counted for the script/function itself.
1732 * Should always be called in pair with prof_child_exit().
1733 */
1734 void
1735prof_child_enter(tm)
1736 proftime_T *tm; /* place to store waittime */
1737{
1738 funccall_T *fc = current_funccal;
1739
1740 if (fc != NULL && fc->func->uf_profiling)
1741 profile_start(&fc->prof_child);
1742 script_prof_save(tm);
1743}
1744
1745/*
1746 * Take care of time spent in a child.
1747 * Should always be called after prof_child_enter().
1748 */
1749 void
1750prof_child_exit(tm)
1751 proftime_T *tm; /* where waittime was stored */
1752{
1753 funccall_T *fc = current_funccal;
1754
1755 if (fc != NULL && fc->func->uf_profiling)
1756 {
1757 profile_end(&fc->prof_child);
1758 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1759 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1760 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1761 }
1762 script_prof_restore(tm);
1763}
1764#endif
1765
1766
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767#ifdef FEAT_FOLDING
1768/*
1769 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1770 * it in "*cp". Doesn't give error messages.
1771 */
1772 int
1773eval_foldexpr(arg, cp)
1774 char_u *arg;
1775 int *cp;
1776{
Bram Moolenaar33570922005-01-25 22:26:29 +00001777 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 int retval;
1779 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001780 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1781 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001784 if (use_sandbox)
1785 ++sandbox;
1786 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 retval = 0;
1790 else
1791 {
1792 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 if (tv.v_type == VAR_NUMBER)
1794 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001795 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 retval = 0;
1797 else
1798 {
1799 /* If the result is a string, check if there is a non-digit before
1800 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (!VIM_ISDIGIT(*s) && *s != '-')
1803 *cp = *s++;
1804 retval = atol((char *)s);
1805 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 }
1808 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001809 if (use_sandbox)
1810 --sandbox;
1811 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
1813 return retval;
1814}
1815#endif
1816
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818 * ":let" list all variable values
1819 * ":let var1 var2" list variable values
1820 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001821 * ":let var += expr" assignment command.
1822 * ":let var -= expr" assignment command.
1823 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 */
1826 void
1827ex_let(eap)
1828 exarg_T *eap;
1829{
1830 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001832 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834 int var_count = 0;
1835 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001836 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001837 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001838 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
Bram Moolenaardb552d602006-03-23 22:59:57 +00001840 argend = skip_var_list(arg, &var_count, &semicolon);
1841 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001842 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001843 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1844 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001845 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001848 /*
1849 * ":let" without "=": list variables
1850 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 if (*arg == '[')
1852 EMSG(_(e_invarg));
1853 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001854 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001855 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001857 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001859 list_glob_vars(&first);
1860 list_buf_vars(&first);
1861 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001862#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001863 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001864#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001865 list_script_vars(&first);
1866 list_func_vars(&first);
1867 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 eap->nextcmd = check_nextcmd(arg);
1870 }
1871 else
1872 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001873 op[0] = '=';
1874 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001875 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001876 {
1877 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1878 op[0] = expr[-1]; /* +=, -= or .= */
1879 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001880 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (eap->skip)
1883 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (eap->skip)
1886 {
1887 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 --emsg_skip;
1890 }
1891 else if (i != FAIL)
1892 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001894 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
1897 }
1898}
1899
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900/*
1901 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1902 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 * When "nextchars" is not NULL it points to a string with characters that
1904 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1905 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 * Returns OK or FAIL;
1907 */
1908 static int
1909ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1910 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001911 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 int copy; /* copy values from "tv", don't move */
1913 int semicolon; /* from skip_var_list() */
1914 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916{
1917 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001918 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001920 listitem_T *item;
1921 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922
1923 if (*arg != '[')
1924 {
1925 /*
1926 * ":let var = expr" or ":for var in list"
1927 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001928 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 return FAIL;
1930 return OK;
1931 }
1932
1933 /*
1934 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1935 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001936 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937 {
1938 EMSG(_(e_listreq));
1939 return FAIL;
1940 }
1941
1942 i = list_len(l);
1943 if (semicolon == 0 && var_count < i)
1944 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001945 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 return FAIL;
1947 }
1948 if (var_count - semicolon > i)
1949 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001950 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 return FAIL;
1952 }
1953
1954 item = l->lv_first;
1955 while (*arg != ']')
1956 {
1957 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001958 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 item = item->li_next;
1960 if (arg == NULL)
1961 return FAIL;
1962
1963 arg = skipwhite(arg);
1964 if (*arg == ';')
1965 {
1966 /* Put the rest of the list (may be empty) in the var after ';'.
1967 * Create a new list for this. */
1968 l = list_alloc();
1969 if (l == NULL)
1970 return FAIL;
1971 while (item != NULL)
1972 {
1973 list_append_tv(l, &item->li_tv);
1974 item = item->li_next;
1975 }
1976
1977 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001978 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 ltv.vval.v_list = l;
1980 l->lv_refcount = 1;
1981
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001982 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1983 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984 clear_tv(&ltv);
1985 if (arg == NULL)
1986 return FAIL;
1987 break;
1988 }
1989 else if (*arg != ',' && *arg != ']')
1990 {
1991 EMSG2(_(e_intern2), "ex_let_vars()");
1992 return FAIL;
1993 }
1994 }
1995
1996 return OK;
1997}
1998
1999/*
2000 * Skip over assignable variable "var" or list of variables "[var, var]".
2001 * Used for ":let varvar = expr" and ":for varvar in expr".
2002 * For "[var, var]" increment "*var_count" for each variable.
2003 * for "[var, var; var]" set "semicolon".
2004 * Return NULL for an error.
2005 */
2006 static char_u *
2007skip_var_list(arg, var_count, semicolon)
2008 char_u *arg;
2009 int *var_count;
2010 int *semicolon;
2011{
2012 char_u *p, *s;
2013
2014 if (*arg == '[')
2015 {
2016 /* "[var, var]": find the matching ']'. */
2017 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002018 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 {
2020 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2021 s = skip_var_one(p);
2022 if (s == p)
2023 {
2024 EMSG2(_(e_invarg2), p);
2025 return NULL;
2026 }
2027 ++*var_count;
2028
2029 p = skipwhite(s);
2030 if (*p == ']')
2031 break;
2032 else if (*p == ';')
2033 {
2034 if (*semicolon == 1)
2035 {
2036 EMSG(_("Double ; in list of variables"));
2037 return NULL;
2038 }
2039 *semicolon = 1;
2040 }
2041 else if (*p != ',')
2042 {
2043 EMSG2(_(e_invarg2), p);
2044 return NULL;
2045 }
2046 }
2047 return p + 1;
2048 }
2049 else
2050 return skip_var_one(arg);
2051}
2052
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002053/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002054 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002055 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002056 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002057 static char_u *
2058skip_var_one(arg)
2059 char_u *arg;
2060{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002061 if (*arg == '@' && arg[1] != NUL)
2062 return arg + 2;
2063 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2064 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002065}
2066
Bram Moolenaara7043832005-01-21 11:56:39 +00002067/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002068 * List variables for hashtab "ht" with prefix "prefix".
2069 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002070 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002071 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002072list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002073 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002074 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002075 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002076 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002077{
Bram Moolenaar33570922005-01-25 22:26:29 +00002078 hashitem_T *hi;
2079 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002080 int todo;
2081
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002082 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002083 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2084 {
2085 if (!HASHITEM_EMPTY(hi))
2086 {
2087 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002088 di = HI2DI(hi);
2089 if (empty || di->di_tv.v_type != VAR_STRING
2090 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002091 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002092 }
2093 }
2094}
2095
2096/*
2097 * List global variables.
2098 */
2099 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002100list_glob_vars(first)
2101 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002103 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002104}
2105
2106/*
2107 * List buffer variables.
2108 */
2109 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110list_buf_vars(first)
2111 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002112{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002113 char_u numbuf[NUMBUFLEN];
2114
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002115 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2116 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002117
2118 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2120 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002121}
2122
2123/*
2124 * List window variables.
2125 */
2126 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002127list_win_vars(first)
2128 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002129{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002130 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2131 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002132}
2133
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002134#ifdef FEAT_WINDOWS
2135/*
2136 * List tab page variables.
2137 */
2138 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139list_tab_vars(first)
2140 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002141{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002142 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2143 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002144}
2145#endif
2146
Bram Moolenaara7043832005-01-21 11:56:39 +00002147/*
2148 * List Vim variables.
2149 */
2150 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151list_vim_vars(first)
2152 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002153{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155}
2156
2157/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002158 * List script-local variables, if there is a script.
2159 */
2160 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161list_script_vars(first)
2162 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002163{
2164 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002165 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2166 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002167}
2168
2169/*
2170 * List function variables, if there is a function.
2171 */
2172 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173list_func_vars(first)
2174 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002175{
2176 if (current_funccal != NULL)
2177 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002179}
2180
2181/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182 * List variables in "arg".
2183 */
2184 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002185list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 exarg_T *eap;
2187 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189{
2190 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002191 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002193 char_u *name_start;
2194 char_u *arg_subsc;
2195 char_u *tofree;
2196 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197
2198 while (!ends_excmd(*arg) && !got_int)
2199 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002200 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002202 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002203 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2204 {
2205 emsg_severe = TRUE;
2206 EMSG(_(e_trailing));
2207 break;
2208 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002210 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 /* get_name_len() takes care of expanding curly braces */
2213 name_start = name = arg;
2214 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2215 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002217 /* This is mainly to keep test 49 working: when expanding
2218 * curly braces fails overrule the exception error message. */
2219 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221 emsg_severe = TRUE;
2222 EMSG2(_(e_invarg2), arg);
2223 break;
2224 }
2225 error = TRUE;
2226 }
2227 else
2228 {
2229 if (tofree != NULL)
2230 name = tofree;
2231 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 else
2234 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 /* handle d.key, l[idx], f(expr) */
2236 arg_subsc = arg;
2237 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002238 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002242 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002243 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002244 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002245 case 'g': list_glob_vars(first); break;
2246 case 'b': list_buf_vars(first); break;
2247 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002248#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002249 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002250#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002251 case 'v': list_vim_vars(first); break;
2252 case 's': list_script_vars(first); break;
2253 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 default:
2255 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002256 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 }
2258 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 {
2260 char_u numbuf[NUMBUFLEN];
2261 char_u *tf;
2262 int c;
2263 char_u *s;
2264
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002265 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 c = *arg;
2267 *arg = NUL;
2268 list_one_var_a((char_u *)"",
2269 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002270 tv.v_type,
2271 s == NULL ? (char_u *)"" : s,
2272 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 *arg = c;
2274 vim_free(tf);
2275 }
2276 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002277 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 }
2279 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280
2281 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283
2284 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 }
2286
2287 return arg;
2288}
2289
2290/*
2291 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2292 * Returns a pointer to the char just after the var name.
2293 * Returns NULL if there is an error.
2294 */
2295 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002296ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002298 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002300 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002301 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302{
2303 int c1;
2304 char_u *name;
2305 char_u *p;
2306 char_u *arg_end = NULL;
2307 int len;
2308 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002309 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310
2311 /*
2312 * ":let $VAR = expr": Set environment variable.
2313 */
2314 if (*arg == '$')
2315 {
2316 /* Find the end of the name. */
2317 ++arg;
2318 name = arg;
2319 len = get_env_len(&arg);
2320 if (len == 0)
2321 EMSG2(_(e_invarg2), name - 1);
2322 else
2323 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002324 if (op != NULL && (*op == '+' || *op == '-'))
2325 EMSG2(_(e_letwrong), op);
2326 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002327 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002329 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 {
2331 c1 = name[len];
2332 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002333 p = get_tv_string_chk(tv);
2334 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002335 {
2336 int mustfree = FALSE;
2337 char_u *s = vim_getenv(name, &mustfree);
2338
2339 if (s != NULL)
2340 {
2341 p = tofree = concat_str(s, p);
2342 if (mustfree)
2343 vim_free(s);
2344 }
2345 }
2346 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002347 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002348 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002349 if (STRICMP(name, "HOME") == 0)
2350 init_homedir();
2351 else if (didset_vim && STRICMP(name, "VIM") == 0)
2352 didset_vim = FALSE;
2353 else if (didset_vimruntime
2354 && STRICMP(name, "VIMRUNTIME") == 0)
2355 didset_vimruntime = FALSE;
2356 arg_end = arg;
2357 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002359 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002360 }
2361 }
2362 }
2363
2364 /*
2365 * ":let &option = expr": Set option value.
2366 * ":let &l:option = expr": Set local option value.
2367 * ":let &g:option = expr": Set global option value.
2368 */
2369 else if (*arg == '&')
2370 {
2371 /* Find the end of the name. */
2372 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002373 if (p == NULL || (endchars != NULL
2374 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375 EMSG(_(e_letunexp));
2376 else
2377 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 long n;
2379 int opt_type;
2380 long numval;
2381 char_u *stringval = NULL;
2382 char_u *s;
2383
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 c1 = *p;
2385 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386
2387 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002388 s = get_tv_string_chk(tv); /* != NULL if number or string */
2389 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 {
2391 opt_type = get_option_value(arg, &numval,
2392 &stringval, opt_flags);
2393 if ((opt_type == 1 && *op == '.')
2394 || (opt_type == 0 && *op != '.'))
2395 EMSG2(_(e_letwrong), op);
2396 else
2397 {
2398 if (opt_type == 1) /* number */
2399 {
2400 if (*op == '+')
2401 n = numval + n;
2402 else
2403 n = numval - n;
2404 }
2405 else if (opt_type == 0 && stringval != NULL) /* string */
2406 {
2407 s = concat_str(stringval, s);
2408 vim_free(stringval);
2409 stringval = s;
2410 }
2411 }
2412 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002413 if (s != NULL)
2414 {
2415 set_option_value(arg, n, s, opt_flags);
2416 arg_end = p;
2417 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002418 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 }
2421 }
2422
2423 /*
2424 * ":let @r = expr": Set register contents.
2425 */
2426 else if (*arg == '@')
2427 {
2428 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429 if (op != NULL && (*op == '+' || *op == '-'))
2430 EMSG2(_(e_letwrong), op);
2431 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002432 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 EMSG(_(e_letunexp));
2434 else
2435 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002436 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 char_u *s;
2438
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002439 p = get_tv_string_chk(tv);
2440 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002442 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 if (s != NULL)
2444 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002445 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 vim_free(s);
2447 }
2448 }
2449 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002450 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002452 arg_end = arg + 1;
2453 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002454 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 }
2456 }
2457
2458 /*
2459 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002462 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002464 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002466 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2470 EMSG(_(e_letunexp));
2471 else
2472 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 arg_end = p;
2475 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478 }
2479
2480 else
2481 EMSG2(_(e_invarg2), arg);
2482
2483 return arg_end;
2484}
2485
2486/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2488 */
2489 static int
2490check_changedtick(arg)
2491 char_u *arg;
2492{
2493 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2494 {
2495 EMSG2(_(e_readonlyvar), arg);
2496 return TRUE;
2497 }
2498 return FALSE;
2499}
2500
2501/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 * Get an lval: variable, Dict item or List item that can be assigned a value
2503 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2504 * "name.key", "name.key[expr]" etc.
2505 * Indexing only works if "name" is an existing List or Dictionary.
2506 * "name" points to the start of the name.
2507 * If "rettv" is not NULL it points to the value to be assigned.
2508 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2509 * wrong; must end in space or cmd separator.
2510 *
2511 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002512 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 */
2515 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002516get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002518 typval_T *rettv;
2519 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 int unlet;
2521 int skip;
2522 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002523 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 char_u *expr_start, *expr_end;
2527 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002528 dictitem_T *v;
2529 typval_T var1;
2530 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002532 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002538 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539
2540 if (skip)
2541 {
2542 /* When skipping just find the end of the name. */
2543 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002544 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 }
2546
2547 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002548 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 if (expr_start != NULL)
2550 {
2551 /* Don't expand the name when we already know there is an error. */
2552 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2553 && *p != '[' && *p != '.')
2554 {
2555 EMSG(_(e_trailing));
2556 return NULL;
2557 }
2558
2559 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2560 if (lp->ll_exp_name == NULL)
2561 {
2562 /* Report an invalid expression in braces, unless the
2563 * expression evaluation has been cancelled due to an
2564 * aborting error, an interrupt, or an exception. */
2565 if (!aborting() && !quiet)
2566 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002567 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 EMSG2(_(e_invarg2), name);
2569 return NULL;
2570 }
2571 }
2572 lp->ll_name = lp->ll_exp_name;
2573 }
2574 else
2575 lp->ll_name = name;
2576
2577 /* Without [idx] or .key we are done. */
2578 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2579 return p;
2580
2581 cc = *p;
2582 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002583 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 if (v == NULL && !quiet)
2585 EMSG2(_(e_undefvar), lp->ll_name);
2586 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002587 if (v == NULL)
2588 return NULL;
2589
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 /*
2591 * Loop until no more [idx] or .key is following.
2592 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002593 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002595 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2597 && !(lp->ll_tv->v_type == VAR_DICT
2598 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 if (!quiet)
2601 EMSG(_("E689: Can only index a List or Dictionary"));
2602 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002603 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 if (!quiet)
2607 EMSG(_("E708: [:] must come last"));
2608 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002610
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 len = -1;
2612 if (*p == '.')
2613 {
2614 key = p + 1;
2615 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2616 ;
2617 if (len == 0)
2618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 if (!quiet)
2620 EMSG(_(e_emptykey));
2621 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002622 }
2623 p = key + len;
2624 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002625 else
2626 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 if (*p == ':')
2630 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002631 else
2632 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 empty1 = FALSE;
2634 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002636 if (get_tv_string_chk(&var1) == NULL)
2637 {
2638 /* not a number or string */
2639 clear_tv(&var1);
2640 return NULL;
2641 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 }
2643
2644 /* Optionally get the second index [ :expr]. */
2645 if (*p == ':')
2646 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002650 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002651 if (!empty1)
2652 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002654 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (rettv != NULL && (rettv->v_type != VAR_LIST
2656 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 if (!quiet)
2659 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 if (!empty1)
2661 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 }
2664 p = skipwhite(p + 1);
2665 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 else
2668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2671 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 if (!empty1)
2673 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002676 if (get_tv_string_chk(&var2) == NULL)
2677 {
2678 /* not a number or string */
2679 if (!empty1)
2680 clear_tv(&var1);
2681 clear_tv(&var2);
2682 return NULL;
2683 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002689
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!quiet)
2693 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 if (!empty1)
2695 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 }
2700
2701 /* Skip to past ']'. */
2702 ++p;
2703 }
2704
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 {
2707 if (len == -1)
2708 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002710 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 if (*key == NUL)
2712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (!quiet)
2714 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002719 lp->ll_list = NULL;
2720 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002721 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002722
2723 /* When assigning to g: check that a function and variable name is
2724 * valid. */
2725 if (rettv != NULL && lp->ll_dict == &globvardict)
2726 {
2727 if (rettv->v_type == VAR_FUNC
2728 && var_check_func_name(key, lp->ll_di == NULL))
2729 return NULL;
2730 if (!valid_varname(key))
2731 return NULL;
2732 }
2733
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002736 /* Can't add "v:" variable. */
2737 if (lp->ll_dict == &vimvardict)
2738 {
2739 EMSG2(_(e_illvar), name);
2740 return NULL;
2741 }
2742
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002743 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002747 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 if (len == -1)
2749 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 }
2752 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 if (len == -1)
2757 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 p = NULL;
2760 break;
2761 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002762 /* existing variable, need to check if it can be changed */
2763 else if (var_check_ro(lp->ll_di->di_flags, name))
2764 return NULL;
2765
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 if (len == -1)
2767 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 }
2770 else
2771 {
2772 /*
2773 * Get the number and item for the only or first index of the List.
2774 */
2775 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 else
2778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002779 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 clear_tv(&var1);
2781 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002782 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 lp->ll_list = lp->ll_tv->vval.v_list;
2784 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2785 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002787 if (lp->ll_n1 < 0)
2788 {
2789 lp->ll_n1 = 0;
2790 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2791 }
2792 }
2793 if (lp->ll_li == NULL)
2794 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 }
2799
2800 /*
2801 * May need to find the item or absolute index for the second
2802 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 * When no index given: "lp->ll_empty2" is TRUE.
2804 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002808 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 }
2817
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2819 if (lp->ll_n1 < 0)
2820 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2821 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002823 }
2824
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002826 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002827 }
2828
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 return p;
2830}
2831
2832/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002833 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 */
2835 static void
2836clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002837 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838{
2839 vim_free(lp->ll_exp_name);
2840 vim_free(lp->ll_newkey);
2841}
2842
2843/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002844 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002846 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 */
2848 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002849set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002850 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002852 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002854 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855{
2856 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002857 listitem_T *ri;
2858 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859
2860 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002861 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002863 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 cc = *endp;
2865 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002866 if (op != NULL && *op != '=')
2867 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002868 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002869
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002870 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002871 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002872 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002873 {
2874 if (tv_op(&tv, rettv, op) == OK)
2875 set_var(lp->ll_name, &tv, FALSE);
2876 clear_tv(&tv);
2877 }
2878 }
2879 else
2880 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002882 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002884 else if (tv_check_lock(lp->ll_newkey == NULL
2885 ? lp->ll_tv->v_lock
2886 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2887 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 else if (lp->ll_range)
2889 {
2890 /*
2891 * Assign the List values to the list items.
2892 */
2893 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002894 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002895 if (op != NULL && *op != '=')
2896 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2897 else
2898 {
2899 clear_tv(&lp->ll_li->li_tv);
2900 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2901 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 ri = ri->li_next;
2903 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2904 break;
2905 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002906 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002908 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002909 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 ri = NULL;
2911 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002912 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002913 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 lp->ll_li = lp->ll_li->li_next;
2915 ++lp->ll_n1;
2916 }
2917 if (ri != NULL)
2918 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002919 else if (lp->ll_empty2
2920 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 : lp->ll_n1 != lp->ll_n2)
2922 EMSG(_("E711: List value has not enough items"));
2923 }
2924 else
2925 {
2926 /*
2927 * Assign to a List or Dictionary item.
2928 */
2929 if (lp->ll_newkey != NULL)
2930 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002931 if (op != NULL && *op != '=')
2932 {
2933 EMSG2(_(e_letwrong), op);
2934 return;
2935 }
2936
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002938 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 if (di == NULL)
2940 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002941 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2942 {
2943 vim_free(di);
2944 return;
2945 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002947 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002948 else if (op != NULL && *op != '=')
2949 {
2950 tv_op(lp->ll_tv, rettv, op);
2951 return;
2952 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002953 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002955
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956 /*
2957 * Assign the value to the variable or list item.
2958 */
2959 if (copy)
2960 copy_tv(rettv, lp->ll_tv);
2961 else
2962 {
2963 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002964 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002966 }
2967 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002968}
2969
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002970/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002971 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2972 * Returns OK or FAIL.
2973 */
2974 static int
2975tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002976 typval_T *tv1;
2977 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002978 char_u *op;
2979{
2980 long n;
2981 char_u numbuf[NUMBUFLEN];
2982 char_u *s;
2983
2984 /* Can't do anything with a Funcref or a Dict on the right. */
2985 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2986 {
2987 switch (tv1->v_type)
2988 {
2989 case VAR_DICT:
2990 case VAR_FUNC:
2991 break;
2992
2993 case VAR_LIST:
2994 if (*op != '+' || tv2->v_type != VAR_LIST)
2995 break;
2996 /* List += List */
2997 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2998 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2999 return OK;
3000
3001 case VAR_NUMBER:
3002 case VAR_STRING:
3003 if (tv2->v_type == VAR_LIST)
3004 break;
3005 if (*op == '+' || *op == '-')
3006 {
3007 /* nr += nr or nr -= nr*/
3008 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003009#ifdef FEAT_FLOAT
3010 if (tv2->v_type == VAR_FLOAT)
3011 {
3012 float_T f = n;
3013
3014 if (*op == '+')
3015 f += tv2->vval.v_float;
3016 else
3017 f -= tv2->vval.v_float;
3018 clear_tv(tv1);
3019 tv1->v_type = VAR_FLOAT;
3020 tv1->vval.v_float = f;
3021 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003022 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003023#endif
3024 {
3025 if (*op == '+')
3026 n += get_tv_number(tv2);
3027 else
3028 n -= get_tv_number(tv2);
3029 clear_tv(tv1);
3030 tv1->v_type = VAR_NUMBER;
3031 tv1->vval.v_number = n;
3032 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003033 }
3034 else
3035 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003036 if (tv2->v_type == VAR_FLOAT)
3037 break;
3038
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003039 /* str .= str */
3040 s = get_tv_string(tv1);
3041 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3042 clear_tv(tv1);
3043 tv1->v_type = VAR_STRING;
3044 tv1->vval.v_string = s;
3045 }
3046 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003047
3048#ifdef FEAT_FLOAT
3049 case VAR_FLOAT:
3050 {
3051 float_T f;
3052
3053 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3054 && tv2->v_type != VAR_NUMBER
3055 && tv2->v_type != VAR_STRING))
3056 break;
3057 if (tv2->v_type == VAR_FLOAT)
3058 f = tv2->vval.v_float;
3059 else
3060 f = get_tv_number(tv2);
3061 if (*op == '+')
3062 tv1->vval.v_float += f;
3063 else
3064 tv1->vval.v_float -= f;
3065 }
3066 return OK;
3067#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003068 }
3069 }
3070
3071 EMSG2(_(e_letwrong), op);
3072 return FAIL;
3073}
3074
3075/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003076 * Add a watcher to a list.
3077 */
3078 static void
3079list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003080 list_T *l;
3081 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003082{
3083 lw->lw_next = l->lv_watch;
3084 l->lv_watch = lw;
3085}
3086
3087/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003088 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003089 * No warning when it isn't found...
3090 */
3091 static void
3092list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003093 list_T *l;
3094 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003095{
Bram Moolenaar33570922005-01-25 22:26:29 +00003096 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003097
3098 lwp = &l->lv_watch;
3099 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3100 {
3101 if (lw == lwrem)
3102 {
3103 *lwp = lw->lw_next;
3104 break;
3105 }
3106 lwp = &lw->lw_next;
3107 }
3108}
3109
3110/*
3111 * Just before removing an item from a list: advance watchers to the next
3112 * item.
3113 */
3114 static void
3115list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003116 list_T *l;
3117 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003118{
Bram Moolenaar33570922005-01-25 22:26:29 +00003119 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003120
3121 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3122 if (lw->lw_item == item)
3123 lw->lw_item = item->li_next;
3124}
3125
3126/*
3127 * Evaluate the expression used in a ":for var in expr" command.
3128 * "arg" points to "var".
3129 * Set "*errp" to TRUE for an error, FALSE otherwise;
3130 * Return a pointer that holds the info. Null when there is an error.
3131 */
3132 void *
3133eval_for_line(arg, errp, nextcmdp, skip)
3134 char_u *arg;
3135 int *errp;
3136 char_u **nextcmdp;
3137 int skip;
3138{
Bram Moolenaar33570922005-01-25 22:26:29 +00003139 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003140 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003141 typval_T tv;
3142 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003143
3144 *errp = TRUE; /* default: there is an error */
3145
Bram Moolenaar33570922005-01-25 22:26:29 +00003146 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003147 if (fi == NULL)
3148 return NULL;
3149
3150 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3151 if (expr == NULL)
3152 return fi;
3153
3154 expr = skipwhite(expr);
3155 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3156 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003157 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003158 return fi;
3159 }
3160
3161 if (skip)
3162 ++emsg_skip;
3163 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3164 {
3165 *errp = FALSE;
3166 if (!skip)
3167 {
3168 l = tv.vval.v_list;
3169 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003170 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003172 clear_tv(&tv);
3173 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174 else
3175 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003176 /* No need to increment the refcount, it's already set for the
3177 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178 fi->fi_list = l;
3179 list_add_watch(l, &fi->fi_lw);
3180 fi->fi_lw.lw_item = l->lv_first;
3181 }
3182 }
3183 }
3184 if (skip)
3185 --emsg_skip;
3186
3187 return fi;
3188}
3189
3190/*
3191 * Use the first item in a ":for" list. Advance to the next.
3192 * Assign the values to the variable (list). "arg" points to the first one.
3193 * Return TRUE when a valid item was found, FALSE when at end of list or
3194 * something wrong.
3195 */
3196 int
3197next_for_item(fi_void, arg)
3198 void *fi_void;
3199 char_u *arg;
3200{
Bram Moolenaar33570922005-01-25 22:26:29 +00003201 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003203 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204
3205 item = fi->fi_lw.lw_item;
3206 if (item == NULL)
3207 result = FALSE;
3208 else
3209 {
3210 fi->fi_lw.lw_item = item->li_next;
3211 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3212 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3213 }
3214 return result;
3215}
3216
3217/*
3218 * Free the structure used to store info used by ":for".
3219 */
3220 void
3221free_for_info(fi_void)
3222 void *fi_void;
3223{
Bram Moolenaar33570922005-01-25 22:26:29 +00003224 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003226 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003227 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003228 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003229 list_unref(fi->fi_list);
3230 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003231 vim_free(fi);
3232}
3233
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3235
3236 void
3237set_context_for_expression(xp, arg, cmdidx)
3238 expand_T *xp;
3239 char_u *arg;
3240 cmdidx_T cmdidx;
3241{
3242 int got_eq = FALSE;
3243 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 if (cmdidx == CMD_let)
3247 {
3248 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003249 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003250 {
3251 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003252 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253 {
3254 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003255 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256 if (vim_iswhite(*p))
3257 break;
3258 }
3259 return;
3260 }
3261 }
3262 else
3263 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3264 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 while ((xp->xp_pattern = vim_strpbrk(arg,
3266 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3267 {
3268 c = *xp->xp_pattern;
3269 if (c == '&')
3270 {
3271 c = xp->xp_pattern[1];
3272 if (c == '&')
3273 {
3274 ++xp->xp_pattern;
3275 xp->xp_context = cmdidx != CMD_let || got_eq
3276 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3277 }
3278 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003279 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003281 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3282 xp->xp_pattern += 2;
3283
3284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 }
3286 else if (c == '$')
3287 {
3288 /* environment variable */
3289 xp->xp_context = EXPAND_ENV_VARS;
3290 }
3291 else if (c == '=')
3292 {
3293 got_eq = TRUE;
3294 xp->xp_context = EXPAND_EXPRESSION;
3295 }
3296 else if (c == '<'
3297 && xp->xp_context == EXPAND_FUNCTIONS
3298 && vim_strchr(xp->xp_pattern, '(') == NULL)
3299 {
3300 /* Function name can start with "<SNR>" */
3301 break;
3302 }
3303 else if (cmdidx != CMD_let || got_eq)
3304 {
3305 if (c == '"') /* string */
3306 {
3307 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3308 if (c == '\\' && xp->xp_pattern[1] != NUL)
3309 ++xp->xp_pattern;
3310 xp->xp_context = EXPAND_NOTHING;
3311 }
3312 else if (c == '\'') /* literal string */
3313 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003314 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3316 /* skip */ ;
3317 xp->xp_context = EXPAND_NOTHING;
3318 }
3319 else if (c == '|')
3320 {
3321 if (xp->xp_pattern[1] == '|')
3322 {
3323 ++xp->xp_pattern;
3324 xp->xp_context = EXPAND_EXPRESSION;
3325 }
3326 else
3327 xp->xp_context = EXPAND_COMMANDS;
3328 }
3329 else
3330 xp->xp_context = EXPAND_EXPRESSION;
3331 }
3332 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 /* Doesn't look like something valid, expand as an expression
3334 * anyway. */
3335 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 arg = xp->xp_pattern;
3337 if (*arg != NUL)
3338 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3339 /* skip */ ;
3340 }
3341 xp->xp_pattern = arg;
3342}
3343
3344#endif /* FEAT_CMDL_COMPL */
3345
3346/*
3347 * ":1,25call func(arg1, arg2)" function call.
3348 */
3349 void
3350ex_call(eap)
3351 exarg_T *eap;
3352{
3353 char_u *arg = eap->arg;
3354 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003356 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003358 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 linenr_T lnum;
3360 int doesrange;
3361 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003362 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003364 if (eap->skip)
3365 {
3366 /* trans_function_name() doesn't work well when skipping, use eval0()
3367 * instead to skip to any following command, e.g. for:
3368 * :if 0 | call dict.foo().bar() | endif */
3369 eval0(eap->arg, &rettv, &eap->nextcmd, FALSE);
3370 return;
3371 }
3372
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003373 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003374 if (fudi.fd_newkey != NULL)
3375 {
3376 /* Still need to give an error message for missing key. */
3377 EMSG2(_(e_dictkey), fudi.fd_newkey);
3378 vim_free(fudi.fd_newkey);
3379 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003380 if (tofree == NULL)
3381 return;
3382
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003383 /* Increase refcount on dictionary, it could get deleted when evaluating
3384 * the arguments. */
3385 if (fudi.fd_dict != NULL)
3386 ++fudi.fd_dict->dv_refcount;
3387
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003388 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003389 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003390 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391
Bram Moolenaar532c7802005-01-27 14:44:31 +00003392 /* Skip white space to allow ":call func ()". Not good, but required for
3393 * backward compatibility. */
3394 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003395 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396
3397 if (*startarg != '(')
3398 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003399 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 goto end;
3401 }
3402
3403 /*
3404 * When skipping, evaluate the function once, to find the end of the
3405 * arguments.
3406 * When the function takes a range, this is discovered after the first
3407 * call, and the loop is broken.
3408 */
3409 if (eap->skip)
3410 {
3411 ++emsg_skip;
3412 lnum = eap->line2; /* do it once, also with an invalid range */
3413 }
3414 else
3415 lnum = eap->line1;
3416 for ( ; lnum <= eap->line2; ++lnum)
3417 {
3418 if (!eap->skip && eap->addr_count > 0)
3419 {
3420 curwin->w_cursor.lnum = lnum;
3421 curwin->w_cursor.col = 0;
3422 }
3423 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003424 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003425 eap->line1, eap->line2, &doesrange,
3426 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 {
3428 failed = TRUE;
3429 break;
3430 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003431
3432 /* Handle a function returning a Funcref, Dictionary or List. */
3433 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3434 {
3435 failed = TRUE;
3436 break;
3437 }
3438
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003439 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 if (doesrange || eap->skip)
3441 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003442
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003444 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003445 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003446 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 if (aborting())
3448 break;
3449 }
3450 if (eap->skip)
3451 --emsg_skip;
3452
3453 if (!failed)
3454 {
3455 /* Check for trailing illegal characters and a following command. */
3456 if (!ends_excmd(*arg))
3457 {
3458 emsg_severe = TRUE;
3459 EMSG(_(e_trailing));
3460 }
3461 else
3462 eap->nextcmd = check_nextcmd(arg);
3463 }
3464
3465end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003466 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003467 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468}
3469
3470/*
3471 * ":unlet[!] var1 ... " command.
3472 */
3473 void
3474ex_unlet(eap)
3475 exarg_T *eap;
3476{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003477 ex_unletlock(eap, eap->arg, 0);
3478}
3479
3480/*
3481 * ":lockvar" and ":unlockvar" commands
3482 */
3483 void
3484ex_lockvar(eap)
3485 exarg_T *eap;
3486{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003488 int deep = 2;
3489
3490 if (eap->forceit)
3491 deep = -1;
3492 else if (vim_isdigit(*arg))
3493 {
3494 deep = getdigits(&arg);
3495 arg = skipwhite(arg);
3496 }
3497
3498 ex_unletlock(eap, arg, deep);
3499}
3500
3501/*
3502 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3503 */
3504 static void
3505ex_unletlock(eap, argstart, deep)
3506 exarg_T *eap;
3507 char_u *argstart;
3508 int deep;
3509{
3510 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003513 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
3515 do
3516 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003517 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003518 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3519 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003520 if (lv.ll_name == NULL)
3521 error = TRUE; /* error but continue parsing */
3522 if (name_end == NULL || (!vim_iswhite(*name_end)
3523 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003525 if (name_end != NULL)
3526 {
3527 emsg_severe = TRUE;
3528 EMSG(_(e_trailing));
3529 }
3530 if (!(eap->skip || error))
3531 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 break;
3533 }
3534
3535 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003536 {
3537 if (eap->cmdidx == CMD_unlet)
3538 {
3539 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3540 error = TRUE;
3541 }
3542 else
3543 {
3544 if (do_lock_var(&lv, name_end, deep,
3545 eap->cmdidx == CMD_lockvar) == FAIL)
3546 error = TRUE;
3547 }
3548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003550 if (!eap->skip)
3551 clear_lval(&lv);
3552
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 arg = skipwhite(name_end);
3554 } while (!ends_excmd(*arg));
3555
3556 eap->nextcmd = check_nextcmd(arg);
3557}
3558
Bram Moolenaar8c711452005-01-14 21:53:12 +00003559 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003560do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003561 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003562 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003563 int forceit;
3564{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003565 int ret = OK;
3566 int cc;
3567
3568 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003569 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003570 cc = *name_end;
3571 *name_end = NUL;
3572
3573 /* Normal name or expanded name. */
3574 if (check_changedtick(lp->ll_name))
3575 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003576 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003577 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003578 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003579 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003580 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3581 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003582 else if (lp->ll_range)
3583 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003584 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003585
3586 /* Delete a range of List items. */
3587 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3588 {
3589 li = lp->ll_li->li_next;
3590 listitem_remove(lp->ll_list, lp->ll_li);
3591 lp->ll_li = li;
3592 ++lp->ll_n1;
3593 }
3594 }
3595 else
3596 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003597 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003598 /* unlet a List item. */
3599 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003600 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003601 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003602 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003603 }
3604
3605 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003606}
3607
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608/*
3609 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003610 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 */
3612 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003613do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003615 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616{
Bram Moolenaar33570922005-01-25 22:26:29 +00003617 hashtab_T *ht;
3618 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003619 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003620 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621
Bram Moolenaar33570922005-01-25 22:26:29 +00003622 ht = find_var_ht(name, &varname);
3623 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003625 hi = hash_find(ht, varname);
3626 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003627 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003628 di = HI2DI(hi);
3629 if (var_check_fixed(di->di_flags, name)
3630 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003631 return FAIL;
3632 delete_var(ht, hi);
3633 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003636 if (forceit)
3637 return OK;
3638 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 return FAIL;
3640}
3641
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003642/*
3643 * Lock or unlock variable indicated by "lp".
3644 * "deep" is the levels to go (-1 for unlimited);
3645 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3646 */
3647 static int
3648do_lock_var(lp, name_end, deep, lock)
3649 lval_T *lp;
3650 char_u *name_end;
3651 int deep;
3652 int lock;
3653{
3654 int ret = OK;
3655 int cc;
3656 dictitem_T *di;
3657
3658 if (deep == 0) /* nothing to do */
3659 return OK;
3660
3661 if (lp->ll_tv == NULL)
3662 {
3663 cc = *name_end;
3664 *name_end = NUL;
3665
3666 /* Normal name or expanded name. */
3667 if (check_changedtick(lp->ll_name))
3668 ret = FAIL;
3669 else
3670 {
3671 di = find_var(lp->ll_name, NULL);
3672 if (di == NULL)
3673 ret = FAIL;
3674 else
3675 {
3676 if (lock)
3677 di->di_flags |= DI_FLAGS_LOCK;
3678 else
3679 di->di_flags &= ~DI_FLAGS_LOCK;
3680 item_lock(&di->di_tv, deep, lock);
3681 }
3682 }
3683 *name_end = cc;
3684 }
3685 else if (lp->ll_range)
3686 {
3687 listitem_T *li = lp->ll_li;
3688
3689 /* (un)lock a range of List items. */
3690 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3691 {
3692 item_lock(&li->li_tv, deep, lock);
3693 li = li->li_next;
3694 ++lp->ll_n1;
3695 }
3696 }
3697 else if (lp->ll_list != NULL)
3698 /* (un)lock a List item. */
3699 item_lock(&lp->ll_li->li_tv, deep, lock);
3700 else
3701 /* un(lock) a Dictionary item. */
3702 item_lock(&lp->ll_di->di_tv, deep, lock);
3703
3704 return ret;
3705}
3706
3707/*
3708 * Lock or unlock an item. "deep" is nr of levels to go.
3709 */
3710 static void
3711item_lock(tv, deep, lock)
3712 typval_T *tv;
3713 int deep;
3714 int lock;
3715{
3716 static int recurse = 0;
3717 list_T *l;
3718 listitem_T *li;
3719 dict_T *d;
3720 hashitem_T *hi;
3721 int todo;
3722
3723 if (recurse >= DICT_MAXNEST)
3724 {
3725 EMSG(_("E743: variable nested too deep for (un)lock"));
3726 return;
3727 }
3728 if (deep == 0)
3729 return;
3730 ++recurse;
3731
3732 /* lock/unlock the item itself */
3733 if (lock)
3734 tv->v_lock |= VAR_LOCKED;
3735 else
3736 tv->v_lock &= ~VAR_LOCKED;
3737
3738 switch (tv->v_type)
3739 {
3740 case VAR_LIST:
3741 if ((l = tv->vval.v_list) != NULL)
3742 {
3743 if (lock)
3744 l->lv_lock |= VAR_LOCKED;
3745 else
3746 l->lv_lock &= ~VAR_LOCKED;
3747 if (deep < 0 || deep > 1)
3748 /* recursive: lock/unlock the items the List contains */
3749 for (li = l->lv_first; li != NULL; li = li->li_next)
3750 item_lock(&li->li_tv, deep - 1, lock);
3751 }
3752 break;
3753 case VAR_DICT:
3754 if ((d = tv->vval.v_dict) != NULL)
3755 {
3756 if (lock)
3757 d->dv_lock |= VAR_LOCKED;
3758 else
3759 d->dv_lock &= ~VAR_LOCKED;
3760 if (deep < 0 || deep > 1)
3761 {
3762 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003763 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003764 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3765 {
3766 if (!HASHITEM_EMPTY(hi))
3767 {
3768 --todo;
3769 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3770 }
3771 }
3772 }
3773 }
3774 }
3775 --recurse;
3776}
3777
Bram Moolenaara40058a2005-07-11 22:42:07 +00003778/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003779 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3780 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003781 */
3782 static int
3783tv_islocked(tv)
3784 typval_T *tv;
3785{
3786 return (tv->v_lock & VAR_LOCKED)
3787 || (tv->v_type == VAR_LIST
3788 && tv->vval.v_list != NULL
3789 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3790 || (tv->v_type == VAR_DICT
3791 && tv->vval.v_dict != NULL
3792 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3793}
3794
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3796/*
3797 * Delete all "menutrans_" variables.
3798 */
3799 void
3800del_menutrans_vars()
3801{
Bram Moolenaar33570922005-01-25 22:26:29 +00003802 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003803 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804
Bram Moolenaar33570922005-01-25 22:26:29 +00003805 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003806 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003807 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003808 {
3809 if (!HASHITEM_EMPTY(hi))
3810 {
3811 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003812 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3813 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003814 }
3815 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003816 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817}
3818#endif
3819
3820#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3821
3822/*
3823 * Local string buffer for the next two functions to store a variable name
3824 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3825 * get_user_var_name().
3826 */
3827
3828static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3829
3830static char_u *varnamebuf = NULL;
3831static int varnamebuflen = 0;
3832
3833/*
3834 * Function to concatenate a prefix and a variable name.
3835 */
3836 static char_u *
3837cat_prefix_varname(prefix, name)
3838 int prefix;
3839 char_u *name;
3840{
3841 int len;
3842
3843 len = (int)STRLEN(name) + 3;
3844 if (len > varnamebuflen)
3845 {
3846 vim_free(varnamebuf);
3847 len += 10; /* some additional space */
3848 varnamebuf = alloc(len);
3849 if (varnamebuf == NULL)
3850 {
3851 varnamebuflen = 0;
3852 return NULL;
3853 }
3854 varnamebuflen = len;
3855 }
3856 *varnamebuf = prefix;
3857 varnamebuf[1] = ':';
3858 STRCPY(varnamebuf + 2, name);
3859 return varnamebuf;
3860}
3861
3862/*
3863 * Function given to ExpandGeneric() to obtain the list of user defined
3864 * (global/buffer/window/built-in) variable names.
3865 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 char_u *
3867get_user_var_name(xp, idx)
3868 expand_T *xp;
3869 int idx;
3870{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003871 static long_u gdone;
3872 static long_u bdone;
3873 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003874#ifdef FEAT_WINDOWS
3875 static long_u tdone;
3876#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003877 static int vidx;
3878 static hashitem_T *hi;
3879 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880
3881 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003882 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003883 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003884#ifdef FEAT_WINDOWS
3885 tdone = 0;
3886#endif
3887 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003888
3889 /* Global variables */
3890 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003892 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003893 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003894 else
3895 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003896 while (HASHITEM_EMPTY(hi))
3897 ++hi;
3898 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3899 return cat_prefix_varname('g', hi->hi_key);
3900 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003902
3903 /* b: variables */
3904 ht = &curbuf->b_vars.dv_hashtab;
3905 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003907 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003908 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003909 else
3910 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003911 while (HASHITEM_EMPTY(hi))
3912 ++hi;
3913 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003915 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003917 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 return (char_u *)"b:changedtick";
3919 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003920
3921 /* w: variables */
3922 ht = &curwin->w_vars.dv_hashtab;
3923 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003925 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003926 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003927 else
3928 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003929 while (HASHITEM_EMPTY(hi))
3930 ++hi;
3931 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003933
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003934#ifdef FEAT_WINDOWS
3935 /* t: variables */
3936 ht = &curtab->tp_vars.dv_hashtab;
3937 if (tdone < ht->ht_used)
3938 {
3939 if (tdone++ == 0)
3940 hi = ht->ht_array;
3941 else
3942 ++hi;
3943 while (HASHITEM_EMPTY(hi))
3944 ++hi;
3945 return cat_prefix_varname('t', hi->hi_key);
3946 }
3947#endif
3948
Bram Moolenaar33570922005-01-25 22:26:29 +00003949 /* v: variables */
3950 if (vidx < VV_LEN)
3951 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
3953 vim_free(varnamebuf);
3954 varnamebuf = NULL;
3955 varnamebuflen = 0;
3956 return NULL;
3957}
3958
3959#endif /* FEAT_CMDL_COMPL */
3960
3961/*
3962 * types for expressions.
3963 */
3964typedef enum
3965{
3966 TYPE_UNKNOWN = 0
3967 , TYPE_EQUAL /* == */
3968 , TYPE_NEQUAL /* != */
3969 , TYPE_GREATER /* > */
3970 , TYPE_GEQUAL /* >= */
3971 , TYPE_SMALLER /* < */
3972 , TYPE_SEQUAL /* <= */
3973 , TYPE_MATCH /* =~ */
3974 , TYPE_NOMATCH /* !~ */
3975} exptype_T;
3976
3977/*
3978 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003979 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3981 */
3982
3983/*
3984 * Handle zero level expression.
3985 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003986 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003987 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 * Return OK or FAIL.
3989 */
3990 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003991eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 char_u **nextcmd;
3995 int evaluate;
3996{
3997 int ret;
3998 char_u *p;
3999
4000 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004001 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 if (ret == FAIL || !ends_excmd(*p))
4003 {
4004 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004005 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 /*
4007 * Report the invalid expression unless the expression evaluation has
4008 * been cancelled due to an aborting error, an interrupt, or an
4009 * exception.
4010 */
4011 if (!aborting())
4012 EMSG2(_(e_invexpr2), arg);
4013 ret = FAIL;
4014 }
4015 if (nextcmd != NULL)
4016 *nextcmd = check_nextcmd(p);
4017
4018 return ret;
4019}
4020
4021/*
4022 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004023 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 *
4025 * "arg" must point to the first non-white of the expression.
4026 * "arg" is advanced to the next non-white after the recognized expression.
4027 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004028 * Note: "rettv.v_lock" is not set.
4029 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 * Return OK or FAIL.
4031 */
4032 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004033eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 int evaluate;
4037{
4038 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004039 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040
4041 /*
4042 * Get the first variable.
4043 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004044 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 return FAIL;
4046
4047 if ((*arg)[0] == '?')
4048 {
4049 result = FALSE;
4050 if (evaluate)
4051 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004052 int error = FALSE;
4053
4054 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004056 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004057 if (error)
4058 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 }
4060
4061 /*
4062 * Get the second variable.
4063 */
4064 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004065 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 return FAIL;
4067
4068 /*
4069 * Check for the ":".
4070 */
4071 if ((*arg)[0] != ':')
4072 {
4073 EMSG(_("E109: Missing ':' after '?'"));
4074 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004075 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 return FAIL;
4077 }
4078
4079 /*
4080 * Get the third variable.
4081 */
4082 *arg = skipwhite(*arg + 1);
4083 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4084 {
4085 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004086 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 return FAIL;
4088 }
4089 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004090 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 }
4092
4093 return OK;
4094}
4095
4096/*
4097 * Handle first level expression:
4098 * expr2 || expr2 || expr2 logical OR
4099 *
4100 * "arg" must point to the first non-white of the expression.
4101 * "arg" is advanced to the next non-white after the recognized expression.
4102 *
4103 * Return OK or FAIL.
4104 */
4105 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 int evaluate;
4110{
Bram Moolenaar33570922005-01-25 22:26:29 +00004111 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 long result;
4113 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004114 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115
4116 /*
4117 * Get the first variable.
4118 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004119 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 return FAIL;
4121
4122 /*
4123 * Repeat until there is no following "||".
4124 */
4125 first = TRUE;
4126 result = FALSE;
4127 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4128 {
4129 if (evaluate && first)
4130 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004131 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004134 if (error)
4135 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 first = FALSE;
4137 }
4138
4139 /*
4140 * Get the second variable.
4141 */
4142 *arg = skipwhite(*arg + 2);
4143 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4144 return FAIL;
4145
4146 /*
4147 * Compute the result.
4148 */
4149 if (evaluate && !result)
4150 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004151 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004153 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004154 if (error)
4155 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 }
4157 if (evaluate)
4158 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004159 rettv->v_type = VAR_NUMBER;
4160 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 }
4162 }
4163
4164 return OK;
4165}
4166
4167/*
4168 * Handle second level expression:
4169 * expr3 && expr3 && expr3 logical AND
4170 *
4171 * "arg" must point to the first non-white of the expression.
4172 * "arg" is advanced to the next non-white after the recognized expression.
4173 *
4174 * Return OK or FAIL.
4175 */
4176 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004177eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 int evaluate;
4181{
Bram Moolenaar33570922005-01-25 22:26:29 +00004182 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 long result;
4184 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004185 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186
4187 /*
4188 * Get the first variable.
4189 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 return FAIL;
4192
4193 /*
4194 * Repeat until there is no following "&&".
4195 */
4196 first = TRUE;
4197 result = TRUE;
4198 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4199 {
4200 if (evaluate && first)
4201 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004202 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004205 if (error)
4206 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 first = FALSE;
4208 }
4209
4210 /*
4211 * Get the second variable.
4212 */
4213 *arg = skipwhite(*arg + 2);
4214 if (eval4(arg, &var2, evaluate && result) == FAIL)
4215 return FAIL;
4216
4217 /*
4218 * Compute the result.
4219 */
4220 if (evaluate && result)
4221 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004222 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004225 if (error)
4226 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 }
4228 if (evaluate)
4229 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230 rettv->v_type = VAR_NUMBER;
4231 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
4233 }
4234
4235 return OK;
4236}
4237
4238/*
4239 * Handle third level expression:
4240 * var1 == var2
4241 * var1 =~ var2
4242 * var1 != var2
4243 * var1 !~ var2
4244 * var1 > var2
4245 * var1 >= var2
4246 * var1 < var2
4247 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004248 * var1 is var2
4249 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 *
4251 * "arg" must point to the first non-white of the expression.
4252 * "arg" is advanced to the next non-white after the recognized expression.
4253 *
4254 * Return OK or FAIL.
4255 */
4256 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 int evaluate;
4261{
Bram Moolenaar33570922005-01-25 22:26:29 +00004262 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 char_u *p;
4264 int i;
4265 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004266 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 int len = 2;
4268 long n1, n2;
4269 char_u *s1, *s2;
4270 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4271 regmatch_T regmatch;
4272 int ic;
4273 char_u *save_cpo;
4274
4275 /*
4276 * Get the first variable.
4277 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 return FAIL;
4280
4281 p = *arg;
4282 switch (p[0])
4283 {
4284 case '=': if (p[1] == '=')
4285 type = TYPE_EQUAL;
4286 else if (p[1] == '~')
4287 type = TYPE_MATCH;
4288 break;
4289 case '!': if (p[1] == '=')
4290 type = TYPE_NEQUAL;
4291 else if (p[1] == '~')
4292 type = TYPE_NOMATCH;
4293 break;
4294 case '>': if (p[1] != '=')
4295 {
4296 type = TYPE_GREATER;
4297 len = 1;
4298 }
4299 else
4300 type = TYPE_GEQUAL;
4301 break;
4302 case '<': if (p[1] != '=')
4303 {
4304 type = TYPE_SMALLER;
4305 len = 1;
4306 }
4307 else
4308 type = TYPE_SEQUAL;
4309 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004310 case 'i': if (p[1] == 's')
4311 {
4312 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4313 len = 5;
4314 if (!vim_isIDc(p[len]))
4315 {
4316 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4317 type_is = TRUE;
4318 }
4319 }
4320 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 }
4322
4323 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004324 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 */
4326 if (type != TYPE_UNKNOWN)
4327 {
4328 /* extra question mark appended: ignore case */
4329 if (p[len] == '?')
4330 {
4331 ic = TRUE;
4332 ++len;
4333 }
4334 /* extra '#' appended: match case */
4335 else if (p[len] == '#')
4336 {
4337 ic = FALSE;
4338 ++len;
4339 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004340 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 else
4342 ic = p_ic;
4343
4344 /*
4345 * Get the second variable.
4346 */
4347 *arg = skipwhite(p + len);
4348 if (eval5(arg, &var2, evaluate) == FAIL)
4349 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004350 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 return FAIL;
4352 }
4353
4354 if (evaluate)
4355 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004356 if (type_is && rettv->v_type != var2.v_type)
4357 {
4358 /* For "is" a different type always means FALSE, for "notis"
4359 * it means TRUE. */
4360 n1 = (type == TYPE_NEQUAL);
4361 }
4362 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4363 {
4364 if (type_is)
4365 {
4366 n1 = (rettv->v_type == var2.v_type
4367 && rettv->vval.v_list == var2.vval.v_list);
4368 if (type == TYPE_NEQUAL)
4369 n1 = !n1;
4370 }
4371 else if (rettv->v_type != var2.v_type
4372 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4373 {
4374 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004375 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004376 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004377 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004378 clear_tv(rettv);
4379 clear_tv(&var2);
4380 return FAIL;
4381 }
4382 else
4383 {
4384 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004385 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4386 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004387 if (type == TYPE_NEQUAL)
4388 n1 = !n1;
4389 }
4390 }
4391
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004392 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4393 {
4394 if (type_is)
4395 {
4396 n1 = (rettv->v_type == var2.v_type
4397 && rettv->vval.v_dict == var2.vval.v_dict);
4398 if (type == TYPE_NEQUAL)
4399 n1 = !n1;
4400 }
4401 else if (rettv->v_type != var2.v_type
4402 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4403 {
4404 if (rettv->v_type != var2.v_type)
4405 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4406 else
4407 EMSG(_("E736: Invalid operation for Dictionary"));
4408 clear_tv(rettv);
4409 clear_tv(&var2);
4410 return FAIL;
4411 }
4412 else
4413 {
4414 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004415 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4416 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004417 if (type == TYPE_NEQUAL)
4418 n1 = !n1;
4419 }
4420 }
4421
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004422 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4423 {
4424 if (rettv->v_type != var2.v_type
4425 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4426 {
4427 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004428 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004429 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004430 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004431 clear_tv(rettv);
4432 clear_tv(&var2);
4433 return FAIL;
4434 }
4435 else
4436 {
4437 /* Compare two Funcrefs for being equal or unequal. */
4438 if (rettv->vval.v_string == NULL
4439 || var2.vval.v_string == NULL)
4440 n1 = FALSE;
4441 else
4442 n1 = STRCMP(rettv->vval.v_string,
4443 var2.vval.v_string) == 0;
4444 if (type == TYPE_NEQUAL)
4445 n1 = !n1;
4446 }
4447 }
4448
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004449#ifdef FEAT_FLOAT
4450 /*
4451 * If one of the two variables is a float, compare as a float.
4452 * When using "=~" or "!~", always compare as string.
4453 */
4454 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4455 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4456 {
4457 float_T f1, f2;
4458
4459 if (rettv->v_type == VAR_FLOAT)
4460 f1 = rettv->vval.v_float;
4461 else
4462 f1 = get_tv_number(rettv);
4463 if (var2.v_type == VAR_FLOAT)
4464 f2 = var2.vval.v_float;
4465 else
4466 f2 = get_tv_number(&var2);
4467 n1 = FALSE;
4468 switch (type)
4469 {
4470 case TYPE_EQUAL: n1 = (f1 == f2); break;
4471 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4472 case TYPE_GREATER: n1 = (f1 > f2); break;
4473 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4474 case TYPE_SMALLER: n1 = (f1 < f2); break;
4475 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4476 case TYPE_UNKNOWN:
4477 case TYPE_MATCH:
4478 case TYPE_NOMATCH: break; /* avoid gcc warning */
4479 }
4480 }
4481#endif
4482
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 /*
4484 * If one of the two variables is a number, compare as a number.
4485 * When using "=~" or "!~", always compare as string.
4486 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004487 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4489 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004490 n1 = get_tv_number(rettv);
4491 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 switch (type)
4493 {
4494 case TYPE_EQUAL: n1 = (n1 == n2); break;
4495 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4496 case TYPE_GREATER: n1 = (n1 > n2); break;
4497 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4498 case TYPE_SMALLER: n1 = (n1 < n2); break;
4499 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4500 case TYPE_UNKNOWN:
4501 case TYPE_MATCH:
4502 case TYPE_NOMATCH: break; /* avoid gcc warning */
4503 }
4504 }
4505 else
4506 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004507 s1 = get_tv_string_buf(rettv, buf1);
4508 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4510 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4511 else
4512 i = 0;
4513 n1 = FALSE;
4514 switch (type)
4515 {
4516 case TYPE_EQUAL: n1 = (i == 0); break;
4517 case TYPE_NEQUAL: n1 = (i != 0); break;
4518 case TYPE_GREATER: n1 = (i > 0); break;
4519 case TYPE_GEQUAL: n1 = (i >= 0); break;
4520 case TYPE_SMALLER: n1 = (i < 0); break;
4521 case TYPE_SEQUAL: n1 = (i <= 0); break;
4522
4523 case TYPE_MATCH:
4524 case TYPE_NOMATCH:
4525 /* avoid 'l' flag in 'cpoptions' */
4526 save_cpo = p_cpo;
4527 p_cpo = (char_u *)"";
4528 regmatch.regprog = vim_regcomp(s2,
4529 RE_MAGIC + RE_STRING);
4530 regmatch.rm_ic = ic;
4531 if (regmatch.regprog != NULL)
4532 {
4533 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4534 vim_free(regmatch.regprog);
4535 if (type == TYPE_NOMATCH)
4536 n1 = !n1;
4537 }
4538 p_cpo = save_cpo;
4539 break;
4540
4541 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4542 }
4543 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004544 clear_tv(rettv);
4545 clear_tv(&var2);
4546 rettv->v_type = VAR_NUMBER;
4547 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 }
4549 }
4550
4551 return OK;
4552}
4553
4554/*
4555 * Handle fourth level expression:
4556 * + number addition
4557 * - number subtraction
4558 * . string concatenation
4559 *
4560 * "arg" must point to the first non-white of the expression.
4561 * "arg" is advanced to the next non-white after the recognized expression.
4562 *
4563 * Return OK or FAIL.
4564 */
4565 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004566eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 int evaluate;
4570{
Bram Moolenaar33570922005-01-25 22:26:29 +00004571 typval_T var2;
4572 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 int op;
4574 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004575#ifdef FEAT_FLOAT
4576 float_T f1 = 0, f2 = 0;
4577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 char_u *s1, *s2;
4579 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4580 char_u *p;
4581
4582 /*
4583 * Get the first variable.
4584 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004585 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 return FAIL;
4587
4588 /*
4589 * Repeat computing, until no '+', '-' or '.' is following.
4590 */
4591 for (;;)
4592 {
4593 op = **arg;
4594 if (op != '+' && op != '-' && op != '.')
4595 break;
4596
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004597 if ((op != '+' || rettv->v_type != VAR_LIST)
4598#ifdef FEAT_FLOAT
4599 && (op == '.' || rettv->v_type != VAR_FLOAT)
4600#endif
4601 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004602 {
4603 /* For "list + ...", an illegal use of the first operand as
4604 * a number cannot be determined before evaluating the 2nd
4605 * operand: if this is also a list, all is ok.
4606 * For "something . ...", "something - ..." or "non-list + ...",
4607 * we know that the first operand needs to be a string or number
4608 * without evaluating the 2nd operand. So check before to avoid
4609 * side effects after an error. */
4610 if (evaluate && get_tv_string_chk(rettv) == NULL)
4611 {
4612 clear_tv(rettv);
4613 return FAIL;
4614 }
4615 }
4616
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 /*
4618 * Get the second variable.
4619 */
4620 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004621 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004623 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 return FAIL;
4625 }
4626
4627 if (evaluate)
4628 {
4629 /*
4630 * Compute the result.
4631 */
4632 if (op == '.')
4633 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004634 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4635 s2 = get_tv_string_buf_chk(&var2, buf2);
4636 if (s2 == NULL) /* type error ? */
4637 {
4638 clear_tv(rettv);
4639 clear_tv(&var2);
4640 return FAIL;
4641 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004642 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004643 clear_tv(rettv);
4644 rettv->v_type = VAR_STRING;
4645 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004647 else if (op == '+' && rettv->v_type == VAR_LIST
4648 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004649 {
4650 /* concatenate Lists */
4651 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4652 &var3) == FAIL)
4653 {
4654 clear_tv(rettv);
4655 clear_tv(&var2);
4656 return FAIL;
4657 }
4658 clear_tv(rettv);
4659 *rettv = var3;
4660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 else
4662 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004663 int error = FALSE;
4664
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004665#ifdef FEAT_FLOAT
4666 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004667 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004668 f1 = rettv->vval.v_float;
4669 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004670 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004671 else
4672#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004673 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004674 n1 = get_tv_number_chk(rettv, &error);
4675 if (error)
4676 {
4677 /* This can only happen for "list + non-list". For
4678 * "non-list + ..." or "something - ...", we returned
4679 * before evaluating the 2nd operand. */
4680 clear_tv(rettv);
4681 return FAIL;
4682 }
4683#ifdef FEAT_FLOAT
4684 if (var2.v_type == VAR_FLOAT)
4685 f1 = n1;
4686#endif
4687 }
4688#ifdef FEAT_FLOAT
4689 if (var2.v_type == VAR_FLOAT)
4690 {
4691 f2 = var2.vval.v_float;
4692 n2 = 0;
4693 }
4694 else
4695#endif
4696 {
4697 n2 = get_tv_number_chk(&var2, &error);
4698 if (error)
4699 {
4700 clear_tv(rettv);
4701 clear_tv(&var2);
4702 return FAIL;
4703 }
4704#ifdef FEAT_FLOAT
4705 if (rettv->v_type == VAR_FLOAT)
4706 f2 = n2;
4707#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004708 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004709 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004710
4711#ifdef FEAT_FLOAT
4712 /* If there is a float on either side the result is a float. */
4713 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4714 {
4715 if (op == '+')
4716 f1 = f1 + f2;
4717 else
4718 f1 = f1 - f2;
4719 rettv->v_type = VAR_FLOAT;
4720 rettv->vval.v_float = f1;
4721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004723#endif
4724 {
4725 if (op == '+')
4726 n1 = n1 + n2;
4727 else
4728 n1 = n1 - n2;
4729 rettv->v_type = VAR_NUMBER;
4730 rettv->vval.v_number = n1;
4731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004733 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 }
4735 }
4736 return OK;
4737}
4738
4739/*
4740 * Handle fifth level expression:
4741 * * number multiplication
4742 * / number division
4743 * % number modulo
4744 *
4745 * "arg" must point to the first non-white of the expression.
4746 * "arg" is advanced to the next non-white after the recognized expression.
4747 *
4748 * Return OK or FAIL.
4749 */
4750 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004751eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004755 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756{
Bram Moolenaar33570922005-01-25 22:26:29 +00004757 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 int op;
4759 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004760#ifdef FEAT_FLOAT
4761 int use_float = FALSE;
4762 float_T f1 = 0, f2;
4763#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004764 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765
4766 /*
4767 * Get the first variable.
4768 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004769 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 return FAIL;
4771
4772 /*
4773 * Repeat computing, until no '*', '/' or '%' is following.
4774 */
4775 for (;;)
4776 {
4777 op = **arg;
4778 if (op != '*' && op != '/' && op != '%')
4779 break;
4780
4781 if (evaluate)
4782 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004783#ifdef FEAT_FLOAT
4784 if (rettv->v_type == VAR_FLOAT)
4785 {
4786 f1 = rettv->vval.v_float;
4787 use_float = TRUE;
4788 n1 = 0;
4789 }
4790 else
4791#endif
4792 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004793 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 if (error)
4795 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
4797 else
4798 n1 = 0;
4799
4800 /*
4801 * Get the second variable.
4802 */
4803 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004804 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 return FAIL;
4806
4807 if (evaluate)
4808 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004809#ifdef FEAT_FLOAT
4810 if (var2.v_type == VAR_FLOAT)
4811 {
4812 if (!use_float)
4813 {
4814 f1 = n1;
4815 use_float = TRUE;
4816 }
4817 f2 = var2.vval.v_float;
4818 n2 = 0;
4819 }
4820 else
4821#endif
4822 {
4823 n2 = get_tv_number_chk(&var2, &error);
4824 clear_tv(&var2);
4825 if (error)
4826 return FAIL;
4827#ifdef FEAT_FLOAT
4828 if (use_float)
4829 f2 = n2;
4830#endif
4831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832
4833 /*
4834 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004835 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004837#ifdef FEAT_FLOAT
4838 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004840 if (op == '*')
4841 f1 = f1 * f2;
4842 else if (op == '/')
4843 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004844# ifdef VMS
4845 /* VMS crashes on divide by zero, work around it */
4846 if (f2 == 0.0)
4847 {
4848 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004849 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004850 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004851 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004852 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004853 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004854 }
4855 else
4856 f1 = f1 / f2;
4857# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004858 /* We rely on the floating point library to handle divide
4859 * by zero to result in "inf" and not a crash. */
4860 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004861# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004864 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004865 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004866 return FAIL;
4867 }
4868 rettv->v_type = VAR_FLOAT;
4869 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 }
4871 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004874 if (op == '*')
4875 n1 = n1 * n2;
4876 else if (op == '/')
4877 {
4878 if (n2 == 0) /* give an error message? */
4879 {
4880 if (n1 == 0)
4881 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4882 else if (n1 < 0)
4883 n1 = -0x7fffffffL;
4884 else
4885 n1 = 0x7fffffffL;
4886 }
4887 else
4888 n1 = n1 / n2;
4889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004891 {
4892 if (n2 == 0) /* give an error message? */
4893 n1 = 0;
4894 else
4895 n1 = n1 % n2;
4896 }
4897 rettv->v_type = VAR_NUMBER;
4898 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 }
4901 }
4902
4903 return OK;
4904}
4905
4906/*
4907 * Handle sixth level expression:
4908 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004909 * "string" string constant
4910 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 * &option-name option value
4912 * @r register contents
4913 * identifier variable value
4914 * function() function call
4915 * $VAR environment variable
4916 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004917 * [expr, expr] List
4918 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 *
4920 * Also handle:
4921 * ! in front logical NOT
4922 * - in front unary minus
4923 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004924 * trailing [] subscript in String or List
4925 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 *
4927 * "arg" must point to the first non-white of the expression.
4928 * "arg" is advanced to the next non-white after the recognized expression.
4929 *
4930 * Return OK or FAIL.
4931 */
4932 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004933eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004937 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 long n;
4940 int len;
4941 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 char_u *start_leader, *end_leader;
4943 int ret = OK;
4944 char_u *alias;
4945
4946 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004947 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004948 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004950 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951
4952 /*
4953 * Skip '!' and '-' characters. They are handled later.
4954 */
4955 start_leader = *arg;
4956 while (**arg == '!' || **arg == '-' || **arg == '+')
4957 *arg = skipwhite(*arg + 1);
4958 end_leader = *arg;
4959
4960 switch (**arg)
4961 {
4962 /*
4963 * Number constant.
4964 */
4965 case '0':
4966 case '1':
4967 case '2':
4968 case '3':
4969 case '4':
4970 case '5':
4971 case '6':
4972 case '7':
4973 case '8':
4974 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004975 {
4976#ifdef FEAT_FLOAT
4977 char_u *p = skipdigits(*arg + 1);
4978 int get_float = FALSE;
4979
4980 /* We accept a float when the format matches
4981 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004982 * strict to avoid backwards compatibility problems.
4983 * Don't look for a float after the "." operator, so that
4984 * ":let vers = 1.2.3" doesn't fail. */
4985 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004987 get_float = TRUE;
4988 p = skipdigits(p + 2);
4989 if (*p == 'e' || *p == 'E')
4990 {
4991 ++p;
4992 if (*p == '-' || *p == '+')
4993 ++p;
4994 if (!vim_isdigit(*p))
4995 get_float = FALSE;
4996 else
4997 p = skipdigits(p + 1);
4998 }
4999 if (ASCII_ISALPHA(*p) || *p == '.')
5000 get_float = FALSE;
5001 }
5002 if (get_float)
5003 {
5004 float_T f;
5005
5006 *arg += string2float(*arg, &f);
5007 if (evaluate)
5008 {
5009 rettv->v_type = VAR_FLOAT;
5010 rettv->vval.v_float = f;
5011 }
5012 }
5013 else
5014#endif
5015 {
5016 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5017 *arg += len;
5018 if (evaluate)
5019 {
5020 rettv->v_type = VAR_NUMBER;
5021 rettv->vval.v_number = n;
5022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
5024 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026
5027 /*
5028 * String constant: "string".
5029 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005030 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 break;
5032
5033 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005034 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005036 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005037 break;
5038
5039 /*
5040 * List: [expr, expr]
5041 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005042 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 break;
5044
5045 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 * Dictionary: {key: val, key: val}
5047 */
5048 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5049 break;
5050
5051 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005052 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005054 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 break;
5056
5057 /*
5058 * Environment variable: $VAR.
5059 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005060 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 break;
5062
5063 /*
5064 * Register contents: @r.
5065 */
5066 case '@': ++*arg;
5067 if (evaluate)
5068 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005069 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005070 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 if (**arg != NUL)
5073 ++*arg;
5074 break;
5075
5076 /*
5077 * nested expression: (expression).
5078 */
5079 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005080 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 if (**arg == ')')
5082 ++*arg;
5083 else if (ret == OK)
5084 {
5085 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005086 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 ret = FAIL;
5088 }
5089 break;
5090
Bram Moolenaar8c711452005-01-14 21:53:12 +00005091 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 break;
5093 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094
5095 if (ret == NOTDONE)
5096 {
5097 /*
5098 * Must be a variable or function name.
5099 * Can also be a curly-braces kind of name: {expr}.
5100 */
5101 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005102 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005103 if (alias != NULL)
5104 s = alias;
5105
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005106 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005107 ret = FAIL;
5108 else
5109 {
5110 if (**arg == '(') /* recursive! */
5111 {
5112 /* If "s" is the name of a variable of type VAR_FUNC
5113 * use its contents. */
5114 s = deref_func_name(s, &len);
5115
5116 /* Invoke the function. */
5117 ret = get_func_tv(s, len, rettv, arg,
5118 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005119 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005120 /* Stop the expression evaluation when immediately
5121 * aborting on error, or when an interrupt occurred or
5122 * an exception was thrown but not caught. */
5123 if (aborting())
5124 {
5125 if (ret == OK)
5126 clear_tv(rettv);
5127 ret = FAIL;
5128 }
5129 }
5130 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005131 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005132 else
5133 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005134 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005135 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005136 }
5137
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 *arg = skipwhite(*arg);
5139
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005140 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5141 * expr(expr). */
5142 if (ret == OK)
5143 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144
5145 /*
5146 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5147 */
5148 if (ret == OK && evaluate && end_leader > start_leader)
5149 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005150 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005151 int val = 0;
5152#ifdef FEAT_FLOAT
5153 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005154
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005155 if (rettv->v_type == VAR_FLOAT)
5156 f = rettv->vval.v_float;
5157 else
5158#endif
5159 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005160 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005162 clear_tv(rettv);
5163 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005165 else
5166 {
5167 while (end_leader > start_leader)
5168 {
5169 --end_leader;
5170 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005171 {
5172#ifdef FEAT_FLOAT
5173 if (rettv->v_type == VAR_FLOAT)
5174 f = !f;
5175 else
5176#endif
5177 val = !val;
5178 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005179 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005180 {
5181#ifdef FEAT_FLOAT
5182 if (rettv->v_type == VAR_FLOAT)
5183 f = -f;
5184 else
5185#endif
5186 val = -val;
5187 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005188 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005189#ifdef FEAT_FLOAT
5190 if (rettv->v_type == VAR_FLOAT)
5191 {
5192 clear_tv(rettv);
5193 rettv->vval.v_float = f;
5194 }
5195 else
5196#endif
5197 {
5198 clear_tv(rettv);
5199 rettv->v_type = VAR_NUMBER;
5200 rettv->vval.v_number = val;
5201 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 }
5204
5205 return ret;
5206}
5207
5208/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005209 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5210 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005211 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5212 */
5213 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005214eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005215 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005216 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005217 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005218 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005219{
5220 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005221 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005222 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 long len = -1;
5224 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005225 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005227
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005228 if (rettv->v_type == VAR_FUNC
5229#ifdef FEAT_FLOAT
5230 || rettv->v_type == VAR_FLOAT
5231#endif
5232 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005233 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005234 if (verbose)
5235 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005236 return FAIL;
5237 }
5238
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005240 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005241 /*
5242 * dict.name
5243 */
5244 key = *arg + 1;
5245 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5246 ;
5247 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005248 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 }
5251 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253 /*
5254 * something[idx]
5255 *
5256 * Get the (first) variable from inside the [].
5257 */
5258 *arg = skipwhite(*arg + 1);
5259 if (**arg == ':')
5260 empty1 = TRUE;
5261 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5262 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005263 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5264 {
5265 /* not a number or string */
5266 clear_tv(&var1);
5267 return FAIL;
5268 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269
5270 /*
5271 * Get the second variable from inside the [:].
5272 */
5273 if (**arg == ':')
5274 {
5275 range = TRUE;
5276 *arg = skipwhite(*arg + 1);
5277 if (**arg == ']')
5278 empty2 = TRUE;
5279 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005281 if (!empty1)
5282 clear_tv(&var1);
5283 return FAIL;
5284 }
5285 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5286 {
5287 /* not a number or string */
5288 if (!empty1)
5289 clear_tv(&var1);
5290 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 return FAIL;
5292 }
5293 }
5294
5295 /* Check for the ']'. */
5296 if (**arg != ']')
5297 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005298 if (verbose)
5299 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005300 clear_tv(&var1);
5301 if (range)
5302 clear_tv(&var2);
5303 return FAIL;
5304 }
5305 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306 }
5307
5308 if (evaluate)
5309 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005310 n1 = 0;
5311 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005313 n1 = get_tv_number(&var1);
5314 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005315 }
5316 if (range)
5317 {
5318 if (empty2)
5319 n2 = -1;
5320 else
5321 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005322 n2 = get_tv_number(&var2);
5323 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324 }
5325 }
5326
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005327 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328 {
5329 case VAR_NUMBER:
5330 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005331 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005332 len = (long)STRLEN(s);
5333 if (range)
5334 {
5335 /* The resulting variable is a substring. If the indexes
5336 * are out of range the result is empty. */
5337 if (n1 < 0)
5338 {
5339 n1 = len + n1;
5340 if (n1 < 0)
5341 n1 = 0;
5342 }
5343 if (n2 < 0)
5344 n2 = len + n2;
5345 else if (n2 >= len)
5346 n2 = len;
5347 if (n1 >= len || n2 < 0 || n1 > n2)
5348 s = NULL;
5349 else
5350 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5351 }
5352 else
5353 {
5354 /* The resulting variable is a string of a single
5355 * character. If the index is too big or negative the
5356 * result is empty. */
5357 if (n1 >= len || n1 < 0)
5358 s = NULL;
5359 else
5360 s = vim_strnsave(s + n1, 1);
5361 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005362 clear_tv(rettv);
5363 rettv->v_type = VAR_STRING;
5364 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 break;
5366
5367 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005368 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 if (n1 < 0)
5370 n1 = len + n1;
5371 if (!empty1 && (n1 < 0 || n1 >= len))
5372 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005373 /* For a range we allow invalid values and return an empty
5374 * list. A list index out of range is an error. */
5375 if (!range)
5376 {
5377 if (verbose)
5378 EMSGN(_(e_listidx), n1);
5379 return FAIL;
5380 }
5381 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 }
5383 if (range)
5384 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005385 list_T *l;
5386 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387
5388 if (n2 < 0)
5389 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005390 else if (n2 >= len)
5391 n2 = len - 1;
5392 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005393 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 l = list_alloc();
5395 if (l == NULL)
5396 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005397 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 n1 <= n2; ++n1)
5399 {
5400 if (list_append_tv(l, &item->li_tv) == FAIL)
5401 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005402 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 return FAIL;
5404 }
5405 item = item->li_next;
5406 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005407 clear_tv(rettv);
5408 rettv->v_type = VAR_LIST;
5409 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005410 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411 }
5412 else
5413 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005414 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 clear_tv(rettv);
5416 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417 }
5418 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005419
5420 case VAR_DICT:
5421 if (range)
5422 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005423 if (verbose)
5424 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005425 if (len == -1)
5426 clear_tv(&var1);
5427 return FAIL;
5428 }
5429 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005430 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005431
5432 if (len == -1)
5433 {
5434 key = get_tv_string(&var1);
5435 if (*key == NUL)
5436 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005437 if (verbose)
5438 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005439 clear_tv(&var1);
5440 return FAIL;
5441 }
5442 }
5443
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005444 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005446 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005447 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005448 if (len == -1)
5449 clear_tv(&var1);
5450 if (item == NULL)
5451 return FAIL;
5452
5453 copy_tv(&item->di_tv, &var1);
5454 clear_tv(rettv);
5455 *rettv = var1;
5456 }
5457 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 }
5459 }
5460
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 return OK;
5462}
5463
5464/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 * Get an option value.
5466 * "arg" points to the '&' or '+' before the option name.
5467 * "arg" is advanced to character after the option name.
5468 * Return OK or FAIL.
5469 */
5470 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005471get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005473 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 int evaluate;
5475{
5476 char_u *option_end;
5477 long numval;
5478 char_u *stringval;
5479 int opt_type;
5480 int c;
5481 int working = (**arg == '+'); /* has("+option") */
5482 int ret = OK;
5483 int opt_flags;
5484
5485 /*
5486 * Isolate the option name and find its value.
5487 */
5488 option_end = find_option_end(arg, &opt_flags);
5489 if (option_end == NULL)
5490 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005491 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 EMSG2(_("E112: Option name missing: %s"), *arg);
5493 return FAIL;
5494 }
5495
5496 if (!evaluate)
5497 {
5498 *arg = option_end;
5499 return OK;
5500 }
5501
5502 c = *option_end;
5503 *option_end = NUL;
5504 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005505 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506
5507 if (opt_type == -3) /* invalid name */
5508 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005509 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 EMSG2(_("E113: Unknown option: %s"), *arg);
5511 ret = FAIL;
5512 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005513 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 {
5515 if (opt_type == -2) /* hidden string option */
5516 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005517 rettv->v_type = VAR_STRING;
5518 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 }
5520 else if (opt_type == -1) /* hidden number option */
5521 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005522 rettv->v_type = VAR_NUMBER;
5523 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 }
5525 else if (opt_type == 1) /* number option */
5526 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005527 rettv->v_type = VAR_NUMBER;
5528 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 }
5530 else /* string option */
5531 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005532 rettv->v_type = VAR_STRING;
5533 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 }
5535 }
5536 else if (working && (opt_type == -2 || opt_type == -1))
5537 ret = FAIL;
5538
5539 *option_end = c; /* put back for error messages */
5540 *arg = option_end;
5541
5542 return ret;
5543}
5544
5545/*
5546 * Allocate a variable for a string constant.
5547 * Return OK or FAIL.
5548 */
5549 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 int evaluate;
5554{
5555 char_u *p;
5556 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 int extra = 0;
5558
5559 /*
5560 * Find the end of the string, skipping backslashed characters.
5561 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005562 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 {
5564 if (*p == '\\' && p[1] != NUL)
5565 {
5566 ++p;
5567 /* A "\<x>" form occupies at least 4 characters, and produces up
5568 * to 6 characters: reserve space for 2 extra */
5569 if (*p == '<')
5570 extra += 2;
5571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 }
5573
5574 if (*p != '"')
5575 {
5576 EMSG2(_("E114: Missing quote: %s"), *arg);
5577 return FAIL;
5578 }
5579
5580 /* If only parsing, set *arg and return here */
5581 if (!evaluate)
5582 {
5583 *arg = p + 1;
5584 return OK;
5585 }
5586
5587 /*
5588 * Copy the string into allocated memory, handling backslashed
5589 * characters.
5590 */
5591 name = alloc((unsigned)(p - *arg + extra));
5592 if (name == NULL)
5593 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005594 rettv->v_type = VAR_STRING;
5595 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596
Bram Moolenaar8c711452005-01-14 21:53:12 +00005597 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 {
5599 if (*p == '\\')
5600 {
5601 switch (*++p)
5602 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005603 case 'b': *name++ = BS; ++p; break;
5604 case 'e': *name++ = ESC; ++p; break;
5605 case 'f': *name++ = FF; ++p; break;
5606 case 'n': *name++ = NL; ++p; break;
5607 case 'r': *name++ = CAR; ++p; break;
5608 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609
5610 case 'X': /* hex: "\x1", "\x12" */
5611 case 'x':
5612 case 'u': /* Unicode: "\u0023" */
5613 case 'U':
5614 if (vim_isxdigit(p[1]))
5615 {
5616 int n, nr;
5617 int c = toupper(*p);
5618
5619 if (c == 'X')
5620 n = 2;
5621 else
5622 n = 4;
5623 nr = 0;
5624 while (--n >= 0 && vim_isxdigit(p[1]))
5625 {
5626 ++p;
5627 nr = (nr << 4) + hex2nr(*p);
5628 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005629 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630#ifdef FEAT_MBYTE
5631 /* For "\u" store the number according to
5632 * 'encoding'. */
5633 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005634 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 else
5636#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005637 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 break;
5640
5641 /* octal: "\1", "\12", "\123" */
5642 case '0':
5643 case '1':
5644 case '2':
5645 case '3':
5646 case '4':
5647 case '5':
5648 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005649 case '7': *name = *p++ - '0';
5650 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005652 *name = (*name << 3) + *p++ - '0';
5653 if (*p >= '0' && *p <= '7')
5654 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005656 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 break;
5658
5659 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005660 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 if (extra != 0)
5662 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005663 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 break;
5665 }
5666 /* FALLTHROUGH */
5667
Bram Moolenaar8c711452005-01-14 21:53:12 +00005668 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 break;
5670 }
5671 }
5672 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005673 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005676 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 *arg = p + 1;
5678
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 return OK;
5680}
5681
5682/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005683 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 * Return OK or FAIL.
5685 */
5686 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005687get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005689 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 int evaluate;
5691{
5692 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005693 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005694 int reduce = 0;
5695
5696 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005697 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005698 */
5699 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5700 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005702 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005703 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005704 break;
5705 ++reduce;
5706 ++p;
5707 }
5708 }
5709
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 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005713 return FAIL;
5714 }
5715
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005717 if (!evaluate)
5718 {
5719 *arg = p + 1;
5720 return OK;
5721 }
5722
5723 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005725 */
5726 str = alloc((unsigned)((p - *arg) - reduce));
5727 if (str == NULL)
5728 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005729 rettv->v_type = VAR_STRING;
5730 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005731
Bram Moolenaar8c711452005-01-14 21:53:12 +00005732 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005733 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005734 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005735 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005736 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005737 break;
5738 ++p;
5739 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005741 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005743 *arg = p + 1;
5744
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005745 return OK;
5746}
5747
5748/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005749 * Allocate a variable for a List and fill it from "*arg".
5750 * Return OK or FAIL.
5751 */
5752 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005753get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005754 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005755 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005756 int evaluate;
5757{
Bram Moolenaar33570922005-01-25 22:26:29 +00005758 list_T *l = NULL;
5759 typval_T tv;
5760 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005761
5762 if (evaluate)
5763 {
5764 l = list_alloc();
5765 if (l == NULL)
5766 return FAIL;
5767 }
5768
5769 *arg = skipwhite(*arg + 1);
5770 while (**arg != ']' && **arg != NUL)
5771 {
5772 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5773 goto failret;
5774 if (evaluate)
5775 {
5776 item = listitem_alloc();
5777 if (item != NULL)
5778 {
5779 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005780 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005781 list_append(l, item);
5782 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005783 else
5784 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005785 }
5786
5787 if (**arg == ']')
5788 break;
5789 if (**arg != ',')
5790 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005792 goto failret;
5793 }
5794 *arg = skipwhite(*arg + 1);
5795 }
5796
5797 if (**arg != ']')
5798 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005799 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005800failret:
5801 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005802 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803 return FAIL;
5804 }
5805
5806 *arg = skipwhite(*arg + 1);
5807 if (evaluate)
5808 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005809 rettv->v_type = VAR_LIST;
5810 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811 ++l->lv_refcount;
5812 }
5813
5814 return OK;
5815}
5816
5817/*
5818 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005819 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005820 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005821 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005822list_alloc()
5823{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005824 list_T *l;
5825
5826 l = (list_T *)alloc_clear(sizeof(list_T));
5827 if (l != NULL)
5828 {
5829 /* Prepend the list to the list of lists for garbage collection. */
5830 if (first_list != NULL)
5831 first_list->lv_used_prev = l;
5832 l->lv_used_prev = NULL;
5833 l->lv_used_next = first_list;
5834 first_list = l;
5835 }
5836 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005837}
5838
5839/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005840 * Allocate an empty list for a return value.
5841 * Returns OK or FAIL.
5842 */
5843 static int
5844rettv_list_alloc(rettv)
5845 typval_T *rettv;
5846{
5847 list_T *l = list_alloc();
5848
5849 if (l == NULL)
5850 return FAIL;
5851
5852 rettv->vval.v_list = l;
5853 rettv->v_type = VAR_LIST;
5854 ++l->lv_refcount;
5855 return OK;
5856}
5857
5858/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005859 * Unreference a list: decrement the reference count and free it when it
5860 * becomes zero.
5861 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005862 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005863list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005864 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005866 if (l != NULL && --l->lv_refcount <= 0)
5867 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005868}
5869
5870/*
5871 * Free a list, including all items it points to.
5872 * Ignores the reference count.
5873 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005874 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005875list_free(l, recurse)
5876 list_T *l;
5877 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878{
Bram Moolenaar33570922005-01-25 22:26:29 +00005879 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005881 /* Remove the list from the list of lists for garbage collection. */
5882 if (l->lv_used_prev == NULL)
5883 first_list = l->lv_used_next;
5884 else
5885 l->lv_used_prev->lv_used_next = l->lv_used_next;
5886 if (l->lv_used_next != NULL)
5887 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5888
Bram Moolenaard9fba312005-06-26 22:34:35 +00005889 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005891 /* Remove the item before deleting it. */
5892 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005893 if (recurse || (item->li_tv.v_type != VAR_LIST
5894 && item->li_tv.v_type != VAR_DICT))
5895 clear_tv(&item->li_tv);
5896 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005897 }
5898 vim_free(l);
5899}
5900
5901/*
5902 * Allocate a list item.
5903 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005904 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005905listitem_alloc()
5906{
Bram Moolenaar33570922005-01-25 22:26:29 +00005907 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908}
5909
5910/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005911 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912 */
5913 static void
5914listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005915 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005917 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918 vim_free(item);
5919}
5920
5921/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005922 * Remove a list item from a List and free it. Also clears the value.
5923 */
5924 static void
5925listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 list_T *l;
5927 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005928{
5929 list_remove(l, item, item);
5930 listitem_free(item);
5931}
5932
5933/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005934 * Get the number of items in a list.
5935 */
5936 static long
5937list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005938 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 if (l == NULL)
5941 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005942 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005943}
5944
5945/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005946 * Return TRUE when two lists have exactly the same values.
5947 */
5948 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005949list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005950 list_T *l1;
5951 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005952 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005953 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005954{
Bram Moolenaar33570922005-01-25 22:26:29 +00005955 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005956
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005957 if (l1 == NULL || l2 == NULL)
5958 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005959 if (l1 == l2)
5960 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005961 if (list_len(l1) != list_len(l2))
5962 return FALSE;
5963
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005964 for (item1 = l1->lv_first, item2 = l2->lv_first;
5965 item1 != NULL && item2 != NULL;
5966 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005967 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005968 return FALSE;
5969 return item1 == NULL && item2 == NULL;
5970}
5971
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005972#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5973 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005974/*
5975 * Return the dictitem that an entry in a hashtable points to.
5976 */
5977 dictitem_T *
5978dict_lookup(hi)
5979 hashitem_T *hi;
5980{
5981 return HI2DI(hi);
5982}
5983#endif
5984
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005985/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005986 * Return TRUE when two dictionaries have exactly the same key/values.
5987 */
5988 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005989dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005990 dict_T *d1;
5991 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005992 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005993 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005994{
Bram Moolenaar33570922005-01-25 22:26:29 +00005995 hashitem_T *hi;
5996 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005997 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005998
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005999 if (d1 == NULL || d2 == NULL)
6000 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006001 if (d1 == d2)
6002 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006003 if (dict_len(d1) != dict_len(d2))
6004 return FALSE;
6005
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006006 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006007 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006008 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006009 if (!HASHITEM_EMPTY(hi))
6010 {
6011 item2 = dict_find(d2, hi->hi_key, -1);
6012 if (item2 == NULL)
6013 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006014 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006015 return FALSE;
6016 --todo;
6017 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006018 }
6019 return TRUE;
6020}
6021
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006022static int tv_equal_recurse_limit;
6023
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006024/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006025 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006026 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006027 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006028 */
6029 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006030tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006031 typval_T *tv1;
6032 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006033 int ic; /* ignore case */
6034 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006035{
6036 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006037 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006038 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006039 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006040
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006041 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006042 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006043
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006044 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006045 * recursiveness to a limit. We guess they are equal then.
6046 * A fixed limit has the problem of still taking an awful long time.
6047 * Reduce the limit every time running into it. That should work fine for
6048 * deeply linked structures that are not recursively linked and catch
6049 * recursiveness quickly. */
6050 if (!recursive)
6051 tv_equal_recurse_limit = 1000;
6052 if (recursive_cnt >= tv_equal_recurse_limit)
6053 {
6054 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006055 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006056 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006057
6058 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006059 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006060 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006061 ++recursive_cnt;
6062 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6063 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006064 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006065
6066 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006067 ++recursive_cnt;
6068 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6069 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006070 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006071
6072 case VAR_FUNC:
6073 return (tv1->vval.v_string != NULL
6074 && tv2->vval.v_string != NULL
6075 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6076
6077 case VAR_NUMBER:
6078 return tv1->vval.v_number == tv2->vval.v_number;
6079
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006080#ifdef FEAT_FLOAT
6081 case VAR_FLOAT:
6082 return tv1->vval.v_float == tv2->vval.v_float;
6083#endif
6084
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006085 case VAR_STRING:
6086 s1 = get_tv_string_buf(tv1, buf1);
6087 s2 = get_tv_string_buf(tv2, buf2);
6088 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006089 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006090
6091 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006092 return TRUE;
6093}
6094
6095/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096 * Locate item with index "n" in list "l" and return it.
6097 * A negative index is counted from the end; -1 is the last item.
6098 * Returns NULL when "n" is out of range.
6099 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006100 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006101list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006102 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006103 long n;
6104{
Bram Moolenaar33570922005-01-25 22:26:29 +00006105 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006106 long idx;
6107
6108 if (l == NULL)
6109 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006110
6111 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006112 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006113 n = l->lv_len + n;
6114
6115 /* Check for index out of range. */
6116 if (n < 0 || n >= l->lv_len)
6117 return NULL;
6118
6119 /* When there is a cached index may start search from there. */
6120 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006121 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006122 if (n < l->lv_idx / 2)
6123 {
6124 /* closest to the start of the list */
6125 item = l->lv_first;
6126 idx = 0;
6127 }
6128 else if (n > (l->lv_idx + l->lv_len) / 2)
6129 {
6130 /* closest to the end of the list */
6131 item = l->lv_last;
6132 idx = l->lv_len - 1;
6133 }
6134 else
6135 {
6136 /* closest to the cached index */
6137 item = l->lv_idx_item;
6138 idx = l->lv_idx;
6139 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006140 }
6141 else
6142 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006143 if (n < l->lv_len / 2)
6144 {
6145 /* closest to the start of the list */
6146 item = l->lv_first;
6147 idx = 0;
6148 }
6149 else
6150 {
6151 /* closest to the end of the list */
6152 item = l->lv_last;
6153 idx = l->lv_len - 1;
6154 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006155 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006156
6157 while (n > idx)
6158 {
6159 /* search forward */
6160 item = item->li_next;
6161 ++idx;
6162 }
6163 while (n < idx)
6164 {
6165 /* search backward */
6166 item = item->li_prev;
6167 --idx;
6168 }
6169
6170 /* cache the used index */
6171 l->lv_idx = idx;
6172 l->lv_idx_item = item;
6173
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006174 return item;
6175}
6176
6177/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006178 * Get list item "l[idx]" as a number.
6179 */
6180 static long
6181list_find_nr(l, idx, errorp)
6182 list_T *l;
6183 long idx;
6184 int *errorp; /* set to TRUE when something wrong */
6185{
6186 listitem_T *li;
6187
6188 li = list_find(l, idx);
6189 if (li == NULL)
6190 {
6191 if (errorp != NULL)
6192 *errorp = TRUE;
6193 return -1L;
6194 }
6195 return get_tv_number_chk(&li->li_tv, errorp);
6196}
6197
6198/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006199 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6200 */
6201 char_u *
6202list_find_str(l, idx)
6203 list_T *l;
6204 long idx;
6205{
6206 listitem_T *li;
6207
6208 li = list_find(l, idx - 1);
6209 if (li == NULL)
6210 {
6211 EMSGN(_(e_listidx), idx);
6212 return NULL;
6213 }
6214 return get_tv_string(&li->li_tv);
6215}
6216
6217/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006218 * Locate "item" list "l" and return its index.
6219 * Returns -1 when "item" is not in the list.
6220 */
6221 static long
6222list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006223 list_T *l;
6224 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006225{
6226 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006227 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006228
6229 if (l == NULL)
6230 return -1;
6231 idx = 0;
6232 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6233 ++idx;
6234 if (li == NULL)
6235 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006236 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006237}
6238
6239/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006240 * Append item "item" to the end of list "l".
6241 */
6242 static void
6243list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006244 list_T *l;
6245 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006246{
6247 if (l->lv_last == NULL)
6248 {
6249 /* empty list */
6250 l->lv_first = item;
6251 l->lv_last = item;
6252 item->li_prev = NULL;
6253 }
6254 else
6255 {
6256 l->lv_last->li_next = item;
6257 item->li_prev = l->lv_last;
6258 l->lv_last = item;
6259 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006260 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261 item->li_next = NULL;
6262}
6263
6264/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006265 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006266 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006267 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006268 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006269list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006270 list_T *l;
6271 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006272{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006273 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006274
Bram Moolenaar05159a02005-02-26 23:04:13 +00006275 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006276 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006277 copy_tv(tv, &li->li_tv);
6278 list_append(l, li);
6279 return OK;
6280}
6281
6282/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006283 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006284 * Return FAIL when out of memory.
6285 */
6286 int
6287list_append_dict(list, dict)
6288 list_T *list;
6289 dict_T *dict;
6290{
6291 listitem_T *li = listitem_alloc();
6292
6293 if (li == NULL)
6294 return FAIL;
6295 li->li_tv.v_type = VAR_DICT;
6296 li->li_tv.v_lock = 0;
6297 li->li_tv.vval.v_dict = dict;
6298 list_append(list, li);
6299 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006300 return OK;
6301}
6302
6303/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006304 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006305 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006306 * Returns FAIL when out of memory.
6307 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006308 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006309list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006310 list_T *l;
6311 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006312 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006313{
6314 listitem_T *li = listitem_alloc();
6315
6316 if (li == NULL)
6317 return FAIL;
6318 list_append(l, li);
6319 li->li_tv.v_type = VAR_STRING;
6320 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006321 if (str == NULL)
6322 li->li_tv.vval.v_string = NULL;
6323 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006324 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006325 return FAIL;
6326 return OK;
6327}
6328
6329/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006330 * Append "n" to list "l".
6331 * Returns FAIL when out of memory.
6332 */
6333 static int
6334list_append_number(l, n)
6335 list_T *l;
6336 varnumber_T n;
6337{
6338 listitem_T *li;
6339
6340 li = listitem_alloc();
6341 if (li == NULL)
6342 return FAIL;
6343 li->li_tv.v_type = VAR_NUMBER;
6344 li->li_tv.v_lock = 0;
6345 li->li_tv.vval.v_number = n;
6346 list_append(l, li);
6347 return OK;
6348}
6349
6350/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006351 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006352 * If "item" is NULL append at the end.
6353 * Return FAIL when out of memory.
6354 */
6355 static int
6356list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006357 list_T *l;
6358 typval_T *tv;
6359 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006360{
Bram Moolenaar33570922005-01-25 22:26:29 +00006361 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006362
6363 if (ni == NULL)
6364 return FAIL;
6365 copy_tv(tv, &ni->li_tv);
6366 if (item == NULL)
6367 /* Append new item at end of list. */
6368 list_append(l, ni);
6369 else
6370 {
6371 /* Insert new item before existing item. */
6372 ni->li_prev = item->li_prev;
6373 ni->li_next = item;
6374 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006375 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006376 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006377 ++l->lv_idx;
6378 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006379 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006380 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006381 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006382 l->lv_idx_item = NULL;
6383 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006384 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006385 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006386 }
6387 return OK;
6388}
6389
6390/*
6391 * Extend "l1" with "l2".
6392 * If "bef" is NULL append at the end, otherwise insert before this item.
6393 * Returns FAIL when out of memory.
6394 */
6395 static int
6396list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006397 list_T *l1;
6398 list_T *l2;
6399 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006400{
Bram Moolenaar33570922005-01-25 22:26:29 +00006401 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006402 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006404 /* We also quit the loop when we have inserted the original item count of
6405 * the list, avoid a hang when we extend a list with itself. */
6406 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006407 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6408 return FAIL;
6409 return OK;
6410}
6411
6412/*
6413 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6414 * Return FAIL when out of memory.
6415 */
6416 static int
6417list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006418 list_T *l1;
6419 list_T *l2;
6420 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006421{
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006423
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006424 if (l1 == NULL || l2 == NULL)
6425 return FAIL;
6426
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006427 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006428 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 if (l == NULL)
6430 return FAIL;
6431 tv->v_type = VAR_LIST;
6432 tv->vval.v_list = l;
6433
6434 /* append all items from the second list */
6435 return list_extend(l, l2, NULL);
6436}
6437
6438/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006439 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006440 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006441 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006442 * Returns NULL when out of memory.
6443 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006444 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006445list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006446 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006447 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006448 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006449{
Bram Moolenaar33570922005-01-25 22:26:29 +00006450 list_T *copy;
6451 listitem_T *item;
6452 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006453
6454 if (orig == NULL)
6455 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006456
6457 copy = list_alloc();
6458 if (copy != NULL)
6459 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006460 if (copyID != 0)
6461 {
6462 /* Do this before adding the items, because one of the items may
6463 * refer back to this list. */
6464 orig->lv_copyID = copyID;
6465 orig->lv_copylist = copy;
6466 }
6467 for (item = orig->lv_first; item != NULL && !got_int;
6468 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006469 {
6470 ni = listitem_alloc();
6471 if (ni == NULL)
6472 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006473 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006474 {
6475 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6476 {
6477 vim_free(ni);
6478 break;
6479 }
6480 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006481 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006482 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006483 list_append(copy, ni);
6484 }
6485 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006486 if (item != NULL)
6487 {
6488 list_unref(copy);
6489 copy = NULL;
6490 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006491 }
6492
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006493 return copy;
6494}
6495
6496/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006497 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006498 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006499 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006501list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006502 list_T *l;
6503 listitem_T *item;
6504 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006505{
Bram Moolenaar33570922005-01-25 22:26:29 +00006506 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006507
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006508 /* notify watchers */
6509 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006510 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006511 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006512 list_fix_watch(l, ip);
6513 if (ip == item2)
6514 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006515 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006516
6517 if (item2->li_next == NULL)
6518 l->lv_last = item->li_prev;
6519 else
6520 item2->li_next->li_prev = item->li_prev;
6521 if (item->li_prev == NULL)
6522 l->lv_first = item2->li_next;
6523 else
6524 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006525 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006526}
6527
6528/*
6529 * Return an allocated string with the string representation of a list.
6530 * May return NULL.
6531 */
6532 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006533list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006534 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006535 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006536{
6537 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006538
6539 if (tv->vval.v_list == NULL)
6540 return NULL;
6541 ga_init2(&ga, (int)sizeof(char), 80);
6542 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006543 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006544 {
6545 vim_free(ga.ga_data);
6546 return NULL;
6547 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006548 ga_append(&ga, ']');
6549 ga_append(&ga, NUL);
6550 return (char_u *)ga.ga_data;
6551}
6552
6553/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006554 * Join list "l" into a string in "*gap", using separator "sep".
6555 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006556 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006557 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006558 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006559list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006560 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006562 char_u *sep;
6563 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006564 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006565{
6566 int first = TRUE;
6567 char_u *tofree;
6568 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006569 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006570 char_u *s;
6571
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006572 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006573 {
6574 if (first)
6575 first = FALSE;
6576 else
6577 ga_concat(gap, sep);
6578
6579 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006580 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006581 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006582 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006583 if (s != NULL)
6584 ga_concat(gap, s);
6585 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006586 if (s == NULL)
6587 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006588 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006589 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006590 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006591}
6592
6593/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006594 * Garbage collection for lists and dictionaries.
6595 *
6596 * We use reference counts to be able to free most items right away when they
6597 * are no longer used. But for composite items it's possible that it becomes
6598 * unused while the reference count is > 0: When there is a recursive
6599 * reference. Example:
6600 * :let l = [1, 2, 3]
6601 * :let d = {9: l}
6602 * :let l[1] = d
6603 *
6604 * Since this is quite unusual we handle this with garbage collection: every
6605 * once in a while find out which lists and dicts are not referenced from any
6606 * variable.
6607 *
6608 * Here is a good reference text about garbage collection (refers to Python
6609 * but it applies to all reference-counting mechanisms):
6610 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006611 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006612
6613/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006614 * Do garbage collection for lists and dicts.
6615 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006616 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006617 int
6618garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006619{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006620 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006621 buf_T *buf;
6622 win_T *wp;
6623 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006624 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006625 int did_free;
6626 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006627#ifdef FEAT_WINDOWS
6628 tabpage_T *tp;
6629#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006630
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006631 /* Only do this once. */
6632 want_garbage_collect = FALSE;
6633 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006634 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006635
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006636 /* We advance by two because we add one for items referenced through
6637 * previous_funccal. */
6638 current_copyID += COPYID_INC;
6639 copyID = current_copyID;
6640
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006641 /*
6642 * 1. Go through all accessible variables and mark all lists and dicts
6643 * with copyID.
6644 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006645
6646 /* Don't free variables in the previous_funccal list unless they are only
6647 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006648 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006649 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6650 {
6651 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6652 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6653 }
6654
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006655 /* script-local variables */
6656 for (i = 1; i <= ga_scripts.ga_len; ++i)
6657 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6658
6659 /* buffer-local variables */
6660 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6661 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6662
6663 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006664 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006665 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6666
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006667#ifdef FEAT_WINDOWS
6668 /* tabpage-local variables */
6669 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6670 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6671#endif
6672
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006673 /* global variables */
6674 set_ref_in_ht(&globvarht, copyID);
6675
6676 /* function-local variables */
6677 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6678 {
6679 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6680 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6681 }
6682
Bram Moolenaard812df62008-11-09 12:46:09 +00006683 /* v: vars */
6684 set_ref_in_ht(&vimvarht, copyID);
6685
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006686 /*
6687 * 2. Free lists and dictionaries that are not referenced.
6688 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006689 did_free = free_unref_items(copyID);
6690
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006691 /*
6692 * 3. Check if any funccal can be freed now.
6693 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006694 for (pfc = &previous_funccal; *pfc != NULL; )
6695 {
6696 if (can_free_funccal(*pfc, copyID))
6697 {
6698 fc = *pfc;
6699 *pfc = fc->caller;
6700 free_funccal(fc, TRUE);
6701 did_free = TRUE;
6702 did_free_funccal = TRUE;
6703 }
6704 else
6705 pfc = &(*pfc)->caller;
6706 }
6707 if (did_free_funccal)
6708 /* When a funccal was freed some more items might be garbage
6709 * collected, so run again. */
6710 (void)garbage_collect();
6711
6712 return did_free;
6713}
6714
6715/*
6716 * Free lists and dictionaries that are no longer referenced.
6717 */
6718 static int
6719free_unref_items(copyID)
6720 int copyID;
6721{
6722 dict_T *dd;
6723 list_T *ll;
6724 int did_free = FALSE;
6725
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006726 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006727 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006728 */
6729 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006730 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006731 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006732 /* Free the Dictionary and ordinary items it contains, but don't
6733 * recurse into Lists and Dictionaries, they will be in the list
6734 * of dicts or list of lists. */
6735 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006736 did_free = TRUE;
6737
6738 /* restart, next dict may also have been freed */
6739 dd = first_dict;
6740 }
6741 else
6742 dd = dd->dv_used_next;
6743
6744 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006745 * Go through the list of lists and free items without the copyID.
6746 * But don't free a list that has a watcher (used in a for loop), these
6747 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006748 */
6749 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006750 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6751 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006752 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006753 /* Free the List and ordinary items it contains, but don't recurse
6754 * into Lists and Dictionaries, they will be in the list of dicts
6755 * or list of lists. */
6756 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006757 did_free = TRUE;
6758
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006759 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006760 ll = first_list;
6761 }
6762 else
6763 ll = ll->lv_used_next;
6764
6765 return did_free;
6766}
6767
6768/*
6769 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6770 */
6771 static void
6772set_ref_in_ht(ht, copyID)
6773 hashtab_T *ht;
6774 int copyID;
6775{
6776 int todo;
6777 hashitem_T *hi;
6778
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006779 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006780 for (hi = ht->ht_array; todo > 0; ++hi)
6781 if (!HASHITEM_EMPTY(hi))
6782 {
6783 --todo;
6784 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6785 }
6786}
6787
6788/*
6789 * Mark all lists and dicts referenced through list "l" with "copyID".
6790 */
6791 static void
6792set_ref_in_list(l, copyID)
6793 list_T *l;
6794 int copyID;
6795{
6796 listitem_T *li;
6797
6798 for (li = l->lv_first; li != NULL; li = li->li_next)
6799 set_ref_in_item(&li->li_tv, copyID);
6800}
6801
6802/*
6803 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6804 */
6805 static void
6806set_ref_in_item(tv, copyID)
6807 typval_T *tv;
6808 int copyID;
6809{
6810 dict_T *dd;
6811 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006812
6813 switch (tv->v_type)
6814 {
6815 case VAR_DICT:
6816 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006817 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006818 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006819 /* Didn't see this dict yet. */
6820 dd->dv_copyID = copyID;
6821 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006822 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006823 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006824
6825 case VAR_LIST:
6826 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006827 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006828 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006829 /* Didn't see this list yet. */
6830 ll->lv_copyID = copyID;
6831 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006832 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006833 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006834 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006835 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006836}
6837
6838/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006839 * Allocate an empty header for a dictionary.
6840 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006841 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006842dict_alloc()
6843{
Bram Moolenaar33570922005-01-25 22:26:29 +00006844 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006845
Bram Moolenaar33570922005-01-25 22:26:29 +00006846 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006847 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006848 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006849 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006850 if (first_dict != NULL)
6851 first_dict->dv_used_prev = d;
6852 d->dv_used_next = first_dict;
6853 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006854 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006855
Bram Moolenaar33570922005-01-25 22:26:29 +00006856 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006857 d->dv_lock = 0;
6858 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006859 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006860 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006861 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006862}
6863
6864/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006865 * Allocate an empty dict for a return value.
6866 * Returns OK or FAIL.
6867 */
6868 static int
6869rettv_dict_alloc(rettv)
6870 typval_T *rettv;
6871{
6872 dict_T *d = dict_alloc();
6873
6874 if (d == NULL)
6875 return FAIL;
6876
6877 rettv->vval.v_dict = d;
6878 rettv->v_type = VAR_DICT;
6879 ++d->dv_refcount;
6880 return OK;
6881}
6882
6883
6884/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006885 * Unreference a Dictionary: decrement the reference count and free it when it
6886 * becomes zero.
6887 */
6888 static void
6889dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006890 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006891{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006892 if (d != NULL && --d->dv_refcount <= 0)
6893 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006894}
6895
6896/*
6897 * Free a Dictionary, including all items it contains.
6898 * Ignores the reference count.
6899 */
6900 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006901dict_free(d, recurse)
6902 dict_T *d;
6903 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006904{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006905 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006906 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006907 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006908
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909 /* Remove the dict from the list of dicts for garbage collection. */
6910 if (d->dv_used_prev == NULL)
6911 first_dict = d->dv_used_next;
6912 else
6913 d->dv_used_prev->dv_used_next = d->dv_used_next;
6914 if (d->dv_used_next != NULL)
6915 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6916
6917 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006918 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006919 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006920 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006921 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006922 if (!HASHITEM_EMPTY(hi))
6923 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006924 /* Remove the item before deleting it, just in case there is
6925 * something recursive causing trouble. */
6926 di = HI2DI(hi);
6927 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006928 if (recurse || (di->di_tv.v_type != VAR_LIST
6929 && di->di_tv.v_type != VAR_DICT))
6930 clear_tv(&di->di_tv);
6931 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006932 --todo;
6933 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006934 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006935 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006936 vim_free(d);
6937}
6938
6939/*
6940 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006941 * The "key" is copied to the new item.
6942 * Note that the value of the item "di_tv" still needs to be initialized!
6943 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006944 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006945 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006946dictitem_alloc(key)
6947 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006948{
Bram Moolenaar33570922005-01-25 22:26:29 +00006949 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006950
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006951 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006952 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006953 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006954 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006955 di->di_flags = 0;
6956 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006957 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006958}
6959
6960/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006961 * Make a copy of a Dictionary item.
6962 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006963 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006964dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006965 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006966{
Bram Moolenaar33570922005-01-25 22:26:29 +00006967 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006968
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006969 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6970 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006971 if (di != NULL)
6972 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006973 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006974 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006975 copy_tv(&org->di_tv, &di->di_tv);
6976 }
6977 return di;
6978}
6979
6980/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006981 * Remove item "item" from Dictionary "dict" and free it.
6982 */
6983 static void
6984dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006985 dict_T *dict;
6986 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006987{
Bram Moolenaar33570922005-01-25 22:26:29 +00006988 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006989
Bram Moolenaar33570922005-01-25 22:26:29 +00006990 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006991 if (HASHITEM_EMPTY(hi))
6992 EMSG2(_(e_intern2), "dictitem_remove()");
6993 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006994 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006995 dictitem_free(item);
6996}
6997
6998/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006999 * Free a dict item. Also clears the value.
7000 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007001 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007002dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007003 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007004{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007005 clear_tv(&item->di_tv);
7006 vim_free(item);
7007}
7008
7009/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007010 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7011 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007012 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007013 * Returns NULL when out of memory.
7014 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007015 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007016dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007017 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007018 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007019 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007020{
Bram Moolenaar33570922005-01-25 22:26:29 +00007021 dict_T *copy;
7022 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007023 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007024 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007025
7026 if (orig == NULL)
7027 return NULL;
7028
7029 copy = dict_alloc();
7030 if (copy != NULL)
7031 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007032 if (copyID != 0)
7033 {
7034 orig->dv_copyID = copyID;
7035 orig->dv_copydict = copy;
7036 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007037 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007038 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007039 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007040 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007041 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007042 --todo;
7043
7044 di = dictitem_alloc(hi->hi_key);
7045 if (di == NULL)
7046 break;
7047 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007048 {
7049 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7050 copyID) == FAIL)
7051 {
7052 vim_free(di);
7053 break;
7054 }
7055 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007056 else
7057 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7058 if (dict_add(copy, di) == FAIL)
7059 {
7060 dictitem_free(di);
7061 break;
7062 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007063 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007064 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007065
Bram Moolenaare9a41262005-01-15 22:18:47 +00007066 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007067 if (todo > 0)
7068 {
7069 dict_unref(copy);
7070 copy = NULL;
7071 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007072 }
7073
7074 return copy;
7075}
7076
7077/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007078 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007079 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007080 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007081 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007082dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007083 dict_T *d;
7084 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007085{
Bram Moolenaar33570922005-01-25 22:26:29 +00007086 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007087}
7088
Bram Moolenaar8c711452005-01-14 21:53:12 +00007089/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007090 * Add a number or string entry to dictionary "d".
7091 * When "str" is NULL use number "nr", otherwise use "str".
7092 * Returns FAIL when out of memory and when key already exists.
7093 */
7094 int
7095dict_add_nr_str(d, key, nr, str)
7096 dict_T *d;
7097 char *key;
7098 long nr;
7099 char_u *str;
7100{
7101 dictitem_T *item;
7102
7103 item = dictitem_alloc((char_u *)key);
7104 if (item == NULL)
7105 return FAIL;
7106 item->di_tv.v_lock = 0;
7107 if (str == NULL)
7108 {
7109 item->di_tv.v_type = VAR_NUMBER;
7110 item->di_tv.vval.v_number = nr;
7111 }
7112 else
7113 {
7114 item->di_tv.v_type = VAR_STRING;
7115 item->di_tv.vval.v_string = vim_strsave(str);
7116 }
7117 if (dict_add(d, item) == FAIL)
7118 {
7119 dictitem_free(item);
7120 return FAIL;
7121 }
7122 return OK;
7123}
7124
7125/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007126 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007127 * Returns FAIL when out of memory and when key already exists.
7128 */
7129 int
7130dict_add_list(d, key, list)
7131 dict_T *d;
7132 char *key;
7133 list_T *list;
7134{
7135 dictitem_T *item;
7136
7137 item = dictitem_alloc((char_u *)key);
7138 if (item == NULL)
7139 return FAIL;
7140 item->di_tv.v_lock = 0;
7141 item->di_tv.v_type = VAR_LIST;
7142 item->di_tv.vval.v_list = list;
7143 if (dict_add(d, item) == FAIL)
7144 {
7145 dictitem_free(item);
7146 return FAIL;
7147 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007148 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007149 return OK;
7150}
7151
7152/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007153 * Get the number of items in a Dictionary.
7154 */
7155 static long
7156dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007157 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007158{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007159 if (d == NULL)
7160 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007161 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007162}
7163
7164/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007165 * Find item "key[len]" in Dictionary "d".
7166 * If "len" is negative use strlen(key).
7167 * Returns NULL when not found.
7168 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007169 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007170dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007171 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172 char_u *key;
7173 int len;
7174{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007175#define AKEYLEN 200
7176 char_u buf[AKEYLEN];
7177 char_u *akey;
7178 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007179 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007180
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007181 if (len < 0)
7182 akey = key;
7183 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007184 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007185 tofree = akey = vim_strnsave(key, len);
7186 if (akey == NULL)
7187 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007188 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007189 else
7190 {
7191 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007192 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007193 akey = buf;
7194 }
7195
Bram Moolenaar33570922005-01-25 22:26:29 +00007196 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007197 vim_free(tofree);
7198 if (HASHITEM_EMPTY(hi))
7199 return NULL;
7200 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007201}
7202
7203/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007204 * Get a string item from a dictionary.
7205 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007206 * Returns NULL if the entry doesn't exist or out of memory.
7207 */
7208 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007209get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007210 dict_T *d;
7211 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007212 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007213{
7214 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007215 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007216
7217 di = dict_find(d, key, -1);
7218 if (di == NULL)
7219 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007220 s = get_tv_string(&di->di_tv);
7221 if (save && s != NULL)
7222 s = vim_strsave(s);
7223 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007224}
7225
7226/*
7227 * Get a number item from a dictionary.
7228 * Returns 0 if the entry doesn't exist or out of memory.
7229 */
7230 long
7231get_dict_number(d, key)
7232 dict_T *d;
7233 char_u *key;
7234{
7235 dictitem_T *di;
7236
7237 di = dict_find(d, key, -1);
7238 if (di == NULL)
7239 return 0;
7240 return get_tv_number(&di->di_tv);
7241}
7242
7243/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244 * Return an allocated string with the string representation of a Dictionary.
7245 * May return NULL.
7246 */
7247 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007248dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007249 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007250 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251{
7252 garray_T ga;
7253 int first = TRUE;
7254 char_u *tofree;
7255 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007256 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007258 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007259 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007260
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007261 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262 return NULL;
7263 ga_init2(&ga, (int)sizeof(char), 80);
7264 ga_append(&ga, '{');
7265
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007266 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007267 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007269 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007271 --todo;
7272
7273 if (first)
7274 first = FALSE;
7275 else
7276 ga_concat(&ga, (char_u *)", ");
7277
7278 tofree = string_quote(hi->hi_key, FALSE);
7279 if (tofree != NULL)
7280 {
7281 ga_concat(&ga, tofree);
7282 vim_free(tofree);
7283 }
7284 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007285 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286 if (s != NULL)
7287 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007289 if (s == NULL)
7290 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007291 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007292 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007293 if (todo > 0)
7294 {
7295 vim_free(ga.ga_data);
7296 return NULL;
7297 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007298
7299 ga_append(&ga, '}');
7300 ga_append(&ga, NUL);
7301 return (char_u *)ga.ga_data;
7302}
7303
7304/*
7305 * Allocate a variable for a Dictionary and fill it from "*arg".
7306 * Return OK or FAIL. Returns NOTDONE for {expr}.
7307 */
7308 static int
7309get_dict_tv(arg, rettv, evaluate)
7310 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007311 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007312 int evaluate;
7313{
Bram Moolenaar33570922005-01-25 22:26:29 +00007314 dict_T *d = NULL;
7315 typval_T tvkey;
7316 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007317 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007318 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007319 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007321
7322 /*
7323 * First check if it's not a curly-braces thing: {expr}.
7324 * Must do this without evaluating, otherwise a function may be called
7325 * twice. Unfortunately this means we need to call eval1() twice for the
7326 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007328 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007329 if (*start != '}')
7330 {
7331 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7332 return FAIL;
7333 if (*start == '}')
7334 return NOTDONE;
7335 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336
7337 if (evaluate)
7338 {
7339 d = dict_alloc();
7340 if (d == NULL)
7341 return FAIL;
7342 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007343 tvkey.v_type = VAR_UNKNOWN;
7344 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345
7346 *arg = skipwhite(*arg + 1);
7347 while (**arg != '}' && **arg != NUL)
7348 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007350 goto failret;
7351 if (**arg != ':')
7352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007353 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007354 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007355 goto failret;
7356 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007357 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007358 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007359 key = get_tv_string_buf_chk(&tvkey, buf);
7360 if (key == NULL || *key == NUL)
7361 {
7362 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7363 if (key != NULL)
7364 EMSG(_(e_emptykey));
7365 clear_tv(&tvkey);
7366 goto failret;
7367 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007368 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007369
7370 *arg = skipwhite(*arg + 1);
7371 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7372 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007373 if (evaluate)
7374 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007375 goto failret;
7376 }
7377 if (evaluate)
7378 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007379 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007380 if (item != NULL)
7381 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007382 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007383 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007384 clear_tv(&tv);
7385 goto failret;
7386 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007387 item = dictitem_alloc(key);
7388 clear_tv(&tvkey);
7389 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007391 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007392 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007393 if (dict_add(d, item) == FAIL)
7394 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007395 }
7396 }
7397
7398 if (**arg == '}')
7399 break;
7400 if (**arg != ',')
7401 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007402 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007403 goto failret;
7404 }
7405 *arg = skipwhite(*arg + 1);
7406 }
7407
7408 if (**arg != '}')
7409 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007410 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411failret:
7412 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007413 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414 return FAIL;
7415 }
7416
7417 *arg = skipwhite(*arg + 1);
7418 if (evaluate)
7419 {
7420 rettv->v_type = VAR_DICT;
7421 rettv->vval.v_dict = d;
7422 ++d->dv_refcount;
7423 }
7424
7425 return OK;
7426}
7427
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007429 * Return a string with the string representation of a variable.
7430 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007431 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007432 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007433 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007434 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007435 */
7436 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007437echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007438 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007439 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007440 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007441 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007442{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007443 static int recurse = 0;
7444 char_u *r = NULL;
7445
Bram Moolenaar33570922005-01-25 22:26:29 +00007446 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007447 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007448 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007449 *tofree = NULL;
7450 return NULL;
7451 }
7452 ++recurse;
7453
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007454 switch (tv->v_type)
7455 {
7456 case VAR_FUNC:
7457 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007458 r = tv->vval.v_string;
7459 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007460
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007461 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007462 if (tv->vval.v_list == NULL)
7463 {
7464 *tofree = NULL;
7465 r = NULL;
7466 }
7467 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7468 {
7469 *tofree = NULL;
7470 r = (char_u *)"[...]";
7471 }
7472 else
7473 {
7474 tv->vval.v_list->lv_copyID = copyID;
7475 *tofree = list2string(tv, copyID);
7476 r = *tofree;
7477 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007478 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007479
Bram Moolenaar8c711452005-01-14 21:53:12 +00007480 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007481 if (tv->vval.v_dict == NULL)
7482 {
7483 *tofree = NULL;
7484 r = NULL;
7485 }
7486 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7487 {
7488 *tofree = NULL;
7489 r = (char_u *)"{...}";
7490 }
7491 else
7492 {
7493 tv->vval.v_dict->dv_copyID = copyID;
7494 *tofree = dict2string(tv, copyID);
7495 r = *tofree;
7496 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007497 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007498
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007499 case VAR_STRING:
7500 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007501 *tofree = NULL;
7502 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007503 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007504
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007505#ifdef FEAT_FLOAT
7506 case VAR_FLOAT:
7507 *tofree = NULL;
7508 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7509 r = numbuf;
7510 break;
7511#endif
7512
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007513 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007514 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007515 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007516 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007517
7518 --recurse;
7519 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007520}
7521
7522/*
7523 * Return a string with the string representation of a variable.
7524 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7525 * "numbuf" is used for a number.
7526 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007527 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007528 */
7529 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007530tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007531 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007532 char_u **tofree;
7533 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007534 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007535{
7536 switch (tv->v_type)
7537 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007538 case VAR_FUNC:
7539 *tofree = string_quote(tv->vval.v_string, TRUE);
7540 return *tofree;
7541 case VAR_STRING:
7542 *tofree = string_quote(tv->vval.v_string, FALSE);
7543 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007544#ifdef FEAT_FLOAT
7545 case VAR_FLOAT:
7546 *tofree = NULL;
7547 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7548 return numbuf;
7549#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007550 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007551 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007552 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007553 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007554 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007555 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007556 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007557 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007558}
7559
7560/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007561 * Return string "str" in ' quotes, doubling ' characters.
7562 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007564 */
7565 static char_u *
7566string_quote(str, function)
7567 char_u *str;
7568 int function;
7569{
Bram Moolenaar33570922005-01-25 22:26:29 +00007570 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007571 char_u *p, *r, *s;
7572
Bram Moolenaar33570922005-01-25 22:26:29 +00007573 len = (function ? 13 : 3);
7574 if (str != NULL)
7575 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007576 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007577 for (p = str; *p != NUL; mb_ptr_adv(p))
7578 if (*p == '\'')
7579 ++len;
7580 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007581 s = r = alloc(len);
7582 if (r != NULL)
7583 {
7584 if (function)
7585 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007586 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007587 r += 10;
7588 }
7589 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007590 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 if (str != NULL)
7592 for (p = str; *p != NUL; )
7593 {
7594 if (*p == '\'')
7595 *r++ = '\'';
7596 MB_COPY_CHAR(p, r);
7597 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007598 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007599 if (function)
7600 *r++ = ')';
7601 *r++ = NUL;
7602 }
7603 return s;
7604}
7605
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007606#ifdef FEAT_FLOAT
7607/*
7608 * Convert the string "text" to a floating point number.
7609 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7610 * this always uses a decimal point.
7611 * Returns the length of the text that was consumed.
7612 */
7613 static int
7614string2float(text, value)
7615 char_u *text;
7616 float_T *value; /* result stored here */
7617{
7618 char *s = (char *)text;
7619 float_T f;
7620
7621 f = strtod(s, &s);
7622 *value = f;
7623 return (int)((char_u *)s - text);
7624}
7625#endif
7626
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 * Get the value of an environment variable.
7629 * "arg" is pointing to the '$'. It is advanced to after the name.
7630 * If the environment variable was not set, silently assume it is empty.
7631 * Always return OK.
7632 */
7633 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007634get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 int evaluate;
7638{
7639 char_u *string = NULL;
7640 int len;
7641 int cc;
7642 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007643 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644
7645 ++*arg;
7646 name = *arg;
7647 len = get_env_len(arg);
7648 if (evaluate)
7649 {
7650 if (len != 0)
7651 {
7652 cc = name[len];
7653 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007654 /* first try vim_getenv(), fast for normal environment vars */
7655 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007657 {
7658 if (!mustfree)
7659 string = vim_strsave(string);
7660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 else
7662 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007663 if (mustfree)
7664 vim_free(string);
7665
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 /* next try expanding things like $VIM and ${HOME} */
7667 string = expand_env_save(name - 1);
7668 if (string != NULL && *string == '$')
7669 {
7670 vim_free(string);
7671 string = NULL;
7672 }
7673 }
7674 name[len] = cc;
7675 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007676 rettv->v_type = VAR_STRING;
7677 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 }
7679
7680 return OK;
7681}
7682
7683/*
7684 * Array with names and number of arguments of all internal functions
7685 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7686 */
7687static struct fst
7688{
7689 char *f_name; /* function name */
7690 char f_min_argc; /* minimal number of arguments */
7691 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007692 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007693 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694} functions[] =
7695{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007696#ifdef FEAT_FLOAT
7697 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007698 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007699#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007700 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 {"append", 2, 2, f_append},
7702 {"argc", 0, 0, f_argc},
7703 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007704 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007705#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007706 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007707 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007708 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007711 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 {"bufexists", 1, 1, f_bufexists},
7713 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7714 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7715 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7716 {"buflisted", 1, 1, f_buflisted},
7717 {"bufloaded", 1, 1, f_bufloaded},
7718 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007719 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 {"bufwinnr", 1, 1, f_bufwinnr},
7721 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007722 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007723 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007724#ifdef FEAT_FLOAT
7725 {"ceil", 1, 1, f_ceil},
7726#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007727 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 {"char2nr", 1, 1, f_char2nr},
7729 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007730 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007732#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007733 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007734 {"complete_add", 1, 1, f_complete_add},
7735 {"complete_check", 0, 0, f_complete_check},
7736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007738 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007739#ifdef FEAT_FLOAT
7740 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007741 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007742#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007743 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007745 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007746 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 {"delete", 1, 1, f_delete},
7748 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007749 {"diff_filler", 1, 1, f_diff_filler},
7750 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007751 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007753 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 {"eventhandler", 0, 0, f_eventhandler},
7755 {"executable", 1, 1, f_executable},
7756 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007757#ifdef FEAT_FLOAT
7758 {"exp", 1, 1, f_exp},
7759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007761 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007762 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7764 {"filereadable", 1, 1, f_filereadable},
7765 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007766 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007767 {"finddir", 1, 3, f_finddir},
7768 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007769#ifdef FEAT_FLOAT
7770 {"float2nr", 1, 1, f_float2nr},
7771 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007772 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007773#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007774 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 {"fnamemodify", 2, 2, f_fnamemodify},
7776 {"foldclosed", 1, 1, f_foldclosed},
7777 {"foldclosedend", 1, 1, f_foldclosedend},
7778 {"foldlevel", 1, 1, f_foldlevel},
7779 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007780 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007782 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007783 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007784 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007785 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 {"getbufvar", 2, 2, f_getbufvar},
7787 {"getchar", 0, 1, f_getchar},
7788 {"getcharmod", 0, 0, f_getcharmod},
7789 {"getcmdline", 0, 0, f_getcmdline},
7790 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007791 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007793 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007794 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 {"getfsize", 1, 1, f_getfsize},
7796 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007797 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007798 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007799 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007800 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007801 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007802 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007803 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007804 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007806 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007807 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 {"getwinposx", 0, 0, f_getwinposx},
7809 {"getwinposy", 0, 0, f_getwinposy},
7810 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007811 {"glob", 1, 2, f_glob},
7812 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007814 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007815 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007816 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7818 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7819 {"histadd", 2, 2, f_histadd},
7820 {"histdel", 1, 2, f_histdel},
7821 {"histget", 1, 2, f_histget},
7822 {"histnr", 1, 1, f_histnr},
7823 {"hlID", 1, 1, f_hlID},
7824 {"hlexists", 1, 1, f_hlexists},
7825 {"hostname", 0, 0, f_hostname},
7826 {"iconv", 3, 3, f_iconv},
7827 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007828 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007829 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007831 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 {"inputrestore", 0, 0, f_inputrestore},
7833 {"inputsave", 0, 0, f_inputsave},
7834 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007835 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007837 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007838 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007839 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007840 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007842 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 {"libcall", 3, 3, f_libcall},
7844 {"libcallnr", 3, 3, f_libcallnr},
7845 {"line", 1, 1, f_line},
7846 {"line2byte", 1, 1, f_line2byte},
7847 {"lispindent", 1, 1, f_lispindent},
7848 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007849#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007850 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007851 {"log10", 1, 1, f_log10},
7852#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007853 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007854 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007855 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007856 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007857 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007858 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007859 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007860 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007861 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007862 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007863 {"max", 1, 1, f_max},
7864 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007865#ifdef vim_mkdir
7866 {"mkdir", 1, 3, f_mkdir},
7867#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007868 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007869#ifdef FEAT_MZSCHEME
7870 {"mzeval", 1, 1, f_mzeval},
7871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 {"nextnonblank", 1, 1, f_nextnonblank},
7873 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007874 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007875#ifdef FEAT_FLOAT
7876 {"pow", 2, 2, f_pow},
7877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007879 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007880 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007881 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007882 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007883 {"reltime", 0, 2, f_reltime},
7884 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 {"remote_expr", 2, 3, f_remote_expr},
7886 {"remote_foreground", 1, 1, f_remote_foreground},
7887 {"remote_peek", 1, 2, f_remote_peek},
7888 {"remote_read", 1, 1, f_remote_read},
7889 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007890 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007892 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007894 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007895#ifdef FEAT_FLOAT
7896 {"round", 1, 1, f_round},
7897#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007898 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007899 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007900 {"searchpair", 3, 7, f_searchpair},
7901 {"searchpairpos", 3, 7, f_searchpairpos},
7902 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 {"server2client", 2, 2, f_server2client},
7904 {"serverlist", 0, 0, f_serverlist},
7905 {"setbufvar", 3, 3, f_setbufvar},
7906 {"setcmdpos", 1, 1, f_setcmdpos},
7907 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007908 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007909 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007910 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007911 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007913 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007914 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007916 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007918#ifdef FEAT_FLOAT
7919 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007920 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007921#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007922 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007923 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007924 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007925 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007926 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007927#ifdef FEAT_FLOAT
7928 {"sqrt", 1, 1, f_sqrt},
7929 {"str2float", 1, 1, f_str2float},
7930#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007931 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007932 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007933 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934#ifdef HAVE_STRFTIME
7935 {"strftime", 1, 2, f_strftime},
7936#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007937 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007938 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {"strlen", 1, 1, f_strlen},
7940 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007941 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007943 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 {"submatch", 1, 1, f_submatch},
7945 {"substitute", 4, 4, f_substitute},
7946 {"synID", 3, 3, f_synID},
7947 {"synIDattr", 2, 3, f_synIDattr},
7948 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007949 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007950 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007951 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007952 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007953 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007954 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007955 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007956 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007957#ifdef FEAT_FLOAT
7958 {"tan", 1, 1, f_tan},
7959 {"tanh", 1, 1, f_tanh},
7960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007962 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 {"tolower", 1, 1, f_tolower},
7964 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007965 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007966#ifdef FEAT_FLOAT
7967 {"trunc", 1, 1, f_trunc},
7968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007970 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007971 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007972 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {"virtcol", 1, 1, f_virtcol},
7974 {"visualmode", 0, 1, f_visualmode},
7975 {"winbufnr", 1, 1, f_winbufnr},
7976 {"wincol", 0, 0, f_wincol},
7977 {"winheight", 1, 1, f_winheight},
7978 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007979 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007981 {"winrestview", 1, 1, f_winrestview},
7982 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007984 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985};
7986
7987#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7988
7989/*
7990 * Function given to ExpandGeneric() to obtain the list of internal
7991 * or user defined function names.
7992 */
7993 char_u *
7994get_function_name(xp, idx)
7995 expand_T *xp;
7996 int idx;
7997{
7998 static int intidx = -1;
7999 char_u *name;
8000
8001 if (idx == 0)
8002 intidx = -1;
8003 if (intidx < 0)
8004 {
8005 name = get_user_func_name(xp, idx);
8006 if (name != NULL)
8007 return name;
8008 }
8009 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8010 {
8011 STRCPY(IObuff, functions[intidx].f_name);
8012 STRCAT(IObuff, "(");
8013 if (functions[intidx].f_max_argc == 0)
8014 STRCAT(IObuff, ")");
8015 return IObuff;
8016 }
8017
8018 return NULL;
8019}
8020
8021/*
8022 * Function given to ExpandGeneric() to obtain the list of internal or
8023 * user defined variable or function names.
8024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 char_u *
8026get_expr_name(xp, idx)
8027 expand_T *xp;
8028 int idx;
8029{
8030 static int intidx = -1;
8031 char_u *name;
8032
8033 if (idx == 0)
8034 intidx = -1;
8035 if (intidx < 0)
8036 {
8037 name = get_function_name(xp, idx);
8038 if (name != NULL)
8039 return name;
8040 }
8041 return get_user_var_name(xp, ++intidx);
8042}
8043
8044#endif /* FEAT_CMDL_COMPL */
8045
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008046#if defined(EBCDIC) || defined(PROTO)
8047/*
8048 * Compare struct fst by function name.
8049 */
8050 static int
8051compare_func_name(s1, s2)
8052 const void *s1;
8053 const void *s2;
8054{
8055 struct fst *p1 = (struct fst *)s1;
8056 struct fst *p2 = (struct fst *)s2;
8057
8058 return STRCMP(p1->f_name, p2->f_name);
8059}
8060
8061/*
8062 * Sort the function table by function name.
8063 * The sorting of the table above is ASCII dependant.
8064 * On machines using EBCDIC we have to sort it.
8065 */
8066 static void
8067sortFunctions()
8068{
8069 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8070
8071 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8072}
8073#endif
8074
8075
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076/*
8077 * Find internal function in table above.
8078 * Return index, or -1 if not found
8079 */
8080 static int
8081find_internal_func(name)
8082 char_u *name; /* name of the function */
8083{
8084 int first = 0;
8085 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8086 int cmp;
8087 int x;
8088
8089 /*
8090 * Find the function name in the table. Binary search.
8091 */
8092 while (first <= last)
8093 {
8094 x = first + ((unsigned)(last - first) >> 1);
8095 cmp = STRCMP(name, functions[x].f_name);
8096 if (cmp < 0)
8097 last = x - 1;
8098 else if (cmp > 0)
8099 first = x + 1;
8100 else
8101 return x;
8102 }
8103 return -1;
8104}
8105
8106/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008107 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8108 * name it contains, otherwise return "name".
8109 */
8110 static char_u *
8111deref_func_name(name, lenp)
8112 char_u *name;
8113 int *lenp;
8114{
Bram Moolenaar33570922005-01-25 22:26:29 +00008115 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008116 int cc;
8117
8118 cc = name[*lenp];
8119 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008120 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008121 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008122 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008123 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008124 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008125 {
8126 *lenp = 0;
8127 return (char_u *)""; /* just in case */
8128 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008129 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008130 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008131 }
8132
8133 return name;
8134}
8135
8136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 * Allocate a variable for the result of a function.
8138 * Return OK or FAIL.
8139 */
8140 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008141get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8142 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 char_u *name; /* name of the function */
8144 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 char_u **arg; /* argument, pointing to the '(' */
8147 linenr_T firstline; /* first line of range */
8148 linenr_T lastline; /* last line of range */
8149 int *doesrange; /* return: function handled range */
8150 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008151 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152{
8153 char_u *argp;
8154 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008155 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 int argcount = 0; /* number of arguments found */
8157
8158 /*
8159 * Get the arguments.
8160 */
8161 argp = *arg;
8162 while (argcount < MAX_FUNC_ARGS)
8163 {
8164 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8165 if (*argp == ')' || *argp == ',' || *argp == NUL)
8166 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8168 {
8169 ret = FAIL;
8170 break;
8171 }
8172 ++argcount;
8173 if (*argp != ',')
8174 break;
8175 }
8176 if (*argp == ')')
8177 ++argp;
8178 else
8179 ret = FAIL;
8180
8181 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008182 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008183 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008185 {
8186 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008187 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008188 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008189 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191
8192 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008193 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194
8195 *arg = skipwhite(argp);
8196 return ret;
8197}
8198
8199
8200/*
8201 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008202 * Return OK when the function can't be called, FAIL otherwise.
8203 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 */
8205 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008206call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008207 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008208 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008210 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008212 typval_T *argvars; /* vars for arguments, must have "argcount"
8213 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 linenr_T firstline; /* first line of range */
8215 linenr_T lastline; /* last line of range */
8216 int *doesrange; /* return: function handled range */
8217 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008218 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219{
8220 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221#define ERROR_UNKNOWN 0
8222#define ERROR_TOOMANY 1
8223#define ERROR_TOOFEW 2
8224#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008225#define ERROR_DICT 4
8226#define ERROR_NONE 5
8227#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 int error = ERROR_NONE;
8229 int i;
8230 int llen;
8231 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232#define FLEN_FIXED 40
8233 char_u fname_buf[FLEN_FIXED + 1];
8234 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008235 char_u *name;
8236
8237 /* Make a copy of the name, if it comes from a funcref variable it could
8238 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008239 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008240 if (name == NULL)
8241 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242
8243 /*
8244 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8245 * Change <SNR>123_name() to K_SNR 123_name().
8246 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 llen = eval_fname_script(name);
8249 if (llen > 0)
8250 {
8251 fname_buf[0] = K_SPECIAL;
8252 fname_buf[1] = KS_EXTRA;
8253 fname_buf[2] = (int)KE_SNR;
8254 i = 3;
8255 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8256 {
8257 if (current_SID <= 0)
8258 error = ERROR_SCRIPT;
8259 else
8260 {
8261 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8262 i = (int)STRLEN(fname_buf);
8263 }
8264 }
8265 if (i + STRLEN(name + llen) < FLEN_FIXED)
8266 {
8267 STRCPY(fname_buf + i, name + llen);
8268 fname = fname_buf;
8269 }
8270 else
8271 {
8272 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8273 if (fname == NULL)
8274 error = ERROR_OTHER;
8275 else
8276 {
8277 mch_memmove(fname, fname_buf, (size_t)i);
8278 STRCPY(fname + i, name + llen);
8279 }
8280 }
8281 }
8282 else
8283 fname = name;
8284
8285 *doesrange = FALSE;
8286
8287
8288 /* execute the function if no errors detected and executing */
8289 if (evaluate && error == ERROR_NONE)
8290 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008291 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8292 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 error = ERROR_UNKNOWN;
8294
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008295 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 {
8297 /*
8298 * User defined function.
8299 */
8300 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008301
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008303 /* Trigger FuncUndefined event, may load the function. */
8304 if (fp == NULL
8305 && apply_autocmds(EVENT_FUNCUNDEFINED,
8306 fname, fname, TRUE, NULL)
8307 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008309 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 fp = find_func(fname);
8311 }
8312#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008313 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008314 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008315 {
8316 /* loaded a package, search for the function again */
8317 fp = find_func(fname);
8318 }
8319
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 if (fp != NULL)
8321 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008322 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008324 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008326 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008328 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008329 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 else
8331 {
8332 /*
8333 * Call the user function.
8334 * Save and restore search patterns, script variables and
8335 * redo buffer.
8336 */
8337 save_search_patterns();
8338 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008339 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008340 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008341 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008342 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8343 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8344 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008345 /* Function was unreferenced while being used, free it
8346 * now. */
8347 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 restoreRedobuff();
8349 restore_search_patterns();
8350 error = ERROR_NONE;
8351 }
8352 }
8353 }
8354 else
8355 {
8356 /*
8357 * Find the function name in the table, call its implementation.
8358 */
8359 i = find_internal_func(fname);
8360 if (i >= 0)
8361 {
8362 if (argcount < functions[i].f_min_argc)
8363 error = ERROR_TOOFEW;
8364 else if (argcount > functions[i].f_max_argc)
8365 error = ERROR_TOOMANY;
8366 else
8367 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008368 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008369 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 error = ERROR_NONE;
8371 }
8372 }
8373 }
8374 /*
8375 * The function call (or "FuncUndefined" autocommand sequence) might
8376 * have been aborted by an error, an interrupt, or an explicitly thrown
8377 * exception that has not been caught so far. This situation can be
8378 * tested for by calling aborting(). For an error in an internal
8379 * function or for the "E132" error in call_user_func(), however, the
8380 * throw point at which the "force_abort" flag (temporarily reset by
8381 * emsg()) is normally updated has not been reached yet. We need to
8382 * update that flag first to make aborting() reliable.
8383 */
8384 update_force_abort();
8385 }
8386 if (error == ERROR_NONE)
8387 ret = OK;
8388
8389 /*
8390 * Report an error unless the argument evaluation or function call has been
8391 * cancelled due to an aborting error, an interrupt, or an exception.
8392 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008393 if (!aborting())
8394 {
8395 switch (error)
8396 {
8397 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008398 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008399 break;
8400 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008401 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008402 break;
8403 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008404 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008405 name);
8406 break;
8407 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008408 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008409 name);
8410 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008411 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008412 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008413 name);
8414 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008415 }
8416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 if (fname != name && fname != fname_buf)
8419 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008420 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421
8422 return ret;
8423}
8424
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008425/*
8426 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008427 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008428 */
8429 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008430emsg_funcname(ermsg, name)
8431 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008432 char_u *name;
8433{
8434 char_u *p;
8435
8436 if (*name == K_SPECIAL)
8437 p = concat_str((char_u *)"<SNR>", name + 3);
8438 else
8439 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008440 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008441 if (p != name)
8442 vim_free(p);
8443}
8444
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008445/*
8446 * Return TRUE for a non-zero Number and a non-empty String.
8447 */
8448 static int
8449non_zero_arg(argvars)
8450 typval_T *argvars;
8451{
8452 return ((argvars[0].v_type == VAR_NUMBER
8453 && argvars[0].vval.v_number != 0)
8454 || (argvars[0].v_type == VAR_STRING
8455 && argvars[0].vval.v_string != NULL
8456 && *argvars[0].vval.v_string != NUL));
8457}
8458
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459/*********************************************
8460 * Implementation of the built-in functions
8461 */
8462
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008463#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008464static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8465
8466/*
8467 * Get the float value of "argvars[0]" into "f".
8468 * Returns FAIL when the argument is not a Number or Float.
8469 */
8470 static int
8471get_float_arg(argvars, f)
8472 typval_T *argvars;
8473 float_T *f;
8474{
8475 if (argvars[0].v_type == VAR_FLOAT)
8476 {
8477 *f = argvars[0].vval.v_float;
8478 return OK;
8479 }
8480 if (argvars[0].v_type == VAR_NUMBER)
8481 {
8482 *f = (float_T)argvars[0].vval.v_number;
8483 return OK;
8484 }
8485 EMSG(_("E808: Number or Float required"));
8486 return FAIL;
8487}
8488
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008489/*
8490 * "abs(expr)" function
8491 */
8492 static void
8493f_abs(argvars, rettv)
8494 typval_T *argvars;
8495 typval_T *rettv;
8496{
8497 if (argvars[0].v_type == VAR_FLOAT)
8498 {
8499 rettv->v_type = VAR_FLOAT;
8500 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8501 }
8502 else
8503 {
8504 varnumber_T n;
8505 int error = FALSE;
8506
8507 n = get_tv_number_chk(&argvars[0], &error);
8508 if (error)
8509 rettv->vval.v_number = -1;
8510 else if (n > 0)
8511 rettv->vval.v_number = n;
8512 else
8513 rettv->vval.v_number = -n;
8514 }
8515}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008516
8517/*
8518 * "acos()" function
8519 */
8520 static void
8521f_acos(argvars, rettv)
8522 typval_T *argvars;
8523 typval_T *rettv;
8524{
8525 float_T f;
8526
8527 rettv->v_type = VAR_FLOAT;
8528 if (get_float_arg(argvars, &f) == OK)
8529 rettv->vval.v_float = acos(f);
8530 else
8531 rettv->vval.v_float = 0.0;
8532}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008533#endif
8534
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008536 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 */
8538 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008539f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008540 typval_T *argvars;
8541 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542{
Bram Moolenaar33570922005-01-25 22:26:29 +00008543 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008545 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008546 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008548 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008549 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008550 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008551 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008552 }
8553 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008554 EMSG(_(e_listreq));
8555}
8556
8557/*
8558 * "append(lnum, string/list)" function
8559 */
8560 static void
8561f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008562 typval_T *argvars;
8563 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008564{
8565 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008566 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008567 list_T *l = NULL;
8568 listitem_T *li = NULL;
8569 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008570 long added = 0;
8571
Bram Moolenaar0d660222005-01-07 21:51:51 +00008572 lnum = get_tv_lnum(argvars);
8573 if (lnum >= 0
8574 && lnum <= curbuf->b_ml.ml_line_count
8575 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008576 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008577 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008578 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008579 l = argvars[1].vval.v_list;
8580 if (l == NULL)
8581 return;
8582 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008583 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008584 for (;;)
8585 {
8586 if (l == NULL)
8587 tv = &argvars[1]; /* append a string */
8588 else if (li == NULL)
8589 break; /* end of list */
8590 else
8591 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008592 line = get_tv_string_chk(tv);
8593 if (line == NULL) /* type error */
8594 {
8595 rettv->vval.v_number = 1; /* Failed */
8596 break;
8597 }
8598 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008599 ++added;
8600 if (l == NULL)
8601 break;
8602 li = li->li_next;
8603 }
8604
8605 appended_lines_mark(lnum, added);
8606 if (curwin->w_cursor.lnum > lnum)
8607 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008609 else
8610 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611}
8612
8613/*
8614 * "argc()" function
8615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008617f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008618 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008621 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622}
8623
8624/*
8625 * "argidx()" function
8626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008628f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008629 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008632 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633}
8634
8635/*
8636 * "argv(nr)" function
8637 */
8638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008639f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008640 typval_T *argvars;
8641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642{
8643 int idx;
8644
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008645 if (argvars[0].v_type != VAR_UNKNOWN)
8646 {
8647 idx = get_tv_number_chk(&argvars[0], NULL);
8648 if (idx >= 0 && idx < ARGCOUNT)
8649 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8650 else
8651 rettv->vval.v_string = NULL;
8652 rettv->v_type = VAR_STRING;
8653 }
8654 else if (rettv_list_alloc(rettv) == OK)
8655 for (idx = 0; idx < ARGCOUNT; ++idx)
8656 list_append_string(rettv->vval.v_list,
8657 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658}
8659
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008660#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008661/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008662 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008663 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008664 static void
8665f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008666 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008667 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008668{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008669 float_T f;
8670
8671 rettv->v_type = VAR_FLOAT;
8672 if (get_float_arg(argvars, &f) == OK)
8673 rettv->vval.v_float = asin(f);
8674 else
8675 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008676}
8677
8678/*
8679 * "atan()" function
8680 */
8681 static void
8682f_atan(argvars, rettv)
8683 typval_T *argvars;
8684 typval_T *rettv;
8685{
8686 float_T f;
8687
8688 rettv->v_type = VAR_FLOAT;
8689 if (get_float_arg(argvars, &f) == OK)
8690 rettv->vval.v_float = atan(f);
8691 else
8692 rettv->vval.v_float = 0.0;
8693}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008694
8695/*
8696 * "atan2()" function
8697 */
8698 static void
8699f_atan2(argvars, rettv)
8700 typval_T *argvars;
8701 typval_T *rettv;
8702{
8703 float_T fx, fy;
8704
8705 rettv->v_type = VAR_FLOAT;
8706 if (get_float_arg(argvars, &fx) == OK
8707 && get_float_arg(&argvars[1], &fy) == OK)
8708 rettv->vval.v_float = atan2(fx, fy);
8709 else
8710 rettv->vval.v_float = 0.0;
8711}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008712#endif
8713
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714/*
8715 * "browse(save, title, initdir, default)" function
8716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008718f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008719 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721{
8722#ifdef FEAT_BROWSE
8723 int save;
8724 char_u *title;
8725 char_u *initdir;
8726 char_u *defname;
8727 char_u buf[NUMBUFLEN];
8728 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008729 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008731 save = get_tv_number_chk(&argvars[0], &error);
8732 title = get_tv_string_chk(&argvars[1]);
8733 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8734 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008736 if (error || title == NULL || initdir == NULL || defname == NULL)
8737 rettv->vval.v_string = NULL;
8738 else
8739 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008740 do_browse(save ? BROWSE_SAVE : 0,
8741 title, defname, NULL, initdir, NULL, curbuf);
8742#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008743 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008744#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008745 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008746}
8747
8748/*
8749 * "browsedir(title, initdir)" function
8750 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008752f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008753 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008754 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008755{
8756#ifdef FEAT_BROWSE
8757 char_u *title;
8758 char_u *initdir;
8759 char_u buf[NUMBUFLEN];
8760
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008761 title = get_tv_string_chk(&argvars[0]);
8762 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008763
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008764 if (title == NULL || initdir == NULL)
8765 rettv->vval.v_string = NULL;
8766 else
8767 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008768 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008772 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773}
8774
Bram Moolenaar33570922005-01-25 22:26:29 +00008775static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008776
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777/*
8778 * Find a buffer by number or exact name.
8779 */
8780 static buf_T *
8781find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008782 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783{
8784 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008786 if (avar->v_type == VAR_NUMBER)
8787 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008788 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008790 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008791 if (buf == NULL)
8792 {
8793 /* No full path name match, try a match with a URL or a "nofile"
8794 * buffer, these don't use the full path. */
8795 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8796 if (buf->b_fname != NULL
8797 && (path_with_url(buf->b_fname)
8798#ifdef FEAT_QUICKFIX
8799 || bt_nofile(buf)
8800#endif
8801 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008802 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008803 break;
8804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 }
8806 return buf;
8807}
8808
8809/*
8810 * "bufexists(expr)" function
8811 */
8812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008813f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008814 typval_T *argvars;
8815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008817 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818}
8819
8820/*
8821 * "buflisted(expr)" function
8822 */
8823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008824f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008825 typval_T *argvars;
8826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827{
8828 buf_T *buf;
8829
8830 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832}
8833
8834/*
8835 * "bufloaded(expr)" function
8836 */
8837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008838f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008839 typval_T *argvars;
8840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841{
8842 buf_T *buf;
8843
8844 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008845 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846}
8847
Bram Moolenaar33570922005-01-25 22:26:29 +00008848static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008849
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850/*
8851 * Get buffer by number or pattern.
8852 */
8853 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008854get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008855 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008857 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 int save_magic;
8859 char_u *save_cpo;
8860 buf_T *buf;
8861
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008862 if (tv->v_type == VAR_NUMBER)
8863 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008864 if (tv->v_type != VAR_STRING)
8865 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 if (name == NULL || *name == NUL)
8867 return curbuf;
8868 if (name[0] == '$' && name[1] == NUL)
8869 return lastbuf;
8870
8871 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8872 save_magic = p_magic;
8873 p_magic = TRUE;
8874 save_cpo = p_cpo;
8875 p_cpo = (char_u *)"";
8876
8877 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8878 TRUE, FALSE));
8879
8880 p_magic = save_magic;
8881 p_cpo = save_cpo;
8882
8883 /* If not found, try expanding the name, like done for bufexists(). */
8884 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008885 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886
8887 return buf;
8888}
8889
8890/*
8891 * "bufname(expr)" function
8892 */
8893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008894f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008895 typval_T *argvars;
8896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897{
8898 buf_T *buf;
8899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008900 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008902 buf = get_buf_tv(&argvars[0]);
8903 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008905 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008907 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 --emsg_off;
8909}
8910
8911/*
8912 * "bufnr(expr)" function
8913 */
8914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008915f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008916 typval_T *argvars;
8917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918{
8919 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008920 int error = FALSE;
8921 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008923 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008925 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008926 --emsg_off;
8927
8928 /* If the buffer isn't found and the second argument is not zero create a
8929 * new buffer. */
8930 if (buf == NULL
8931 && argvars[1].v_type != VAR_UNKNOWN
8932 && get_tv_number_chk(&argvars[1], &error) != 0
8933 && !error
8934 && (name = get_tv_string_chk(&argvars[0])) != NULL
8935 && !error)
8936 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8937
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008939 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008941 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942}
8943
8944/*
8945 * "bufwinnr(nr)" function
8946 */
8947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008948f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008949 typval_T *argvars;
8950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951{
8952#ifdef FEAT_WINDOWS
8953 win_T *wp;
8954 int winnr = 0;
8955#endif
8956 buf_T *buf;
8957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008958 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008960 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961#ifdef FEAT_WINDOWS
8962 for (wp = firstwin; wp; wp = wp->w_next)
8963 {
8964 ++winnr;
8965 if (wp->w_buffer == buf)
8966 break;
8967 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008968 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008970 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971#endif
8972 --emsg_off;
8973}
8974
8975/*
8976 * "byte2line(byte)" function
8977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008979f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008980 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982{
8983#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008984 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985#else
8986 long boff = 0;
8987
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008988 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008990 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008992 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 (linenr_T)0, &boff);
8994#endif
8995}
8996
8997/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008998 * "byteidx()" function
8999 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009001f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009002 typval_T *argvars;
9003 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009004{
9005#ifdef FEAT_MBYTE
9006 char_u *t;
9007#endif
9008 char_u *str;
9009 long idx;
9010
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009011 str = get_tv_string_chk(&argvars[0]);
9012 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009013 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009014 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009015 return;
9016
9017#ifdef FEAT_MBYTE
9018 t = str;
9019 for ( ; idx > 0; idx--)
9020 {
9021 if (*t == NUL) /* EOL reached */
9022 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009023 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009024 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009025 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009026#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009027 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009028 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009029#endif
9030}
9031
9032/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009033 * "call(func, arglist)" function
9034 */
9035 static void
9036f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009037 typval_T *argvars;
9038 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009039{
9040 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009041 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009042 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00009043 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009044 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00009045 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009046
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009047 if (argvars[1].v_type != VAR_LIST)
9048 {
9049 EMSG(_(e_listreq));
9050 return;
9051 }
9052 if (argvars[1].vval.v_list == NULL)
9053 return;
9054
9055 if (argvars[0].v_type == VAR_FUNC)
9056 func = argvars[0].vval.v_string;
9057 else
9058 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009059 if (*func == NUL)
9060 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009061
Bram Moolenaare9a41262005-01-15 22:18:47 +00009062 if (argvars[2].v_type != VAR_UNKNOWN)
9063 {
9064 if (argvars[2].v_type != VAR_DICT)
9065 {
9066 EMSG(_(e_dictreq));
9067 return;
9068 }
9069 selfdict = argvars[2].vval.v_dict;
9070 }
9071
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009072 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9073 item = item->li_next)
9074 {
9075 if (argc == MAX_FUNC_ARGS)
9076 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009077 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009078 break;
9079 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009080 /* Make a copy of each argument. This is needed to be able to set
9081 * v_lock to VAR_FIXED in the copy without changing the original list.
9082 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009083 copy_tv(&item->li_tv, &argv[argc++]);
9084 }
9085
9086 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009087 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009088 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9089 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009090
9091 /* Free the arguments. */
9092 while (argc > 0)
9093 clear_tv(&argv[--argc]);
9094}
9095
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009096#ifdef FEAT_FLOAT
9097/*
9098 * "ceil({float})" function
9099 */
9100 static void
9101f_ceil(argvars, rettv)
9102 typval_T *argvars;
9103 typval_T *rettv;
9104{
9105 float_T f;
9106
9107 rettv->v_type = VAR_FLOAT;
9108 if (get_float_arg(argvars, &f) == OK)
9109 rettv->vval.v_float = ceil(f);
9110 else
9111 rettv->vval.v_float = 0.0;
9112}
9113#endif
9114
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009115/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009116 * "changenr()" function
9117 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009118 static void
9119f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009120 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009121 typval_T *rettv;
9122{
9123 rettv->vval.v_number = curbuf->b_u_seq_cur;
9124}
9125
9126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 * "char2nr(string)" function
9128 */
9129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009130f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009131 typval_T *argvars;
9132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133{
9134#ifdef FEAT_MBYTE
9135 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009136 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 else
9138#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009139 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140}
9141
9142/*
9143 * "cindent(lnum)" function
9144 */
9145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009146f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009147 typval_T *argvars;
9148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149{
9150#ifdef FEAT_CINDENT
9151 pos_T pos;
9152 linenr_T lnum;
9153
9154 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009155 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9157 {
9158 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009159 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 curwin->w_cursor = pos;
9161 }
9162 else
9163#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009164 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165}
9166
9167/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009168 * "clearmatches()" function
9169 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009170 static void
9171f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009172 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009173 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009174{
9175#ifdef FEAT_SEARCH_EXTRA
9176 clear_matches(curwin);
9177#endif
9178}
9179
9180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 * "col(string)" function
9182 */
9183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009185 typval_T *argvars;
9186 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187{
9188 colnr_T col = 0;
9189 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009190 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009192 fp = var2fpos(&argvars[0], FALSE, &fnum);
9193 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 {
9195 if (fp->col == MAXCOL)
9196 {
9197 /* '> can be MAXCOL, get the length of the line then */
9198 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009199 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 else
9201 col = MAXCOL;
9202 }
9203 else
9204 {
9205 col = fp->col + 1;
9206#ifdef FEAT_VIRTUALEDIT
9207 /* col(".") when the cursor is on the NUL at the end of the line
9208 * because of "coladd" can be seen as an extra column. */
9209 if (virtual_active() && fp == &curwin->w_cursor)
9210 {
9211 char_u *p = ml_get_cursor();
9212
9213 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9214 curwin->w_virtcol - curwin->w_cursor.coladd))
9215 {
9216# ifdef FEAT_MBYTE
9217 int l;
9218
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009219 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 col += l;
9221# else
9222 if (*p != NUL && p[1] == NUL)
9223 ++col;
9224# endif
9225 }
9226 }
9227#endif
9228 }
9229 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009230 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231}
9232
Bram Moolenaar572cb562005-08-05 21:35:02 +00009233#if defined(FEAT_INS_EXPAND)
9234/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009235 * "complete()" function
9236 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009237 static void
9238f_complete(argvars, rettv)
9239 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009240 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009241{
9242 int startcol;
9243
9244 if ((State & INSERT) == 0)
9245 {
9246 EMSG(_("E785: complete() can only be used in Insert mode"));
9247 return;
9248 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009249
9250 /* Check for undo allowed here, because if something was already inserted
9251 * the line was already saved for undo and this check isn't done. */
9252 if (!undo_allowed())
9253 return;
9254
Bram Moolenaarade00832006-03-10 21:46:58 +00009255 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9256 {
9257 EMSG(_(e_invarg));
9258 return;
9259 }
9260
9261 startcol = get_tv_number_chk(&argvars[0], NULL);
9262 if (startcol <= 0)
9263 return;
9264
9265 set_completion(startcol - 1, argvars[1].vval.v_list);
9266}
9267
9268/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009269 * "complete_add()" function
9270 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009271 static void
9272f_complete_add(argvars, rettv)
9273 typval_T *argvars;
9274 typval_T *rettv;
9275{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009276 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009277}
9278
9279/*
9280 * "complete_check()" function
9281 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009282 static void
9283f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009284 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009285 typval_T *rettv;
9286{
9287 int saved = RedrawingDisabled;
9288
9289 RedrawingDisabled = 0;
9290 ins_compl_check_keys(0);
9291 rettv->vval.v_number = compl_interrupted;
9292 RedrawingDisabled = saved;
9293}
9294#endif
9295
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296/*
9297 * "confirm(message, buttons[, default [, type]])" function
9298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009300f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009301 typval_T *argvars UNUSED;
9302 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303{
9304#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9305 char_u *message;
9306 char_u *buttons = NULL;
9307 char_u buf[NUMBUFLEN];
9308 char_u buf2[NUMBUFLEN];
9309 int def = 1;
9310 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009311 char_u *typestr;
9312 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009314 message = get_tv_string_chk(&argvars[0]);
9315 if (message == NULL)
9316 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009317 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009319 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9320 if (buttons == NULL)
9321 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009322 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009324 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009325 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009327 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9328 if (typestr == NULL)
9329 error = TRUE;
9330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009332 switch (TOUPPER_ASC(*typestr))
9333 {
9334 case 'E': type = VIM_ERROR; break;
9335 case 'Q': type = VIM_QUESTION; break;
9336 case 'I': type = VIM_INFO; break;
9337 case 'W': type = VIM_WARNING; break;
9338 case 'G': type = VIM_GENERIC; break;
9339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 }
9341 }
9342 }
9343 }
9344
9345 if (buttons == NULL || *buttons == NUL)
9346 buttons = (char_u *)_("&Ok");
9347
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009348 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009349 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009350 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351#endif
9352}
9353
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009354/*
9355 * "copy()" function
9356 */
9357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009358f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009359 typval_T *argvars;
9360 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009361{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009362 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009363}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009365#ifdef FEAT_FLOAT
9366/*
9367 * "cos()" function
9368 */
9369 static void
9370f_cos(argvars, rettv)
9371 typval_T *argvars;
9372 typval_T *rettv;
9373{
9374 float_T f;
9375
9376 rettv->v_type = VAR_FLOAT;
9377 if (get_float_arg(argvars, &f) == OK)
9378 rettv->vval.v_float = cos(f);
9379 else
9380 rettv->vval.v_float = 0.0;
9381}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009382
9383/*
9384 * "cosh()" function
9385 */
9386 static void
9387f_cosh(argvars, rettv)
9388 typval_T *argvars;
9389 typval_T *rettv;
9390{
9391 float_T f;
9392
9393 rettv->v_type = VAR_FLOAT;
9394 if (get_float_arg(argvars, &f) == OK)
9395 rettv->vval.v_float = cosh(f);
9396 else
9397 rettv->vval.v_float = 0.0;
9398}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009399#endif
9400
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009402 * "count()" function
9403 */
9404 static void
9405f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009406 typval_T *argvars;
9407 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009408{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009409 long n = 0;
9410 int ic = FALSE;
9411
Bram Moolenaare9a41262005-01-15 22:18:47 +00009412 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009413 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009414 listitem_T *li;
9415 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009416 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009417
Bram Moolenaare9a41262005-01-15 22:18:47 +00009418 if ((l = argvars[0].vval.v_list) != NULL)
9419 {
9420 li = l->lv_first;
9421 if (argvars[2].v_type != VAR_UNKNOWN)
9422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009423 int error = FALSE;
9424
9425 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009426 if (argvars[3].v_type != VAR_UNKNOWN)
9427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009428 idx = get_tv_number_chk(&argvars[3], &error);
9429 if (!error)
9430 {
9431 li = list_find(l, idx);
9432 if (li == NULL)
9433 EMSGN(_(e_listidx), idx);
9434 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009435 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009436 if (error)
9437 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009438 }
9439
9440 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009441 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009442 ++n;
9443 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009444 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009445 else if (argvars[0].v_type == VAR_DICT)
9446 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009447 int todo;
9448 dict_T *d;
9449 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009450
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009451 if ((d = argvars[0].vval.v_dict) != NULL)
9452 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009453 int error = FALSE;
9454
Bram Moolenaare9a41262005-01-15 22:18:47 +00009455 if (argvars[2].v_type != VAR_UNKNOWN)
9456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009457 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009458 if (argvars[3].v_type != VAR_UNKNOWN)
9459 EMSG(_(e_invarg));
9460 }
9461
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009462 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009463 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009464 {
9465 if (!HASHITEM_EMPTY(hi))
9466 {
9467 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009468 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009469 ++n;
9470 }
9471 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009472 }
9473 }
9474 else
9475 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009476 rettv->vval.v_number = n;
9477}
9478
9479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9481 *
9482 * Checks the existence of a cscope connection.
9483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009485f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009486 typval_T *argvars UNUSED;
9487 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488{
9489#ifdef FEAT_CSCOPE
9490 int num = 0;
9491 char_u *dbpath = NULL;
9492 char_u *prepend = NULL;
9493 char_u buf[NUMBUFLEN];
9494
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009495 if (argvars[0].v_type != VAR_UNKNOWN
9496 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009498 num = (int)get_tv_number(&argvars[0]);
9499 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009500 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009501 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 }
9503
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009504 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505#endif
9506}
9507
9508/*
9509 * "cursor(lnum, col)" function
9510 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009511 * Moves the cursor to the specified line and column.
9512 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009515f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009516 typval_T *argvars;
9517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518{
9519 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009520#ifdef FEAT_VIRTUALEDIT
9521 long coladd = 0;
9522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009524 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009525 if (argvars[1].v_type == VAR_UNKNOWN)
9526 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009527 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009528
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009529 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009530 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009531 line = pos.lnum;
9532 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009533#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009534 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009535#endif
9536 }
9537 else
9538 {
9539 line = get_tv_lnum(argvars);
9540 col = get_tv_number_chk(&argvars[1], NULL);
9541#ifdef FEAT_VIRTUALEDIT
9542 if (argvars[2].v_type != VAR_UNKNOWN)
9543 coladd = get_tv_number_chk(&argvars[2], NULL);
9544#endif
9545 }
9546 if (line < 0 || col < 0
9547#ifdef FEAT_VIRTUALEDIT
9548 || coladd < 0
9549#endif
9550 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009551 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 if (line > 0)
9553 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 if (col > 0)
9555 curwin->w_cursor.col = col - 1;
9556#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009557 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558#endif
9559
9560 /* Make sure the cursor is in a valid position. */
9561 check_cursor();
9562#ifdef FEAT_MBYTE
9563 /* Correct cursor for multi-byte character. */
9564 if (has_mbyte)
9565 mb_adjust_cursor();
9566#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009567
9568 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009569 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570}
9571
9572/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009573 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 */
9575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009576f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009577 typval_T *argvars;
9578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009580 int noref = 0;
9581
9582 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009583 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009584 if (noref < 0 || noref > 1)
9585 EMSG(_(e_invarg));
9586 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009587 {
9588 current_copyID += COPYID_INC;
9589 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591}
9592
9593/*
9594 * "delete()" function
9595 */
9596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009597f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009598 typval_T *argvars;
9599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600{
9601 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009602 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009604 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605}
9606
9607/*
9608 * "did_filetype()" function
9609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009611f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009612 typval_T *argvars UNUSED;
9613 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614{
9615#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617#endif
9618}
9619
9620/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009621 * "diff_filler()" function
9622 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009624f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009625 typval_T *argvars UNUSED;
9626 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009627{
9628#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009630#endif
9631}
9632
9633/*
9634 * "diff_hlID()" function
9635 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009637f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009638 typval_T *argvars UNUSED;
9639 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009640{
9641#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009642 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009643 static linenr_T prev_lnum = 0;
9644 static int changedtick = 0;
9645 static int fnum = 0;
9646 static int change_start = 0;
9647 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009648 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009649 int filler_lines;
9650 int col;
9651
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009652 if (lnum < 0) /* ignore type error in {lnum} arg */
9653 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009654 if (lnum != prev_lnum
9655 || changedtick != curbuf->b_changedtick
9656 || fnum != curbuf->b_fnum)
9657 {
9658 /* New line, buffer, change: need to get the values. */
9659 filler_lines = diff_check(curwin, lnum);
9660 if (filler_lines < 0)
9661 {
9662 if (filler_lines == -1)
9663 {
9664 change_start = MAXCOL;
9665 change_end = -1;
9666 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9667 hlID = HLF_ADD; /* added line */
9668 else
9669 hlID = HLF_CHD; /* changed line */
9670 }
9671 else
9672 hlID = HLF_ADD; /* added line */
9673 }
9674 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009675 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009676 prev_lnum = lnum;
9677 changedtick = curbuf->b_changedtick;
9678 fnum = curbuf->b_fnum;
9679 }
9680
9681 if (hlID == HLF_CHD || hlID == HLF_TXD)
9682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009683 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009684 if (col >= change_start && col <= change_end)
9685 hlID = HLF_TXD; /* changed text */
9686 else
9687 hlID = HLF_CHD; /* changed line */
9688 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009689 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009690#endif
9691}
9692
9693/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009694 * "empty({expr})" function
9695 */
9696 static void
9697f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009698 typval_T *argvars;
9699 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009700{
9701 int n;
9702
9703 switch (argvars[0].v_type)
9704 {
9705 case VAR_STRING:
9706 case VAR_FUNC:
9707 n = argvars[0].vval.v_string == NULL
9708 || *argvars[0].vval.v_string == NUL;
9709 break;
9710 case VAR_NUMBER:
9711 n = argvars[0].vval.v_number == 0;
9712 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009713#ifdef FEAT_FLOAT
9714 case VAR_FLOAT:
9715 n = argvars[0].vval.v_float == 0.0;
9716 break;
9717#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009718 case VAR_LIST:
9719 n = argvars[0].vval.v_list == NULL
9720 || argvars[0].vval.v_list->lv_first == NULL;
9721 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009722 case VAR_DICT:
9723 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009724 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009725 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009726 default:
9727 EMSG2(_(e_intern2), "f_empty()");
9728 n = 0;
9729 }
9730
9731 rettv->vval.v_number = n;
9732}
9733
9734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 * "escape({string}, {chars})" function
9736 */
9737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009739 typval_T *argvars;
9740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741{
9742 char_u buf[NUMBUFLEN];
9743
Bram Moolenaar758711c2005-02-02 23:11:38 +00009744 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9745 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009746 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747}
9748
9749/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009750 * "eval()" function
9751 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009752 static void
9753f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009754 typval_T *argvars;
9755 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009756{
9757 char_u *s;
9758
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009759 s = get_tv_string_chk(&argvars[0]);
9760 if (s != NULL)
9761 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009763 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9764 {
9765 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009766 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009767 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009768 else if (*s != NUL)
9769 EMSG(_(e_trailing));
9770}
9771
9772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 * "eventhandler()" function
9774 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009776f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009777 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009780 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781}
9782
9783/*
9784 * "executable()" function
9785 */
9786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009787f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009788 typval_T *argvars;
9789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009791 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792}
9793
9794/*
9795 * "exists()" function
9796 */
9797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009798f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009799 typval_T *argvars;
9800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801{
9802 char_u *p;
9803 char_u *name;
9804 int n = FALSE;
9805 int len = 0;
9806
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009807 no_autoload = TRUE;
9808
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 if (*p == '$') /* environment variable */
9811 {
9812 /* first try "normal" environment variables (fast) */
9813 if (mch_getenv(p + 1) != NULL)
9814 n = TRUE;
9815 else
9816 {
9817 /* try expanding things like $VIM and ${HOME} */
9818 p = expand_env_save(p);
9819 if (p != NULL && *p != '$')
9820 n = TRUE;
9821 vim_free(p);
9822 }
9823 }
9824 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009825 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009826 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009827 if (*skipwhite(p) != NUL)
9828 n = FALSE; /* trailing garbage */
9829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 else if (*p == '*') /* internal or user defined function */
9831 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009832 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 }
9834 else if (*p == ':')
9835 {
9836 n = cmd_exists(p + 1);
9837 }
9838 else if (*p == '#')
9839 {
9840#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009841 if (p[1] == '#')
9842 n = autocmd_supported(p + 2);
9843 else
9844 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845#endif
9846 }
9847 else /* internal variable */
9848 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009849 char_u *tofree;
9850 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009852 /* get_name_len() takes care of expanding curly braces */
9853 name = p;
9854 len = get_name_len(&p, &tofree, TRUE, FALSE);
9855 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009857 if (tofree != NULL)
9858 name = tofree;
9859 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9860 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009862 /* handle d.key, l[idx], f(expr) */
9863 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9864 if (n)
9865 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 }
9867 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009868 if (*p != NUL)
9869 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009871 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 }
9873
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009874 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009875
9876 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877}
9878
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009879#ifdef FEAT_FLOAT
9880/*
9881 * "exp()" function
9882 */
9883 static void
9884f_exp(argvars, rettv)
9885 typval_T *argvars;
9886 typval_T *rettv;
9887{
9888 float_T f;
9889
9890 rettv->v_type = VAR_FLOAT;
9891 if (get_float_arg(argvars, &f) == OK)
9892 rettv->vval.v_float = exp(f);
9893 else
9894 rettv->vval.v_float = 0.0;
9895}
9896#endif
9897
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898/*
9899 * "expand()" function
9900 */
9901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009902f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009903 typval_T *argvars;
9904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905{
9906 char_u *s;
9907 int len;
9908 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009909 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009911 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009913 rettv->v_type = VAR_STRING;
9914 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 if (*s == '%' || *s == '#' || *s == '<')
9916 {
9917 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009918 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 --emsg_off;
9920 }
9921 else
9922 {
9923 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009924 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009925 if (argvars[1].v_type != VAR_UNKNOWN
9926 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009927 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009928 if (!error)
9929 {
9930 ExpandInit(&xpc);
9931 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009932 if (p_wic)
9933 options += WILD_ICASE;
9934 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, options, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009935 }
9936 else
9937 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 }
9939}
9940
9941/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009942 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009943 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009944 */
9945 static void
9946f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009947 typval_T *argvars;
9948 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009949{
Bram Moolenaar32f649e2011-04-11 13:46:13 +02009950 char *arg_errmsg = N_("extend() argument");
9951
Bram Moolenaare9a41262005-01-15 22:18:47 +00009952 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009953 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009954 list_T *l1, *l2;
9955 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009956 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009957 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009958
Bram Moolenaare9a41262005-01-15 22:18:47 +00009959 l1 = argvars[0].vval.v_list;
9960 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +02009961 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009962 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009963 {
9964 if (argvars[2].v_type != VAR_UNKNOWN)
9965 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009966 before = get_tv_number_chk(&argvars[2], &error);
9967 if (error)
9968 return; /* type error; errmsg already given */
9969
Bram Moolenaar758711c2005-02-02 23:11:38 +00009970 if (before == l1->lv_len)
9971 item = NULL;
9972 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009973 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009974 item = list_find(l1, before);
9975 if (item == NULL)
9976 {
9977 EMSGN(_(e_listidx), before);
9978 return;
9979 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009980 }
9981 }
9982 else
9983 item = NULL;
9984 list_extend(l1, l2, item);
9985
Bram Moolenaare9a41262005-01-15 22:18:47 +00009986 copy_tv(&argvars[0], rettv);
9987 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009988 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009989 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9990 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009991 dict_T *d1, *d2;
9992 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009993 char_u *action;
9994 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009995 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009996 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009997
9998 d1 = argvars[0].vval.v_dict;
9999 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010000 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010001 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010002 {
10003 /* Check the third argument. */
10004 if (argvars[2].v_type != VAR_UNKNOWN)
10005 {
10006 static char *(av[]) = {"keep", "force", "error"};
10007
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010008 action = get_tv_string_chk(&argvars[2]);
10009 if (action == NULL)
10010 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010011 for (i = 0; i < 3; ++i)
10012 if (STRCMP(action, av[i]) == 0)
10013 break;
10014 if (i == 3)
10015 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010016 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010017 return;
10018 }
10019 }
10020 else
10021 action = (char_u *)"force";
10022
10023 /* Go over all entries in the second dict and add them to the
10024 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010025 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010026 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010027 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010028 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010029 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010030 --todo;
10031 di1 = dict_find(d1, hi2->hi_key, -1);
10032 if (di1 == NULL)
10033 {
10034 di1 = dictitem_copy(HI2DI(hi2));
10035 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10036 dictitem_free(di1);
10037 }
10038 else if (*action == 'e')
10039 {
10040 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10041 break;
10042 }
10043 else if (*action == 'f')
10044 {
10045 clear_tv(&di1->di_tv);
10046 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10047 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010048 }
10049 }
10050
Bram Moolenaare9a41262005-01-15 22:18:47 +000010051 copy_tv(&argvars[0], rettv);
10052 }
10053 }
10054 else
10055 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010056}
10057
10058/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010059 * "feedkeys()" function
10060 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010061 static void
10062f_feedkeys(argvars, rettv)
10063 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010064 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010065{
10066 int remap = TRUE;
10067 char_u *keys, *flags;
10068 char_u nbuf[NUMBUFLEN];
10069 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010070 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010071
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010072 /* This is not allowed in the sandbox. If the commands would still be
10073 * executed in the sandbox it would be OK, but it probably happens later,
10074 * when "sandbox" is no longer set. */
10075 if (check_secure())
10076 return;
10077
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010078 keys = get_tv_string(&argvars[0]);
10079 if (*keys != NUL)
10080 {
10081 if (argvars[1].v_type != VAR_UNKNOWN)
10082 {
10083 flags = get_tv_string_buf(&argvars[1], nbuf);
10084 for ( ; *flags != NUL; ++flags)
10085 {
10086 switch (*flags)
10087 {
10088 case 'n': remap = FALSE; break;
10089 case 'm': remap = TRUE; break;
10090 case 't': typed = TRUE; break;
10091 }
10092 }
10093 }
10094
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010095 /* Need to escape K_SPECIAL and CSI before putting the string in the
10096 * typeahead buffer. */
10097 keys_esc = vim_strsave_escape_csi(keys);
10098 if (keys_esc != NULL)
10099 {
10100 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010101 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010102 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010103 if (vgetc_busy)
10104 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010105 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010106 }
10107}
10108
10109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 * "filereadable()" function
10111 */
10112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010113f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010114 typval_T *argvars;
10115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010117 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 char_u *p;
10119 int n;
10120
Bram Moolenaarc236c162008-07-13 17:41:49 +000010121#ifndef O_NONBLOCK
10122# define O_NONBLOCK 0
10123#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010124 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010125 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10126 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 {
10128 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010129 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 }
10131 else
10132 n = FALSE;
10133
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010134 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135}
10136
10137/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010138 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 * rights to write into.
10140 */
10141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010142f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010143 typval_T *argvars;
10144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010146 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147}
10148
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010149static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010150
10151 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010152findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010153 typval_T *argvars;
10154 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010155 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010156{
10157#ifdef FEAT_SEARCHPATH
10158 char_u *fname;
10159 char_u *fresult = NULL;
10160 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10161 char_u *p;
10162 char_u pathbuf[NUMBUFLEN];
10163 int count = 1;
10164 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010165 int error = FALSE;
10166#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010167
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010168 rettv->vval.v_string = NULL;
10169 rettv->v_type = VAR_STRING;
10170
10171#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010172 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010173
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010174 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010176 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10177 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010178 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010179 else
10180 {
10181 if (*p != NUL)
10182 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010183
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010184 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010185 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010186 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010187 }
10188
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010189 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10190 error = TRUE;
10191
10192 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010194 do
10195 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010196 if (rettv->v_type == VAR_STRING)
10197 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010198 fresult = find_file_in_path_option(first ? fname : NULL,
10199 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010200 0, first, path,
10201 find_what,
10202 curbuf->b_ffname,
10203 find_what == FINDFILE_DIR
10204 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010205 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010206
10207 if (fresult != NULL && rettv->v_type == VAR_LIST)
10208 list_append_string(rettv->vval.v_list, fresult, -1);
10209
10210 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010211 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010212
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010213 if (rettv->v_type == VAR_STRING)
10214 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010215#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010216}
10217
Bram Moolenaar33570922005-01-25 22:26:29 +000010218static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10219static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010220
10221/*
10222 * Implementation of map() and filter().
10223 */
10224 static void
10225filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010226 typval_T *argvars;
10227 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010228 int map;
10229{
10230 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010231 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010232 listitem_T *li, *nli;
10233 list_T *l = NULL;
10234 dictitem_T *di;
10235 hashtab_T *ht;
10236 hashitem_T *hi;
10237 dict_T *d = NULL;
10238 typval_T save_val;
10239 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010240 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010241 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010242 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10243 char *arg_errmsg = (map ? N_("map() argument")
10244 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010245 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010246 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010247
Bram Moolenaare9a41262005-01-15 22:18:47 +000010248 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010249 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010250 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010251 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010252 return;
10253 }
10254 else if (argvars[0].v_type == VAR_DICT)
10255 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010256 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010257 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010258 return;
10259 }
10260 else
10261 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010262 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010263 return;
10264 }
10265
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010266 expr = get_tv_string_buf_chk(&argvars[1], buf);
10267 /* On type errors, the preceding call has already displayed an error
10268 * message. Avoid a misleading error message for an empty string that
10269 * was not passed as argument. */
10270 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010271 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010272 prepare_vimvar(VV_VAL, &save_val);
10273 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010274
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010275 /* We reset "did_emsg" to be able to detect whether an error
10276 * occurred during evaluation of the expression. */
10277 save_did_emsg = did_emsg;
10278 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010279
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010280 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010281 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010283 vimvars[VV_KEY].vv_type = VAR_STRING;
10284
10285 ht = &d->dv_hashtab;
10286 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010287 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010288 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010289 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010290 if (!HASHITEM_EMPTY(hi))
10291 {
10292 --todo;
10293 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010294 if (tv_check_lock(di->di_tv.v_lock,
10295 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010296 break;
10297 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010298 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010299 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010300 break;
10301 if (!map && rem)
10302 dictitem_remove(d, di);
10303 clear_tv(&vimvars[VV_KEY].vv_tv);
10304 }
10305 }
10306 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010307 }
10308 else
10309 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010310 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10311
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010312 for (li = l->lv_first; li != NULL; li = nli)
10313 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010314 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010315 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010316 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010317 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010318 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010319 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010320 break;
10321 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010322 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010323 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010324 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010325 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010326
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010327 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010328 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010329
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010330 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010331 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010332
10333 copy_tv(&argvars[0], rettv);
10334}
10335
10336 static int
10337filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010338 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010339 char_u *expr;
10340 int map;
10341 int *remp;
10342{
Bram Moolenaar33570922005-01-25 22:26:29 +000010343 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010344 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010345 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010346
Bram Moolenaar33570922005-01-25 22:26:29 +000010347 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010348 s = expr;
10349 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010350 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010351 if (*s != NUL) /* check for trailing chars after expr */
10352 {
10353 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010354 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010355 }
10356 if (map)
10357 {
10358 /* map(): replace the list item value */
10359 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010360 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010361 *tv = rettv;
10362 }
10363 else
10364 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010365 int error = FALSE;
10366
Bram Moolenaare9a41262005-01-15 22:18:47 +000010367 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010368 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010369 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010370 /* On type error, nothing has been removed; return FAIL to stop the
10371 * loop. The error message was given by get_tv_number_chk(). */
10372 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010373 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010374 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010375 retval = OK;
10376theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010377 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010378 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010379}
10380
10381/*
10382 * "filter()" function
10383 */
10384 static void
10385f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010386 typval_T *argvars;
10387 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010388{
10389 filter_map(argvars, rettv, FALSE);
10390}
10391
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010392/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010393 * "finddir({fname}[, {path}[, {count}]])" function
10394 */
10395 static void
10396f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010397 typval_T *argvars;
10398 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010399{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010400 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010401}
10402
10403/*
10404 * "findfile({fname}[, {path}[, {count}]])" function
10405 */
10406 static void
10407f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010408 typval_T *argvars;
10409 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010410{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010411 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010412}
10413
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010414#ifdef FEAT_FLOAT
10415/*
10416 * "float2nr({float})" function
10417 */
10418 static void
10419f_float2nr(argvars, rettv)
10420 typval_T *argvars;
10421 typval_T *rettv;
10422{
10423 float_T f;
10424
10425 if (get_float_arg(argvars, &f) == OK)
10426 {
10427 if (f < -0x7fffffff)
10428 rettv->vval.v_number = -0x7fffffff;
10429 else if (f > 0x7fffffff)
10430 rettv->vval.v_number = 0x7fffffff;
10431 else
10432 rettv->vval.v_number = (varnumber_T)f;
10433 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010434}
10435
10436/*
10437 * "floor({float})" function
10438 */
10439 static void
10440f_floor(argvars, rettv)
10441 typval_T *argvars;
10442 typval_T *rettv;
10443{
10444 float_T f;
10445
10446 rettv->v_type = VAR_FLOAT;
10447 if (get_float_arg(argvars, &f) == OK)
10448 rettv->vval.v_float = floor(f);
10449 else
10450 rettv->vval.v_float = 0.0;
10451}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010452
10453/*
10454 * "fmod()" function
10455 */
10456 static void
10457f_fmod(argvars, rettv)
10458 typval_T *argvars;
10459 typval_T *rettv;
10460{
10461 float_T fx, fy;
10462
10463 rettv->v_type = VAR_FLOAT;
10464 if (get_float_arg(argvars, &fx) == OK
10465 && get_float_arg(&argvars[1], &fy) == OK)
10466 rettv->vval.v_float = fmod(fx, fy);
10467 else
10468 rettv->vval.v_float = 0.0;
10469}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010470#endif
10471
Bram Moolenaar0d660222005-01-07 21:51:51 +000010472/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010473 * "fnameescape({string})" function
10474 */
10475 static void
10476f_fnameescape(argvars, rettv)
10477 typval_T *argvars;
10478 typval_T *rettv;
10479{
10480 rettv->vval.v_string = vim_strsave_fnameescape(
10481 get_tv_string(&argvars[0]), FALSE);
10482 rettv->v_type = VAR_STRING;
10483}
10484
10485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 * "fnamemodify({fname}, {mods})" function
10487 */
10488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010489f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010490 typval_T *argvars;
10491 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492{
10493 char_u *fname;
10494 char_u *mods;
10495 int usedlen = 0;
10496 int len;
10497 char_u *fbuf = NULL;
10498 char_u buf[NUMBUFLEN];
10499
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010500 fname = get_tv_string_chk(&argvars[0]);
10501 mods = get_tv_string_buf_chk(&argvars[1], buf);
10502 if (fname == NULL || mods == NULL)
10503 fname = NULL;
10504 else
10505 {
10506 len = (int)STRLEN(fname);
10507 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010510 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010512 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010514 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 vim_free(fbuf);
10516}
10517
Bram Moolenaar33570922005-01-25 22:26:29 +000010518static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519
10520/*
10521 * "foldclosed()" function
10522 */
10523 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010524foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010525 typval_T *argvars;
10526 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527 int end;
10528{
10529#ifdef FEAT_FOLDING
10530 linenr_T lnum;
10531 linenr_T first, last;
10532
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010533 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10535 {
10536 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10537 {
10538 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010539 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010541 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 return;
10543 }
10544 }
10545#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010546 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547}
10548
10549/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010550 * "foldclosed()" function
10551 */
10552 static void
10553f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010554 typval_T *argvars;
10555 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010556{
10557 foldclosed_both(argvars, rettv, FALSE);
10558}
10559
10560/*
10561 * "foldclosedend()" function
10562 */
10563 static void
10564f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010565 typval_T *argvars;
10566 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010567{
10568 foldclosed_both(argvars, rettv, TRUE);
10569}
10570
10571/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 * "foldlevel()" function
10573 */
10574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010575f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010576 typval_T *argvars;
10577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578{
10579#ifdef FEAT_FOLDING
10580 linenr_T lnum;
10581
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010582 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010584 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586}
10587
10588/*
10589 * "foldtext()" function
10590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010592f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010593 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595{
10596#ifdef FEAT_FOLDING
10597 linenr_T lnum;
10598 char_u *s;
10599 char_u *r;
10600 int len;
10601 char *txt;
10602#endif
10603
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010604 rettv->v_type = VAR_STRING;
10605 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010607 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10608 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10609 <= curbuf->b_ml.ml_line_count
10610 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611 {
10612 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010613 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10614 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 {
10616 if (!linewhite(lnum))
10617 break;
10618 ++lnum;
10619 }
10620
10621 /* Find interesting text in this line. */
10622 s = skipwhite(ml_get(lnum));
10623 /* skip C comment-start */
10624 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010625 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010627 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010628 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010629 {
10630 s = skipwhite(ml_get(lnum + 1));
10631 if (*s == '*')
10632 s = skipwhite(s + 1);
10633 }
10634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635 txt = _("+-%s%3ld lines: ");
10636 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010637 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 + 20 /* for %3ld */
10639 + STRLEN(s))); /* concatenated */
10640 if (r != NULL)
10641 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010642 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10643 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10644 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 len = (int)STRLEN(r);
10646 STRCAT(r, s);
10647 /* remove 'foldmarker' and 'commentstring' */
10648 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010649 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 }
10651 }
10652#endif
10653}
10654
10655/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010656 * "foldtextresult(lnum)" function
10657 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010659f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010660 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010661 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010662{
10663#ifdef FEAT_FOLDING
10664 linenr_T lnum;
10665 char_u *text;
10666 char_u buf[51];
10667 foldinfo_T foldinfo;
10668 int fold_count;
10669#endif
10670
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010671 rettv->v_type = VAR_STRING;
10672 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010673#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010674 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010675 /* treat illegal types and illegal string values for {lnum} the same */
10676 if (lnum < 0)
10677 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010678 fold_count = foldedCount(curwin, lnum, &foldinfo);
10679 if (fold_count > 0)
10680 {
10681 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10682 &foldinfo, buf);
10683 if (text == buf)
10684 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010685 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010686 }
10687#endif
10688}
10689
10690/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 * "foreground()" function
10692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010694f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010695 typval_T *argvars UNUSED;
10696 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698#ifdef FEAT_GUI
10699 if (gui.in_use)
10700 gui_mch_set_foreground();
10701#else
10702# ifdef WIN32
10703 win32_set_foreground();
10704# endif
10705#endif
10706}
10707
10708/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010709 * "function()" function
10710 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010712f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010713 typval_T *argvars;
10714 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010715{
10716 char_u *s;
10717
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010718 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010719 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010720 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010721 /* Don't check an autoload name for existence here. */
10722 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010723 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010724 else
10725 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010726 rettv->vval.v_string = vim_strsave(s);
10727 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010728 }
10729}
10730
10731/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010732 * "garbagecollect()" function
10733 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010734 static void
10735f_garbagecollect(argvars, rettv)
10736 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010737 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010738{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010739 /* This is postponed until we are back at the toplevel, because we may be
10740 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10741 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010742
10743 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10744 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010745}
10746
10747/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010748 * "get()" function
10749 */
10750 static void
10751f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010752 typval_T *argvars;
10753 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010754{
Bram Moolenaar33570922005-01-25 22:26:29 +000010755 listitem_T *li;
10756 list_T *l;
10757 dictitem_T *di;
10758 dict_T *d;
10759 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010760
Bram Moolenaare9a41262005-01-15 22:18:47 +000010761 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010762 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010763 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010765 int error = FALSE;
10766
10767 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10768 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010769 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010770 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010771 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010772 else if (argvars[0].v_type == VAR_DICT)
10773 {
10774 if ((d = argvars[0].vval.v_dict) != NULL)
10775 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010776 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010777 if (di != NULL)
10778 tv = &di->di_tv;
10779 }
10780 }
10781 else
10782 EMSG2(_(e_listdictarg), "get()");
10783
10784 if (tv == NULL)
10785 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010786 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010787 copy_tv(&argvars[2], rettv);
10788 }
10789 else
10790 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010791}
10792
Bram Moolenaar342337a2005-07-21 21:11:17 +000010793static 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 +000010794
10795/*
10796 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010797 * Return a range (from start to end) of lines in rettv from the specified
10798 * buffer.
10799 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010800 */
10801 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010802get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010803 buf_T *buf;
10804 linenr_T start;
10805 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010806 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010807 typval_T *rettv;
10808{
10809 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010810
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010811 if (retlist && rettv_list_alloc(rettv) == FAIL)
10812 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010813
10814 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10815 return;
10816
10817 if (!retlist)
10818 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010819 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10820 p = ml_get_buf(buf, start, FALSE);
10821 else
10822 p = (char_u *)"";
10823
10824 rettv->v_type = VAR_STRING;
10825 rettv->vval.v_string = vim_strsave(p);
10826 }
10827 else
10828 {
10829 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010830 return;
10831
10832 if (start < 1)
10833 start = 1;
10834 if (end > buf->b_ml.ml_line_count)
10835 end = buf->b_ml.ml_line_count;
10836 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010837 if (list_append_string(rettv->vval.v_list,
10838 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010839 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010840 }
10841}
10842
10843/*
10844 * "getbufline()" function
10845 */
10846 static void
10847f_getbufline(argvars, rettv)
10848 typval_T *argvars;
10849 typval_T *rettv;
10850{
10851 linenr_T lnum;
10852 linenr_T end;
10853 buf_T *buf;
10854
10855 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10856 ++emsg_off;
10857 buf = get_buf_tv(&argvars[0]);
10858 --emsg_off;
10859
Bram Moolenaar661b1822005-07-28 22:36:45 +000010860 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010861 if (argvars[2].v_type == VAR_UNKNOWN)
10862 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010863 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010864 end = get_tv_lnum_buf(&argvars[2], buf);
10865
Bram Moolenaar342337a2005-07-21 21:11:17 +000010866 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010867}
10868
Bram Moolenaar0d660222005-01-07 21:51:51 +000010869/*
10870 * "getbufvar()" function
10871 */
10872 static void
10873f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010874 typval_T *argvars;
10875 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010876{
10877 buf_T *buf;
10878 buf_T *save_curbuf;
10879 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010880 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010881
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010882 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10883 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010884 ++emsg_off;
10885 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010886
10887 rettv->v_type = VAR_STRING;
10888 rettv->vval.v_string = NULL;
10889
10890 if (buf != NULL && varname != NULL)
10891 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010892 /* set curbuf to be our buf, temporarily */
10893 save_curbuf = curbuf;
10894 curbuf = buf;
10895
Bram Moolenaar0d660222005-01-07 21:51:51 +000010896 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010897 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010010898 else if (STRCMP(varname, "changedtick") == 0)
10899 {
10900 rettv->v_type = VAR_NUMBER;
10901 rettv->vval.v_number = curbuf->b_changedtick;
10902 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010903 else
10904 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010905 if (*varname == NUL)
10906 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10907 * scope prefix before the NUL byte is required by
10908 * find_var_in_ht(). */
10909 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010910 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010911 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010912 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010913 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010914 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010915
10916 /* restore previous notion of curbuf */
10917 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010918 }
10919
10920 --emsg_off;
10921}
10922
10923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 * "getchar()" function
10925 */
10926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010927f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010928 typval_T *argvars;
10929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930{
10931 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010932 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010934 /* Position the cursor. Needed after a message that ends in a space. */
10935 windgoto(msg_row, msg_col);
10936
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 ++no_mapping;
10938 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010939 for (;;)
10940 {
10941 if (argvars[0].v_type == VAR_UNKNOWN)
10942 /* getchar(): blocking wait. */
10943 n = safe_vgetc();
10944 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10945 /* getchar(1): only check if char avail */
10946 n = vpeekc();
10947 else if (error || vpeekc() == NUL)
10948 /* illegal argument or getchar(0) and no char avail: return zero */
10949 n = 0;
10950 else
10951 /* getchar(0) and char avail: return char */
10952 n = safe_vgetc();
10953 if (n == K_IGNORE)
10954 continue;
10955 break;
10956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 --no_mapping;
10958 --allow_keys;
10959
Bram Moolenaar219b8702006-11-01 14:32:36 +000010960 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10961 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10962 vimvars[VV_MOUSE_COL].vv_nr = 0;
10963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 if (IS_SPECIAL(n) || mod_mask != 0)
10966 {
10967 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10968 int i = 0;
10969
10970 /* Turn a special key into three bytes, plus modifier. */
10971 if (mod_mask != 0)
10972 {
10973 temp[i++] = K_SPECIAL;
10974 temp[i++] = KS_MODIFIER;
10975 temp[i++] = mod_mask;
10976 }
10977 if (IS_SPECIAL(n))
10978 {
10979 temp[i++] = K_SPECIAL;
10980 temp[i++] = K_SECOND(n);
10981 temp[i++] = K_THIRD(n);
10982 }
10983#ifdef FEAT_MBYTE
10984 else if (has_mbyte)
10985 i += (*mb_char2bytes)(n, temp + i);
10986#endif
10987 else
10988 temp[i++] = n;
10989 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010990 rettv->v_type = VAR_STRING;
10991 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010992
10993#ifdef FEAT_MOUSE
10994 if (n == K_LEFTMOUSE
10995 || n == K_LEFTMOUSE_NM
10996 || n == K_LEFTDRAG
10997 || n == K_LEFTRELEASE
10998 || n == K_LEFTRELEASE_NM
10999 || n == K_MIDDLEMOUSE
11000 || n == K_MIDDLEDRAG
11001 || n == K_MIDDLERELEASE
11002 || n == K_RIGHTMOUSE
11003 || n == K_RIGHTDRAG
11004 || n == K_RIGHTRELEASE
11005 || n == K_X1MOUSE
11006 || n == K_X1DRAG
11007 || n == K_X1RELEASE
11008 || n == K_X2MOUSE
11009 || n == K_X2DRAG
11010 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011011 || n == K_MOUSELEFT
11012 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011013 || n == K_MOUSEDOWN
11014 || n == K_MOUSEUP)
11015 {
11016 int row = mouse_row;
11017 int col = mouse_col;
11018 win_T *win;
11019 linenr_T lnum;
11020# ifdef FEAT_WINDOWS
11021 win_T *wp;
11022# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011023 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011024
11025 if (row >= 0 && col >= 0)
11026 {
11027 /* Find the window at the mouse coordinates and compute the
11028 * text position. */
11029 win = mouse_find_win(&row, &col);
11030 (void)mouse_comp_pos(win, &row, &col, &lnum);
11031# ifdef FEAT_WINDOWS
11032 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011033 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011034# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011035 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011036 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11037 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11038 }
11039 }
11040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 }
11042}
11043
11044/*
11045 * "getcharmod()" function
11046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011048f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011049 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011052 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053}
11054
11055/*
11056 * "getcmdline()" function
11057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011059f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011060 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011063 rettv->v_type = VAR_STRING;
11064 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065}
11066
11067/*
11068 * "getcmdpos()" function
11069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011071f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011072 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011075 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076}
11077
11078/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011079 * "getcmdtype()" function
11080 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011081 static void
11082f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011083 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011084 typval_T *rettv;
11085{
11086 rettv->v_type = VAR_STRING;
11087 rettv->vval.v_string = alloc(2);
11088 if (rettv->vval.v_string != NULL)
11089 {
11090 rettv->vval.v_string[0] = get_cmdline_type();
11091 rettv->vval.v_string[1] = NUL;
11092 }
11093}
11094
11095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 * "getcwd()" function
11097 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011099f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011100 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102{
11103 char_u cwd[MAXPATHL];
11104
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011105 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011107 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 else
11109 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011110 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000011112 if (rettv->vval.v_string != NULL)
11113 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114#endif
11115 }
11116}
11117
11118/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011119 * "getfontname()" function
11120 */
11121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011122f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011123 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011124 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011125{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011126 rettv->v_type = VAR_STRING;
11127 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011128#ifdef FEAT_GUI
11129 if (gui.in_use)
11130 {
11131 GuiFont font;
11132 char_u *name = NULL;
11133
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011134 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011135 {
11136 /* Get the "Normal" font. Either the name saved by
11137 * hl_set_font_name() or from the font ID. */
11138 font = gui.norm_font;
11139 name = hl_get_font_name();
11140 }
11141 else
11142 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011143 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011144 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11145 return;
11146 font = gui_mch_get_font(name, FALSE);
11147 if (font == NOFONT)
11148 return; /* Invalid font name, return empty string. */
11149 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011150 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011151 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011152 gui_mch_free_font(font);
11153 }
11154#endif
11155}
11156
11157/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011158 * "getfperm({fname})" function
11159 */
11160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011161f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011162 typval_T *argvars;
11163 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011164{
11165 char_u *fname;
11166 struct stat st;
11167 char_u *perm = NULL;
11168 char_u flags[] = "rwx";
11169 int i;
11170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011171 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011173 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011174 if (mch_stat((char *)fname, &st) >= 0)
11175 {
11176 perm = vim_strsave((char_u *)"---------");
11177 if (perm != NULL)
11178 {
11179 for (i = 0; i < 9; i++)
11180 {
11181 if (st.st_mode & (1 << (8 - i)))
11182 perm[i] = flags[i % 3];
11183 }
11184 }
11185 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011186 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011187}
11188
11189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190 * "getfsize({fname})" function
11191 */
11192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011193f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011194 typval_T *argvars;
11195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196{
11197 char_u *fname;
11198 struct stat st;
11199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011200 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011202 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203
11204 if (mch_stat((char *)fname, &st) >= 0)
11205 {
11206 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011207 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011209 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011210 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011211
11212 /* non-perfect check for overflow */
11213 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11214 rettv->vval.v_number = -2;
11215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216 }
11217 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219}
11220
11221/*
11222 * "getftime({fname})" function
11223 */
11224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011225f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011226 typval_T *argvars;
11227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228{
11229 char_u *fname;
11230 struct stat st;
11231
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011232 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233
11234 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011235 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011237 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238}
11239
11240/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011241 * "getftype({fname})" function
11242 */
11243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011244f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011245 typval_T *argvars;
11246 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011247{
11248 char_u *fname;
11249 struct stat st;
11250 char_u *type = NULL;
11251 char *t;
11252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011253 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011255 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011256 if (mch_lstat((char *)fname, &st) >= 0)
11257 {
11258#ifdef S_ISREG
11259 if (S_ISREG(st.st_mode))
11260 t = "file";
11261 else if (S_ISDIR(st.st_mode))
11262 t = "dir";
11263# ifdef S_ISLNK
11264 else if (S_ISLNK(st.st_mode))
11265 t = "link";
11266# endif
11267# ifdef S_ISBLK
11268 else if (S_ISBLK(st.st_mode))
11269 t = "bdev";
11270# endif
11271# ifdef S_ISCHR
11272 else if (S_ISCHR(st.st_mode))
11273 t = "cdev";
11274# endif
11275# ifdef S_ISFIFO
11276 else if (S_ISFIFO(st.st_mode))
11277 t = "fifo";
11278# endif
11279# ifdef S_ISSOCK
11280 else if (S_ISSOCK(st.st_mode))
11281 t = "fifo";
11282# endif
11283 else
11284 t = "other";
11285#else
11286# ifdef S_IFMT
11287 switch (st.st_mode & S_IFMT)
11288 {
11289 case S_IFREG: t = "file"; break;
11290 case S_IFDIR: t = "dir"; break;
11291# ifdef S_IFLNK
11292 case S_IFLNK: t = "link"; break;
11293# endif
11294# ifdef S_IFBLK
11295 case S_IFBLK: t = "bdev"; break;
11296# endif
11297# ifdef S_IFCHR
11298 case S_IFCHR: t = "cdev"; break;
11299# endif
11300# ifdef S_IFIFO
11301 case S_IFIFO: t = "fifo"; break;
11302# endif
11303# ifdef S_IFSOCK
11304 case S_IFSOCK: t = "socket"; break;
11305# endif
11306 default: t = "other";
11307 }
11308# else
11309 if (mch_isdir(fname))
11310 t = "dir";
11311 else
11312 t = "file";
11313# endif
11314#endif
11315 type = vim_strsave((char_u *)t);
11316 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011317 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011318}
11319
11320/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011321 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011322 */
11323 static void
11324f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011325 typval_T *argvars;
11326 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011327{
11328 linenr_T lnum;
11329 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011330 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011331
11332 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011333 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011334 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011335 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011336 retlist = FALSE;
11337 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011338 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011339 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011340 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011341 retlist = TRUE;
11342 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011343
Bram Moolenaar342337a2005-07-21 21:11:17 +000011344 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011345}
11346
11347/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011348 * "getmatches()" function
11349 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011350 static void
11351f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011352 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011353 typval_T *rettv;
11354{
11355#ifdef FEAT_SEARCH_EXTRA
11356 dict_T *dict;
11357 matchitem_T *cur = curwin->w_match_head;
11358
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011359 if (rettv_list_alloc(rettv) == OK)
11360 {
11361 while (cur != NULL)
11362 {
11363 dict = dict_alloc();
11364 if (dict == NULL)
11365 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011366 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11367 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11368 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11369 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11370 list_append_dict(rettv->vval.v_list, dict);
11371 cur = cur->next;
11372 }
11373 }
11374#endif
11375}
11376
11377/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011378 * "getpid()" function
11379 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011380 static void
11381f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011382 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011383 typval_T *rettv;
11384{
11385 rettv->vval.v_number = mch_get_pid();
11386}
11387
11388/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011389 * "getpos(string)" function
11390 */
11391 static void
11392f_getpos(argvars, rettv)
11393 typval_T *argvars;
11394 typval_T *rettv;
11395{
11396 pos_T *fp;
11397 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011398 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011399
11400 if (rettv_list_alloc(rettv) == OK)
11401 {
11402 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011403 fp = var2fpos(&argvars[0], TRUE, &fnum);
11404 if (fnum != -1)
11405 list_append_number(l, (varnumber_T)fnum);
11406 else
11407 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011408 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11409 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011410 list_append_number(l, (fp != NULL)
11411 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011412 : (varnumber_T)0);
11413 list_append_number(l,
11414#ifdef FEAT_VIRTUALEDIT
11415 (fp != NULL) ? (varnumber_T)fp->coladd :
11416#endif
11417 (varnumber_T)0);
11418 }
11419 else
11420 rettv->vval.v_number = FALSE;
11421}
11422
11423/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011424 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011425 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011426 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011427f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011428 typval_T *argvars UNUSED;
11429 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011430{
11431#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011432 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011433#endif
11434
Bram Moolenaar2641f772005-03-25 21:58:17 +000011435#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011436 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011437 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011438 wp = NULL;
11439 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11440 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011441 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011442 if (wp == NULL)
11443 return;
11444 }
11445
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011446 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011447 }
11448#endif
11449}
11450
11451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452 * "getreg()" function
11453 */
11454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011455f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011456 typval_T *argvars;
11457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458{
11459 char_u *strregname;
11460 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011461 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011462 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011464 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011465 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011466 strregname = get_tv_string_chk(&argvars[0]);
11467 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011468 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011469 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011472 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 regname = (strregname == NULL ? '"' : *strregname);
11474 if (regname == 0)
11475 regname = '"';
11476
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011477 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011478 rettv->vval.v_string = error ? NULL :
11479 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480}
11481
11482/*
11483 * "getregtype()" function
11484 */
11485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011486f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011487 typval_T *argvars;
11488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489{
11490 char_u *strregname;
11491 int regname;
11492 char_u buf[NUMBUFLEN + 2];
11493 long reglen = 0;
11494
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011495 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011496 {
11497 strregname = get_tv_string_chk(&argvars[0]);
11498 if (strregname == NULL) /* type error; errmsg already given */
11499 {
11500 rettv->v_type = VAR_STRING;
11501 rettv->vval.v_string = NULL;
11502 return;
11503 }
11504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505 else
11506 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011507 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011508
11509 regname = (strregname == NULL ? '"' : *strregname);
11510 if (regname == 0)
11511 regname = '"';
11512
11513 buf[0] = NUL;
11514 buf[1] = NUL;
11515 switch (get_reg_type(regname, &reglen))
11516 {
11517 case MLINE: buf[0] = 'V'; break;
11518 case MCHAR: buf[0] = 'v'; break;
11519#ifdef FEAT_VISUAL
11520 case MBLOCK:
11521 buf[0] = Ctrl_V;
11522 sprintf((char *)buf + 1, "%ld", reglen + 1);
11523 break;
11524#endif
11525 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011526 rettv->v_type = VAR_STRING;
11527 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528}
11529
11530/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011531 * "gettabvar()" function
11532 */
11533 static void
11534f_gettabvar(argvars, rettv)
11535 typval_T *argvars;
11536 typval_T *rettv;
11537{
11538 tabpage_T *tp;
11539 dictitem_T *v;
11540 char_u *varname;
11541
11542 rettv->v_type = VAR_STRING;
11543 rettv->vval.v_string = NULL;
11544
11545 varname = get_tv_string_chk(&argvars[1]);
11546 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11547 if (tp != NULL && varname != NULL)
11548 {
11549 /* look up the variable */
11550 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11551 if (v != NULL)
11552 copy_tv(&v->di_tv, rettv);
11553 }
11554}
11555
11556/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011557 * "gettabwinvar()" function
11558 */
11559 static void
11560f_gettabwinvar(argvars, rettv)
11561 typval_T *argvars;
11562 typval_T *rettv;
11563{
11564 getwinvar(argvars, rettv, 1);
11565}
11566
11567/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568 * "getwinposx()" function
11569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011571f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011572 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011575 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576#ifdef FEAT_GUI
11577 if (gui.in_use)
11578 {
11579 int x, y;
11580
11581 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011582 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583 }
11584#endif
11585}
11586
11587/*
11588 * "getwinposy()" function
11589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011591f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011592 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011595 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596#ifdef FEAT_GUI
11597 if (gui.in_use)
11598 {
11599 int x, y;
11600
11601 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011602 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603 }
11604#endif
11605}
11606
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011607/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011608 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011609 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011610 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011611find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011612 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011613 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011614{
11615#ifdef FEAT_WINDOWS
11616 win_T *wp;
11617#endif
11618 int nr;
11619
11620 nr = get_tv_number_chk(vp, NULL);
11621
11622#ifdef FEAT_WINDOWS
11623 if (nr < 0)
11624 return NULL;
11625 if (nr == 0)
11626 return curwin;
11627
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011628 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11629 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011630 if (--nr <= 0)
11631 break;
11632 return wp;
11633#else
11634 if (nr == 0 || nr == 1)
11635 return curwin;
11636 return NULL;
11637#endif
11638}
11639
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640/*
11641 * "getwinvar()" function
11642 */
11643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011644f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011645 typval_T *argvars;
11646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011648 getwinvar(argvars, rettv, 0);
11649}
11650
11651/*
11652 * getwinvar() and gettabwinvar()
11653 */
11654 static void
11655getwinvar(argvars, rettv, off)
11656 typval_T *argvars;
11657 typval_T *rettv;
11658 int off; /* 1 for gettabwinvar() */
11659{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 win_T *win, *oldcurwin;
11661 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011662 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011663 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011665#ifdef FEAT_WINDOWS
11666 if (off == 1)
11667 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11668 else
11669 tp = curtab;
11670#endif
11671 win = find_win_by_nr(&argvars[off], tp);
11672 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011673 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011675 rettv->v_type = VAR_STRING;
11676 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677
11678 if (win != NULL && varname != NULL)
11679 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011680 /* Set curwin to be our win, temporarily. Also set curbuf, so
11681 * that we can get buffer-local options. */
11682 oldcurwin = curwin;
11683 curwin = win;
11684 curbuf = win->w_buffer;
11685
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011687 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688 else
11689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011690 if (*varname == NUL)
11691 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11692 * scope prefix before the NUL byte is required by
11693 * find_var_in_ht(). */
11694 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011695 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011696 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011698 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011700
11701 /* restore previous notion of curwin */
11702 curwin = oldcurwin;
11703 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704 }
11705
11706 --emsg_off;
11707}
11708
11709/*
11710 * "glob()" function
11711 */
11712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011713f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011714 typval_T *argvars;
11715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011717 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011719 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011721 /* When the optional second argument is non-zero, don't remove matches
11722 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11723 if (argvars[1].v_type != VAR_UNKNOWN
11724 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011725 options |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011726 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011727 if (!error)
11728 {
11729 ExpandInit(&xpc);
11730 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011731 if (p_wic)
11732 options += WILD_ICASE;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011733 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011734 NULL, options, WILD_ALL);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011735 }
11736 else
11737 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738}
11739
11740/*
11741 * "globpath()" function
11742 */
11743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011744f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011745 typval_T *argvars;
11746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011748 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011750 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011751 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011753 /* When the optional second argument is non-zero, don't remove matches
11754 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11755 if (argvars[2].v_type != VAR_UNKNOWN
11756 && get_tv_number_chk(&argvars[2], &error))
11757 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011758 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011759 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011760 rettv->vval.v_string = NULL;
11761 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011762 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11763 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764}
11765
11766/*
11767 * "has()" function
11768 */
11769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011770f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011771 typval_T *argvars;
11772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011773{
11774 int i;
11775 char_u *name;
11776 int n = FALSE;
11777 static char *(has_list[]) =
11778 {
11779#ifdef AMIGA
11780 "amiga",
11781# ifdef FEAT_ARP
11782 "arp",
11783# endif
11784#endif
11785#ifdef __BEOS__
11786 "beos",
11787#endif
11788#ifdef MSDOS
11789# ifdef DJGPP
11790 "dos32",
11791# else
11792 "dos16",
11793# endif
11794#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011795#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796 "mac",
11797#endif
11798#if defined(MACOS_X_UNIX)
11799 "macunix",
11800#endif
11801#ifdef OS2
11802 "os2",
11803#endif
11804#ifdef __QNX__
11805 "qnx",
11806#endif
11807#ifdef RISCOS
11808 "riscos",
11809#endif
11810#ifdef UNIX
11811 "unix",
11812#endif
11813#ifdef VMS
11814 "vms",
11815#endif
11816#ifdef WIN16
11817 "win16",
11818#endif
11819#ifdef WIN32
11820 "win32",
11821#endif
11822#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11823 "win32unix",
11824#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011825#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826 "win64",
11827#endif
11828#ifdef EBCDIC
11829 "ebcdic",
11830#endif
11831#ifndef CASE_INSENSITIVE_FILENAME
11832 "fname_case",
11833#endif
11834#ifdef FEAT_ARABIC
11835 "arabic",
11836#endif
11837#ifdef FEAT_AUTOCMD
11838 "autocmd",
11839#endif
11840#ifdef FEAT_BEVAL
11841 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011842# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11843 "balloon_multiline",
11844# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845#endif
11846#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11847 "builtin_terms",
11848# ifdef ALL_BUILTIN_TCAPS
11849 "all_builtin_terms",
11850# endif
11851#endif
11852#ifdef FEAT_BYTEOFF
11853 "byte_offset",
11854#endif
11855#ifdef FEAT_CINDENT
11856 "cindent",
11857#endif
11858#ifdef FEAT_CLIENTSERVER
11859 "clientserver",
11860#endif
11861#ifdef FEAT_CLIPBOARD
11862 "clipboard",
11863#endif
11864#ifdef FEAT_CMDL_COMPL
11865 "cmdline_compl",
11866#endif
11867#ifdef FEAT_CMDHIST
11868 "cmdline_hist",
11869#endif
11870#ifdef FEAT_COMMENTS
11871 "comments",
11872#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011873#ifdef FEAT_CONCEAL
11874 "conceal",
11875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876#ifdef FEAT_CRYPT
11877 "cryptv",
11878#endif
11879#ifdef FEAT_CSCOPE
11880 "cscope",
11881#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011882#ifdef FEAT_CURSORBIND
11883 "cursorbind",
11884#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011885#ifdef CURSOR_SHAPE
11886 "cursorshape",
11887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888#ifdef DEBUG
11889 "debug",
11890#endif
11891#ifdef FEAT_CON_DIALOG
11892 "dialog_con",
11893#endif
11894#ifdef FEAT_GUI_DIALOG
11895 "dialog_gui",
11896#endif
11897#ifdef FEAT_DIFF
11898 "diff",
11899#endif
11900#ifdef FEAT_DIGRAPHS
11901 "digraphs",
11902#endif
11903#ifdef FEAT_DND
11904 "dnd",
11905#endif
11906#ifdef FEAT_EMACS_TAGS
11907 "emacs_tags",
11908#endif
11909 "eval", /* always present, of course! */
11910#ifdef FEAT_EX_EXTRA
11911 "ex_extra",
11912#endif
11913#ifdef FEAT_SEARCH_EXTRA
11914 "extra_search",
11915#endif
11916#ifdef FEAT_FKMAP
11917 "farsi",
11918#endif
11919#ifdef FEAT_SEARCHPATH
11920 "file_in_path",
11921#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011922#if defined(UNIX) && !defined(USE_SYSTEM)
11923 "filterpipe",
11924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925#ifdef FEAT_FIND_ID
11926 "find_in_path",
11927#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011928#ifdef FEAT_FLOAT
11929 "float",
11930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931#ifdef FEAT_FOLDING
11932 "folding",
11933#endif
11934#ifdef FEAT_FOOTER
11935 "footer",
11936#endif
11937#if !defined(USE_SYSTEM) && defined(UNIX)
11938 "fork",
11939#endif
11940#ifdef FEAT_GETTEXT
11941 "gettext",
11942#endif
11943#ifdef FEAT_GUI
11944 "gui",
11945#endif
11946#ifdef FEAT_GUI_ATHENA
11947# ifdef FEAT_GUI_NEXTAW
11948 "gui_neXtaw",
11949# else
11950 "gui_athena",
11951# endif
11952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953#ifdef FEAT_GUI_GTK
11954 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011955 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011957#ifdef FEAT_GUI_GNOME
11958 "gui_gnome",
11959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960#ifdef FEAT_GUI_MAC
11961 "gui_mac",
11962#endif
11963#ifdef FEAT_GUI_MOTIF
11964 "gui_motif",
11965#endif
11966#ifdef FEAT_GUI_PHOTON
11967 "gui_photon",
11968#endif
11969#ifdef FEAT_GUI_W16
11970 "gui_win16",
11971#endif
11972#ifdef FEAT_GUI_W32
11973 "gui_win32",
11974#endif
11975#ifdef FEAT_HANGULIN
11976 "hangul_input",
11977#endif
11978#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11979 "iconv",
11980#endif
11981#ifdef FEAT_INS_EXPAND
11982 "insert_expand",
11983#endif
11984#ifdef FEAT_JUMPLIST
11985 "jumplist",
11986#endif
11987#ifdef FEAT_KEYMAP
11988 "keymap",
11989#endif
11990#ifdef FEAT_LANGMAP
11991 "langmap",
11992#endif
11993#ifdef FEAT_LIBCALL
11994 "libcall",
11995#endif
11996#ifdef FEAT_LINEBREAK
11997 "linebreak",
11998#endif
11999#ifdef FEAT_LISP
12000 "lispindent",
12001#endif
12002#ifdef FEAT_LISTCMDS
12003 "listcmds",
12004#endif
12005#ifdef FEAT_LOCALMAP
12006 "localmap",
12007#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012008#ifdef FEAT_LUA
12009# ifndef DYNAMIC_LUA
12010 "lua",
12011# endif
12012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013#ifdef FEAT_MENU
12014 "menu",
12015#endif
12016#ifdef FEAT_SESSION
12017 "mksession",
12018#endif
12019#ifdef FEAT_MODIFY_FNAME
12020 "modify_fname",
12021#endif
12022#ifdef FEAT_MOUSE
12023 "mouse",
12024#endif
12025#ifdef FEAT_MOUSESHAPE
12026 "mouseshape",
12027#endif
12028#if defined(UNIX) || defined(VMS)
12029# ifdef FEAT_MOUSE_DEC
12030 "mouse_dec",
12031# endif
12032# ifdef FEAT_MOUSE_GPM
12033 "mouse_gpm",
12034# endif
12035# ifdef FEAT_MOUSE_JSB
12036 "mouse_jsbterm",
12037# endif
12038# ifdef FEAT_MOUSE_NET
12039 "mouse_netterm",
12040# endif
12041# ifdef FEAT_MOUSE_PTERM
12042 "mouse_pterm",
12043# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012044# ifdef FEAT_SYSMOUSE
12045 "mouse_sysmouse",
12046# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047# ifdef FEAT_MOUSE_XTERM
12048 "mouse_xterm",
12049# endif
12050#endif
12051#ifdef FEAT_MBYTE
12052 "multi_byte",
12053#endif
12054#ifdef FEAT_MBYTE_IME
12055 "multi_byte_ime",
12056#endif
12057#ifdef FEAT_MULTI_LANG
12058 "multi_lang",
12059#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012060#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012061#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012062 "mzscheme",
12063#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065#ifdef FEAT_OLE
12066 "ole",
12067#endif
12068#ifdef FEAT_OSFILETYPE
12069 "osfiletype",
12070#endif
12071#ifdef FEAT_PATH_EXTRA
12072 "path_extra",
12073#endif
12074#ifdef FEAT_PERL
12075#ifndef DYNAMIC_PERL
12076 "perl",
12077#endif
12078#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012079#ifdef FEAT_PERSISTENT_UNDO
12080 "persistent_undo",
12081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082#ifdef FEAT_PYTHON
12083#ifndef DYNAMIC_PYTHON
12084 "python",
12085#endif
12086#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012087#ifdef FEAT_PYTHON3
12088#ifndef DYNAMIC_PYTHON3
12089 "python3",
12090#endif
12091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012092#ifdef FEAT_POSTSCRIPT
12093 "postscript",
12094#endif
12095#ifdef FEAT_PRINTER
12096 "printer",
12097#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012098#ifdef FEAT_PROFILE
12099 "profile",
12100#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012101#ifdef FEAT_RELTIME
12102 "reltime",
12103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104#ifdef FEAT_QUICKFIX
12105 "quickfix",
12106#endif
12107#ifdef FEAT_RIGHTLEFT
12108 "rightleft",
12109#endif
12110#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12111 "ruby",
12112#endif
12113#ifdef FEAT_SCROLLBIND
12114 "scrollbind",
12115#endif
12116#ifdef FEAT_CMDL_INFO
12117 "showcmd",
12118 "cmdline_info",
12119#endif
12120#ifdef FEAT_SIGNS
12121 "signs",
12122#endif
12123#ifdef FEAT_SMARTINDENT
12124 "smartindent",
12125#endif
12126#ifdef FEAT_SNIFF
12127 "sniff",
12128#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012129#ifdef STARTUPTIME
12130 "startuptime",
12131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132#ifdef FEAT_STL_OPT
12133 "statusline",
12134#endif
12135#ifdef FEAT_SUN_WORKSHOP
12136 "sun_workshop",
12137#endif
12138#ifdef FEAT_NETBEANS_INTG
12139 "netbeans_intg",
12140#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012141#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012142 "spell",
12143#endif
12144#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145 "syntax",
12146#endif
12147#if defined(USE_SYSTEM) || !defined(UNIX)
12148 "system",
12149#endif
12150#ifdef FEAT_TAG_BINS
12151 "tag_binary",
12152#endif
12153#ifdef FEAT_TAG_OLDSTATIC
12154 "tag_old_static",
12155#endif
12156#ifdef FEAT_TAG_ANYWHITE
12157 "tag_any_white",
12158#endif
12159#ifdef FEAT_TCL
12160# ifndef DYNAMIC_TCL
12161 "tcl",
12162# endif
12163#endif
12164#ifdef TERMINFO
12165 "terminfo",
12166#endif
12167#ifdef FEAT_TERMRESPONSE
12168 "termresponse",
12169#endif
12170#ifdef FEAT_TEXTOBJ
12171 "textobjects",
12172#endif
12173#ifdef HAVE_TGETENT
12174 "tgetent",
12175#endif
12176#ifdef FEAT_TITLE
12177 "title",
12178#endif
12179#ifdef FEAT_TOOLBAR
12180 "toolbar",
12181#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012182#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12183 "unnamedplus",
12184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185#ifdef FEAT_USR_CMDS
12186 "user-commands", /* was accidentally included in 5.4 */
12187 "user_commands",
12188#endif
12189#ifdef FEAT_VIMINFO
12190 "viminfo",
12191#endif
12192#ifdef FEAT_VERTSPLIT
12193 "vertsplit",
12194#endif
12195#ifdef FEAT_VIRTUALEDIT
12196 "virtualedit",
12197#endif
12198#ifdef FEAT_VISUAL
12199 "visual",
12200#endif
12201#ifdef FEAT_VISUALEXTRA
12202 "visualextra",
12203#endif
12204#ifdef FEAT_VREPLACE
12205 "vreplace",
12206#endif
12207#ifdef FEAT_WILDIGN
12208 "wildignore",
12209#endif
12210#ifdef FEAT_WILDMENU
12211 "wildmenu",
12212#endif
12213#ifdef FEAT_WINDOWS
12214 "windows",
12215#endif
12216#ifdef FEAT_WAK
12217 "winaltkeys",
12218#endif
12219#ifdef FEAT_WRITEBACKUP
12220 "writebackup",
12221#endif
12222#ifdef FEAT_XIM
12223 "xim",
12224#endif
12225#ifdef FEAT_XFONTSET
12226 "xfontset",
12227#endif
12228#ifdef USE_XSMP
12229 "xsmp",
12230#endif
12231#ifdef USE_XSMP_INTERACT
12232 "xsmp_interact",
12233#endif
12234#ifdef FEAT_XCLIPBOARD
12235 "xterm_clipboard",
12236#endif
12237#ifdef FEAT_XTERM_SAVE
12238 "xterm_save",
12239#endif
12240#if defined(UNIX) && defined(FEAT_X11)
12241 "X11",
12242#endif
12243 NULL
12244 };
12245
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012246 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247 for (i = 0; has_list[i] != NULL; ++i)
12248 if (STRICMP(name, has_list[i]) == 0)
12249 {
12250 n = TRUE;
12251 break;
12252 }
12253
12254 if (n == FALSE)
12255 {
12256 if (STRNICMP(name, "patch", 5) == 0)
12257 n = has_patch(atoi((char *)name + 5));
12258 else if (STRICMP(name, "vim_starting") == 0)
12259 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012260#ifdef FEAT_MBYTE
12261 else if (STRICMP(name, "multi_byte_encoding") == 0)
12262 n = has_mbyte;
12263#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012264#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12265 else if (STRICMP(name, "balloon_multiline") == 0)
12266 n = multiline_balloon_available();
12267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012268#ifdef DYNAMIC_TCL
12269 else if (STRICMP(name, "tcl") == 0)
12270 n = tcl_enabled(FALSE);
12271#endif
12272#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12273 else if (STRICMP(name, "iconv") == 0)
12274 n = iconv_enabled(FALSE);
12275#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012276#ifdef DYNAMIC_LUA
12277 else if (STRICMP(name, "lua") == 0)
12278 n = lua_enabled(FALSE);
12279#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012280#ifdef DYNAMIC_MZSCHEME
12281 else if (STRICMP(name, "mzscheme") == 0)
12282 n = mzscheme_enabled(FALSE);
12283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284#ifdef DYNAMIC_RUBY
12285 else if (STRICMP(name, "ruby") == 0)
12286 n = ruby_enabled(FALSE);
12287#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012288#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289#ifdef DYNAMIC_PYTHON
12290 else if (STRICMP(name, "python") == 0)
12291 n = python_enabled(FALSE);
12292#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012293#endif
12294#ifdef FEAT_PYTHON3
12295#ifdef DYNAMIC_PYTHON3
12296 else if (STRICMP(name, "python3") == 0)
12297 n = python3_enabled(FALSE);
12298#endif
12299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300#ifdef DYNAMIC_PERL
12301 else if (STRICMP(name, "perl") == 0)
12302 n = perl_enabled(FALSE);
12303#endif
12304#ifdef FEAT_GUI
12305 else if (STRICMP(name, "gui_running") == 0)
12306 n = (gui.in_use || gui.starting);
12307# ifdef FEAT_GUI_W32
12308 else if (STRICMP(name, "gui_win32s") == 0)
12309 n = gui_is_win32s();
12310# endif
12311# ifdef FEAT_BROWSE
12312 else if (STRICMP(name, "browse") == 0)
12313 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12314# endif
12315#endif
12316#ifdef FEAT_SYN_HL
12317 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012318 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012319#endif
12320#if defined(WIN3264)
12321 else if (STRICMP(name, "win95") == 0)
12322 n = mch_windows95();
12323#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012324#ifdef FEAT_NETBEANS_INTG
12325 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012326 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328 }
12329
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012330 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331}
12332
12333/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012334 * "has_key()" function
12335 */
12336 static void
12337f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012338 typval_T *argvars;
12339 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012340{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012341 if (argvars[0].v_type != VAR_DICT)
12342 {
12343 EMSG(_(e_dictreq));
12344 return;
12345 }
12346 if (argvars[0].vval.v_dict == NULL)
12347 return;
12348
12349 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012350 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012351}
12352
12353/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012354 * "haslocaldir()" function
12355 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012356 static void
12357f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012358 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012359 typval_T *rettv;
12360{
12361 rettv->vval.v_number = (curwin->w_localdir != NULL);
12362}
12363
12364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365 * "hasmapto()" function
12366 */
12367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012368f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012369 typval_T *argvars;
12370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371{
12372 char_u *name;
12373 char_u *mode;
12374 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012375 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012377 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012378 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379 mode = (char_u *)"nvo";
12380 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012381 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012382 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012383 if (argvars[2].v_type != VAR_UNKNOWN)
12384 abbr = get_tv_number(&argvars[2]);
12385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386
Bram Moolenaar2c932302006-03-18 21:42:09 +000012387 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012388 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012390 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012391}
12392
12393/*
12394 * "histadd()" function
12395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012397f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012398 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400{
12401#ifdef FEAT_CMDHIST
12402 int histype;
12403 char_u *str;
12404 char_u buf[NUMBUFLEN];
12405#endif
12406
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012407 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408 if (check_restricted() || check_secure())
12409 return;
12410#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012411 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12412 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413 if (histype >= 0)
12414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012415 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416 if (*str != NUL)
12417 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012418 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012420 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421 return;
12422 }
12423 }
12424#endif
12425}
12426
12427/*
12428 * "histdel()" function
12429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012431f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012432 typval_T *argvars UNUSED;
12433 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434{
12435#ifdef FEAT_CMDHIST
12436 int n;
12437 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012438 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012440 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12441 if (str == NULL)
12442 n = 0;
12443 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012445 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012446 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012448 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012449 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450 else
12451 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012452 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012453 get_tv_string_buf(&argvars[1], buf));
12454 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455#endif
12456}
12457
12458/*
12459 * "histget()" function
12460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012462f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012463 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465{
12466#ifdef FEAT_CMDHIST
12467 int type;
12468 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012469 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012471 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12472 if (str == NULL)
12473 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012474 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012475 {
12476 type = get_histtype(str);
12477 if (argvars[1].v_type == VAR_UNKNOWN)
12478 idx = get_history_idx(type);
12479 else
12480 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12481 /* -1 on type error */
12482 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012485 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012487 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488}
12489
12490/*
12491 * "histnr()" function
12492 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012494f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012495 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012496 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497{
12498 int i;
12499
12500#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012501 char_u *history = get_tv_string_chk(&argvars[0]);
12502
12503 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504 if (i >= HIST_CMD && i < HIST_COUNT)
12505 i = get_history_idx(i);
12506 else
12507#endif
12508 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012509 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510}
12511
12512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513 * "highlightID(name)" function
12514 */
12515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012516f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012517 typval_T *argvars;
12518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012520 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012521}
12522
12523/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012524 * "highlight_exists()" function
12525 */
12526 static void
12527f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012528 typval_T *argvars;
12529 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012530{
12531 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12532}
12533
12534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535 * "hostname()" function
12536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012538f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012539 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012541{
12542 char_u hostname[256];
12543
12544 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012545 rettv->v_type = VAR_STRING;
12546 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012547}
12548
12549/*
12550 * iconv() function
12551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012553f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012554 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556{
12557#ifdef FEAT_MBYTE
12558 char_u buf1[NUMBUFLEN];
12559 char_u buf2[NUMBUFLEN];
12560 char_u *from, *to, *str;
12561 vimconv_T vimconv;
12562#endif
12563
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012564 rettv->v_type = VAR_STRING;
12565 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012566
12567#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012568 str = get_tv_string(&argvars[0]);
12569 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12570 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571 vimconv.vc_type = CONV_NONE;
12572 convert_setup(&vimconv, from, to);
12573
12574 /* If the encodings are equal, no conversion needed. */
12575 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012576 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012578 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579
12580 convert_setup(&vimconv, NULL, NULL);
12581 vim_free(from);
12582 vim_free(to);
12583#endif
12584}
12585
12586/*
12587 * "indent()" function
12588 */
12589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012590f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012591 typval_T *argvars;
12592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012593{
12594 linenr_T lnum;
12595
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012596 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012598 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012599 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012600 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012601}
12602
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012603/*
12604 * "index()" function
12605 */
12606 static void
12607f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012608 typval_T *argvars;
12609 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012610{
Bram Moolenaar33570922005-01-25 22:26:29 +000012611 list_T *l;
12612 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012613 long idx = 0;
12614 int ic = FALSE;
12615
12616 rettv->vval.v_number = -1;
12617 if (argvars[0].v_type != VAR_LIST)
12618 {
12619 EMSG(_(e_listreq));
12620 return;
12621 }
12622 l = argvars[0].vval.v_list;
12623 if (l != NULL)
12624 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012625 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012626 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012628 int error = FALSE;
12629
Bram Moolenaar758711c2005-02-02 23:11:38 +000012630 /* Start at specified item. Use the cached index that list_find()
12631 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012632 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012633 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012634 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012635 ic = get_tv_number_chk(&argvars[3], &error);
12636 if (error)
12637 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012638 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012639
Bram Moolenaar758711c2005-02-02 23:11:38 +000012640 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012641 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012642 {
12643 rettv->vval.v_number = idx;
12644 break;
12645 }
12646 }
12647}
12648
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649static int inputsecret_flag = 0;
12650
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012651static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12652
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012654 * This function is used by f_input() and f_inputdialog() functions. The third
12655 * argument to f_input() specifies the type of completion to use at the
12656 * prompt. The third argument to f_inputdialog() specifies the value to return
12657 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658 */
12659 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012660get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012661 typval_T *argvars;
12662 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012663 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012664{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012665 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012666 char_u *p = NULL;
12667 int c;
12668 char_u buf[NUMBUFLEN];
12669 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012670 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012671 int xp_type = EXPAND_NOTHING;
12672 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012673
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012674 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012675 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676
12677#ifdef NO_CONSOLE_INPUT
12678 /* While starting up, there is no place to enter text. */
12679 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681#endif
12682
12683 cmd_silent = FALSE; /* Want to see the prompt. */
12684 if (prompt != NULL)
12685 {
12686 /* Only the part of the message after the last NL is considered as
12687 * prompt for the command line */
12688 p = vim_strrchr(prompt, '\n');
12689 if (p == NULL)
12690 p = prompt;
12691 else
12692 {
12693 ++p;
12694 c = *p;
12695 *p = NUL;
12696 msg_start();
12697 msg_clr_eos();
12698 msg_puts_attr(prompt, echo_attr);
12699 msg_didout = FALSE;
12700 msg_starthere();
12701 *p = c;
12702 }
12703 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012705 if (argvars[1].v_type != VAR_UNKNOWN)
12706 {
12707 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12708 if (defstr != NULL)
12709 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012711 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012712 {
12713 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012714 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012715 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012716
Bram Moolenaar4463f292005-09-25 22:20:24 +000012717 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012718
Bram Moolenaar4463f292005-09-25 22:20:24 +000012719 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12720 if (xp_name == NULL)
12721 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012722
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012723 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012724
Bram Moolenaar4463f292005-09-25 22:20:24 +000012725 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12726 &xp_arg) == FAIL)
12727 return;
12728 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012729 }
12730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012731 if (defstr != NULL)
12732 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012733 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12734 xp_type, xp_arg);
12735
12736 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012738 /* since the user typed this, no need to wait for return */
12739 need_wait_return = FALSE;
12740 msg_didout = FALSE;
12741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742 cmd_silent = cmd_silent_save;
12743}
12744
12745/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012746 * "input()" function
12747 * Also handles inputsecret() when inputsecret is set.
12748 */
12749 static void
12750f_input(argvars, rettv)
12751 typval_T *argvars;
12752 typval_T *rettv;
12753{
12754 get_user_input(argvars, rettv, FALSE);
12755}
12756
12757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758 * "inputdialog()" function
12759 */
12760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012761f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012762 typval_T *argvars;
12763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764{
12765#if defined(FEAT_GUI_TEXTDIALOG)
12766 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12767 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12768 {
12769 char_u *message;
12770 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012771 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012773 message = get_tv_string_chk(&argvars[0]);
12774 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012775 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012776 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777 else
12778 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012779 if (message != NULL && defstr != NULL
12780 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010012781 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012782 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783 else
12784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012785 if (message != NULL && defstr != NULL
12786 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012787 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012788 rettv->vval.v_string = vim_strsave(
12789 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012791 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012793 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794 }
12795 else
12796#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012797 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798}
12799
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012800/*
12801 * "inputlist()" function
12802 */
12803 static void
12804f_inputlist(argvars, rettv)
12805 typval_T *argvars;
12806 typval_T *rettv;
12807{
12808 listitem_T *li;
12809 int selected;
12810 int mouse_used;
12811
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012812#ifdef NO_CONSOLE_INPUT
12813 /* While starting up, there is no place to enter text. */
12814 if (no_console_input())
12815 return;
12816#endif
12817 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12818 {
12819 EMSG2(_(e_listarg), "inputlist()");
12820 return;
12821 }
12822
12823 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012824 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012825 lines_left = Rows; /* avoid more prompt */
12826 msg_scroll = TRUE;
12827 msg_clr_eos();
12828
12829 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12830 {
12831 msg_puts(get_tv_string(&li->li_tv));
12832 msg_putchar('\n');
12833 }
12834
12835 /* Ask for choice. */
12836 selected = prompt_for_number(&mouse_used);
12837 if (mouse_used)
12838 selected -= lines_left;
12839
12840 rettv->vval.v_number = selected;
12841}
12842
12843
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12845
12846/*
12847 * "inputrestore()" function
12848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012850f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012851 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853{
12854 if (ga_userinput.ga_len > 0)
12855 {
12856 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012857 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12858 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012859 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860 }
12861 else if (p_verbose > 1)
12862 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012863 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012864 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865 }
12866}
12867
12868/*
12869 * "inputsave()" function
12870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012872f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012873 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012876 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877 if (ga_grow(&ga_userinput, 1) == OK)
12878 {
12879 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12880 + ga_userinput.ga_len);
12881 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012882 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883 }
12884 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886}
12887
12888/*
12889 * "inputsecret()" function
12890 */
12891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012892f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012893 typval_T *argvars;
12894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895{
12896 ++cmdline_star;
12897 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012898 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899 --cmdline_star;
12900 --inputsecret_flag;
12901}
12902
12903/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012904 * "insert()" function
12905 */
12906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012907f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012908 typval_T *argvars;
12909 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012910{
12911 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012912 listitem_T *item;
12913 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012914 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012915
12916 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012917 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012918 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012919 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012920 {
12921 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012922 before = get_tv_number_chk(&argvars[2], &error);
12923 if (error)
12924 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012925
Bram Moolenaar758711c2005-02-02 23:11:38 +000012926 if (before == l->lv_len)
12927 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012928 else
12929 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012930 item = list_find(l, before);
12931 if (item == NULL)
12932 {
12933 EMSGN(_(e_listidx), before);
12934 l = NULL;
12935 }
12936 }
12937 if (l != NULL)
12938 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012939 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012940 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012941 }
12942 }
12943}
12944
12945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946 * "isdirectory()" function
12947 */
12948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012949f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012950 typval_T *argvars;
12951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012953 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012954}
12955
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012956/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012957 * "islocked()" function
12958 */
12959 static void
12960f_islocked(argvars, rettv)
12961 typval_T *argvars;
12962 typval_T *rettv;
12963{
12964 lval_T lv;
12965 char_u *end;
12966 dictitem_T *di;
12967
12968 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012969 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12970 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012971 if (end != NULL && lv.ll_name != NULL)
12972 {
12973 if (*end != NUL)
12974 EMSG(_(e_trailing));
12975 else
12976 {
12977 if (lv.ll_tv == NULL)
12978 {
12979 if (check_changedtick(lv.ll_name))
12980 rettv->vval.v_number = 1; /* always locked */
12981 else
12982 {
12983 di = find_var(lv.ll_name, NULL);
12984 if (di != NULL)
12985 {
12986 /* Consider a variable locked when:
12987 * 1. the variable itself is locked
12988 * 2. the value of the variable is locked.
12989 * 3. the List or Dict value is locked.
12990 */
12991 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12992 || tv_islocked(&di->di_tv));
12993 }
12994 }
12995 }
12996 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012997 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012998 else if (lv.ll_newkey != NULL)
12999 EMSG2(_(e_dictkey), lv.ll_newkey);
13000 else if (lv.ll_list != NULL)
13001 /* List item. */
13002 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13003 else
13004 /* Dictionary item. */
13005 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13006 }
13007 }
13008
13009 clear_lval(&lv);
13010}
13011
Bram Moolenaar33570922005-01-25 22:26:29 +000013012static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013013
13014/*
13015 * Turn a dict into a list:
13016 * "what" == 0: list of keys
13017 * "what" == 1: list of values
13018 * "what" == 2: list of items
13019 */
13020 static void
13021dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013022 typval_T *argvars;
13023 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013024 int what;
13025{
Bram Moolenaar33570922005-01-25 22:26:29 +000013026 list_T *l2;
13027 dictitem_T *di;
13028 hashitem_T *hi;
13029 listitem_T *li;
13030 listitem_T *li2;
13031 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013032 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013033
Bram Moolenaar8c711452005-01-14 21:53:12 +000013034 if (argvars[0].v_type != VAR_DICT)
13035 {
13036 EMSG(_(e_dictreq));
13037 return;
13038 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013039 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013040 return;
13041
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013042 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013043 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013044
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013045 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013046 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013047 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013048 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013049 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013050 --todo;
13051 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013052
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013053 li = listitem_alloc();
13054 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013055 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013056 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013057
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013058 if (what == 0)
13059 {
13060 /* keys() */
13061 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013062 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013063 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13064 }
13065 else if (what == 1)
13066 {
13067 /* values() */
13068 copy_tv(&di->di_tv, &li->li_tv);
13069 }
13070 else
13071 {
13072 /* items() */
13073 l2 = list_alloc();
13074 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013075 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013076 li->li_tv.vval.v_list = l2;
13077 if (l2 == NULL)
13078 break;
13079 ++l2->lv_refcount;
13080
13081 li2 = listitem_alloc();
13082 if (li2 == NULL)
13083 break;
13084 list_append(l2, li2);
13085 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013086 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013087 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13088
13089 li2 = listitem_alloc();
13090 if (li2 == NULL)
13091 break;
13092 list_append(l2, li2);
13093 copy_tv(&di->di_tv, &li2->li_tv);
13094 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013095 }
13096 }
13097}
13098
13099/*
13100 * "items(dict)" function
13101 */
13102 static void
13103f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013104 typval_T *argvars;
13105 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013106{
13107 dict_list(argvars, rettv, 2);
13108}
13109
Bram Moolenaar071d4272004-06-13 20:20:40 +000013110/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013111 * "join()" function
13112 */
13113 static void
13114f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013115 typval_T *argvars;
13116 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013117{
13118 garray_T ga;
13119 char_u *sep;
13120
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013121 if (argvars[0].v_type != VAR_LIST)
13122 {
13123 EMSG(_(e_listreq));
13124 return;
13125 }
13126 if (argvars[0].vval.v_list == NULL)
13127 return;
13128 if (argvars[1].v_type == VAR_UNKNOWN)
13129 sep = (char_u *)" ";
13130 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013131 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013132
13133 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013134
13135 if (sep != NULL)
13136 {
13137 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013138 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013139 ga_append(&ga, NUL);
13140 rettv->vval.v_string = (char_u *)ga.ga_data;
13141 }
13142 else
13143 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013144}
13145
13146/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013147 * "keys()" function
13148 */
13149 static void
13150f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013151 typval_T *argvars;
13152 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013153{
13154 dict_list(argvars, rettv, 0);
13155}
13156
13157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158 * "last_buffer_nr()" function.
13159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013161f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013162 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164{
13165 int n = 0;
13166 buf_T *buf;
13167
13168 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13169 if (n < buf->b_fnum)
13170 n = buf->b_fnum;
13171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013172 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013173}
13174
13175/*
13176 * "len()" function
13177 */
13178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013179f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013180 typval_T *argvars;
13181 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013182{
13183 switch (argvars[0].v_type)
13184 {
13185 case VAR_STRING:
13186 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013187 rettv->vval.v_number = (varnumber_T)STRLEN(
13188 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013189 break;
13190 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013191 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013192 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013193 case VAR_DICT:
13194 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13195 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013196 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013197 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013198 break;
13199 }
13200}
13201
Bram Moolenaar33570922005-01-25 22:26:29 +000013202static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013203
13204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013205libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013206 typval_T *argvars;
13207 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013208 int type;
13209{
13210#ifdef FEAT_LIBCALL
13211 char_u *string_in;
13212 char_u **string_result;
13213 int nr_result;
13214#endif
13215
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013216 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013217 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013218 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013219
13220 if (check_restricted() || check_secure())
13221 return;
13222
13223#ifdef FEAT_LIBCALL
13224 /* The first two args must be strings, otherwise its meaningless */
13225 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13226 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013227 string_in = NULL;
13228 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013229 string_in = argvars[2].vval.v_string;
13230 if (type == VAR_NUMBER)
13231 string_result = NULL;
13232 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013233 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013234 if (mch_libcall(argvars[0].vval.v_string,
13235 argvars[1].vval.v_string,
13236 string_in,
13237 argvars[2].vval.v_number,
13238 string_result,
13239 &nr_result) == OK
13240 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013241 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013242 }
13243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244}
13245
13246/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013247 * "libcall()" function
13248 */
13249 static void
13250f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013251 typval_T *argvars;
13252 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013253{
13254 libcall_common(argvars, rettv, VAR_STRING);
13255}
13256
13257/*
13258 * "libcallnr()" function
13259 */
13260 static void
13261f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013262 typval_T *argvars;
13263 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013264{
13265 libcall_common(argvars, rettv, VAR_NUMBER);
13266}
13267
13268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013269 * "line(string)" function
13270 */
13271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013272f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013273 typval_T *argvars;
13274 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275{
13276 linenr_T lnum = 0;
13277 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013278 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013280 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281 if (fp != NULL)
13282 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013283 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284}
13285
13286/*
13287 * "line2byte(lnum)" function
13288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013290f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013291 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293{
13294#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013295 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296#else
13297 linenr_T lnum;
13298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013299 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013301 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013303 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13304 if (rettv->vval.v_number >= 0)
13305 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306#endif
13307}
13308
13309/*
13310 * "lispindent(lnum)" function
13311 */
13312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013313f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013314 typval_T *argvars;
13315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316{
13317#ifdef FEAT_LISP
13318 pos_T pos;
13319 linenr_T lnum;
13320
13321 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013322 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13324 {
13325 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013326 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327 curwin->w_cursor = pos;
13328 }
13329 else
13330#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013331 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332}
13333
13334/*
13335 * "localtime()" function
13336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013337 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013338f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013339 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013340 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013342 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343}
13344
Bram Moolenaar33570922005-01-25 22:26:29 +000013345static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346
13347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013348get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013349 typval_T *argvars;
13350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351 int exact;
13352{
13353 char_u *keys;
13354 char_u *which;
13355 char_u buf[NUMBUFLEN];
13356 char_u *keys_buf = NULL;
13357 char_u *rhs;
13358 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013359 int abbr = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013360 int get_dict = FALSE;
13361 mapblock_T *mp;
13362 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363
13364 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013365 rettv->v_type = VAR_STRING;
13366 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013368 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369 if (*keys == NUL)
13370 return;
13371
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013372 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013373 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013374 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013375 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013376 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013377 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013378 if (argvars[3].v_type != VAR_UNKNOWN)
13379 get_dict = get_tv_number(&argvars[3]);
13380 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382 else
13383 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013384 if (which == NULL)
13385 return;
13386
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387 mode = get_map_mode(&which, 0);
13388
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013389 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013390 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013392
13393 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013395 /* Return a string. */
13396 if (rhs != NULL)
13397 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013398
Bram Moolenaarbd743252010-10-20 21:23:33 +020013399 }
13400 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13401 {
13402 /* Return a dictionary. */
13403 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13404 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13405 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406
Bram Moolenaarbd743252010-10-20 21:23:33 +020013407 dict_add_nr_str(dict, "lhs", 0L, lhs);
13408 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13409 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13410 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13411 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13412 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13413 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13414 dict_add_nr_str(dict, "mode", 0L, mapmode);
13415
13416 vim_free(lhs);
13417 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418 }
13419}
13420
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013421#ifdef FEAT_FLOAT
13422/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013423 * "log()" function
13424 */
13425 static void
13426f_log(argvars, rettv)
13427 typval_T *argvars;
13428 typval_T *rettv;
13429{
13430 float_T f;
13431
13432 rettv->v_type = VAR_FLOAT;
13433 if (get_float_arg(argvars, &f) == OK)
13434 rettv->vval.v_float = log(f);
13435 else
13436 rettv->vval.v_float = 0.0;
13437}
13438
13439/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013440 * "log10()" function
13441 */
13442 static void
13443f_log10(argvars, rettv)
13444 typval_T *argvars;
13445 typval_T *rettv;
13446{
13447 float_T f;
13448
13449 rettv->v_type = VAR_FLOAT;
13450 if (get_float_arg(argvars, &f) == OK)
13451 rettv->vval.v_float = log10(f);
13452 else
13453 rettv->vval.v_float = 0.0;
13454}
13455#endif
13456
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013458 * "map()" function
13459 */
13460 static void
13461f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013462 typval_T *argvars;
13463 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013464{
13465 filter_map(argvars, rettv, TRUE);
13466}
13467
13468/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013469 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470 */
13471 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013472f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013473 typval_T *argvars;
13474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013476 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013477}
13478
13479/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013480 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481 */
13482 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013483f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013484 typval_T *argvars;
13485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013487 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488}
13489
Bram Moolenaar33570922005-01-25 22:26:29 +000013490static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491
13492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013493find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013494 typval_T *argvars;
13495 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496 int type;
13497{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013498 char_u *str = NULL;
13499 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500 char_u *pat;
13501 regmatch_T regmatch;
13502 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013503 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504 char_u *save_cpo;
13505 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013506 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013507 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013508 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013509 list_T *l = NULL;
13510 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013511 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013512 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513
13514 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13515 save_cpo = p_cpo;
13516 p_cpo = (char_u *)"";
13517
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013518 rettv->vval.v_number = -1;
13519 if (type == 3)
13520 {
13521 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013522 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013523 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013524 }
13525 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013527 rettv->v_type = VAR_STRING;
13528 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013531 if (argvars[0].v_type == VAR_LIST)
13532 {
13533 if ((l = argvars[0].vval.v_list) == NULL)
13534 goto theend;
13535 li = l->lv_first;
13536 }
13537 else
13538 expr = str = get_tv_string(&argvars[0]);
13539
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013540 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13541 if (pat == NULL)
13542 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013543
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013544 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013546 int error = FALSE;
13547
13548 start = get_tv_number_chk(&argvars[2], &error);
13549 if (error)
13550 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013551 if (l != NULL)
13552 {
13553 li = list_find(l, start);
13554 if (li == NULL)
13555 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013556 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013557 }
13558 else
13559 {
13560 if (start < 0)
13561 start = 0;
13562 if (start > (long)STRLEN(str))
13563 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013564 /* When "count" argument is there ignore matches before "start",
13565 * otherwise skip part of the string. Differs when pattern is "^"
13566 * or "\<". */
13567 if (argvars[3].v_type != VAR_UNKNOWN)
13568 startcol = start;
13569 else
13570 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013571 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013572
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013573 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013574 nth = get_tv_number_chk(&argvars[3], &error);
13575 if (error)
13576 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577 }
13578
13579 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13580 if (regmatch.regprog != NULL)
13581 {
13582 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013583
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013584 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013585 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013586 if (l != NULL)
13587 {
13588 if (li == NULL)
13589 {
13590 match = FALSE;
13591 break;
13592 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013593 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013594 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013595 if (str == NULL)
13596 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013597 }
13598
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013599 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013600
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013601 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013602 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013603 if (l == NULL && !match)
13604 break;
13605
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013606 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013607 if (l != NULL)
13608 {
13609 li = li->li_next;
13610 ++idx;
13611 }
13612 else
13613 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013614#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013615 startcol = (colnr_T)(regmatch.startp[0]
13616 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013617#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013618 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013619#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013620 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013621 }
13622
13623 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013625 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013626 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013627 int i;
13628
13629 /* return list with matched string and submatches */
13630 for (i = 0; i < NSUBEXP; ++i)
13631 {
13632 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013633 {
13634 if (list_append_string(rettv->vval.v_list,
13635 (char_u *)"", 0) == FAIL)
13636 break;
13637 }
13638 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013639 regmatch.startp[i],
13640 (int)(regmatch.endp[i] - regmatch.startp[i]))
13641 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013642 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013643 }
13644 }
13645 else if (type == 2)
13646 {
13647 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013648 if (l != NULL)
13649 copy_tv(&li->li_tv, rettv);
13650 else
13651 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013653 }
13654 else if (l != NULL)
13655 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656 else
13657 {
13658 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013659 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660 (varnumber_T)(regmatch.startp[0] - str);
13661 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013662 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013664 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665 }
13666 }
13667 vim_free(regmatch.regprog);
13668 }
13669
13670theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013671 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672 p_cpo = save_cpo;
13673}
13674
13675/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013676 * "match()" function
13677 */
13678 static void
13679f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013680 typval_T *argvars;
13681 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013682{
13683 find_some_match(argvars, rettv, 1);
13684}
13685
13686/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013687 * "matchadd()" function
13688 */
13689 static void
13690f_matchadd(argvars, rettv)
13691 typval_T *argvars;
13692 typval_T *rettv;
13693{
13694#ifdef FEAT_SEARCH_EXTRA
13695 char_u buf[NUMBUFLEN];
13696 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13697 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13698 int prio = 10; /* default priority */
13699 int id = -1;
13700 int error = FALSE;
13701
13702 rettv->vval.v_number = -1;
13703
13704 if (grp == NULL || pat == NULL)
13705 return;
13706 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013707 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013708 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013709 if (argvars[3].v_type != VAR_UNKNOWN)
13710 id = get_tv_number_chk(&argvars[3], &error);
13711 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013712 if (error == TRUE)
13713 return;
13714 if (id >= 1 && id <= 3)
13715 {
13716 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13717 return;
13718 }
13719
13720 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13721#endif
13722}
13723
13724/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013725 * "matcharg()" function
13726 */
13727 static void
13728f_matcharg(argvars, rettv)
13729 typval_T *argvars;
13730 typval_T *rettv;
13731{
13732 if (rettv_list_alloc(rettv) == OK)
13733 {
13734#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013735 int id = get_tv_number(&argvars[0]);
13736 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013737
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013738 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013739 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013740 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13741 {
13742 list_append_string(rettv->vval.v_list,
13743 syn_id2name(m->hlg_id), -1);
13744 list_append_string(rettv->vval.v_list, m->pattern, -1);
13745 }
13746 else
13747 {
13748 list_append_string(rettv->vval.v_list, NUL, -1);
13749 list_append_string(rettv->vval.v_list, NUL, -1);
13750 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013751 }
13752#endif
13753 }
13754}
13755
13756/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013757 * "matchdelete()" function
13758 */
13759 static void
13760f_matchdelete(argvars, rettv)
13761 typval_T *argvars;
13762 typval_T *rettv;
13763{
13764#ifdef FEAT_SEARCH_EXTRA
13765 rettv->vval.v_number = match_delete(curwin,
13766 (int)get_tv_number(&argvars[0]), TRUE);
13767#endif
13768}
13769
13770/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013771 * "matchend()" function
13772 */
13773 static void
13774f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013775 typval_T *argvars;
13776 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013777{
13778 find_some_match(argvars, rettv, 0);
13779}
13780
13781/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013782 * "matchlist()" function
13783 */
13784 static void
13785f_matchlist(argvars, rettv)
13786 typval_T *argvars;
13787 typval_T *rettv;
13788{
13789 find_some_match(argvars, rettv, 3);
13790}
13791
13792/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013793 * "matchstr()" function
13794 */
13795 static void
13796f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013797 typval_T *argvars;
13798 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013799{
13800 find_some_match(argvars, rettv, 2);
13801}
13802
Bram Moolenaar33570922005-01-25 22:26:29 +000013803static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013804
13805 static void
13806max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013807 typval_T *argvars;
13808 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013809 int domax;
13810{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013811 long n = 0;
13812 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013813 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013814
13815 if (argvars[0].v_type == VAR_LIST)
13816 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013817 list_T *l;
13818 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013819
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013820 l = argvars[0].vval.v_list;
13821 if (l != NULL)
13822 {
13823 li = l->lv_first;
13824 if (li != NULL)
13825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013826 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013827 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013828 {
13829 li = li->li_next;
13830 if (li == NULL)
13831 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013832 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013833 if (domax ? i > n : i < n)
13834 n = i;
13835 }
13836 }
13837 }
13838 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013839 else if (argvars[0].v_type == VAR_DICT)
13840 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013841 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013842 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013843 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013844 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013845
13846 d = argvars[0].vval.v_dict;
13847 if (d != NULL)
13848 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013849 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013850 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013851 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013852 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013853 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013854 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013855 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013856 if (first)
13857 {
13858 n = i;
13859 first = FALSE;
13860 }
13861 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013862 n = i;
13863 }
13864 }
13865 }
13866 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013867 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013868 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013869 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013870}
13871
13872/*
13873 * "max()" function
13874 */
13875 static void
13876f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013877 typval_T *argvars;
13878 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013879{
13880 max_min(argvars, rettv, TRUE);
13881}
13882
13883/*
13884 * "min()" function
13885 */
13886 static void
13887f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013888 typval_T *argvars;
13889 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013890{
13891 max_min(argvars, rettv, FALSE);
13892}
13893
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013894static int mkdir_recurse __ARGS((char_u *dir, int prot));
13895
13896/*
13897 * Create the directory in which "dir" is located, and higher levels when
13898 * needed.
13899 */
13900 static int
13901mkdir_recurse(dir, prot)
13902 char_u *dir;
13903 int prot;
13904{
13905 char_u *p;
13906 char_u *updir;
13907 int r = FAIL;
13908
13909 /* Get end of directory name in "dir".
13910 * We're done when it's "/" or "c:/". */
13911 p = gettail_sep(dir);
13912 if (p <= get_past_head(dir))
13913 return OK;
13914
13915 /* If the directory exists we're done. Otherwise: create it.*/
13916 updir = vim_strnsave(dir, (int)(p - dir));
13917 if (updir == NULL)
13918 return FAIL;
13919 if (mch_isdir(updir))
13920 r = OK;
13921 else if (mkdir_recurse(updir, prot) == OK)
13922 r = vim_mkdir_emsg(updir, prot);
13923 vim_free(updir);
13924 return r;
13925}
13926
13927#ifdef vim_mkdir
13928/*
13929 * "mkdir()" function
13930 */
13931 static void
13932f_mkdir(argvars, rettv)
13933 typval_T *argvars;
13934 typval_T *rettv;
13935{
13936 char_u *dir;
13937 char_u buf[NUMBUFLEN];
13938 int prot = 0755;
13939
13940 rettv->vval.v_number = FAIL;
13941 if (check_restricted() || check_secure())
13942 return;
13943
13944 dir = get_tv_string_buf(&argvars[0], buf);
13945 if (argvars[1].v_type != VAR_UNKNOWN)
13946 {
13947 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013948 prot = get_tv_number_chk(&argvars[2], NULL);
13949 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013950 mkdir_recurse(dir, prot);
13951 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013952 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013953}
13954#endif
13955
Bram Moolenaar0d660222005-01-07 21:51:51 +000013956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957 * "mode()" function
13958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013960f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013961 typval_T *argvars;
13962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013964 char_u buf[3];
13965
13966 buf[1] = NUL;
13967 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968
13969#ifdef FEAT_VISUAL
13970 if (VIsual_active)
13971 {
13972 if (VIsual_select)
13973 buf[0] = VIsual_mode + 's' - 'v';
13974 else
13975 buf[0] = VIsual_mode;
13976 }
13977 else
13978#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013979 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13980 || State == CONFIRM)
13981 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013983 if (State == ASKMORE)
13984 buf[1] = 'm';
13985 else if (State == CONFIRM)
13986 buf[1] = '?';
13987 }
13988 else if (State == EXTERNCMD)
13989 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990 else if (State & INSERT)
13991 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013992#ifdef FEAT_VREPLACE
13993 if (State & VREPLACE_FLAG)
13994 {
13995 buf[0] = 'R';
13996 buf[1] = 'v';
13997 }
13998 else
13999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000 if (State & REPLACE_FLAG)
14001 buf[0] = 'R';
14002 else
14003 buf[0] = 'i';
14004 }
14005 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014006 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014008 if (exmode_active)
14009 buf[1] = 'v';
14010 }
14011 else if (exmode_active)
14012 {
14013 buf[0] = 'c';
14014 buf[1] = 'e';
14015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014019 if (finish_op)
14020 buf[1] = 'o';
14021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014023 /* Clear out the minor mode when the argument is not a non-zero number or
14024 * non-empty string. */
14025 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014026 buf[1] = NUL;
14027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014028 rettv->vval.v_string = vim_strsave(buf);
14029 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030}
14031
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014032#ifdef FEAT_MZSCHEME
14033/*
14034 * "mzeval()" function
14035 */
14036 static void
14037f_mzeval(argvars, rettv)
14038 typval_T *argvars;
14039 typval_T *rettv;
14040{
14041 char_u *str;
14042 char_u buf[NUMBUFLEN];
14043
14044 str = get_tv_string_buf(&argvars[0], buf);
14045 do_mzeval(str, rettv);
14046}
14047#endif
14048
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014050 * "nextnonblank()" function
14051 */
14052 static void
14053f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014054 typval_T *argvars;
14055 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014056{
14057 linenr_T lnum;
14058
14059 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14060 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014061 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014062 {
14063 lnum = 0;
14064 break;
14065 }
14066 if (*skipwhite(ml_get(lnum)) != NUL)
14067 break;
14068 }
14069 rettv->vval.v_number = lnum;
14070}
14071
14072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014073 * "nr2char()" function
14074 */
14075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014076f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014077 typval_T *argvars;
14078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079{
14080 char_u buf[NUMBUFLEN];
14081
14082#ifdef FEAT_MBYTE
14083 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014084 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014085 else
14086#endif
14087 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014088 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089 buf[1] = NUL;
14090 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014091 rettv->v_type = VAR_STRING;
14092 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014093}
14094
14095/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014096 * "pathshorten()" function
14097 */
14098 static void
14099f_pathshorten(argvars, rettv)
14100 typval_T *argvars;
14101 typval_T *rettv;
14102{
14103 char_u *p;
14104
14105 rettv->v_type = VAR_STRING;
14106 p = get_tv_string_chk(&argvars[0]);
14107 if (p == NULL)
14108 rettv->vval.v_string = NULL;
14109 else
14110 {
14111 p = vim_strsave(p);
14112 rettv->vval.v_string = p;
14113 if (p != NULL)
14114 shorten_dir(p);
14115 }
14116}
14117
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014118#ifdef FEAT_FLOAT
14119/*
14120 * "pow()" function
14121 */
14122 static void
14123f_pow(argvars, rettv)
14124 typval_T *argvars;
14125 typval_T *rettv;
14126{
14127 float_T fx, fy;
14128
14129 rettv->v_type = VAR_FLOAT;
14130 if (get_float_arg(argvars, &fx) == OK
14131 && get_float_arg(&argvars[1], &fy) == OK)
14132 rettv->vval.v_float = pow(fx, fy);
14133 else
14134 rettv->vval.v_float = 0.0;
14135}
14136#endif
14137
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014138/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014139 * "prevnonblank()" function
14140 */
14141 static void
14142f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014143 typval_T *argvars;
14144 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014145{
14146 linenr_T lnum;
14147
14148 lnum = get_tv_lnum(argvars);
14149 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14150 lnum = 0;
14151 else
14152 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14153 --lnum;
14154 rettv->vval.v_number = lnum;
14155}
14156
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014157#ifdef HAVE_STDARG_H
14158/* This dummy va_list is here because:
14159 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14160 * - locally in the function results in a "used before set" warning
14161 * - using va_start() to initialize it gives "function with fixed args" error */
14162static va_list ap;
14163#endif
14164
Bram Moolenaar8c711452005-01-14 21:53:12 +000014165/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014166 * "printf()" function
14167 */
14168 static void
14169f_printf(argvars, rettv)
14170 typval_T *argvars;
14171 typval_T *rettv;
14172{
14173 rettv->v_type = VAR_STRING;
14174 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014175#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014176 {
14177 char_u buf[NUMBUFLEN];
14178 int len;
14179 char_u *s;
14180 int saved_did_emsg = did_emsg;
14181 char *fmt;
14182
14183 /* Get the required length, allocate the buffer and do it for real. */
14184 did_emsg = FALSE;
14185 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014186 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014187 if (!did_emsg)
14188 {
14189 s = alloc(len + 1);
14190 if (s != NULL)
14191 {
14192 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014193 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014194 }
14195 }
14196 did_emsg |= saved_did_emsg;
14197 }
14198#endif
14199}
14200
14201/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014202 * "pumvisible()" function
14203 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014204 static void
14205f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014206 typval_T *argvars UNUSED;
14207 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014208{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014209#ifdef FEAT_INS_EXPAND
14210 if (pum_visible())
14211 rettv->vval.v_number = 1;
14212#endif
14213}
14214
14215/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014216 * "range()" function
14217 */
14218 static void
14219f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014220 typval_T *argvars;
14221 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014222{
14223 long start;
14224 long end;
14225 long stride = 1;
14226 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014227 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014228
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014229 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014230 if (argvars[1].v_type == VAR_UNKNOWN)
14231 {
14232 end = start - 1;
14233 start = 0;
14234 }
14235 else
14236 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014237 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014238 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014239 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014240 }
14241
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014242 if (error)
14243 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014244 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014245 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014246 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014247 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014248 else
14249 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014250 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014251 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014252 if (list_append_number(rettv->vval.v_list,
14253 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014254 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014255 }
14256}
14257
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014258/*
14259 * "readfile()" function
14260 */
14261 static void
14262f_readfile(argvars, rettv)
14263 typval_T *argvars;
14264 typval_T *rettv;
14265{
14266 int binary = FALSE;
14267 char_u *fname;
14268 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014269 listitem_T *li;
14270#define FREAD_SIZE 200 /* optimized for text lines */
14271 char_u buf[FREAD_SIZE];
14272 int readlen; /* size of last fread() */
14273 int buflen; /* nr of valid chars in buf[] */
14274 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14275 int tolist; /* first byte in buf[] still to be put in list */
14276 int chop; /* how many CR to chop off */
14277 char_u *prev = NULL; /* previously read bytes, if any */
14278 int prevlen = 0; /* length of "prev" if not NULL */
14279 char_u *s;
14280 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014281 long maxline = MAXLNUM;
14282 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014283
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014284 if (argvars[1].v_type != VAR_UNKNOWN)
14285 {
14286 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14287 binary = TRUE;
14288 if (argvars[2].v_type != VAR_UNKNOWN)
14289 maxline = get_tv_number(&argvars[2]);
14290 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014291
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014292 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014293 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014294
14295 /* Always open the file in binary mode, library functions have a mind of
14296 * their own about CR-LF conversion. */
14297 fname = get_tv_string(&argvars[0]);
14298 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14299 {
14300 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14301 return;
14302 }
14303
14304 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014305 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014306 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014307 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014308 buflen = filtd + readlen;
14309 tolist = 0;
14310 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14311 {
14312 if (buf[filtd] == '\n' || readlen <= 0)
14313 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014314 /* In binary mode add an empty list item when the last
14315 * non-empty line ends in a '\n'. */
14316 if (!binary && readlen == 0 && filtd == 0 && prev == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014317 break;
14318
14319 /* Found end-of-line or end-of-file: add a text line to the
14320 * list. */
14321 chop = 0;
14322 if (!binary)
14323 while (filtd - chop - 1 >= tolist
14324 && buf[filtd - chop - 1] == '\r')
14325 ++chop;
14326 len = filtd - tolist - chop;
14327 if (prev == NULL)
14328 s = vim_strnsave(buf + tolist, len);
14329 else
14330 {
14331 s = alloc((unsigned)(prevlen + len + 1));
14332 if (s != NULL)
14333 {
14334 mch_memmove(s, prev, prevlen);
14335 vim_free(prev);
14336 prev = NULL;
14337 mch_memmove(s + prevlen, buf + tolist, len);
14338 s[prevlen + len] = NUL;
14339 }
14340 }
14341 tolist = filtd + 1;
14342
14343 li = listitem_alloc();
14344 if (li == NULL)
14345 {
14346 vim_free(s);
14347 break;
14348 }
14349 li->li_tv.v_type = VAR_STRING;
14350 li->li_tv.v_lock = 0;
14351 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014352 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014353
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014354 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014355 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014356 if (readlen <= 0)
14357 break;
14358 }
14359 else if (buf[filtd] == NUL)
14360 buf[filtd] = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014361#ifdef FEAT_MBYTE
14362 else if (buf[filtd] == 0xef
14363 && enc_utf8
14364 && filtd + 2 < buflen
14365 && !binary
14366 && buf[filtd + 1] == 0xbb
14367 && buf[filtd + 2] == 0xbf)
14368 {
14369 /* remove utf-8 byte order mark */
14370 mch_memmove(buf + filtd, buf + filtd + 3, buflen - filtd - 3);
14371 --filtd;
14372 buflen -= 3;
14373 }
14374#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014375 }
14376 if (readlen <= 0)
14377 break;
14378
14379 if (tolist == 0)
14380 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014381 if (buflen >= FREAD_SIZE / 2)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014382 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014383 /* "buf" is full, need to move text to an allocated buffer */
14384 if (prev == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014385 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014386 prev = vim_strnsave(buf, buflen);
14387 prevlen = buflen;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014388 }
Bram Moolenaar27b60562011-04-01 16:07:46 +020014389 else
14390 {
14391 s = alloc((unsigned)(prevlen + buflen));
14392 if (s != NULL)
14393 {
14394 mch_memmove(s, prev, prevlen);
14395 mch_memmove(s + prevlen, buf, buflen);
14396 vim_free(prev);
14397 prev = s;
14398 prevlen += buflen;
14399 }
14400 }
14401 filtd = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014402 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014403 }
14404 else
14405 {
14406 mch_memmove(buf, buf + tolist, buflen - tolist);
14407 filtd -= tolist;
14408 }
14409 }
14410
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014411 /*
14412 * For a negative line count use only the lines at the end of the file,
14413 * free the rest.
14414 */
14415 if (maxline < 0)
14416 while (cnt > -maxline)
14417 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014418 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014419 --cnt;
14420 }
14421
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014422 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014423 fclose(fd);
14424}
14425
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014426#if defined(FEAT_RELTIME)
14427static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14428
14429/*
14430 * Convert a List to proftime_T.
14431 * Return FAIL when there is something wrong.
14432 */
14433 static int
14434list2proftime(arg, tm)
14435 typval_T *arg;
14436 proftime_T *tm;
14437{
14438 long n1, n2;
14439 int error = FALSE;
14440
14441 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14442 || arg->vval.v_list->lv_len != 2)
14443 return FAIL;
14444 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14445 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14446# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014447 tm->HighPart = n1;
14448 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014449# else
14450 tm->tv_sec = n1;
14451 tm->tv_usec = n2;
14452# endif
14453 return error ? FAIL : OK;
14454}
14455#endif /* FEAT_RELTIME */
14456
14457/*
14458 * "reltime()" function
14459 */
14460 static void
14461f_reltime(argvars, rettv)
14462 typval_T *argvars;
14463 typval_T *rettv;
14464{
14465#ifdef FEAT_RELTIME
14466 proftime_T res;
14467 proftime_T start;
14468
14469 if (argvars[0].v_type == VAR_UNKNOWN)
14470 {
14471 /* No arguments: get current time. */
14472 profile_start(&res);
14473 }
14474 else if (argvars[1].v_type == VAR_UNKNOWN)
14475 {
14476 if (list2proftime(&argvars[0], &res) == FAIL)
14477 return;
14478 profile_end(&res);
14479 }
14480 else
14481 {
14482 /* Two arguments: compute the difference. */
14483 if (list2proftime(&argvars[0], &start) == FAIL
14484 || list2proftime(&argvars[1], &res) == FAIL)
14485 return;
14486 profile_sub(&res, &start);
14487 }
14488
14489 if (rettv_list_alloc(rettv) == OK)
14490 {
14491 long n1, n2;
14492
14493# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014494 n1 = res.HighPart;
14495 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014496# else
14497 n1 = res.tv_sec;
14498 n2 = res.tv_usec;
14499# endif
14500 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14501 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14502 }
14503#endif
14504}
14505
14506/*
14507 * "reltimestr()" function
14508 */
14509 static void
14510f_reltimestr(argvars, rettv)
14511 typval_T *argvars;
14512 typval_T *rettv;
14513{
14514#ifdef FEAT_RELTIME
14515 proftime_T tm;
14516#endif
14517
14518 rettv->v_type = VAR_STRING;
14519 rettv->vval.v_string = NULL;
14520#ifdef FEAT_RELTIME
14521 if (list2proftime(&argvars[0], &tm) == OK)
14522 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14523#endif
14524}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014525
Bram Moolenaar0d660222005-01-07 21:51:51 +000014526#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14527static void make_connection __ARGS((void));
14528static int check_connection __ARGS((void));
14529
14530 static void
14531make_connection()
14532{
14533 if (X_DISPLAY == NULL
14534# ifdef FEAT_GUI
14535 && !gui.in_use
14536# endif
14537 )
14538 {
14539 x_force_connect = TRUE;
14540 setup_term_clip();
14541 x_force_connect = FALSE;
14542 }
14543}
14544
14545 static int
14546check_connection()
14547{
14548 make_connection();
14549 if (X_DISPLAY == NULL)
14550 {
14551 EMSG(_("E240: No connection to Vim server"));
14552 return FAIL;
14553 }
14554 return OK;
14555}
14556#endif
14557
14558#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014559static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014560
14561 static void
14562remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014563 typval_T *argvars;
14564 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014565 int expr;
14566{
14567 char_u *server_name;
14568 char_u *keys;
14569 char_u *r = NULL;
14570 char_u buf[NUMBUFLEN];
14571# ifdef WIN32
14572 HWND w;
14573# else
14574 Window w;
14575# endif
14576
14577 if (check_restricted() || check_secure())
14578 return;
14579
14580# ifdef FEAT_X11
14581 if (check_connection() == FAIL)
14582 return;
14583# endif
14584
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014585 server_name = get_tv_string_chk(&argvars[0]);
14586 if (server_name == NULL)
14587 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014588 keys = get_tv_string_buf(&argvars[1], buf);
14589# ifdef WIN32
14590 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14591# else
14592 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14593 < 0)
14594# endif
14595 {
14596 if (r != NULL)
14597 EMSG(r); /* sending worked but evaluation failed */
14598 else
14599 EMSG2(_("E241: Unable to send to %s"), server_name);
14600 return;
14601 }
14602
14603 rettv->vval.v_string = r;
14604
14605 if (argvars[2].v_type != VAR_UNKNOWN)
14606 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014607 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014608 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014609 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014610
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014611 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014612 v.di_tv.v_type = VAR_STRING;
14613 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014614 idvar = get_tv_string_chk(&argvars[2]);
14615 if (idvar != NULL)
14616 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014617 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014618 }
14619}
14620#endif
14621
14622/*
14623 * "remote_expr()" function
14624 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014625 static void
14626f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014627 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014628 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014629{
14630 rettv->v_type = VAR_STRING;
14631 rettv->vval.v_string = NULL;
14632#ifdef FEAT_CLIENTSERVER
14633 remote_common(argvars, rettv, TRUE);
14634#endif
14635}
14636
14637/*
14638 * "remote_foreground()" function
14639 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014640 static void
14641f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014642 typval_T *argvars UNUSED;
14643 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014644{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014645#ifdef FEAT_CLIENTSERVER
14646# ifdef WIN32
14647 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014648 {
14649 char_u *server_name = get_tv_string_chk(&argvars[0]);
14650
14651 if (server_name != NULL)
14652 serverForeground(server_name);
14653 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014654# else
14655 /* Send a foreground() expression to the server. */
14656 argvars[1].v_type = VAR_STRING;
14657 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14658 argvars[2].v_type = VAR_UNKNOWN;
14659 remote_common(argvars, rettv, TRUE);
14660 vim_free(argvars[1].vval.v_string);
14661# endif
14662#endif
14663}
14664
Bram Moolenaar0d660222005-01-07 21:51:51 +000014665 static void
14666f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014667 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014668 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014669{
14670#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014671 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014672 char_u *s = NULL;
14673# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014674 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014675# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014676 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014677
14678 if (check_restricted() || check_secure())
14679 {
14680 rettv->vval.v_number = -1;
14681 return;
14682 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014683 serverid = get_tv_string_chk(&argvars[0]);
14684 if (serverid == NULL)
14685 {
14686 rettv->vval.v_number = -1;
14687 return; /* type error; errmsg already given */
14688 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014689# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014690 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014691 if (n == 0)
14692 rettv->vval.v_number = -1;
14693 else
14694 {
14695 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14696 rettv->vval.v_number = (s != NULL);
14697 }
14698# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014699 if (check_connection() == FAIL)
14700 return;
14701
14702 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014703 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014704# endif
14705
14706 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14707 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014708 char_u *retvar;
14709
Bram Moolenaar33570922005-01-25 22:26:29 +000014710 v.di_tv.v_type = VAR_STRING;
14711 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014712 retvar = get_tv_string_chk(&argvars[1]);
14713 if (retvar != NULL)
14714 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014715 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014716 }
14717#else
14718 rettv->vval.v_number = -1;
14719#endif
14720}
14721
Bram Moolenaar0d660222005-01-07 21:51:51 +000014722 static void
14723f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014724 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014725 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014726{
14727 char_u *r = NULL;
14728
14729#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014730 char_u *serverid = get_tv_string_chk(&argvars[0]);
14731
14732 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014733 {
14734# ifdef WIN32
14735 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014736 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014737
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014738 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014739 if (n != 0)
14740 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14741 if (r == NULL)
14742# else
14743 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014744 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014745# endif
14746 EMSG(_("E277: Unable to read a server reply"));
14747 }
14748#endif
14749 rettv->v_type = VAR_STRING;
14750 rettv->vval.v_string = r;
14751}
14752
14753/*
14754 * "remote_send()" function
14755 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014756 static void
14757f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014758 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014759 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014760{
14761 rettv->v_type = VAR_STRING;
14762 rettv->vval.v_string = NULL;
14763#ifdef FEAT_CLIENTSERVER
14764 remote_common(argvars, rettv, FALSE);
14765#endif
14766}
14767
14768/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014769 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014770 */
14771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014772f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014773 typval_T *argvars;
14774 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014775{
Bram Moolenaar33570922005-01-25 22:26:29 +000014776 list_T *l;
14777 listitem_T *item, *item2;
14778 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014779 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014780 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014781 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014782 dict_T *d;
14783 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014784 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014785
Bram Moolenaar8c711452005-01-14 21:53:12 +000014786 if (argvars[0].v_type == VAR_DICT)
14787 {
14788 if (argvars[2].v_type != VAR_UNKNOWN)
14789 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014790 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014791 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014792 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014793 key = get_tv_string_chk(&argvars[1]);
14794 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014795 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014796 di = dict_find(d, key, -1);
14797 if (di == NULL)
14798 EMSG2(_(e_dictkey), key);
14799 else
14800 {
14801 *rettv = di->di_tv;
14802 init_tv(&di->di_tv);
14803 dictitem_remove(d, di);
14804 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014805 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014806 }
14807 }
14808 else if (argvars[0].v_type != VAR_LIST)
14809 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014810 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014811 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014813 int error = FALSE;
14814
14815 idx = get_tv_number_chk(&argvars[1], &error);
14816 if (error)
14817 ; /* type error: do nothing, errmsg already given */
14818 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014819 EMSGN(_(e_listidx), idx);
14820 else
14821 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014822 if (argvars[2].v_type == VAR_UNKNOWN)
14823 {
14824 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014825 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014826 *rettv = item->li_tv;
14827 vim_free(item);
14828 }
14829 else
14830 {
14831 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014832 end = get_tv_number_chk(&argvars[2], &error);
14833 if (error)
14834 ; /* type error: do nothing */
14835 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014836 EMSGN(_(e_listidx), end);
14837 else
14838 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014839 int cnt = 0;
14840
14841 for (li = item; li != NULL; li = li->li_next)
14842 {
14843 ++cnt;
14844 if (li == item2)
14845 break;
14846 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014847 if (li == NULL) /* didn't find "item2" after "item" */
14848 EMSG(_(e_invrange));
14849 else
14850 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014851 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014852 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014853 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014854 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014855 l->lv_first = item;
14856 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014857 item->li_prev = NULL;
14858 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014859 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014860 }
14861 }
14862 }
14863 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014864 }
14865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014866}
14867
14868/*
14869 * "rename({from}, {to})" function
14870 */
14871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014872f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014873 typval_T *argvars;
14874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014875{
14876 char_u buf[NUMBUFLEN];
14877
14878 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014879 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014880 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014881 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14882 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014883}
14884
14885/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014886 * "repeat()" function
14887 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014888 static void
14889f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014890 typval_T *argvars;
14891 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014892{
14893 char_u *p;
14894 int n;
14895 int slen;
14896 int len;
14897 char_u *r;
14898 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014899
14900 n = get_tv_number(&argvars[1]);
14901 if (argvars[0].v_type == VAR_LIST)
14902 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014903 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014904 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014905 if (list_extend(rettv->vval.v_list,
14906 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014907 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014908 }
14909 else
14910 {
14911 p = get_tv_string(&argvars[0]);
14912 rettv->v_type = VAR_STRING;
14913 rettv->vval.v_string = NULL;
14914
14915 slen = (int)STRLEN(p);
14916 len = slen * n;
14917 if (len <= 0)
14918 return;
14919
14920 r = alloc(len + 1);
14921 if (r != NULL)
14922 {
14923 for (i = 0; i < n; i++)
14924 mch_memmove(r + i * slen, p, (size_t)slen);
14925 r[len] = NUL;
14926 }
14927
14928 rettv->vval.v_string = r;
14929 }
14930}
14931
14932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933 * "resolve()" function
14934 */
14935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014936f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014937 typval_T *argvars;
14938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014939{
14940 char_u *p;
14941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014942 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014943#ifdef FEAT_SHORTCUT
14944 {
14945 char_u *v = NULL;
14946
14947 v = mch_resolve_shortcut(p);
14948 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014949 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014950 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014951 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014952 }
14953#else
14954# ifdef HAVE_READLINK
14955 {
14956 char_u buf[MAXPATHL + 1];
14957 char_u *cpy;
14958 int len;
14959 char_u *remain = NULL;
14960 char_u *q;
14961 int is_relative_to_current = FALSE;
14962 int has_trailing_pathsep = FALSE;
14963 int limit = 100;
14964
14965 p = vim_strsave(p);
14966
14967 if (p[0] == '.' && (vim_ispathsep(p[1])
14968 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14969 is_relative_to_current = TRUE;
14970
14971 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014972 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014973 has_trailing_pathsep = TRUE;
14974
14975 q = getnextcomp(p);
14976 if (*q != NUL)
14977 {
14978 /* Separate the first path component in "p", and keep the
14979 * remainder (beginning with the path separator). */
14980 remain = vim_strsave(q - 1);
14981 q[-1] = NUL;
14982 }
14983
14984 for (;;)
14985 {
14986 for (;;)
14987 {
14988 len = readlink((char *)p, (char *)buf, MAXPATHL);
14989 if (len <= 0)
14990 break;
14991 buf[len] = NUL;
14992
14993 if (limit-- == 0)
14994 {
14995 vim_free(p);
14996 vim_free(remain);
14997 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014998 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999 goto fail;
15000 }
15001
15002 /* Ensure that the result will have a trailing path separator
15003 * if the argument has one. */
15004 if (remain == NULL && has_trailing_pathsep)
15005 add_pathsep(buf);
15006
15007 /* Separate the first path component in the link value and
15008 * concatenate the remainders. */
15009 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15010 if (*q != NUL)
15011 {
15012 if (remain == NULL)
15013 remain = vim_strsave(q - 1);
15014 else
15015 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015016 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017 if (cpy != NULL)
15018 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015019 vim_free(remain);
15020 remain = cpy;
15021 }
15022 }
15023 q[-1] = NUL;
15024 }
15025
15026 q = gettail(p);
15027 if (q > p && *q == NUL)
15028 {
15029 /* Ignore trailing path separator. */
15030 q[-1] = NUL;
15031 q = gettail(p);
15032 }
15033 if (q > p && !mch_isFullName(buf))
15034 {
15035 /* symlink is relative to directory of argument */
15036 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15037 if (cpy != NULL)
15038 {
15039 STRCPY(cpy, p);
15040 STRCPY(gettail(cpy), buf);
15041 vim_free(p);
15042 p = cpy;
15043 }
15044 }
15045 else
15046 {
15047 vim_free(p);
15048 p = vim_strsave(buf);
15049 }
15050 }
15051
15052 if (remain == NULL)
15053 break;
15054
15055 /* Append the first path component of "remain" to "p". */
15056 q = getnextcomp(remain + 1);
15057 len = q - remain - (*q != NUL);
15058 cpy = vim_strnsave(p, STRLEN(p) + len);
15059 if (cpy != NULL)
15060 {
15061 STRNCAT(cpy, remain, len);
15062 vim_free(p);
15063 p = cpy;
15064 }
15065 /* Shorten "remain". */
15066 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015067 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015068 else
15069 {
15070 vim_free(remain);
15071 remain = NULL;
15072 }
15073 }
15074
15075 /* If the result is a relative path name, make it explicitly relative to
15076 * the current directory if and only if the argument had this form. */
15077 if (!vim_ispathsep(*p))
15078 {
15079 if (is_relative_to_current
15080 && *p != NUL
15081 && !(p[0] == '.'
15082 && (p[1] == NUL
15083 || vim_ispathsep(p[1])
15084 || (p[1] == '.'
15085 && (p[2] == NUL
15086 || vim_ispathsep(p[2]))))))
15087 {
15088 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015089 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015090 if (cpy != NULL)
15091 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015092 vim_free(p);
15093 p = cpy;
15094 }
15095 }
15096 else if (!is_relative_to_current)
15097 {
15098 /* Strip leading "./". */
15099 q = p;
15100 while (q[0] == '.' && vim_ispathsep(q[1]))
15101 q += 2;
15102 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015103 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015104 }
15105 }
15106
15107 /* Ensure that the result will have no trailing path separator
15108 * if the argument had none. But keep "/" or "//". */
15109 if (!has_trailing_pathsep)
15110 {
15111 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015112 if (after_pathsep(p, q))
15113 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015114 }
15115
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015116 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015117 }
15118# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015119 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015120# endif
15121#endif
15122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015123 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015124
15125#ifdef HAVE_READLINK
15126fail:
15127#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015128 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129}
15130
15131/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015132 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015133 */
15134 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015135f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015136 typval_T *argvars;
15137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138{
Bram Moolenaar33570922005-01-25 22:26:29 +000015139 list_T *l;
15140 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141
Bram Moolenaar0d660222005-01-07 21:51:51 +000015142 if (argvars[0].v_type != VAR_LIST)
15143 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015144 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015145 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015146 {
15147 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015148 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015149 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015150 while (li != NULL)
15151 {
15152 ni = li->li_prev;
15153 list_append(l, li);
15154 li = ni;
15155 }
15156 rettv->vval.v_list = l;
15157 rettv->v_type = VAR_LIST;
15158 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015159 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161}
15162
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015163#define SP_NOMOVE 0x01 /* don't move cursor */
15164#define SP_REPEAT 0x02 /* repeat to find outer pair */
15165#define SP_RETCOUNT 0x04 /* return matchcount */
15166#define SP_SETPCMARK 0x08 /* set previous context mark */
15167#define SP_START 0x10 /* accept match at start position */
15168#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15169#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015170
Bram Moolenaar33570922005-01-25 22:26:29 +000015171static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015172
15173/*
15174 * Get flags for a search function.
15175 * Possibly sets "p_ws".
15176 * Returns BACKWARD, FORWARD or zero (for an error).
15177 */
15178 static int
15179get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015180 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015181 int *flagsp;
15182{
15183 int dir = FORWARD;
15184 char_u *flags;
15185 char_u nbuf[NUMBUFLEN];
15186 int mask;
15187
15188 if (varp->v_type != VAR_UNKNOWN)
15189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015190 flags = get_tv_string_buf_chk(varp, nbuf);
15191 if (flags == NULL)
15192 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015193 while (*flags != NUL)
15194 {
15195 switch (*flags)
15196 {
15197 case 'b': dir = BACKWARD; break;
15198 case 'w': p_ws = TRUE; break;
15199 case 'W': p_ws = FALSE; break;
15200 default: mask = 0;
15201 if (flagsp != NULL)
15202 switch (*flags)
15203 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015204 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015205 case 'e': mask = SP_END; break;
15206 case 'm': mask = SP_RETCOUNT; break;
15207 case 'n': mask = SP_NOMOVE; break;
15208 case 'p': mask = SP_SUBPAT; break;
15209 case 'r': mask = SP_REPEAT; break;
15210 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015211 }
15212 if (mask == 0)
15213 {
15214 EMSG2(_(e_invarg2), flags);
15215 dir = 0;
15216 }
15217 else
15218 *flagsp |= mask;
15219 }
15220 if (dir == 0)
15221 break;
15222 ++flags;
15223 }
15224 }
15225 return dir;
15226}
15227
Bram Moolenaar071d4272004-06-13 20:20:40 +000015228/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015229 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015230 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015231 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015232search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015233 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015234 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015235 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015236{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015237 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015238 char_u *pat;
15239 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015240 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241 int save_p_ws = p_ws;
15242 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015243 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015244 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015245 proftime_T tm;
15246#ifdef FEAT_RELTIME
15247 long time_limit = 0;
15248#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015249 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015250 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015252 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015253 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015254 if (dir == 0)
15255 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015256 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015257 if (flags & SP_START)
15258 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015259 if (flags & SP_END)
15260 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015261
Bram Moolenaar76929292008-01-06 19:07:36 +000015262 /* Optional arguments: line number to stop searching and timeout. */
15263 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015264 {
15265 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15266 if (lnum_stop < 0)
15267 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015268#ifdef FEAT_RELTIME
15269 if (argvars[3].v_type != VAR_UNKNOWN)
15270 {
15271 time_limit = get_tv_number_chk(&argvars[3], NULL);
15272 if (time_limit < 0)
15273 goto theend;
15274 }
15275#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015276 }
15277
Bram Moolenaar76929292008-01-06 19:07:36 +000015278#ifdef FEAT_RELTIME
15279 /* Set the time limit, if there is one. */
15280 profile_setlimit(time_limit, &tm);
15281#endif
15282
Bram Moolenaar231334e2005-07-25 20:46:57 +000015283 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015284 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015285 * Check to make sure only those flags are set.
15286 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15287 * flags cannot be set. Check for that condition also.
15288 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015289 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015290 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015291 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015292 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015293 goto theend;
15294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015295
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015296 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015297 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015298 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015299 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015300 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015301 if (flags & SP_SUBPAT)
15302 retval = subpatnum;
15303 else
15304 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015305 if (flags & SP_SETPCMARK)
15306 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015307 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015308 if (match_pos != NULL)
15309 {
15310 /* Store the match cursor position */
15311 match_pos->lnum = pos.lnum;
15312 match_pos->col = pos.col + 1;
15313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314 /* "/$" will put the cursor after the end of the line, may need to
15315 * correct that here */
15316 check_cursor();
15317 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015318
15319 /* If 'n' flag is used: restore cursor position. */
15320 if (flags & SP_NOMOVE)
15321 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015322 else
15323 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015324theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015326
15327 return retval;
15328}
15329
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015330#ifdef FEAT_FLOAT
15331/*
15332 * "round({float})" function
15333 */
15334 static void
15335f_round(argvars, rettv)
15336 typval_T *argvars;
15337 typval_T *rettv;
15338{
15339 float_T f;
15340
15341 rettv->v_type = VAR_FLOAT;
15342 if (get_float_arg(argvars, &f) == OK)
15343 /* round() is not in C90, use ceil() or floor() instead. */
15344 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15345 else
15346 rettv->vval.v_float = 0.0;
15347}
15348#endif
15349
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015350/*
15351 * "search()" function
15352 */
15353 static void
15354f_search(argvars, rettv)
15355 typval_T *argvars;
15356 typval_T *rettv;
15357{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015358 int flags = 0;
15359
15360 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361}
15362
Bram Moolenaar071d4272004-06-13 20:20:40 +000015363/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015364 * "searchdecl()" function
15365 */
15366 static void
15367f_searchdecl(argvars, rettv)
15368 typval_T *argvars;
15369 typval_T *rettv;
15370{
15371 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015372 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015373 int error = FALSE;
15374 char_u *name;
15375
15376 rettv->vval.v_number = 1; /* default: FAIL */
15377
15378 name = get_tv_string_chk(&argvars[0]);
15379 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015380 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015381 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015382 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15383 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15384 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015385 if (!error && name != NULL)
15386 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015387 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015388}
15389
15390/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015391 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015393 static int
15394searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015395 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015396 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397{
15398 char_u *spat, *mpat, *epat;
15399 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401 int dir;
15402 int flags = 0;
15403 char_u nbuf1[NUMBUFLEN];
15404 char_u nbuf2[NUMBUFLEN];
15405 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015406 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015407 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015408 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015411 spat = get_tv_string_chk(&argvars[0]);
15412 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15413 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15414 if (spat == NULL || mpat == NULL || epat == NULL)
15415 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416
Bram Moolenaar071d4272004-06-13 20:20:40 +000015417 /* Handle the optional fourth argument: flags */
15418 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015419 if (dir == 0)
15420 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015421
15422 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015423 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15424 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015425 if ((flags & (SP_END | SP_SUBPAT)) != 0
15426 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015427 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015428 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015429 goto theend;
15430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015432 /* Using 'r' implies 'W', otherwise it doesn't work. */
15433 if (flags & SP_REPEAT)
15434 p_ws = FALSE;
15435
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015436 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015437 if (argvars[3].v_type == VAR_UNKNOWN
15438 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439 skip = (char_u *)"";
15440 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015442 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015443 if (argvars[5].v_type != VAR_UNKNOWN)
15444 {
15445 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15446 if (lnum_stop < 0)
15447 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015448#ifdef FEAT_RELTIME
15449 if (argvars[6].v_type != VAR_UNKNOWN)
15450 {
15451 time_limit = get_tv_number_chk(&argvars[6], NULL);
15452 if (time_limit < 0)
15453 goto theend;
15454 }
15455#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015456 }
15457 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015458 if (skip == NULL)
15459 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015461 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015462 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015463
15464theend:
15465 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015466
15467 return retval;
15468}
15469
15470/*
15471 * "searchpair()" function
15472 */
15473 static void
15474f_searchpair(argvars, rettv)
15475 typval_T *argvars;
15476 typval_T *rettv;
15477{
15478 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15479}
15480
15481/*
15482 * "searchpairpos()" function
15483 */
15484 static void
15485f_searchpairpos(argvars, rettv)
15486 typval_T *argvars;
15487 typval_T *rettv;
15488{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015489 pos_T match_pos;
15490 int lnum = 0;
15491 int col = 0;
15492
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015493 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015494 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015495
15496 if (searchpair_cmn(argvars, &match_pos) > 0)
15497 {
15498 lnum = match_pos.lnum;
15499 col = match_pos.col;
15500 }
15501
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015502 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15503 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015504}
15505
15506/*
15507 * Search for a start/middle/end thing.
15508 * Used by searchpair(), see its documentation for the details.
15509 * Returns 0 or -1 for no match,
15510 */
15511 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015512do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15513 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015514 char_u *spat; /* start pattern */
15515 char_u *mpat; /* middle pattern */
15516 char_u *epat; /* end pattern */
15517 int dir; /* BACKWARD or FORWARD */
15518 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015519 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015520 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015521 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015522 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015523{
15524 char_u *save_cpo;
15525 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15526 long retval = 0;
15527 pos_T pos;
15528 pos_T firstpos;
15529 pos_T foundpos;
15530 pos_T save_cursor;
15531 pos_T save_pos;
15532 int n;
15533 int r;
15534 int nest = 1;
15535 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015536 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015537 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015538
15539 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15540 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015541 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015542
Bram Moolenaar76929292008-01-06 19:07:36 +000015543#ifdef FEAT_RELTIME
15544 /* Set the time limit, if there is one. */
15545 profile_setlimit(time_limit, &tm);
15546#endif
15547
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015548 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15549 * start/middle/end (pat3, for the top pair). */
15550 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15551 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15552 if (pat2 == NULL || pat3 == NULL)
15553 goto theend;
15554 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15555 if (*mpat == NUL)
15556 STRCPY(pat3, pat2);
15557 else
15558 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15559 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015560 if (flags & SP_START)
15561 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015562
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 save_cursor = curwin->w_cursor;
15564 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015565 clearpos(&firstpos);
15566 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567 pat = pat3;
15568 for (;;)
15569 {
15570 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015571 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15573 /* didn't find it or found the first match again: FAIL */
15574 break;
15575
15576 if (firstpos.lnum == 0)
15577 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015578 if (equalpos(pos, foundpos))
15579 {
15580 /* Found the same position again. Can happen with a pattern that
15581 * has "\zs" at the end and searching backwards. Advance one
15582 * character and try again. */
15583 if (dir == BACKWARD)
15584 decl(&pos);
15585 else
15586 incl(&pos);
15587 }
15588 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015590 /* clear the start flag to avoid getting stuck here */
15591 options &= ~SEARCH_START;
15592
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 /* If the skip pattern matches, ignore this match. */
15594 if (*skip != NUL)
15595 {
15596 save_pos = curwin->w_cursor;
15597 curwin->w_cursor = pos;
15598 r = eval_to_bool(skip, &err, NULL, FALSE);
15599 curwin->w_cursor = save_pos;
15600 if (err)
15601 {
15602 /* Evaluating {skip} caused an error, break here. */
15603 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015604 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605 break;
15606 }
15607 if (r)
15608 continue;
15609 }
15610
15611 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15612 {
15613 /* Found end when searching backwards or start when searching
15614 * forward: nested pair. */
15615 ++nest;
15616 pat = pat2; /* nested, don't search for middle */
15617 }
15618 else
15619 {
15620 /* Found end when searching forward or start when searching
15621 * backward: end of (nested) pair; or found middle in outer pair. */
15622 if (--nest == 1)
15623 pat = pat3; /* outer level, search for middle */
15624 }
15625
15626 if (nest == 0)
15627 {
15628 /* Found the match: return matchcount or line number. */
15629 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015630 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015631 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015632 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015633 if (flags & SP_SETPCMARK)
15634 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015635 curwin->w_cursor = pos;
15636 if (!(flags & SP_REPEAT))
15637 break;
15638 nest = 1; /* search for next unmatched */
15639 }
15640 }
15641
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015642 if (match_pos != NULL)
15643 {
15644 /* Store the match cursor position */
15645 match_pos->lnum = curwin->w_cursor.lnum;
15646 match_pos->col = curwin->w_cursor.col + 1;
15647 }
15648
Bram Moolenaar071d4272004-06-13 20:20:40 +000015649 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015650 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015651 curwin->w_cursor = save_cursor;
15652
15653theend:
15654 vim_free(pat2);
15655 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015656 if (p_cpo == empty_option)
15657 p_cpo = save_cpo;
15658 else
15659 /* Darn, evaluating the {skip} expression changed the value. */
15660 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015661
15662 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663}
15664
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015665/*
15666 * "searchpos()" function
15667 */
15668 static void
15669f_searchpos(argvars, rettv)
15670 typval_T *argvars;
15671 typval_T *rettv;
15672{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015673 pos_T match_pos;
15674 int lnum = 0;
15675 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015676 int n;
15677 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015678
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015679 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015680 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015681
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015682 n = search_cmn(argvars, &match_pos, &flags);
15683 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015684 {
15685 lnum = match_pos.lnum;
15686 col = match_pos.col;
15687 }
15688
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015689 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15690 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015691 if (flags & SP_SUBPAT)
15692 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015693}
15694
15695
Bram Moolenaar0d660222005-01-07 21:51:51 +000015696 static void
15697f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015698 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015701#ifdef FEAT_CLIENTSERVER
15702 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015703 char_u *server = get_tv_string_chk(&argvars[0]);
15704 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705
Bram Moolenaar0d660222005-01-07 21:51:51 +000015706 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015707 if (server == NULL || reply == NULL)
15708 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015709 if (check_restricted() || check_secure())
15710 return;
15711# ifdef FEAT_X11
15712 if (check_connection() == FAIL)
15713 return;
15714# endif
15715
15716 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015717 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015718 EMSG(_("E258: Unable to send to client"));
15719 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015721 rettv->vval.v_number = 0;
15722#else
15723 rettv->vval.v_number = -1;
15724#endif
15725}
15726
Bram Moolenaar0d660222005-01-07 21:51:51 +000015727 static void
15728f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015729 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015730 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015731{
15732 char_u *r = NULL;
15733
15734#ifdef FEAT_CLIENTSERVER
15735# ifdef WIN32
15736 r = serverGetVimNames();
15737# else
15738 make_connection();
15739 if (X_DISPLAY != NULL)
15740 r = serverGetVimNames(X_DISPLAY);
15741# endif
15742#endif
15743 rettv->v_type = VAR_STRING;
15744 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745}
15746
15747/*
15748 * "setbufvar()" function
15749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015751f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015752 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015753 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754{
15755 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015758 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759 char_u nbuf[NUMBUFLEN];
15760
15761 if (check_restricted() || check_secure())
15762 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015763 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15764 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015765 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015766 varp = &argvars[2];
15767
15768 if (buf != NULL && varname != NULL && varp != NULL)
15769 {
15770 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772
15773 if (*varname == '&')
15774 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015775 long numval;
15776 char_u *strval;
15777 int error = FALSE;
15778
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015780 numval = get_tv_number_chk(varp, &error);
15781 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015782 if (!error && strval != NULL)
15783 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015784 }
15785 else
15786 {
15787 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15788 if (bufvarname != NULL)
15789 {
15790 STRCPY(bufvarname, "b:");
15791 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015792 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015793 vim_free(bufvarname);
15794 }
15795 }
15796
15797 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015798 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015800}
15801
15802/*
15803 * "setcmdpos()" function
15804 */
15805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015806f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015807 typval_T *argvars;
15808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015810 int pos = (int)get_tv_number(&argvars[0]) - 1;
15811
15812 if (pos >= 0)
15813 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814}
15815
15816/*
15817 * "setline()" function
15818 */
15819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015820f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015821 typval_T *argvars;
15822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823{
15824 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015825 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015826 list_T *l = NULL;
15827 listitem_T *li = NULL;
15828 long added = 0;
15829 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015830
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015831 lnum = get_tv_lnum(&argvars[0]);
15832 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015834 l = argvars[1].vval.v_list;
15835 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015836 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015837 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015838 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015839
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015840 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015841 for (;;)
15842 {
15843 if (l != NULL)
15844 {
15845 /* list argument, get next string */
15846 if (li == NULL)
15847 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015848 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015849 li = li->li_next;
15850 }
15851
15852 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015853 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015854 break;
15855 if (lnum <= curbuf->b_ml.ml_line_count)
15856 {
15857 /* existing line, replace it */
15858 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15859 {
15860 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015861 if (lnum == curwin->w_cursor.lnum)
15862 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015863 rettv->vval.v_number = 0; /* OK */
15864 }
15865 }
15866 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15867 {
15868 /* lnum is one past the last line, append the line */
15869 ++added;
15870 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15871 rettv->vval.v_number = 0; /* OK */
15872 }
15873
15874 if (l == NULL) /* only one string argument */
15875 break;
15876 ++lnum;
15877 }
15878
15879 if (added > 0)
15880 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015881}
15882
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015883static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15884
Bram Moolenaar071d4272004-06-13 20:20:40 +000015885/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015886 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015887 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015888 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015889set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015890 win_T *wp UNUSED;
15891 typval_T *list_arg UNUSED;
15892 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015893 typval_T *rettv;
15894{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015895#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015896 char_u *act;
15897 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015898#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015899
Bram Moolenaar2641f772005-03-25 21:58:17 +000015900 rettv->vval.v_number = -1;
15901
15902#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015903 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015904 EMSG(_(e_listreq));
15905 else
15906 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015907 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015908
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015909 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015910 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015911 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015912 if (act == NULL)
15913 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015914 if (*act == 'a' || *act == 'r')
15915 action = *act;
15916 }
15917
Bram Moolenaarbc226b62010-08-09 22:14:48 +020015918 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015919 rettv->vval.v_number = 0;
15920 }
15921#endif
15922}
15923
15924/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015925 * "setloclist()" function
15926 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015927 static void
15928f_setloclist(argvars, rettv)
15929 typval_T *argvars;
15930 typval_T *rettv;
15931{
15932 win_T *win;
15933
15934 rettv->vval.v_number = -1;
15935
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015936 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015937 if (win != NULL)
15938 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15939}
15940
15941/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015942 * "setmatches()" function
15943 */
15944 static void
15945f_setmatches(argvars, rettv)
15946 typval_T *argvars;
15947 typval_T *rettv;
15948{
15949#ifdef FEAT_SEARCH_EXTRA
15950 list_T *l;
15951 listitem_T *li;
15952 dict_T *d;
15953
15954 rettv->vval.v_number = -1;
15955 if (argvars[0].v_type != VAR_LIST)
15956 {
15957 EMSG(_(e_listreq));
15958 return;
15959 }
15960 if ((l = argvars[0].vval.v_list) != NULL)
15961 {
15962
15963 /* To some extent make sure that we are dealing with a list from
15964 * "getmatches()". */
15965 li = l->lv_first;
15966 while (li != NULL)
15967 {
15968 if (li->li_tv.v_type != VAR_DICT
15969 || (d = li->li_tv.vval.v_dict) == NULL)
15970 {
15971 EMSG(_(e_invarg));
15972 return;
15973 }
15974 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15975 && dict_find(d, (char_u *)"pattern", -1) != NULL
15976 && dict_find(d, (char_u *)"priority", -1) != NULL
15977 && dict_find(d, (char_u *)"id", -1) != NULL))
15978 {
15979 EMSG(_(e_invarg));
15980 return;
15981 }
15982 li = li->li_next;
15983 }
15984
15985 clear_matches(curwin);
15986 li = l->lv_first;
15987 while (li != NULL)
15988 {
15989 d = li->li_tv.vval.v_dict;
15990 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15991 get_dict_string(d, (char_u *)"pattern", FALSE),
15992 (int)get_dict_number(d, (char_u *)"priority"),
15993 (int)get_dict_number(d, (char_u *)"id"));
15994 li = li->li_next;
15995 }
15996 rettv->vval.v_number = 0;
15997 }
15998#endif
15999}
16000
16001/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016002 * "setpos()" function
16003 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016004 static void
16005f_setpos(argvars, rettv)
16006 typval_T *argvars;
16007 typval_T *rettv;
16008{
16009 pos_T pos;
16010 int fnum;
16011 char_u *name;
16012
Bram Moolenaar08250432008-02-13 11:42:46 +000016013 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016014 name = get_tv_string_chk(argvars);
16015 if (name != NULL)
16016 {
16017 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16018 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016019 if (--pos.col < 0)
16020 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016021 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016022 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016023 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016024 if (fnum == curbuf->b_fnum)
16025 {
16026 curwin->w_cursor = pos;
16027 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016028 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016029 }
16030 else
16031 EMSG(_(e_invarg));
16032 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016033 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16034 {
16035 /* set mark */
16036 if (setmark_pos(name[1], &pos, fnum) == OK)
16037 rettv->vval.v_number = 0;
16038 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016039 else
16040 EMSG(_(e_invarg));
16041 }
16042 }
16043}
16044
16045/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016046 * "setqflist()" function
16047 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016048 static void
16049f_setqflist(argvars, rettv)
16050 typval_T *argvars;
16051 typval_T *rettv;
16052{
16053 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16054}
16055
16056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057 * "setreg()" function
16058 */
16059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016060f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016061 typval_T *argvars;
16062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016063{
16064 int regname;
16065 char_u *strregname;
16066 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016067 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016068 int append;
16069 char_u yank_type;
16070 long block_len;
16071
16072 block_len = -1;
16073 yank_type = MAUTO;
16074 append = FALSE;
16075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016076 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016077 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016078
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016079 if (strregname == NULL)
16080 return; /* type error; errmsg already given */
16081 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082 if (regname == 0 || regname == '@')
16083 regname = '"';
16084 else if (regname == '=')
16085 return;
16086
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016087 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016089 stropt = get_tv_string_chk(&argvars[2]);
16090 if (stropt == NULL)
16091 return; /* type error */
16092 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093 switch (*stropt)
16094 {
16095 case 'a': case 'A': /* append */
16096 append = TRUE;
16097 break;
16098 case 'v': case 'c': /* character-wise selection */
16099 yank_type = MCHAR;
16100 break;
16101 case 'V': case 'l': /* line-wise selection */
16102 yank_type = MLINE;
16103 break;
16104#ifdef FEAT_VISUAL
16105 case 'b': case Ctrl_V: /* block-wise selection */
16106 yank_type = MBLOCK;
16107 if (VIM_ISDIGIT(stropt[1]))
16108 {
16109 ++stropt;
16110 block_len = getdigits(&stropt) - 1;
16111 --stropt;
16112 }
16113 break;
16114#endif
16115 }
16116 }
16117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016118 strval = get_tv_string_chk(&argvars[1]);
16119 if (strval != NULL)
16120 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016121 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016122 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123}
16124
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016125/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016126 * "settabvar()" function
16127 */
16128 static void
16129f_settabvar(argvars, rettv)
16130 typval_T *argvars;
16131 typval_T *rettv;
16132{
16133 tabpage_T *save_curtab;
16134 char_u *varname, *tabvarname;
16135 typval_T *varp;
16136 tabpage_T *tp;
16137
16138 rettv->vval.v_number = 0;
16139
16140 if (check_restricted() || check_secure())
16141 return;
16142
16143 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16144 varname = get_tv_string_chk(&argvars[1]);
16145 varp = &argvars[2];
16146
16147 if (tp != NULL && varname != NULL && varp != NULL)
16148 {
16149 save_curtab = curtab;
16150 goto_tabpage_tp(tp);
16151
16152 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16153 if (tabvarname != NULL)
16154 {
16155 STRCPY(tabvarname, "t:");
16156 STRCPY(tabvarname + 2, varname);
16157 set_var(tabvarname, varp, TRUE);
16158 vim_free(tabvarname);
16159 }
16160
16161 /* Restore current tabpage */
16162 if (valid_tabpage(save_curtab))
16163 goto_tabpage_tp(save_curtab);
16164 }
16165}
16166
16167/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016168 * "settabwinvar()" function
16169 */
16170 static void
16171f_settabwinvar(argvars, rettv)
16172 typval_T *argvars;
16173 typval_T *rettv;
16174{
16175 setwinvar(argvars, rettv, 1);
16176}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016177
16178/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016179 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016182f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016183 typval_T *argvars;
16184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016186 setwinvar(argvars, rettv, 0);
16187}
16188
16189/*
16190 * "setwinvar()" and "settabwinvar()" functions
16191 */
16192 static void
16193setwinvar(argvars, rettv, off)
16194 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016195 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016196 int off;
16197{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016198 win_T *win;
16199#ifdef FEAT_WINDOWS
16200 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016201 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202#endif
16203 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016204 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016206 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207
16208 if (check_restricted() || check_secure())
16209 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016210
16211#ifdef FEAT_WINDOWS
16212 if (off == 1)
16213 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16214 else
16215 tp = curtab;
16216#endif
16217 win = find_win_by_nr(&argvars[off], tp);
16218 varname = get_tv_string_chk(&argvars[off + 1]);
16219 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220
16221 if (win != NULL && varname != NULL && varp != NULL)
16222 {
16223#ifdef FEAT_WINDOWS
16224 /* set curwin to be our win, temporarily */
16225 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016226 save_curtab = curtab;
16227 goto_tabpage_tp(tp);
16228 if (!win_valid(win))
16229 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230 curwin = win;
16231 curbuf = curwin->w_buffer;
16232#endif
16233
16234 if (*varname == '&')
16235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016236 long numval;
16237 char_u *strval;
16238 int error = FALSE;
16239
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016241 numval = get_tv_number_chk(varp, &error);
16242 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016243 if (!error && strval != NULL)
16244 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016245 }
16246 else
16247 {
16248 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16249 if (winvarname != NULL)
16250 {
16251 STRCPY(winvarname, "w:");
16252 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016253 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254 vim_free(winvarname);
16255 }
16256 }
16257
16258#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016259 /* Restore current tabpage and window, if still valid (autocomands can
16260 * make them invalid). */
16261 if (valid_tabpage(save_curtab))
16262 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263 if (win_valid(save_curwin))
16264 {
16265 curwin = save_curwin;
16266 curbuf = curwin->w_buffer;
16267 }
16268#endif
16269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270}
16271
16272/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016273 * "shellescape({string})" function
16274 */
16275 static void
16276f_shellescape(argvars, rettv)
16277 typval_T *argvars;
16278 typval_T *rettv;
16279{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016280 rettv->vval.v_string = vim_strsave_shellescape(
16281 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016282 rettv->v_type = VAR_STRING;
16283}
16284
16285/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016286 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287 */
16288 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016289f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016290 typval_T *argvars;
16291 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016293 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294
Bram Moolenaar0d660222005-01-07 21:51:51 +000016295 p = get_tv_string(&argvars[0]);
16296 rettv->vval.v_string = vim_strsave(p);
16297 simplify_filename(rettv->vval.v_string); /* simplify in place */
16298 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299}
16300
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016301#ifdef FEAT_FLOAT
16302/*
16303 * "sin()" function
16304 */
16305 static void
16306f_sin(argvars, rettv)
16307 typval_T *argvars;
16308 typval_T *rettv;
16309{
16310 float_T f;
16311
16312 rettv->v_type = VAR_FLOAT;
16313 if (get_float_arg(argvars, &f) == OK)
16314 rettv->vval.v_float = sin(f);
16315 else
16316 rettv->vval.v_float = 0.0;
16317}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016318
16319/*
16320 * "sinh()" function
16321 */
16322 static void
16323f_sinh(argvars, rettv)
16324 typval_T *argvars;
16325 typval_T *rettv;
16326{
16327 float_T f;
16328
16329 rettv->v_type = VAR_FLOAT;
16330 if (get_float_arg(argvars, &f) == OK)
16331 rettv->vval.v_float = sinh(f);
16332 else
16333 rettv->vval.v_float = 0.0;
16334}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016335#endif
16336
Bram Moolenaar0d660222005-01-07 21:51:51 +000016337static int
16338#ifdef __BORLANDC__
16339 _RTLENTRYF
16340#endif
16341 item_compare __ARGS((const void *s1, const void *s2));
16342static int
16343#ifdef __BORLANDC__
16344 _RTLENTRYF
16345#endif
16346 item_compare2 __ARGS((const void *s1, const void *s2));
16347
16348static int item_compare_ic;
16349static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016350static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016351#define ITEM_COMPARE_FAIL 999
16352
Bram Moolenaar071d4272004-06-13 20:20:40 +000016353/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016354 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016355 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016356 static int
16357#ifdef __BORLANDC__
16358_RTLENTRYF
16359#endif
16360item_compare(s1, s2)
16361 const void *s1;
16362 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016363{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016364 char_u *p1, *p2;
16365 char_u *tofree1, *tofree2;
16366 int res;
16367 char_u numbuf1[NUMBUFLEN];
16368 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016369
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016370 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16371 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016372 if (p1 == NULL)
16373 p1 = (char_u *)"";
16374 if (p2 == NULL)
16375 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016376 if (item_compare_ic)
16377 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016379 res = STRCMP(p1, p2);
16380 vim_free(tofree1);
16381 vim_free(tofree2);
16382 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383}
16384
16385 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016386#ifdef __BORLANDC__
16387_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016389item_compare2(s1, s2)
16390 const void *s1;
16391 const void *s2;
16392{
16393 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016394 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016395 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016396 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016397
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016398 /* shortcut after failure in previous call; compare all items equal */
16399 if (item_compare_func_err)
16400 return 0;
16401
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016402 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16403 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016404 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16405 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016406
16407 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016408 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016409 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016410 clear_tv(&argv[0]);
16411 clear_tv(&argv[1]);
16412
16413 if (res == FAIL)
16414 res = ITEM_COMPARE_FAIL;
16415 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016416 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16417 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016418 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016419 clear_tv(&rettv);
16420 return res;
16421}
16422
16423/*
16424 * "sort({list})" function
16425 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016426 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016427f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016428 typval_T *argvars;
16429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016430{
Bram Moolenaar33570922005-01-25 22:26:29 +000016431 list_T *l;
16432 listitem_T *li;
16433 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016434 long len;
16435 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436
Bram Moolenaar0d660222005-01-07 21:51:51 +000016437 if (argvars[0].v_type != VAR_LIST)
16438 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016439 else
16440 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016441 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016442 if (l == NULL || tv_check_lock(l->lv_lock,
16443 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016444 return;
16445 rettv->vval.v_list = l;
16446 rettv->v_type = VAR_LIST;
16447 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448
Bram Moolenaar0d660222005-01-07 21:51:51 +000016449 len = list_len(l);
16450 if (len <= 1)
16451 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016452
Bram Moolenaar0d660222005-01-07 21:51:51 +000016453 item_compare_ic = FALSE;
16454 item_compare_func = NULL;
16455 if (argvars[1].v_type != VAR_UNKNOWN)
16456 {
16457 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016458 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016459 else
16460 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016461 int error = FALSE;
16462
16463 i = get_tv_number_chk(&argvars[1], &error);
16464 if (error)
16465 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016466 if (i == 1)
16467 item_compare_ic = TRUE;
16468 else
16469 item_compare_func = get_tv_string(&argvars[1]);
16470 }
16471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016472
Bram Moolenaar0d660222005-01-07 21:51:51 +000016473 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016474 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016475 if (ptrs == NULL)
16476 return;
16477 i = 0;
16478 for (li = l->lv_first; li != NULL; li = li->li_next)
16479 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016481 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016482 /* test the compare function */
16483 if (item_compare_func != NULL
16484 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16485 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016486 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016487 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016488 {
16489 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016490 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016491 item_compare_func == NULL ? item_compare : item_compare2);
16492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016493 if (!item_compare_func_err)
16494 {
16495 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016496 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016497 l->lv_len = 0;
16498 for (i = 0; i < len; ++i)
16499 list_append(l, ptrs[i]);
16500 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016501 }
16502
16503 vim_free(ptrs);
16504 }
16505}
16506
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016507/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016508 * "soundfold({word})" function
16509 */
16510 static void
16511f_soundfold(argvars, rettv)
16512 typval_T *argvars;
16513 typval_T *rettv;
16514{
16515 char_u *s;
16516
16517 rettv->v_type = VAR_STRING;
16518 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016519#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016520 rettv->vval.v_string = eval_soundfold(s);
16521#else
16522 rettv->vval.v_string = vim_strsave(s);
16523#endif
16524}
16525
16526/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016527 * "spellbadword()" function
16528 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016529 static void
16530f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016531 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016532 typval_T *rettv;
16533{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016534 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016535 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016536 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016537
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016538 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016539 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016540
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016541#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016542 if (argvars[0].v_type == VAR_UNKNOWN)
16543 {
16544 /* Find the start and length of the badly spelled word. */
16545 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16546 if (len != 0)
16547 word = ml_get_cursor();
16548 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016549 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016550 {
16551 char_u *str = get_tv_string_chk(&argvars[0]);
16552 int capcol = -1;
16553
16554 if (str != NULL)
16555 {
16556 /* Check the argument for spelling. */
16557 while (*str != NUL)
16558 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016559 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016560 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016561 {
16562 word = str;
16563 break;
16564 }
16565 str += len;
16566 }
16567 }
16568 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016569#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016570
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016571 list_append_string(rettv->vval.v_list, word, len);
16572 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016573 attr == HLF_SPB ? "bad" :
16574 attr == HLF_SPR ? "rare" :
16575 attr == HLF_SPL ? "local" :
16576 attr == HLF_SPC ? "caps" :
16577 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016578}
16579
16580/*
16581 * "spellsuggest()" function
16582 */
16583 static void
16584f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016585 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016586 typval_T *rettv;
16587{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016588#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016589 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016590 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016591 int maxcount;
16592 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016593 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016594 listitem_T *li;
16595 int need_capital = FALSE;
16596#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016597
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016598 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016599 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016600
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016601#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016602 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016603 {
16604 str = get_tv_string(&argvars[0]);
16605 if (argvars[1].v_type != VAR_UNKNOWN)
16606 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016607 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016608 if (maxcount <= 0)
16609 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016610 if (argvars[2].v_type != VAR_UNKNOWN)
16611 {
16612 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16613 if (typeerr)
16614 return;
16615 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016616 }
16617 else
16618 maxcount = 25;
16619
Bram Moolenaar4770d092006-01-12 23:22:24 +000016620 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016621
16622 for (i = 0; i < ga.ga_len; ++i)
16623 {
16624 str = ((char_u **)ga.ga_data)[i];
16625
16626 li = listitem_alloc();
16627 if (li == NULL)
16628 vim_free(str);
16629 else
16630 {
16631 li->li_tv.v_type = VAR_STRING;
16632 li->li_tv.v_lock = 0;
16633 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016634 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016635 }
16636 }
16637 ga_clear(&ga);
16638 }
16639#endif
16640}
16641
Bram Moolenaar0d660222005-01-07 21:51:51 +000016642 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016643f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016644 typval_T *argvars;
16645 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016646{
16647 char_u *str;
16648 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016649 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016650 regmatch_T regmatch;
16651 char_u patbuf[NUMBUFLEN];
16652 char_u *save_cpo;
16653 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016654 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016655 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016656 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016657
16658 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16659 save_cpo = p_cpo;
16660 p_cpo = (char_u *)"";
16661
16662 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016663 if (argvars[1].v_type != VAR_UNKNOWN)
16664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016665 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16666 if (pat == NULL)
16667 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016668 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016669 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016670 }
16671 if (pat == NULL || *pat == NUL)
16672 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016673
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016674 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016675 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016676 if (typeerr)
16677 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016678
Bram Moolenaar0d660222005-01-07 21:51:51 +000016679 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16680 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016681 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016682 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016683 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016684 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016685 if (*str == NUL)
16686 match = FALSE; /* empty item at the end */
16687 else
16688 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016689 if (match)
16690 end = regmatch.startp[0];
16691 else
16692 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016693 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16694 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016695 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016696 if (list_append_string(rettv->vval.v_list, str,
16697 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016698 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016699 }
16700 if (!match)
16701 break;
16702 /* Advance to just after the match. */
16703 if (regmatch.endp[0] > str)
16704 col = 0;
16705 else
16706 {
16707 /* Don't get stuck at the same match. */
16708#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016709 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016710#else
16711 col = 1;
16712#endif
16713 }
16714 str = regmatch.endp[0];
16715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716
Bram Moolenaar0d660222005-01-07 21:51:51 +000016717 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719
Bram Moolenaar0d660222005-01-07 21:51:51 +000016720 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721}
16722
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016723#ifdef FEAT_FLOAT
16724/*
16725 * "sqrt()" function
16726 */
16727 static void
16728f_sqrt(argvars, rettv)
16729 typval_T *argvars;
16730 typval_T *rettv;
16731{
16732 float_T f;
16733
16734 rettv->v_type = VAR_FLOAT;
16735 if (get_float_arg(argvars, &f) == OK)
16736 rettv->vval.v_float = sqrt(f);
16737 else
16738 rettv->vval.v_float = 0.0;
16739}
16740
16741/*
16742 * "str2float()" function
16743 */
16744 static void
16745f_str2float(argvars, rettv)
16746 typval_T *argvars;
16747 typval_T *rettv;
16748{
16749 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16750
16751 if (*p == '+')
16752 p = skipwhite(p + 1);
16753 (void)string2float(p, &rettv->vval.v_float);
16754 rettv->v_type = VAR_FLOAT;
16755}
16756#endif
16757
Bram Moolenaar2c932302006-03-18 21:42:09 +000016758/*
16759 * "str2nr()" function
16760 */
16761 static void
16762f_str2nr(argvars, rettv)
16763 typval_T *argvars;
16764 typval_T *rettv;
16765{
16766 int base = 10;
16767 char_u *p;
16768 long n;
16769
16770 if (argvars[1].v_type != VAR_UNKNOWN)
16771 {
16772 base = get_tv_number(&argvars[1]);
16773 if (base != 8 && base != 10 && base != 16)
16774 {
16775 EMSG(_(e_invarg));
16776 return;
16777 }
16778 }
16779
16780 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016781 if (*p == '+')
16782 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016783 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16784 rettv->vval.v_number = n;
16785}
16786
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787#ifdef HAVE_STRFTIME
16788/*
16789 * "strftime({format}[, {time}])" function
16790 */
16791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016792f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016793 typval_T *argvars;
16794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795{
16796 char_u result_buf[256];
16797 struct tm *curtime;
16798 time_t seconds;
16799 char_u *p;
16800
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016801 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016803 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016804 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016805 seconds = time(NULL);
16806 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016807 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016808 curtime = localtime(&seconds);
16809 /* MSVC returns NULL for an invalid value of seconds. */
16810 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016811 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812 else
16813 {
16814# ifdef FEAT_MBYTE
16815 vimconv_T conv;
16816 char_u *enc;
16817
16818 conv.vc_type = CONV_NONE;
16819 enc = enc_locale();
16820 convert_setup(&conv, p_enc, enc);
16821 if (conv.vc_type != CONV_NONE)
16822 p = string_convert(&conv, p, NULL);
16823# endif
16824 if (p != NULL)
16825 (void)strftime((char *)result_buf, sizeof(result_buf),
16826 (char *)p, curtime);
16827 else
16828 result_buf[0] = NUL;
16829
16830# ifdef FEAT_MBYTE
16831 if (conv.vc_type != CONV_NONE)
16832 vim_free(p);
16833 convert_setup(&conv, enc, p_enc);
16834 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016835 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836 else
16837# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016838 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016839
16840# ifdef FEAT_MBYTE
16841 /* Release conversion descriptors */
16842 convert_setup(&conv, NULL, NULL);
16843 vim_free(enc);
16844# endif
16845 }
16846}
16847#endif
16848
16849/*
16850 * "stridx()" function
16851 */
16852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016853f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016854 typval_T *argvars;
16855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856{
16857 char_u buf[NUMBUFLEN];
16858 char_u *needle;
16859 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016860 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016861 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016862 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016864 needle = get_tv_string_chk(&argvars[1]);
16865 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016866 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016867 if (needle == NULL || haystack == NULL)
16868 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016869
Bram Moolenaar33570922005-01-25 22:26:29 +000016870 if (argvars[2].v_type != VAR_UNKNOWN)
16871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016872 int error = FALSE;
16873
16874 start_idx = get_tv_number_chk(&argvars[2], &error);
16875 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016876 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016877 if (start_idx >= 0)
16878 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016879 }
16880
16881 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16882 if (pos != NULL)
16883 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884}
16885
16886/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016887 * "string()" function
16888 */
16889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016890f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016891 typval_T *argvars;
16892 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016893{
16894 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016895 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016896
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016897 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016898 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016899 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016900 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016901 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902}
16903
16904/*
16905 * "strlen()" function
16906 */
16907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016908f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016909 typval_T *argvars;
16910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016911{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016912 rettv->vval.v_number = (varnumber_T)(STRLEN(
16913 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016914}
16915
16916/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016917 * "strchars()" function
16918 */
16919 static void
16920f_strchars(argvars, rettv)
16921 typval_T *argvars;
16922 typval_T *rettv;
16923{
16924 char_u *s = get_tv_string(&argvars[0]);
16925#ifdef FEAT_MBYTE
16926 varnumber_T len = 0;
16927
16928 while (*s != NUL)
16929 {
16930 mb_cptr2char_adv(&s);
16931 ++len;
16932 }
16933 rettv->vval.v_number = len;
16934#else
16935 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16936#endif
16937}
16938
16939/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016940 * "strdisplaywidth()" function
16941 */
16942 static void
16943f_strdisplaywidth(argvars, rettv)
16944 typval_T *argvars;
16945 typval_T *rettv;
16946{
16947 char_u *s = get_tv_string(&argvars[0]);
16948 int col = 0;
16949
16950 if (argvars[1].v_type != VAR_UNKNOWN)
16951 col = get_tv_number(&argvars[1]);
16952
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016953 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016954}
16955
16956/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016957 * "strwidth()" function
16958 */
16959 static void
16960f_strwidth(argvars, rettv)
16961 typval_T *argvars;
16962 typval_T *rettv;
16963{
16964 char_u *s = get_tv_string(&argvars[0]);
16965
16966 rettv->vval.v_number = (varnumber_T)(
16967#ifdef FEAT_MBYTE
16968 mb_string2cells(s, -1)
16969#else
16970 STRLEN(s)
16971#endif
16972 );
16973}
16974
16975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976 * "strpart()" function
16977 */
16978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016979f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016980 typval_T *argvars;
16981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982{
16983 char_u *p;
16984 int n;
16985 int len;
16986 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016987 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016989 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990 slen = (int)STRLEN(p);
16991
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016992 n = get_tv_number_chk(&argvars[1], &error);
16993 if (error)
16994 len = 0;
16995 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016996 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997 else
16998 len = slen - n; /* default len: all bytes that are available. */
16999
17000 /*
17001 * Only return the overlap between the specified part and the actual
17002 * string.
17003 */
17004 if (n < 0)
17005 {
17006 len += n;
17007 n = 0;
17008 }
17009 else if (n > slen)
17010 n = slen;
17011 if (len < 0)
17012 len = 0;
17013 else if (n + len > slen)
17014 len = slen - n;
17015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017016 rettv->v_type = VAR_STRING;
17017 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018}
17019
17020/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017021 * "strridx()" function
17022 */
17023 static void
17024f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017025 typval_T *argvars;
17026 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017027{
17028 char_u buf[NUMBUFLEN];
17029 char_u *needle;
17030 char_u *haystack;
17031 char_u *rest;
17032 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017033 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017034
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017035 needle = get_tv_string_chk(&argvars[1]);
17036 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017037
17038 rettv->vval.v_number = -1;
17039 if (needle == NULL || haystack == NULL)
17040 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017041
17042 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017043 if (argvars[2].v_type != VAR_UNKNOWN)
17044 {
17045 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017046 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017047 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017048 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017049 }
17050 else
17051 end_idx = haystack_len;
17052
Bram Moolenaar0d660222005-01-07 21:51:51 +000017053 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017054 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017055 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017056 lastmatch = haystack + end_idx;
17057 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017058 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017059 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017060 for (rest = haystack; *rest != '\0'; ++rest)
17061 {
17062 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017063 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017064 break;
17065 lastmatch = rest;
17066 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017067 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017068
17069 if (lastmatch == NULL)
17070 rettv->vval.v_number = -1;
17071 else
17072 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17073}
17074
17075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017076 * "strtrans()" function
17077 */
17078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017079f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017080 typval_T *argvars;
17081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017083 rettv->v_type = VAR_STRING;
17084 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085}
17086
17087/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017088 * "submatch()" function
17089 */
17090 static void
17091f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017092 typval_T *argvars;
17093 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017094{
17095 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017096 rettv->vval.v_string =
17097 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017098}
17099
17100/*
17101 * "substitute()" function
17102 */
17103 static void
17104f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017105 typval_T *argvars;
17106 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017107{
17108 char_u patbuf[NUMBUFLEN];
17109 char_u subbuf[NUMBUFLEN];
17110 char_u flagsbuf[NUMBUFLEN];
17111
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017112 char_u *str = get_tv_string_chk(&argvars[0]);
17113 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17114 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17115 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17116
Bram Moolenaar0d660222005-01-07 21:51:51 +000017117 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017118 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17119 rettv->vval.v_string = NULL;
17120 else
17121 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017122}
17123
17124/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017125 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017128f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017129 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017131{
17132 int id = 0;
17133#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017134 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017135 long col;
17136 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017137 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017138
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017139 lnum = get_tv_lnum(argvars); /* -1 on type error */
17140 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17141 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017143 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017144 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017145 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146#endif
17147
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017148 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017149}
17150
17151/*
17152 * "synIDattr(id, what [, mode])" function
17153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017155f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017156 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158{
17159 char_u *p = NULL;
17160#ifdef FEAT_SYN_HL
17161 int id;
17162 char_u *what;
17163 char_u *mode;
17164 char_u modebuf[NUMBUFLEN];
17165 int modec;
17166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017167 id = get_tv_number(&argvars[0]);
17168 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017169 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017171 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017173 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174 modec = 0; /* replace invalid with current */
17175 }
17176 else
17177 {
17178#ifdef FEAT_GUI
17179 if (gui.in_use)
17180 modec = 'g';
17181 else
17182#endif
17183 if (t_colors > 1)
17184 modec = 'c';
17185 else
17186 modec = 't';
17187 }
17188
17189
17190 switch (TOLOWER_ASC(what[0]))
17191 {
17192 case 'b':
17193 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17194 p = highlight_color(id, what, modec);
17195 else /* bold */
17196 p = highlight_has_attr(id, HL_BOLD, modec);
17197 break;
17198
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017199 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200 p = highlight_color(id, what, modec);
17201 break;
17202
17203 case 'i':
17204 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17205 p = highlight_has_attr(id, HL_INVERSE, modec);
17206 else /* italic */
17207 p = highlight_has_attr(id, HL_ITALIC, modec);
17208 break;
17209
17210 case 'n': /* name */
17211 p = get_highlight_name(NULL, id - 1);
17212 break;
17213
17214 case 'r': /* reverse */
17215 p = highlight_has_attr(id, HL_INVERSE, modec);
17216 break;
17217
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017218 case 's':
17219 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17220 p = highlight_color(id, what, modec);
17221 else /* standout */
17222 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223 break;
17224
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017225 case 'u':
17226 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17227 /* underline */
17228 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17229 else
17230 /* undercurl */
17231 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232 break;
17233 }
17234
17235 if (p != NULL)
17236 p = vim_strsave(p);
17237#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017238 rettv->v_type = VAR_STRING;
17239 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240}
17241
17242/*
17243 * "synIDtrans(id)" function
17244 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017246f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017247 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249{
17250 int id;
17251
17252#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017253 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017254
17255 if (id > 0)
17256 id = syn_get_final_id(id);
17257 else
17258#endif
17259 id = 0;
17260
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017261 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262}
17263
17264/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017265 * "synconcealed(lnum, col)" function
17266 */
17267 static void
17268f_synconcealed(argvars, rettv)
17269 typval_T *argvars UNUSED;
17270 typval_T *rettv;
17271{
17272#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17273 long lnum;
17274 long col;
17275 int syntax_flags = 0;
17276 int cchar;
17277 int matchid = 0;
17278 char_u str[NUMBUFLEN];
17279#endif
17280
17281 rettv->v_type = VAR_LIST;
17282 rettv->vval.v_list = NULL;
17283
17284#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17285 lnum = get_tv_lnum(argvars); /* -1 on type error */
17286 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17287
17288 vim_memset(str, NUL, sizeof(str));
17289
17290 if (rettv_list_alloc(rettv) != FAIL)
17291 {
17292 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17293 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17294 && curwin->w_p_cole > 0)
17295 {
17296 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17297 syntax_flags = get_syntax_info(&matchid);
17298
17299 /* get the conceal character */
17300 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17301 {
17302 cchar = syn_get_sub_char();
17303 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17304 cchar = lcs_conceal;
17305 if (cchar != NUL)
17306 {
17307# ifdef FEAT_MBYTE
17308 if (has_mbyte)
17309 (*mb_char2bytes)(cchar, str);
17310 else
17311# endif
17312 str[0] = cchar;
17313 }
17314 }
17315 }
17316
17317 list_append_number(rettv->vval.v_list,
17318 (syntax_flags & HL_CONCEAL) != 0);
17319 /* -1 to auto-determine strlen */
17320 list_append_string(rettv->vval.v_list, str, -1);
17321 list_append_number(rettv->vval.v_list, matchid);
17322 }
17323#endif
17324}
17325
17326/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017327 * "synstack(lnum, col)" function
17328 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017329 static void
17330f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017331 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017332 typval_T *rettv;
17333{
17334#ifdef FEAT_SYN_HL
17335 long lnum;
17336 long col;
17337 int i;
17338 int id;
17339#endif
17340
17341 rettv->v_type = VAR_LIST;
17342 rettv->vval.v_list = NULL;
17343
17344#ifdef FEAT_SYN_HL
17345 lnum = get_tv_lnum(argvars); /* -1 on type error */
17346 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17347
17348 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017349 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017350 && rettv_list_alloc(rettv) != FAIL)
17351 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017352 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017353 for (i = 0; ; ++i)
17354 {
17355 id = syn_get_stack_item(i);
17356 if (id < 0)
17357 break;
17358 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17359 break;
17360 }
17361 }
17362#endif
17363}
17364
17365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366 * "system()" function
17367 */
17368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017369f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017370 typval_T *argvars;
17371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017373 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017375 char_u *infile = NULL;
17376 char_u buf[NUMBUFLEN];
17377 int err = FALSE;
17378 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017380 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017381 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017382
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017383 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017384 {
17385 /*
17386 * Write the string to a temp file, to be used for input of the shell
17387 * command.
17388 */
17389 if ((infile = vim_tempname('i')) == NULL)
17390 {
17391 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017392 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017393 }
17394
17395 fd = mch_fopen((char *)infile, WRITEBIN);
17396 if (fd == NULL)
17397 {
17398 EMSG2(_(e_notopen), infile);
17399 goto done;
17400 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017401 p = get_tv_string_buf_chk(&argvars[1], buf);
17402 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017403 {
17404 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017405 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017406 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017407 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17408 err = TRUE;
17409 if (fclose(fd) != 0)
17410 err = TRUE;
17411 if (err)
17412 {
17413 EMSG(_("E677: Error writing temp file"));
17414 goto done;
17415 }
17416 }
17417
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017418 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17419 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017420
Bram Moolenaar071d4272004-06-13 20:20:40 +000017421#ifdef USE_CR
17422 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017423 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424 {
17425 char_u *s;
17426
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017427 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428 {
17429 if (*s == CAR)
17430 *s = NL;
17431 }
17432 }
17433#else
17434# ifdef USE_CRNL
17435 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017436 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437 {
17438 char_u *s, *d;
17439
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017440 d = res;
17441 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017442 {
17443 if (s[0] == CAR && s[1] == NL)
17444 ++s;
17445 *d++ = *s;
17446 }
17447 *d = NUL;
17448 }
17449# endif
17450#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017451
17452done:
17453 if (infile != NULL)
17454 {
17455 mch_remove(infile);
17456 vim_free(infile);
17457 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017458 rettv->v_type = VAR_STRING;
17459 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460}
17461
17462/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017463 * "tabpagebuflist()" function
17464 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017465 static void
17466f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017467 typval_T *argvars UNUSED;
17468 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017469{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017470#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017471 tabpage_T *tp;
17472 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017473
17474 if (argvars[0].v_type == VAR_UNKNOWN)
17475 wp = firstwin;
17476 else
17477 {
17478 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17479 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017480 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017481 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017482 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017483 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017484 for (; wp != NULL; wp = wp->w_next)
17485 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017486 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017487 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017488 }
17489#endif
17490}
17491
17492
17493/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017494 * "tabpagenr()" function
17495 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017496 static void
17497f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017498 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017499 typval_T *rettv;
17500{
17501 int nr = 1;
17502#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017503 char_u *arg;
17504
17505 if (argvars[0].v_type != VAR_UNKNOWN)
17506 {
17507 arg = get_tv_string_chk(&argvars[0]);
17508 nr = 0;
17509 if (arg != NULL)
17510 {
17511 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017512 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017513 else
17514 EMSG2(_(e_invexpr2), arg);
17515 }
17516 }
17517 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017518 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017519#endif
17520 rettv->vval.v_number = nr;
17521}
17522
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017523
17524#ifdef FEAT_WINDOWS
17525static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17526
17527/*
17528 * Common code for tabpagewinnr() and winnr().
17529 */
17530 static int
17531get_winnr(tp, argvar)
17532 tabpage_T *tp;
17533 typval_T *argvar;
17534{
17535 win_T *twin;
17536 int nr = 1;
17537 win_T *wp;
17538 char_u *arg;
17539
17540 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17541 if (argvar->v_type != VAR_UNKNOWN)
17542 {
17543 arg = get_tv_string_chk(argvar);
17544 if (arg == NULL)
17545 nr = 0; /* type error; errmsg already given */
17546 else if (STRCMP(arg, "$") == 0)
17547 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17548 else if (STRCMP(arg, "#") == 0)
17549 {
17550 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17551 if (twin == NULL)
17552 nr = 0;
17553 }
17554 else
17555 {
17556 EMSG2(_(e_invexpr2), arg);
17557 nr = 0;
17558 }
17559 }
17560
17561 if (nr > 0)
17562 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17563 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017564 {
17565 if (wp == NULL)
17566 {
17567 /* didn't find it in this tabpage */
17568 nr = 0;
17569 break;
17570 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017571 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017572 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017573 return nr;
17574}
17575#endif
17576
17577/*
17578 * "tabpagewinnr()" function
17579 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017580 static void
17581f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017582 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017583 typval_T *rettv;
17584{
17585 int nr = 1;
17586#ifdef FEAT_WINDOWS
17587 tabpage_T *tp;
17588
17589 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17590 if (tp == NULL)
17591 nr = 0;
17592 else
17593 nr = get_winnr(tp, &argvars[1]);
17594#endif
17595 rettv->vval.v_number = nr;
17596}
17597
17598
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017599/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017600 * "tagfiles()" function
17601 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017602 static void
17603f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017604 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017605 typval_T *rettv;
17606{
17607 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017608 tagname_T tn;
17609 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017610
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017611 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017612 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017613
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017614 for (first = TRUE; ; first = FALSE)
17615 if (get_tagfname(&tn, first, fname) == FAIL
17616 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017617 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017618 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017619}
17620
17621/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017622 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017623 */
17624 static void
17625f_taglist(argvars, rettv)
17626 typval_T *argvars;
17627 typval_T *rettv;
17628{
17629 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017630
17631 tag_pattern = get_tv_string(&argvars[0]);
17632
17633 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017634 if (*tag_pattern == NUL)
17635 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017636
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017637 if (rettv_list_alloc(rettv) == OK)
17638 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017639}
17640
17641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642 * "tempname()" function
17643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017645f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017646 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648{
17649 static int x = 'A';
17650
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017651 rettv->v_type = VAR_STRING;
17652 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653
17654 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17655 * names. Skip 'I' and 'O', they are used for shell redirection. */
17656 do
17657 {
17658 if (x == 'Z')
17659 x = '0';
17660 else if (x == '9')
17661 x = 'A';
17662 else
17663 {
17664#ifdef EBCDIC
17665 if (x == 'I')
17666 x = 'J';
17667 else if (x == 'R')
17668 x = 'S';
17669 else
17670#endif
17671 ++x;
17672 }
17673 } while (x == 'I' || x == 'O');
17674}
17675
17676/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017677 * "test(list)" function: Just checking the walls...
17678 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017679 static void
17680f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017681 typval_T *argvars UNUSED;
17682 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017683{
17684 /* Used for unit testing. Change the code below to your liking. */
17685#if 0
17686 listitem_T *li;
17687 list_T *l;
17688 char_u *bad, *good;
17689
17690 if (argvars[0].v_type != VAR_LIST)
17691 return;
17692 l = argvars[0].vval.v_list;
17693 if (l == NULL)
17694 return;
17695 li = l->lv_first;
17696 if (li == NULL)
17697 return;
17698 bad = get_tv_string(&li->li_tv);
17699 li = li->li_next;
17700 if (li == NULL)
17701 return;
17702 good = get_tv_string(&li->li_tv);
17703 rettv->vval.v_number = test_edit_score(bad, good);
17704#endif
17705}
17706
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017707#ifdef FEAT_FLOAT
17708/*
17709 * "tan()" function
17710 */
17711 static void
17712f_tan(argvars, rettv)
17713 typval_T *argvars;
17714 typval_T *rettv;
17715{
17716 float_T f;
17717
17718 rettv->v_type = VAR_FLOAT;
17719 if (get_float_arg(argvars, &f) == OK)
17720 rettv->vval.v_float = tan(f);
17721 else
17722 rettv->vval.v_float = 0.0;
17723}
17724
17725/*
17726 * "tanh()" function
17727 */
17728 static void
17729f_tanh(argvars, rettv)
17730 typval_T *argvars;
17731 typval_T *rettv;
17732{
17733 float_T f;
17734
17735 rettv->v_type = VAR_FLOAT;
17736 if (get_float_arg(argvars, &f) == OK)
17737 rettv->vval.v_float = tanh(f);
17738 else
17739 rettv->vval.v_float = 0.0;
17740}
17741#endif
17742
Bram Moolenaard52d9742005-08-21 22:20:28 +000017743/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744 * "tolower(string)" function
17745 */
17746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017747f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017748 typval_T *argvars;
17749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750{
17751 char_u *p;
17752
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017753 p = vim_strsave(get_tv_string(&argvars[0]));
17754 rettv->v_type = VAR_STRING;
17755 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756
17757 if (p != NULL)
17758 while (*p != NUL)
17759 {
17760#ifdef FEAT_MBYTE
17761 int l;
17762
17763 if (enc_utf8)
17764 {
17765 int c, lc;
17766
17767 c = utf_ptr2char(p);
17768 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017769 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770 /* TODO: reallocate string when byte count changes. */
17771 if (utf_char2len(lc) == l)
17772 utf_char2bytes(lc, p);
17773 p += l;
17774 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017775 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776 p += l; /* skip multi-byte character */
17777 else
17778#endif
17779 {
17780 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17781 ++p;
17782 }
17783 }
17784}
17785
17786/*
17787 * "toupper(string)" function
17788 */
17789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017790f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017791 typval_T *argvars;
17792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017794 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017795 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796}
17797
17798/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017799 * "tr(string, fromstr, tostr)" function
17800 */
17801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017802f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017803 typval_T *argvars;
17804 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017805{
17806 char_u *instr;
17807 char_u *fromstr;
17808 char_u *tostr;
17809 char_u *p;
17810#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017811 int inlen;
17812 int fromlen;
17813 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017814 int idx;
17815 char_u *cpstr;
17816 int cplen;
17817 int first = TRUE;
17818#endif
17819 char_u buf[NUMBUFLEN];
17820 char_u buf2[NUMBUFLEN];
17821 garray_T ga;
17822
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017823 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017824 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17825 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017826
17827 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017828 rettv->v_type = VAR_STRING;
17829 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017830 if (fromstr == NULL || tostr == NULL)
17831 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017832 ga_init2(&ga, (int)sizeof(char), 80);
17833
17834#ifdef FEAT_MBYTE
17835 if (!has_mbyte)
17836#endif
17837 /* not multi-byte: fromstr and tostr must be the same length */
17838 if (STRLEN(fromstr) != STRLEN(tostr))
17839 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017840#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017841error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017842#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017843 EMSG2(_(e_invarg2), fromstr);
17844 ga_clear(&ga);
17845 return;
17846 }
17847
17848 /* fromstr and tostr have to contain the same number of chars */
17849 while (*instr != NUL)
17850 {
17851#ifdef FEAT_MBYTE
17852 if (has_mbyte)
17853 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017854 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017855 cpstr = instr;
17856 cplen = inlen;
17857 idx = 0;
17858 for (p = fromstr; *p != NUL; p += fromlen)
17859 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017860 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017861 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17862 {
17863 for (p = tostr; *p != NUL; p += tolen)
17864 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017865 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017866 if (idx-- == 0)
17867 {
17868 cplen = tolen;
17869 cpstr = p;
17870 break;
17871 }
17872 }
17873 if (*p == NUL) /* tostr is shorter than fromstr */
17874 goto error;
17875 break;
17876 }
17877 ++idx;
17878 }
17879
17880 if (first && cpstr == instr)
17881 {
17882 /* Check that fromstr and tostr have the same number of
17883 * (multi-byte) characters. Done only once when a character
17884 * of instr doesn't appear in fromstr. */
17885 first = FALSE;
17886 for (p = tostr; *p != NUL; p += tolen)
17887 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017888 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017889 --idx;
17890 }
17891 if (idx != 0)
17892 goto error;
17893 }
17894
17895 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017896 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017897 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017898
17899 instr += inlen;
17900 }
17901 else
17902#endif
17903 {
17904 /* When not using multi-byte chars we can do it faster. */
17905 p = vim_strchr(fromstr, *instr);
17906 if (p != NULL)
17907 ga_append(&ga, tostr[p - fromstr]);
17908 else
17909 ga_append(&ga, *instr);
17910 ++instr;
17911 }
17912 }
17913
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017914 /* add a terminating NUL */
17915 ga_grow(&ga, 1);
17916 ga_append(&ga, NUL);
17917
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017918 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017919}
17920
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017921#ifdef FEAT_FLOAT
17922/*
17923 * "trunc({float})" function
17924 */
17925 static void
17926f_trunc(argvars, rettv)
17927 typval_T *argvars;
17928 typval_T *rettv;
17929{
17930 float_T f;
17931
17932 rettv->v_type = VAR_FLOAT;
17933 if (get_float_arg(argvars, &f) == OK)
17934 /* trunc() is not in C90, use floor() or ceil() instead. */
17935 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17936 else
17937 rettv->vval.v_float = 0.0;
17938}
17939#endif
17940
Bram Moolenaar8299df92004-07-10 09:47:34 +000017941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017942 * "type(expr)" function
17943 */
17944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017945f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017946 typval_T *argvars;
17947 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017949 int n;
17950
17951 switch (argvars[0].v_type)
17952 {
17953 case VAR_NUMBER: n = 0; break;
17954 case VAR_STRING: n = 1; break;
17955 case VAR_FUNC: n = 2; break;
17956 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017957 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017958#ifdef FEAT_FLOAT
17959 case VAR_FLOAT: n = 5; break;
17960#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017961 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17962 }
17963 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964}
17965
17966/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017967 * "undofile(name)" function
17968 */
17969 static void
17970f_undofile(argvars, rettv)
17971 typval_T *argvars;
17972 typval_T *rettv;
17973{
17974 rettv->v_type = VAR_STRING;
17975#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017976 {
17977 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17978
17979 if (ffname != NULL)
17980 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17981 vim_free(ffname);
17982 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017983#else
17984 rettv->vval.v_string = NULL;
17985#endif
17986}
17987
17988/*
Bram Moolenaara800b422010-06-27 01:15:55 +020017989 * "undotree()" function
17990 */
17991 static void
17992f_undotree(argvars, rettv)
17993 typval_T *argvars UNUSED;
17994 typval_T *rettv;
17995{
17996 if (rettv_dict_alloc(rettv) == OK)
17997 {
17998 dict_T *dict = rettv->vval.v_dict;
17999 list_T *list;
18000
Bram Moolenaar730cde92010-06-27 05:18:54 +020018001 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018002 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018003 dict_add_nr_str(dict, "save_last",
18004 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018005 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18006 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018007 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018008
18009 list = list_alloc();
18010 if (list != NULL)
18011 {
18012 u_eval_tree(curbuf->b_u_oldhead, list);
18013 dict_add_list(dict, "entries", list);
18014 }
18015 }
18016}
18017
18018/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018019 * "values(dict)" function
18020 */
18021 static void
18022f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018023 typval_T *argvars;
18024 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018025{
18026 dict_list(argvars, rettv, 1);
18027}
18028
18029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030 * "virtcol(string)" function
18031 */
18032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018033f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018034 typval_T *argvars;
18035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036{
18037 colnr_T vcol = 0;
18038 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018039 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018041 fp = var2fpos(&argvars[0], FALSE, &fnum);
18042 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18043 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044 {
18045 getvvcol(curwin, fp, NULL, NULL, &vcol);
18046 ++vcol;
18047 }
18048
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018049 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050}
18051
18052/*
18053 * "visualmode()" function
18054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018056f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018057 typval_T *argvars UNUSED;
18058 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059{
18060#ifdef FEAT_VISUAL
18061 char_u str[2];
18062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018063 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064 str[0] = curbuf->b_visual_mode_eval;
18065 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018066 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067
18068 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018069 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071#endif
18072}
18073
18074/*
18075 * "winbufnr(nr)" function
18076 */
18077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018078f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018079 typval_T *argvars;
18080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081{
18082 win_T *wp;
18083
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018084 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018086 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018088 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089}
18090
18091/*
18092 * "wincol()" function
18093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018095f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018096 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098{
18099 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018100 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101}
18102
18103/*
18104 * "winheight(nr)" function
18105 */
18106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018107f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018108 typval_T *argvars;
18109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110{
18111 win_T *wp;
18112
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018113 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018114 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018115 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018117 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118}
18119
18120/*
18121 * "winline()" function
18122 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018123 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018124f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018125 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018126 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127{
18128 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018129 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130}
18131
18132/*
18133 * "winnr()" function
18134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018136f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018137 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139{
18140 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018141
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018143 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018144#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018145 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146}
18147
18148/*
18149 * "winrestcmd()" function
18150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018152f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018153 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018155{
18156#ifdef FEAT_WINDOWS
18157 win_T *wp;
18158 int winnr = 1;
18159 garray_T ga;
18160 char_u buf[50];
18161
18162 ga_init2(&ga, (int)sizeof(char), 70);
18163 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18164 {
18165 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18166 ga_concat(&ga, buf);
18167# ifdef FEAT_VERTSPLIT
18168 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18169 ga_concat(&ga, buf);
18170# endif
18171 ++winnr;
18172 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018173 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018175 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018177 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018179 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180}
18181
18182/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018183 * "winrestview()" function
18184 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018185 static void
18186f_winrestview(argvars, rettv)
18187 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018188 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018189{
18190 dict_T *dict;
18191
18192 if (argvars[0].v_type != VAR_DICT
18193 || (dict = argvars[0].vval.v_dict) == NULL)
18194 EMSG(_(e_invarg));
18195 else
18196 {
18197 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18198 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18199#ifdef FEAT_VIRTUALEDIT
18200 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18201#endif
18202 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018203 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018204
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018205 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018206#ifdef FEAT_DIFF
18207 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18208#endif
18209 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18210 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18211
18212 check_cursor();
18213 changed_cline_bef_curs();
18214 invalidate_botline();
18215 redraw_later(VALID);
18216
18217 if (curwin->w_topline == 0)
18218 curwin->w_topline = 1;
18219 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18220 curwin->w_topline = curbuf->b_ml.ml_line_count;
18221#ifdef FEAT_DIFF
18222 check_topfill(curwin, TRUE);
18223#endif
18224 }
18225}
18226
18227/*
18228 * "winsaveview()" function
18229 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018230 static void
18231f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018232 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018233 typval_T *rettv;
18234{
18235 dict_T *dict;
18236
Bram Moolenaara800b422010-06-27 01:15:55 +020018237 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018238 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018239 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018240
18241 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18242 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18243#ifdef FEAT_VIRTUALEDIT
18244 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18245#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018246 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018247 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18248
18249 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18250#ifdef FEAT_DIFF
18251 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18252#endif
18253 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18254 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18255}
18256
18257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258 * "winwidth(nr)" function
18259 */
18260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018261f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018262 typval_T *argvars;
18263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264{
18265 win_T *wp;
18266
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018267 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018268 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018269 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018270 else
18271#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018272 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018274 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275#endif
18276}
18277
Bram Moolenaar071d4272004-06-13 20:20:40 +000018278/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018279 * "writefile()" function
18280 */
18281 static void
18282f_writefile(argvars, rettv)
18283 typval_T *argvars;
18284 typval_T *rettv;
18285{
18286 int binary = FALSE;
18287 char_u *fname;
18288 FILE *fd;
18289 listitem_T *li;
18290 char_u *s;
18291 int ret = 0;
18292 int c;
18293
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018294 if (check_restricted() || check_secure())
18295 return;
18296
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018297 if (argvars[0].v_type != VAR_LIST)
18298 {
18299 EMSG2(_(e_listarg), "writefile()");
18300 return;
18301 }
18302 if (argvars[0].vval.v_list == NULL)
18303 return;
18304
18305 if (argvars[2].v_type != VAR_UNKNOWN
18306 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18307 binary = TRUE;
18308
18309 /* Always open the file in binary mode, library functions have a mind of
18310 * their own about CR-LF conversion. */
18311 fname = get_tv_string(&argvars[1]);
18312 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18313 {
18314 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18315 ret = -1;
18316 }
18317 else
18318 {
18319 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18320 li = li->li_next)
18321 {
18322 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18323 {
18324 if (*s == '\n')
18325 c = putc(NUL, fd);
18326 else
18327 c = putc(*s, fd);
18328 if (c == EOF)
18329 {
18330 ret = -1;
18331 break;
18332 }
18333 }
18334 if (!binary || li->li_next != NULL)
18335 if (putc('\n', fd) == EOF)
18336 {
18337 ret = -1;
18338 break;
18339 }
18340 if (ret < 0)
18341 {
18342 EMSG(_(e_write));
18343 break;
18344 }
18345 }
18346 fclose(fd);
18347 }
18348
18349 rettv->vval.v_number = ret;
18350}
18351
18352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018354 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355 */
18356 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018357var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018358 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018359 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018360 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018361{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018362 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018364 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365
Bram Moolenaara5525202006-03-02 22:52:09 +000018366 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018367 if (varp->v_type == VAR_LIST)
18368 {
18369 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018370 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018371 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018372 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018373
18374 l = varp->vval.v_list;
18375 if (l == NULL)
18376 return NULL;
18377
18378 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018379 pos.lnum = list_find_nr(l, 0L, &error);
18380 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018381 return NULL; /* invalid line number */
18382
18383 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018384 pos.col = list_find_nr(l, 1L, &error);
18385 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018386 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018387 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018388
18389 /* We accept "$" for the column number: last column. */
18390 li = list_find(l, 1L);
18391 if (li != NULL && li->li_tv.v_type == VAR_STRING
18392 && li->li_tv.vval.v_string != NULL
18393 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18394 pos.col = len + 1;
18395
Bram Moolenaara5525202006-03-02 22:52:09 +000018396 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018397 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018398 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018399 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018400
Bram Moolenaara5525202006-03-02 22:52:09 +000018401#ifdef FEAT_VIRTUALEDIT
18402 /* Get the virtual offset. Defaults to zero. */
18403 pos.coladd = list_find_nr(l, 2L, &error);
18404 if (error)
18405 pos.coladd = 0;
18406#endif
18407
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018408 return &pos;
18409 }
18410
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018411 name = get_tv_string_chk(varp);
18412 if (name == NULL)
18413 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018414 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018416#ifdef FEAT_VISUAL
18417 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18418 {
18419 if (VIsual_active)
18420 return &VIsual;
18421 return &curwin->w_cursor;
18422 }
18423#endif
18424 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018425 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018426 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18428 return NULL;
18429 return pp;
18430 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018431
18432#ifdef FEAT_VIRTUALEDIT
18433 pos.coladd = 0;
18434#endif
18435
Bram Moolenaar477933c2007-07-17 14:32:23 +000018436 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018437 {
18438 pos.col = 0;
18439 if (name[1] == '0') /* "w0": first visible line */
18440 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018441 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018442 pos.lnum = curwin->w_topline;
18443 return &pos;
18444 }
18445 else if (name[1] == '$') /* "w$": last visible line */
18446 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018447 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018448 pos.lnum = curwin->w_botline - 1;
18449 return &pos;
18450 }
18451 }
18452 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018454 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455 {
18456 pos.lnum = curbuf->b_ml.ml_line_count;
18457 pos.col = 0;
18458 }
18459 else
18460 {
18461 pos.lnum = curwin->w_cursor.lnum;
18462 pos.col = (colnr_T)STRLEN(ml_get_curline());
18463 }
18464 return &pos;
18465 }
18466 return NULL;
18467}
18468
18469/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018470 * Convert list in "arg" into a position and optional file number.
18471 * When "fnump" is NULL there is no file number, only 3 items.
18472 * Note that the column is passed on as-is, the caller may want to decrement
18473 * it to use 1 for the first column.
18474 * Return FAIL when conversion is not possible, doesn't check the position for
18475 * validity.
18476 */
18477 static int
18478list2fpos(arg, posp, fnump)
18479 typval_T *arg;
18480 pos_T *posp;
18481 int *fnump;
18482{
18483 list_T *l = arg->vval.v_list;
18484 long i = 0;
18485 long n;
18486
Bram Moolenaarbde35262006-07-23 20:12:24 +000018487 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18488 * when "fnump" isn't NULL and "coladd" is optional. */
18489 if (arg->v_type != VAR_LIST
18490 || l == NULL
18491 || l->lv_len < (fnump == NULL ? 2 : 3)
18492 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018493 return FAIL;
18494
18495 if (fnump != NULL)
18496 {
18497 n = list_find_nr(l, i++, NULL); /* fnum */
18498 if (n < 0)
18499 return FAIL;
18500 if (n == 0)
18501 n = curbuf->b_fnum; /* current buffer */
18502 *fnump = n;
18503 }
18504
18505 n = list_find_nr(l, i++, NULL); /* lnum */
18506 if (n < 0)
18507 return FAIL;
18508 posp->lnum = n;
18509
18510 n = list_find_nr(l, i++, NULL); /* col */
18511 if (n < 0)
18512 return FAIL;
18513 posp->col = n;
18514
18515#ifdef FEAT_VIRTUALEDIT
18516 n = list_find_nr(l, i, NULL);
18517 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018518 posp->coladd = 0;
18519 else
18520 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018521#endif
18522
18523 return OK;
18524}
18525
18526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527 * Get the length of an environment variable name.
18528 * Advance "arg" to the first character after the name.
18529 * Return 0 for error.
18530 */
18531 static int
18532get_env_len(arg)
18533 char_u **arg;
18534{
18535 char_u *p;
18536 int len;
18537
18538 for (p = *arg; vim_isIDc(*p); ++p)
18539 ;
18540 if (p == *arg) /* no name found */
18541 return 0;
18542
18543 len = (int)(p - *arg);
18544 *arg = p;
18545 return len;
18546}
18547
18548/*
18549 * Get the length of the name of a function or internal variable.
18550 * "arg" is advanced to the first non-white character after the name.
18551 * Return 0 if something is wrong.
18552 */
18553 static int
18554get_id_len(arg)
18555 char_u **arg;
18556{
18557 char_u *p;
18558 int len;
18559
18560 /* Find the end of the name. */
18561 for (p = *arg; eval_isnamec(*p); ++p)
18562 ;
18563 if (p == *arg) /* no name found */
18564 return 0;
18565
18566 len = (int)(p - *arg);
18567 *arg = skipwhite(p);
18568
18569 return len;
18570}
18571
18572/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018573 * Get the length of the name of a variable or function.
18574 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018576 * Return -1 if curly braces expansion failed.
18577 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578 * If the name contains 'magic' {}'s, expand them and return the
18579 * expanded name in an allocated string via 'alias' - caller must free.
18580 */
18581 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018582get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018583 char_u **arg;
18584 char_u **alias;
18585 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018586 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587{
18588 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018589 char_u *p;
18590 char_u *expr_start;
18591 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592
18593 *alias = NULL; /* default to no alias */
18594
18595 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18596 && (*arg)[2] == (int)KE_SNR)
18597 {
18598 /* hard coded <SNR>, already translated */
18599 *arg += 3;
18600 return get_id_len(arg) + 3;
18601 }
18602 len = eval_fname_script(*arg);
18603 if (len > 0)
18604 {
18605 /* literal "<SID>", "s:" or "<SNR>" */
18606 *arg += len;
18607 }
18608
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018610 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018612 p = find_name_end(*arg, &expr_start, &expr_end,
18613 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614 if (expr_start != NULL)
18615 {
18616 char_u *temp_string;
18617
18618 if (!evaluate)
18619 {
18620 len += (int)(p - *arg);
18621 *arg = skipwhite(p);
18622 return len;
18623 }
18624
18625 /*
18626 * Include any <SID> etc in the expanded string:
18627 * Thus the -len here.
18628 */
18629 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18630 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018631 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632 *alias = temp_string;
18633 *arg = skipwhite(p);
18634 return (int)STRLEN(temp_string);
18635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636
18637 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018638 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018639 EMSG2(_(e_invexpr2), *arg);
18640
18641 return len;
18642}
18643
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018644/*
18645 * Find the end of a variable or function name, taking care of magic braces.
18646 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18647 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018648 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018649 * Return a pointer to just after the name. Equal to "arg" if there is no
18650 * valid name.
18651 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018653find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654 char_u *arg;
18655 char_u **expr_start;
18656 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018657 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018659 int mb_nest = 0;
18660 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661 char_u *p;
18662
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018663 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018665 *expr_start = NULL;
18666 *expr_end = NULL;
18667 }
18668
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018669 /* Quick check for valid starting character. */
18670 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18671 return arg;
18672
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018673 for (p = arg; *p != NUL
18674 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018675 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018676 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018677 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018678 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018679 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018680 if (*p == '\'')
18681 {
18682 /* skip over 'string' to avoid counting [ and ] inside it. */
18683 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18684 ;
18685 if (*p == NUL)
18686 break;
18687 }
18688 else if (*p == '"')
18689 {
18690 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18691 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18692 if (*p == '\\' && p[1] != NUL)
18693 ++p;
18694 if (*p == NUL)
18695 break;
18696 }
18697
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018698 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018700 if (*p == '[')
18701 ++br_nest;
18702 else if (*p == ']')
18703 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018704 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018705
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018706 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018707 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018708 if (*p == '{')
18709 {
18710 mb_nest++;
18711 if (expr_start != NULL && *expr_start == NULL)
18712 *expr_start = p;
18713 }
18714 else if (*p == '}')
18715 {
18716 mb_nest--;
18717 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18718 *expr_end = p;
18719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721 }
18722
18723 return p;
18724}
18725
18726/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018727 * Expands out the 'magic' {}'s in a variable/function name.
18728 * Note that this can call itself recursively, to deal with
18729 * constructs like foo{bar}{baz}{bam}
18730 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18731 * "in_start" ^
18732 * "expr_start" ^
18733 * "expr_end" ^
18734 * "in_end" ^
18735 *
18736 * Returns a new allocated string, which the caller must free.
18737 * Returns NULL for failure.
18738 */
18739 static char_u *
18740make_expanded_name(in_start, expr_start, expr_end, in_end)
18741 char_u *in_start;
18742 char_u *expr_start;
18743 char_u *expr_end;
18744 char_u *in_end;
18745{
18746 char_u c1;
18747 char_u *retval = NULL;
18748 char_u *temp_result;
18749 char_u *nextcmd = NULL;
18750
18751 if (expr_end == NULL || in_end == NULL)
18752 return NULL;
18753 *expr_start = NUL;
18754 *expr_end = NUL;
18755 c1 = *in_end;
18756 *in_end = NUL;
18757
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018758 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018759 if (temp_result != NULL && nextcmd == NULL)
18760 {
18761 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18762 + (in_end - expr_end) + 1));
18763 if (retval != NULL)
18764 {
18765 STRCPY(retval, in_start);
18766 STRCAT(retval, temp_result);
18767 STRCAT(retval, expr_end + 1);
18768 }
18769 }
18770 vim_free(temp_result);
18771
18772 *in_end = c1; /* put char back for error messages */
18773 *expr_start = '{';
18774 *expr_end = '}';
18775
18776 if (retval != NULL)
18777 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018778 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018779 if (expr_start != NULL)
18780 {
18781 /* Further expansion! */
18782 temp_result = make_expanded_name(retval, expr_start,
18783 expr_end, temp_result);
18784 vim_free(retval);
18785 retval = temp_result;
18786 }
18787 }
18788
18789 return retval;
18790}
18791
18792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018794 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018795 */
18796 static int
18797eval_isnamec(c)
18798 int c;
18799{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018800 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18801}
18802
18803/*
18804 * Return TRUE if character "c" can be used as the first character in a
18805 * variable or function name (excluding '{' and '}').
18806 */
18807 static int
18808eval_isnamec1(c)
18809 int c;
18810{
18811 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812}
18813
18814/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018815 * Set number v: variable to "val".
18816 */
18817 void
18818set_vim_var_nr(idx, val)
18819 int idx;
18820 long val;
18821{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018822 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823}
18824
18825/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018826 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018827 */
18828 long
18829get_vim_var_nr(idx)
18830 int idx;
18831{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018832 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833}
18834
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018835/*
18836 * Get string v: variable value. Uses a static buffer, can only be used once.
18837 */
18838 char_u *
18839get_vim_var_str(idx)
18840 int idx;
18841{
18842 return get_tv_string(&vimvars[idx].vv_tv);
18843}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018844
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018846 * Get List v: variable value. Caller must take care of reference count when
18847 * needed.
18848 */
18849 list_T *
18850get_vim_var_list(idx)
18851 int idx;
18852{
18853 return vimvars[idx].vv_list;
18854}
18855
18856/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018857 * Set v:char to character "c".
18858 */
18859 void
18860set_vim_var_char(c)
18861 int c;
18862{
18863#ifdef FEAT_MBYTE
18864 char_u buf[MB_MAXBYTES];
18865#else
18866 char_u buf[2];
18867#endif
18868
18869#ifdef FEAT_MBYTE
18870 if (has_mbyte)
18871 buf[(*mb_char2bytes)(c, buf)] = NUL;
18872 else
18873#endif
18874 {
18875 buf[0] = c;
18876 buf[1] = NUL;
18877 }
18878 set_vim_var_string(VV_CHAR, buf, -1);
18879}
18880
18881/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018882 * Set v:count to "count" and v:count1 to "count1".
18883 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884 */
18885 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018886set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887 long count;
18888 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018889 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018891 if (set_prevcount)
18892 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018893 vimvars[VV_COUNT].vv_nr = count;
18894 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018895}
18896
18897/*
18898 * Set string v: variable to a copy of "val".
18899 */
18900 void
18901set_vim_var_string(idx, val, len)
18902 int idx;
18903 char_u *val;
18904 int len; /* length of "val" to use or -1 (whole string) */
18905{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018906 /* Need to do this (at least) once, since we can't initialize a union.
18907 * Will always be invoked when "v:progname" is set. */
18908 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18909
Bram Moolenaare9a41262005-01-15 22:18:47 +000018910 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018912 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018913 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018914 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018916 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018917}
18918
18919/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018920 * Set List v: variable to "val".
18921 */
18922 void
18923set_vim_var_list(idx, val)
18924 int idx;
18925 list_T *val;
18926{
18927 list_unref(vimvars[idx].vv_list);
18928 vimvars[idx].vv_list = val;
18929 if (val != NULL)
18930 ++val->lv_refcount;
18931}
18932
18933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934 * Set v:register if needed.
18935 */
18936 void
18937set_reg_var(c)
18938 int c;
18939{
18940 char_u regname;
18941
18942 if (c == 0 || c == ' ')
18943 regname = '"';
18944 else
18945 regname = c;
18946 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018947 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948 set_vim_var_string(VV_REG, &regname, 1);
18949}
18950
18951/*
18952 * Get or set v:exception. If "oldval" == NULL, return the current value.
18953 * Otherwise, restore the value to "oldval" and return NULL.
18954 * Must always be called in pairs to save and restore v:exception! Does not
18955 * take care of memory allocations.
18956 */
18957 char_u *
18958v_exception(oldval)
18959 char_u *oldval;
18960{
18961 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018962 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963
Bram Moolenaare9a41262005-01-15 22:18:47 +000018964 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018965 return NULL;
18966}
18967
18968/*
18969 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18970 * Otherwise, restore the value to "oldval" and return NULL.
18971 * Must always be called in pairs to save and restore v:throwpoint! Does not
18972 * take care of memory allocations.
18973 */
18974 char_u *
18975v_throwpoint(oldval)
18976 char_u *oldval;
18977{
18978 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018979 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980
Bram Moolenaare9a41262005-01-15 22:18:47 +000018981 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982 return NULL;
18983}
18984
18985#if defined(FEAT_AUTOCMD) || defined(PROTO)
18986/*
18987 * Set v:cmdarg.
18988 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18989 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18990 * Must always be called in pairs!
18991 */
18992 char_u *
18993set_cmdarg(eap, oldarg)
18994 exarg_T *eap;
18995 char_u *oldarg;
18996{
18997 char_u *oldval;
18998 char_u *newval;
18999 unsigned len;
19000
Bram Moolenaare9a41262005-01-15 22:18:47 +000019001 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019002 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019004 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019005 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019006 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007 }
19008
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019009 if (eap->force_bin == FORCE_BIN)
19010 len = 6;
19011 else if (eap->force_bin == FORCE_NOBIN)
19012 len = 8;
19013 else
19014 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019015
19016 if (eap->read_edit)
19017 len += 7;
19018
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019019 if (eap->force_ff != 0)
19020 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19021# ifdef FEAT_MBYTE
19022 if (eap->force_enc != 0)
19023 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019024 if (eap->bad_char != 0)
19025 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019026# endif
19027
19028 newval = alloc(len + 1);
19029 if (newval == NULL)
19030 return NULL;
19031
19032 if (eap->force_bin == FORCE_BIN)
19033 sprintf((char *)newval, " ++bin");
19034 else if (eap->force_bin == FORCE_NOBIN)
19035 sprintf((char *)newval, " ++nobin");
19036 else
19037 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019038
19039 if (eap->read_edit)
19040 STRCAT(newval, " ++edit");
19041
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019042 if (eap->force_ff != 0)
19043 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19044 eap->cmd + eap->force_ff);
19045# ifdef FEAT_MBYTE
19046 if (eap->force_enc != 0)
19047 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19048 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019049 if (eap->bad_char == BAD_KEEP)
19050 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19051 else if (eap->bad_char == BAD_DROP)
19052 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19053 else if (eap->bad_char != 0)
19054 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019055# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019056 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019057 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058}
19059#endif
19060
19061/*
19062 * Get the value of internal variable "name".
19063 * Return OK or FAIL.
19064 */
19065 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019066get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067 char_u *name;
19068 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019069 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019070 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071{
19072 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019073 typval_T *tv = NULL;
19074 typval_T atv;
19075 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077
19078 /* truncate the name, so that we can use strcmp() */
19079 cc = name[len];
19080 name[len] = NUL;
19081
19082 /*
19083 * Check for "b:changedtick".
19084 */
19085 if (STRCMP(name, "b:changedtick") == 0)
19086 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019087 atv.v_type = VAR_NUMBER;
19088 atv.vval.v_number = curbuf->b_changedtick;
19089 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090 }
19091
19092 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093 * Check for user-defined variables.
19094 */
19095 else
19096 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019097 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019099 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019100 }
19101
Bram Moolenaare9a41262005-01-15 22:18:47 +000019102 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019104 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019105 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019106 ret = FAIL;
19107 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019108 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019109 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110
19111 name[len] = cc;
19112
19113 return ret;
19114}
19115
19116/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019117 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19118 * Also handle function call with Funcref variable: func(expr)
19119 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19120 */
19121 static int
19122handle_subscript(arg, rettv, evaluate, verbose)
19123 char_u **arg;
19124 typval_T *rettv;
19125 int evaluate; /* do more than finding the end */
19126 int verbose; /* give error messages */
19127{
19128 int ret = OK;
19129 dict_T *selfdict = NULL;
19130 char_u *s;
19131 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019132 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019133
19134 while (ret == OK
19135 && (**arg == '['
19136 || (**arg == '.' && rettv->v_type == VAR_DICT)
19137 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19138 && !vim_iswhite(*(*arg - 1)))
19139 {
19140 if (**arg == '(')
19141 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019142 /* need to copy the funcref so that we can clear rettv */
19143 functv = *rettv;
19144 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019145
19146 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019147 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019148 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019149 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19150 &len, evaluate, selfdict);
19151
19152 /* Clear the funcref afterwards, so that deleting it while
19153 * evaluating the arguments is possible (see test55). */
19154 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019155
19156 /* Stop the expression evaluation when immediately aborting on
19157 * error, or when an interrupt occurred or an exception was thrown
19158 * but not caught. */
19159 if (aborting())
19160 {
19161 if (ret == OK)
19162 clear_tv(rettv);
19163 ret = FAIL;
19164 }
19165 dict_unref(selfdict);
19166 selfdict = NULL;
19167 }
19168 else /* **arg == '[' || **arg == '.' */
19169 {
19170 dict_unref(selfdict);
19171 if (rettv->v_type == VAR_DICT)
19172 {
19173 selfdict = rettv->vval.v_dict;
19174 if (selfdict != NULL)
19175 ++selfdict->dv_refcount;
19176 }
19177 else
19178 selfdict = NULL;
19179 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19180 {
19181 clear_tv(rettv);
19182 ret = FAIL;
19183 }
19184 }
19185 }
19186 dict_unref(selfdict);
19187 return ret;
19188}
19189
19190/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019191 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019192 * value).
19193 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019194 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019195alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019196{
Bram Moolenaar33570922005-01-25 22:26:29 +000019197 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019198}
19199
19200/*
19201 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202 * The string "s" must have been allocated, it is consumed.
19203 * Return NULL for out of memory, the variable otherwise.
19204 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019205 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019206alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207 char_u *s;
19208{
Bram Moolenaar33570922005-01-25 22:26:29 +000019209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019210
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019211 rettv = alloc_tv();
19212 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019213 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019214 rettv->v_type = VAR_STRING;
19215 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216 }
19217 else
19218 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019219 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220}
19221
19222/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019223 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019224 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019225 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019226free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019227 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019228{
19229 if (varp != NULL)
19230 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019231 switch (varp->v_type)
19232 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019233 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019234 func_unref(varp->vval.v_string);
19235 /*FALLTHROUGH*/
19236 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019237 vim_free(varp->vval.v_string);
19238 break;
19239 case VAR_LIST:
19240 list_unref(varp->vval.v_list);
19241 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019242 case VAR_DICT:
19243 dict_unref(varp->vval.v_dict);
19244 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019245 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019246#ifdef FEAT_FLOAT
19247 case VAR_FLOAT:
19248#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019249 case VAR_UNKNOWN:
19250 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019251 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019252 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019253 break;
19254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019255 vim_free(varp);
19256 }
19257}
19258
19259/*
19260 * Free the memory for a variable value and set the value to NULL or 0.
19261 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019262 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019263clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019264 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265{
19266 if (varp != NULL)
19267 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019268 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019270 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019271 func_unref(varp->vval.v_string);
19272 /*FALLTHROUGH*/
19273 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019274 vim_free(varp->vval.v_string);
19275 varp->vval.v_string = NULL;
19276 break;
19277 case VAR_LIST:
19278 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019279 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019280 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019281 case VAR_DICT:
19282 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019283 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019284 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019285 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019286 varp->vval.v_number = 0;
19287 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019288#ifdef FEAT_FLOAT
19289 case VAR_FLOAT:
19290 varp->vval.v_float = 0.0;
19291 break;
19292#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019293 case VAR_UNKNOWN:
19294 break;
19295 default:
19296 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019297 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019298 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299 }
19300}
19301
19302/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019303 * Set the value of a variable to NULL without freeing items.
19304 */
19305 static void
19306init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019307 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019308{
19309 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019310 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019311}
19312
19313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019314 * Get the number value of a variable.
19315 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019316 * For incompatible types, return 0.
19317 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19318 * caller of incompatible types: it sets *denote to TRUE if "denote"
19319 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320 */
19321 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019322get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019323 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019325 int error = FALSE;
19326
19327 return get_tv_number_chk(varp, &error); /* return 0L on error */
19328}
19329
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019330 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019331get_tv_number_chk(varp, denote)
19332 typval_T *varp;
19333 int *denote;
19334{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019335 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019337 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019338 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019339 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019340 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019341#ifdef FEAT_FLOAT
19342 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019343 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019344 break;
19345#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019346 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019347 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019348 break;
19349 case VAR_STRING:
19350 if (varp->vval.v_string != NULL)
19351 vim_str2nr(varp->vval.v_string, NULL, NULL,
19352 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019353 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019354 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019355 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019356 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019357 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019358 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019359 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019360 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019361 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019362 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019364 if (denote == NULL) /* useful for values that must be unsigned */
19365 n = -1;
19366 else
19367 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019368 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369}
19370
19371/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019372 * Get the lnum from the first argument.
19373 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019374 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019375 */
19376 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019377get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019378 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379{
Bram Moolenaar33570922005-01-25 22:26:29 +000019380 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381 linenr_T lnum;
19382
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019383 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019384 if (lnum == 0) /* no valid number, try using line() */
19385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019386 rettv.v_type = VAR_NUMBER;
19387 f_line(argvars, &rettv);
19388 lnum = rettv.vval.v_number;
19389 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019390 }
19391 return lnum;
19392}
19393
19394/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019395 * Get the lnum from the first argument.
19396 * Also accepts "$", then "buf" is used.
19397 * Returns 0 on error.
19398 */
19399 static linenr_T
19400get_tv_lnum_buf(argvars, buf)
19401 typval_T *argvars;
19402 buf_T *buf;
19403{
19404 if (argvars[0].v_type == VAR_STRING
19405 && argvars[0].vval.v_string != NULL
19406 && argvars[0].vval.v_string[0] == '$'
19407 && buf != NULL)
19408 return buf->b_ml.ml_line_count;
19409 return get_tv_number_chk(&argvars[0], NULL);
19410}
19411
19412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413 * Get the string value of a variable.
19414 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019415 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19416 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417 * If the String variable has never been set, return an empty string.
19418 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019419 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19420 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019421 */
19422 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019423get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019424 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425{
19426 static char_u mybuf[NUMBUFLEN];
19427
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019428 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429}
19430
19431 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019432get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019433 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019434 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019436 char_u *res = get_tv_string_buf_chk(varp, buf);
19437
19438 return res != NULL ? res : (char_u *)"";
19439}
19440
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019441 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019442get_tv_string_chk(varp)
19443 typval_T *varp;
19444{
19445 static char_u mybuf[NUMBUFLEN];
19446
19447 return get_tv_string_buf_chk(varp, mybuf);
19448}
19449
19450 static char_u *
19451get_tv_string_buf_chk(varp, buf)
19452 typval_T *varp;
19453 char_u *buf;
19454{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019455 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019456 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019457 case VAR_NUMBER:
19458 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19459 return buf;
19460 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019461 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019462 break;
19463 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019464 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019465 break;
19466 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019467 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019468 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019469#ifdef FEAT_FLOAT
19470 case VAR_FLOAT:
19471 EMSG(_("E806: using Float as a String"));
19472 break;
19473#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019474 case VAR_STRING:
19475 if (varp->vval.v_string != NULL)
19476 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019477 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019478 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019479 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019480 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019482 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483}
19484
19485/*
19486 * Find variable "name" in the list of variables.
19487 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019488 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019489 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019490 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019492 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019493find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019495 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019497 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019498 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019499
Bram Moolenaara7043832005-01-21 11:56:39 +000019500 ht = find_var_ht(name, &varname);
19501 if (htp != NULL)
19502 *htp = ht;
19503 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019504 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019505 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506}
19507
19508/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019509 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019510 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019512 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019513find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019514 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019515 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019516 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019517{
Bram Moolenaar33570922005-01-25 22:26:29 +000019518 hashitem_T *hi;
19519
19520 if (*varname == NUL)
19521 {
19522 /* Must be something like "s:", otherwise "ht" would be NULL. */
19523 switch (varname[-2])
19524 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019525 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019526 case 'g': return &globvars_var;
19527 case 'v': return &vimvars_var;
19528 case 'b': return &curbuf->b_bufvar;
19529 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019530#ifdef FEAT_WINDOWS
19531 case 't': return &curtab->tp_winvar;
19532#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019533 case 'l': return current_funccal == NULL
19534 ? NULL : &current_funccal->l_vars_var;
19535 case 'a': return current_funccal == NULL
19536 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019537 }
19538 return NULL;
19539 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019540
19541 hi = hash_find(ht, varname);
19542 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019543 {
19544 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019545 * worked find the variable again. Don't auto-load a script if it was
19546 * loaded already, otherwise it would be loaded every time when
19547 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019548 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019549 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019550 hi = hash_find(ht, varname);
19551 if (HASHITEM_EMPTY(hi))
19552 return NULL;
19553 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019554 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019555}
19556
19557/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019558 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019559 * Set "varname" to the start of name without ':'.
19560 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019561 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019562find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563 char_u *name;
19564 char_u **varname;
19565{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019566 hashitem_T *hi;
19567
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568 if (name[1] != ':')
19569 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019570 /* The name must not start with a colon or #. */
19571 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572 return NULL;
19573 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019574
19575 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019576 hi = hash_find(&compat_hashtab, name);
19577 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019578 return &compat_hashtab;
19579
Bram Moolenaar071d4272004-06-13 20:20:40 +000019580 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019581 return &globvarht; /* global variable */
19582 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019583 }
19584 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019585 if (*name == 'g') /* global variable */
19586 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019587 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19588 */
19589 if (vim_strchr(name + 2, ':') != NULL
19590 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019591 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019592 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019593 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019594 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019595 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019596#ifdef FEAT_WINDOWS
19597 if (*name == 't') /* tab page variable */
19598 return &curtab->tp_vars.dv_hashtab;
19599#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019600 if (*name == 'v') /* v: variable */
19601 return &vimvarht;
19602 if (*name == 'a' && current_funccal != NULL) /* function argument */
19603 return &current_funccal->l_avars.dv_hashtab;
19604 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19605 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606 if (*name == 's' /* script variable */
19607 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19608 return &SCRIPT_VARS(current_SID);
19609 return NULL;
19610}
19611
19612/*
19613 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019614 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615 * Returns NULL when it doesn't exist.
19616 */
19617 char_u *
19618get_var_value(name)
19619 char_u *name;
19620{
Bram Moolenaar33570922005-01-25 22:26:29 +000019621 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622
Bram Moolenaara7043832005-01-21 11:56:39 +000019623 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624 if (v == NULL)
19625 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019626 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627}
19628
19629/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019630 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019631 * sourcing this script and when executing functions defined in the script.
19632 */
19633 void
19634new_script_vars(id)
19635 scid_T id;
19636{
Bram Moolenaara7043832005-01-21 11:56:39 +000019637 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019638 hashtab_T *ht;
19639 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019640
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19642 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019643 /* Re-allocating ga_data means that an ht_array pointing to
19644 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019645 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019646 for (i = 1; i <= ga_scripts.ga_len; ++i)
19647 {
19648 ht = &SCRIPT_VARS(i);
19649 if (ht->ht_mask == HT_INIT_SIZE - 1)
19650 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019651 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019652 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019653 }
19654
Bram Moolenaar071d4272004-06-13 20:20:40 +000019655 while (ga_scripts.ga_len < id)
19656 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019657 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019658 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019659 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019661 }
19662 }
19663}
19664
19665/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019666 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19667 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019668 */
19669 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019670init_var_dict(dict, dict_var)
19671 dict_T *dict;
19672 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673{
Bram Moolenaar33570922005-01-25 22:26:29 +000019674 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019675 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019676 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019677 dict_var->di_tv.vval.v_dict = dict;
19678 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019679 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019680 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19681 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019682}
19683
19684/*
19685 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019686 * Frees all allocated variables and the value they contain.
19687 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688 */
19689 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019690vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019691 hashtab_T *ht;
19692{
19693 vars_clear_ext(ht, TRUE);
19694}
19695
19696/*
19697 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19698 */
19699 static void
19700vars_clear_ext(ht, free_val)
19701 hashtab_T *ht;
19702 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703{
Bram Moolenaara7043832005-01-21 11:56:39 +000019704 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019705 hashitem_T *hi;
19706 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019707
Bram Moolenaar33570922005-01-25 22:26:29 +000019708 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019709 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019710 for (hi = ht->ht_array; todo > 0; ++hi)
19711 {
19712 if (!HASHITEM_EMPTY(hi))
19713 {
19714 --todo;
19715
Bram Moolenaar33570922005-01-25 22:26:29 +000019716 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019717 * ht_array might change then. hash_clear() takes care of it
19718 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019719 v = HI2DI(hi);
19720 if (free_val)
19721 clear_tv(&v->di_tv);
19722 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19723 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019724 }
19725 }
19726 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019727 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728}
19729
Bram Moolenaara7043832005-01-21 11:56:39 +000019730/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019731 * Delete a variable from hashtab "ht" at item "hi".
19732 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019733 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019734 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019735delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019736 hashtab_T *ht;
19737 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019738{
Bram Moolenaar33570922005-01-25 22:26:29 +000019739 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019740
19741 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019742 clear_tv(&di->di_tv);
19743 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744}
19745
19746/*
19747 * List the value of one internal variable.
19748 */
19749 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019750list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019751 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019753 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019755 char_u *tofree;
19756 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019757 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019758
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019759 current_copyID += COPYID_INC;
19760 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019761 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019762 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019763 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764}
19765
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019767list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768 char_u *prefix;
19769 char_u *name;
19770 int type;
19771 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019772 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773{
Bram Moolenaar31859182007-08-14 20:41:13 +000019774 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19775 msg_start();
19776 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777 if (name != NULL) /* "a:" vars don't have a name stored */
19778 msg_puts(name);
19779 msg_putchar(' ');
19780 msg_advance(22);
19781 if (type == VAR_NUMBER)
19782 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019783 else if (type == VAR_FUNC)
19784 msg_putchar('*');
19785 else if (type == VAR_LIST)
19786 {
19787 msg_putchar('[');
19788 if (*string == '[')
19789 ++string;
19790 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019791 else if (type == VAR_DICT)
19792 {
19793 msg_putchar('{');
19794 if (*string == '{')
19795 ++string;
19796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797 else
19798 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019799
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019801
19802 if (type == VAR_FUNC)
19803 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019804 if (*first)
19805 {
19806 msg_clr_eos();
19807 *first = FALSE;
19808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809}
19810
19811/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019812 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813 * If the variable already exists, the value is updated.
19814 * Otherwise the variable is created.
19815 */
19816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019817set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019819 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019820 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821{
Bram Moolenaar33570922005-01-25 22:26:29 +000019822 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019824 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019826 ht = find_var_ht(name, &varname);
19827 if (ht == NULL || *varname == NUL)
19828 {
19829 EMSG2(_(e_illvar), name);
19830 return;
19831 }
19832 v = find_var_in_ht(ht, varname, TRUE);
19833
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019834 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
19835 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019836
Bram Moolenaar33570922005-01-25 22:26:29 +000019837 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019839 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019840 if (var_check_ro(v->di_flags, name)
19841 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019842 return;
19843 if (v->di_tv.v_type != tv->v_type
19844 && !((v->di_tv.v_type == VAR_STRING
19845 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019846 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019847 || tv->v_type == VAR_NUMBER))
19848#ifdef FEAT_FLOAT
19849 && !((v->di_tv.v_type == VAR_NUMBER
19850 || v->di_tv.v_type == VAR_FLOAT)
19851 && (tv->v_type == VAR_NUMBER
19852 || tv->v_type == VAR_FLOAT))
19853#endif
19854 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019855 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019856 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019857 return;
19858 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019859
19860 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019861 * Handle setting internal v: variables separately: we don't change
19862 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019863 */
19864 if (ht == &vimvarht)
19865 {
19866 if (v->di_tv.v_type == VAR_STRING)
19867 {
19868 vim_free(v->di_tv.vval.v_string);
19869 if (copy || tv->v_type != VAR_STRING)
19870 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19871 else
19872 {
19873 /* Take over the string to avoid an extra alloc/free. */
19874 v->di_tv.vval.v_string = tv->vval.v_string;
19875 tv->vval.v_string = NULL;
19876 }
19877 }
19878 else if (v->di_tv.v_type != VAR_NUMBER)
19879 EMSG2(_(e_intern2), "set_var()");
19880 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019881 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019882 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019883 if (STRCMP(varname, "searchforward") == 0)
19884 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19885 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019886 return;
19887 }
19888
19889 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019890 }
19891 else /* add a new variable */
19892 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019893 /* Can't add "v:" variable. */
19894 if (ht == &vimvarht)
19895 {
19896 EMSG2(_(e_illvar), name);
19897 return;
19898 }
19899
Bram Moolenaar92124a32005-06-17 22:03:40 +000019900 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019901 if (!valid_varname(varname))
19902 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019903
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019904 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19905 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019906 if (v == NULL)
19907 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019908 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019909 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019910 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019911 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019912 return;
19913 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019914 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019915 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019916
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019917 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019918 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019919 else
19920 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019921 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019922 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019923 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925}
19926
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019927/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019928 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019929 * Also give an error message.
19930 */
19931 static int
19932var_check_ro(flags, name)
19933 int flags;
19934 char_u *name;
19935{
19936 if (flags & DI_FLAGS_RO)
19937 {
19938 EMSG2(_(e_readonlyvar), name);
19939 return TRUE;
19940 }
19941 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19942 {
19943 EMSG2(_(e_readonlysbx), name);
19944 return TRUE;
19945 }
19946 return FALSE;
19947}
19948
19949/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019950 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19951 * Also give an error message.
19952 */
19953 static int
19954var_check_fixed(flags, name)
19955 int flags;
19956 char_u *name;
19957{
19958 if (flags & DI_FLAGS_FIX)
19959 {
19960 EMSG2(_("E795: Cannot delete variable %s"), name);
19961 return TRUE;
19962 }
19963 return FALSE;
19964}
19965
19966/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019967 * Check if a funcref is assigned to a valid variable name.
19968 * Return TRUE and give an error if not.
19969 */
19970 static int
19971var_check_func_name(name, new_var)
19972 char_u *name; /* points to start of variable name */
19973 int new_var; /* TRUE when creating the variable */
19974{
19975 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19976 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19977 ? name[2] : name[0]))
19978 {
19979 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
19980 name);
19981 return TRUE;
19982 }
19983 /* Don't allow hiding a function. When "v" is not NULL we might be
19984 * assigning another function to the same var, the type is checked
19985 * below. */
19986 if (new_var && function_exists(name))
19987 {
19988 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
19989 name);
19990 return TRUE;
19991 }
19992 return FALSE;
19993}
19994
19995/*
19996 * Check if a variable name is valid.
19997 * Return FALSE and give an error if not.
19998 */
19999 static int
20000valid_varname(varname)
20001 char_u *varname;
20002{
20003 char_u *p;
20004
20005 for (p = varname; *p != NUL; ++p)
20006 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20007 && *p != AUTOLOAD_CHAR)
20008 {
20009 EMSG2(_(e_illvar), varname);
20010 return FALSE;
20011 }
20012 return TRUE;
20013}
20014
20015/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020016 * Return TRUE if typeval "tv" is set to be locked (immutable).
20017 * Also give an error message, using "name".
20018 */
20019 static int
20020tv_check_lock(lock, name)
20021 int lock;
20022 char_u *name;
20023{
20024 if (lock & VAR_LOCKED)
20025 {
20026 EMSG2(_("E741: Value is locked: %s"),
20027 name == NULL ? (char_u *)_("Unknown") : name);
20028 return TRUE;
20029 }
20030 if (lock & VAR_FIXED)
20031 {
20032 EMSG2(_("E742: Cannot change value of %s"),
20033 name == NULL ? (char_u *)_("Unknown") : name);
20034 return TRUE;
20035 }
20036 return FALSE;
20037}
20038
20039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020040 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020041 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020042 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020043 * It is OK for "from" and "to" to point to the same item. This is used to
20044 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020045 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020046 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020047copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020048 typval_T *from;
20049 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020051 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020052 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020053 switch (from->v_type)
20054 {
20055 case VAR_NUMBER:
20056 to->vval.v_number = from->vval.v_number;
20057 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020058#ifdef FEAT_FLOAT
20059 case VAR_FLOAT:
20060 to->vval.v_float = from->vval.v_float;
20061 break;
20062#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020063 case VAR_STRING:
20064 case VAR_FUNC:
20065 if (from->vval.v_string == NULL)
20066 to->vval.v_string = NULL;
20067 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020068 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020069 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020070 if (from->v_type == VAR_FUNC)
20071 func_ref(to->vval.v_string);
20072 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020073 break;
20074 case VAR_LIST:
20075 if (from->vval.v_list == NULL)
20076 to->vval.v_list = NULL;
20077 else
20078 {
20079 to->vval.v_list = from->vval.v_list;
20080 ++to->vval.v_list->lv_refcount;
20081 }
20082 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020083 case VAR_DICT:
20084 if (from->vval.v_dict == NULL)
20085 to->vval.v_dict = NULL;
20086 else
20087 {
20088 to->vval.v_dict = from->vval.v_dict;
20089 ++to->vval.v_dict->dv_refcount;
20090 }
20091 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020092 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020093 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020094 break;
20095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096}
20097
20098/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020099 * Make a copy of an item.
20100 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020101 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20102 * reference to an already copied list/dict can be used.
20103 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020104 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020105 static int
20106item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020107 typval_T *from;
20108 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020109 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020110 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020111{
20112 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020113 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020114
Bram Moolenaar33570922005-01-25 22:26:29 +000020115 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020116 {
20117 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020118 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020119 }
20120 ++recurse;
20121
20122 switch (from->v_type)
20123 {
20124 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020125#ifdef FEAT_FLOAT
20126 case VAR_FLOAT:
20127#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020128 case VAR_STRING:
20129 case VAR_FUNC:
20130 copy_tv(from, to);
20131 break;
20132 case VAR_LIST:
20133 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020134 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020135 if (from->vval.v_list == NULL)
20136 to->vval.v_list = NULL;
20137 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20138 {
20139 /* use the copy made earlier */
20140 to->vval.v_list = from->vval.v_list->lv_copylist;
20141 ++to->vval.v_list->lv_refcount;
20142 }
20143 else
20144 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20145 if (to->vval.v_list == NULL)
20146 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020147 break;
20148 case VAR_DICT:
20149 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020150 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020151 if (from->vval.v_dict == NULL)
20152 to->vval.v_dict = NULL;
20153 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20154 {
20155 /* use the copy made earlier */
20156 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20157 ++to->vval.v_dict->dv_refcount;
20158 }
20159 else
20160 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20161 if (to->vval.v_dict == NULL)
20162 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020163 break;
20164 default:
20165 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020166 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020167 }
20168 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020169 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020170}
20171
20172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173 * ":echo expr1 ..." print each argument separated with a space, add a
20174 * newline at the end.
20175 * ":echon expr1 ..." print each argument plain.
20176 */
20177 void
20178ex_echo(eap)
20179 exarg_T *eap;
20180{
20181 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020182 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020183 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020184 char_u *p;
20185 int needclr = TRUE;
20186 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020187 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188
20189 if (eap->skip)
20190 ++emsg_skip;
20191 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20192 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020193 /* If eval1() causes an error message the text from the command may
20194 * still need to be cleared. E.g., "echo 22,44". */
20195 need_clr_eos = needclr;
20196
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020198 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199 {
20200 /*
20201 * Report the invalid expression unless the expression evaluation
20202 * has been cancelled due to an aborting error, an interrupt, or an
20203 * exception.
20204 */
20205 if (!aborting())
20206 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020207 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208 break;
20209 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020210 need_clr_eos = FALSE;
20211
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 if (!eap->skip)
20213 {
20214 if (atstart)
20215 {
20216 atstart = FALSE;
20217 /* Call msg_start() after eval1(), evaluating the expression
20218 * may cause a message to appear. */
20219 if (eap->cmdidx == CMD_echo)
20220 msg_start();
20221 }
20222 else if (eap->cmdidx == CMD_echo)
20223 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020224 current_copyID += COPYID_INC;
20225 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020226 if (p != NULL)
20227 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020229 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020231 if (*p != TAB && needclr)
20232 {
20233 /* remove any text still there from the command */
20234 msg_clr_eos();
20235 needclr = FALSE;
20236 }
20237 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 }
20239 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020240 {
20241#ifdef FEAT_MBYTE
20242 if (has_mbyte)
20243 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020244 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020245
20246 (void)msg_outtrans_len_attr(p, i, echo_attr);
20247 p += i - 1;
20248 }
20249 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020251 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020254 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020256 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257 arg = skipwhite(arg);
20258 }
20259 eap->nextcmd = check_nextcmd(arg);
20260
20261 if (eap->skip)
20262 --emsg_skip;
20263 else
20264 {
20265 /* remove text that may still be there from the command */
20266 if (needclr)
20267 msg_clr_eos();
20268 if (eap->cmdidx == CMD_echo)
20269 msg_end();
20270 }
20271}
20272
20273/*
20274 * ":echohl {name}".
20275 */
20276 void
20277ex_echohl(eap)
20278 exarg_T *eap;
20279{
20280 int id;
20281
20282 id = syn_name2id(eap->arg);
20283 if (id == 0)
20284 echo_attr = 0;
20285 else
20286 echo_attr = syn_id2attr(id);
20287}
20288
20289/*
20290 * ":execute expr1 ..." execute the result of an expression.
20291 * ":echomsg expr1 ..." Print a message
20292 * ":echoerr expr1 ..." Print an error
20293 * Each gets spaces around each argument and a newline at the end for
20294 * echo commands
20295 */
20296 void
20297ex_execute(eap)
20298 exarg_T *eap;
20299{
20300 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020301 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302 int ret = OK;
20303 char_u *p;
20304 garray_T ga;
20305 int len;
20306 int save_did_emsg;
20307
20308 ga_init2(&ga, 1, 80);
20309
20310 if (eap->skip)
20311 ++emsg_skip;
20312 while (*arg != NUL && *arg != '|' && *arg != '\n')
20313 {
20314 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020315 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316 {
20317 /*
20318 * Report the invalid expression unless the expression evaluation
20319 * has been cancelled due to an aborting error, an interrupt, or an
20320 * exception.
20321 */
20322 if (!aborting())
20323 EMSG2(_(e_invexpr2), p);
20324 ret = FAIL;
20325 break;
20326 }
20327
20328 if (!eap->skip)
20329 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020330 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331 len = (int)STRLEN(p);
20332 if (ga_grow(&ga, len + 2) == FAIL)
20333 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020334 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020335 ret = FAIL;
20336 break;
20337 }
20338 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020340 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341 ga.ga_len += len;
20342 }
20343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020344 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345 arg = skipwhite(arg);
20346 }
20347
20348 if (ret != FAIL && ga.ga_data != NULL)
20349 {
20350 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020351 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020353 out_flush();
20354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355 else if (eap->cmdidx == CMD_echoerr)
20356 {
20357 /* We don't want to abort following commands, restore did_emsg. */
20358 save_did_emsg = did_emsg;
20359 EMSG((char_u *)ga.ga_data);
20360 if (!force_abort)
20361 did_emsg = save_did_emsg;
20362 }
20363 else if (eap->cmdidx == CMD_execute)
20364 do_cmdline((char_u *)ga.ga_data,
20365 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20366 }
20367
20368 ga_clear(&ga);
20369
20370 if (eap->skip)
20371 --emsg_skip;
20372
20373 eap->nextcmd = check_nextcmd(arg);
20374}
20375
20376/*
20377 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20378 * "arg" points to the "&" or '+' when called, to "option" when returning.
20379 * Returns NULL when no option name found. Otherwise pointer to the char
20380 * after the option name.
20381 */
20382 static char_u *
20383find_option_end(arg, opt_flags)
20384 char_u **arg;
20385 int *opt_flags;
20386{
20387 char_u *p = *arg;
20388
20389 ++p;
20390 if (*p == 'g' && p[1] == ':')
20391 {
20392 *opt_flags = OPT_GLOBAL;
20393 p += 2;
20394 }
20395 else if (*p == 'l' && p[1] == ':')
20396 {
20397 *opt_flags = OPT_LOCAL;
20398 p += 2;
20399 }
20400 else
20401 *opt_flags = 0;
20402
20403 if (!ASCII_ISALPHA(*p))
20404 return NULL;
20405 *arg = p;
20406
20407 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20408 p += 4; /* termcap option */
20409 else
20410 while (ASCII_ISALPHA(*p))
20411 ++p;
20412 return p;
20413}
20414
20415/*
20416 * ":function"
20417 */
20418 void
20419ex_function(eap)
20420 exarg_T *eap;
20421{
20422 char_u *theline;
20423 int j;
20424 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020426 char_u *name = NULL;
20427 char_u *p;
20428 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020429 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430 garray_T newargs;
20431 garray_T newlines;
20432 int varargs = FALSE;
20433 int mustend = FALSE;
20434 int flags = 0;
20435 ufunc_T *fp;
20436 int indent;
20437 int nesting;
20438 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020439 dictitem_T *v;
20440 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020441 static int func_nr = 0; /* number for nameless function */
20442 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020443 hashtab_T *ht;
20444 int todo;
20445 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020446 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020447
20448 /*
20449 * ":function" without argument: list functions.
20450 */
20451 if (ends_excmd(*eap->arg))
20452 {
20453 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020454 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020455 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020456 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020457 {
20458 if (!HASHITEM_EMPTY(hi))
20459 {
20460 --todo;
20461 fp = HI2UF(hi);
20462 if (!isdigit(*fp->uf_name))
20463 list_func_head(fp, FALSE);
20464 }
20465 }
20466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467 eap->nextcmd = check_nextcmd(eap->arg);
20468 return;
20469 }
20470
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020471 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020472 * ":function /pat": list functions matching pattern.
20473 */
20474 if (*eap->arg == '/')
20475 {
20476 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20477 if (!eap->skip)
20478 {
20479 regmatch_T regmatch;
20480
20481 c = *p;
20482 *p = NUL;
20483 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20484 *p = c;
20485 if (regmatch.regprog != NULL)
20486 {
20487 regmatch.rm_ic = p_ic;
20488
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020489 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020490 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20491 {
20492 if (!HASHITEM_EMPTY(hi))
20493 {
20494 --todo;
20495 fp = HI2UF(hi);
20496 if (!isdigit(*fp->uf_name)
20497 && vim_regexec(&regmatch, fp->uf_name, 0))
20498 list_func_head(fp, FALSE);
20499 }
20500 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020501 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020502 }
20503 }
20504 if (*p == '/')
20505 ++p;
20506 eap->nextcmd = check_nextcmd(p);
20507 return;
20508 }
20509
20510 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020511 * Get the function name. There are these situations:
20512 * func normal function name
20513 * "name" == func, "fudi.fd_dict" == NULL
20514 * dict.func new dictionary entry
20515 * "name" == NULL, "fudi.fd_dict" set,
20516 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20517 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020518 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020519 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20520 * dict.func existing dict entry that's not a Funcref
20521 * "name" == NULL, "fudi.fd_dict" set,
20522 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020524 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020525 name = trans_function_name(&p, eap->skip, 0, &fudi);
20526 paren = (vim_strchr(p, '(') != NULL);
20527 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020528 {
20529 /*
20530 * Return on an invalid expression in braces, unless the expression
20531 * evaluation has been cancelled due to an aborting error, an
20532 * interrupt, or an exception.
20533 */
20534 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020535 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020536 if (!eap->skip && fudi.fd_newkey != NULL)
20537 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020538 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020541 else
20542 eap->skip = TRUE;
20543 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020544
Bram Moolenaar071d4272004-06-13 20:20:40 +000020545 /* An error in a function call during evaluation of an expression in magic
20546 * braces should not cause the function not to be defined. */
20547 saved_did_emsg = did_emsg;
20548 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020549
20550 /*
20551 * ":function func" with only function name: list function.
20552 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020553 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554 {
20555 if (!ends_excmd(*skipwhite(p)))
20556 {
20557 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020558 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559 }
20560 eap->nextcmd = check_nextcmd(p);
20561 if (eap->nextcmd != NULL)
20562 *p = NUL;
20563 if (!eap->skip && !got_int)
20564 {
20565 fp = find_func(name);
20566 if (fp != NULL)
20567 {
20568 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020569 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020570 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020571 if (FUNCLINE(fp, j) == NULL)
20572 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573 msg_putchar('\n');
20574 msg_outnum((long)(j + 1));
20575 if (j < 9)
20576 msg_putchar(' ');
20577 if (j < 99)
20578 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020579 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020580 out_flush(); /* show a line at a time */
20581 ui_breakcheck();
20582 }
20583 if (!got_int)
20584 {
20585 msg_putchar('\n');
20586 msg_puts((char_u *)" endfunction");
20587 }
20588 }
20589 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020590 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020592 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020593 }
20594
20595 /*
20596 * ":function name(arg1, arg2)" Define function.
20597 */
20598 p = skipwhite(p);
20599 if (*p != '(')
20600 {
20601 if (!eap->skip)
20602 {
20603 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020604 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605 }
20606 /* attempt to continue by skipping some text */
20607 if (vim_strchr(p, '(') != NULL)
20608 p = vim_strchr(p, '(');
20609 }
20610 p = skipwhite(p + 1);
20611
20612 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20613 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20614
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020615 if (!eap->skip)
20616 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020617 /* Check the name of the function. Unless it's a dictionary function
20618 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020619 if (name != NULL)
20620 arg = name;
20621 else
20622 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020623 if (arg != NULL && (fudi.fd_di == NULL
20624 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020625 {
20626 if (*arg == K_SPECIAL)
20627 j = 3;
20628 else
20629 j = 0;
20630 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20631 : eval_isnamec(arg[j])))
20632 ++j;
20633 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020634 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020635 }
20636 }
20637
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638 /*
20639 * Isolate the arguments: "arg1, arg2, ...)"
20640 */
20641 while (*p != ')')
20642 {
20643 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20644 {
20645 varargs = TRUE;
20646 p += 3;
20647 mustend = TRUE;
20648 }
20649 else
20650 {
20651 arg = p;
20652 while (ASCII_ISALNUM(*p) || *p == '_')
20653 ++p;
20654 if (arg == p || isdigit(*arg)
20655 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20656 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20657 {
20658 if (!eap->skip)
20659 EMSG2(_("E125: Illegal argument: %s"), arg);
20660 break;
20661 }
20662 if (ga_grow(&newargs, 1) == FAIL)
20663 goto erret;
20664 c = *p;
20665 *p = NUL;
20666 arg = vim_strsave(arg);
20667 if (arg == NULL)
20668 goto erret;
20669 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20670 *p = c;
20671 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020672 if (*p == ',')
20673 ++p;
20674 else
20675 mustend = TRUE;
20676 }
20677 p = skipwhite(p);
20678 if (mustend && *p != ')')
20679 {
20680 if (!eap->skip)
20681 EMSG2(_(e_invarg2), eap->arg);
20682 break;
20683 }
20684 }
20685 ++p; /* skip the ')' */
20686
Bram Moolenaare9a41262005-01-15 22:18:47 +000020687 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688 for (;;)
20689 {
20690 p = skipwhite(p);
20691 if (STRNCMP(p, "range", 5) == 0)
20692 {
20693 flags |= FC_RANGE;
20694 p += 5;
20695 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020696 else if (STRNCMP(p, "dict", 4) == 0)
20697 {
20698 flags |= FC_DICT;
20699 p += 4;
20700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020701 else if (STRNCMP(p, "abort", 5) == 0)
20702 {
20703 flags |= FC_ABORT;
20704 p += 5;
20705 }
20706 else
20707 break;
20708 }
20709
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020710 /* When there is a line break use what follows for the function body.
20711 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20712 if (*p == '\n')
20713 line_arg = p + 1;
20714 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715 EMSG(_(e_trailing));
20716
20717 /*
20718 * Read the body of the function, until ":endfunction" is found.
20719 */
20720 if (KeyTyped)
20721 {
20722 /* Check if the function already exists, don't let the user type the
20723 * whole function before telling him it doesn't work! For a script we
20724 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020725 if (!eap->skip && !eap->forceit)
20726 {
20727 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20728 EMSG(_(e_funcdict));
20729 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020730 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020732
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020733 if (!eap->skip && did_emsg)
20734 goto erret;
20735
Bram Moolenaar071d4272004-06-13 20:20:40 +000020736 msg_putchar('\n'); /* don't overwrite the function name */
20737 cmdline_row = msg_row;
20738 }
20739
20740 indent = 2;
20741 nesting = 0;
20742 for (;;)
20743 {
20744 msg_scroll = TRUE;
20745 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020746 sourcing_lnum_off = sourcing_lnum;
20747
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020748 if (line_arg != NULL)
20749 {
20750 /* Use eap->arg, split up in parts by line breaks. */
20751 theline = line_arg;
20752 p = vim_strchr(theline, '\n');
20753 if (p == NULL)
20754 line_arg += STRLEN(line_arg);
20755 else
20756 {
20757 *p = NUL;
20758 line_arg = p + 1;
20759 }
20760 }
20761 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762 theline = getcmdline(':', 0L, indent);
20763 else
20764 theline = eap->getline(':', eap->cookie, indent);
20765 if (KeyTyped)
20766 lines_left = Rows - 1;
20767 if (theline == NULL)
20768 {
20769 EMSG(_("E126: Missing :endfunction"));
20770 goto erret;
20771 }
20772
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020773 /* Detect line continuation: sourcing_lnum increased more than one. */
20774 if (sourcing_lnum > sourcing_lnum_off + 1)
20775 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20776 else
20777 sourcing_lnum_off = 0;
20778
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779 if (skip_until != NULL)
20780 {
20781 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20782 * don't check for ":endfunc". */
20783 if (STRCMP(theline, skip_until) == 0)
20784 {
20785 vim_free(skip_until);
20786 skip_until = NULL;
20787 }
20788 }
20789 else
20790 {
20791 /* skip ':' and blanks*/
20792 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20793 ;
20794
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020795 /* Check for "endfunction". */
20796 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020798 if (line_arg == NULL)
20799 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800 break;
20801 }
20802
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020803 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020804 * at "end". */
20805 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20806 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020807 else if (STRNCMP(p, "if", 2) == 0
20808 || STRNCMP(p, "wh", 2) == 0
20809 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810 || STRNCMP(p, "try", 3) == 0)
20811 indent += 2;
20812
20813 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020814 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020815 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020816 if (*p == '!')
20817 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020818 p += eval_fname_script(p);
20819 if (ASCII_ISALPHA(*p))
20820 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020821 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020822 if (*skipwhite(p) == '(')
20823 {
20824 ++nesting;
20825 indent += 2;
20826 }
20827 }
20828 }
20829
20830 /* Check for ":append" or ":insert". */
20831 p = skip_range(p, NULL);
20832 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20833 || (p[0] == 'i'
20834 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20835 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20836 skip_until = vim_strsave((char_u *)".");
20837
20838 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20839 arg = skipwhite(skiptowhite(p));
20840 if (arg[0] == '<' && arg[1] =='<'
20841 && ((p[0] == 'p' && p[1] == 'y'
20842 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20843 || (p[0] == 'p' && p[1] == 'e'
20844 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20845 || (p[0] == 't' && p[1] == 'c'
20846 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20847 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20848 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020849 || (p[0] == 'm' && p[1] == 'z'
20850 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851 ))
20852 {
20853 /* ":python <<" continues until a dot, like ":append" */
20854 p = skipwhite(arg + 2);
20855 if (*p == NUL)
20856 skip_until = vim_strsave((char_u *)".");
20857 else
20858 skip_until = vim_strsave(p);
20859 }
20860 }
20861
20862 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020863 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020864 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020865 if (line_arg == NULL)
20866 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020867 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020868 }
20869
20870 /* Copy the line to newly allocated memory. get_one_sourceline()
20871 * allocates 250 bytes per line, this saves 80% on average. The cost
20872 * is an extra alloc/free. */
20873 p = vim_strsave(theline);
20874 if (p != NULL)
20875 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020876 if (line_arg == NULL)
20877 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020878 theline = p;
20879 }
20880
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020881 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20882
20883 /* Add NULL lines for continuation lines, so that the line count is
20884 * equal to the index in the growarray. */
20885 while (sourcing_lnum_off-- > 0)
20886 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020887
20888 /* Check for end of eap->arg. */
20889 if (line_arg != NULL && *line_arg == NUL)
20890 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020891 }
20892
20893 /* Don't define the function when skipping commands or when an error was
20894 * detected. */
20895 if (eap->skip || did_emsg)
20896 goto erret;
20897
20898 /*
20899 * If there are no errors, add the function
20900 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020901 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020902 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020903 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020904 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020905 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020906 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020907 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020908 goto erret;
20909 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020910
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020911 fp = find_func(name);
20912 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020914 if (!eap->forceit)
20915 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020916 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020917 goto erret;
20918 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020919 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020920 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020921 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020922 name);
20923 goto erret;
20924 }
20925 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020926 ga_clear_strings(&(fp->uf_args));
20927 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020928 vim_free(name);
20929 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931 }
20932 else
20933 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020934 char numbuf[20];
20935
20936 fp = NULL;
20937 if (fudi.fd_newkey == NULL && !eap->forceit)
20938 {
20939 EMSG(_(e_funcdict));
20940 goto erret;
20941 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020942 if (fudi.fd_di == NULL)
20943 {
20944 /* Can't add a function to a locked dictionary */
20945 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20946 goto erret;
20947 }
20948 /* Can't change an existing function if it is locked */
20949 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20950 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020951
20952 /* Give the function a sequential number. Can only be used with a
20953 * Funcref! */
20954 vim_free(name);
20955 sprintf(numbuf, "%d", ++func_nr);
20956 name = vim_strsave((char_u *)numbuf);
20957 if (name == NULL)
20958 goto erret;
20959 }
20960
20961 if (fp == NULL)
20962 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020963 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020964 {
20965 int slen, plen;
20966 char_u *scriptname;
20967
20968 /* Check that the autoload name matches the script name. */
20969 j = FAIL;
20970 if (sourcing_name != NULL)
20971 {
20972 scriptname = autoload_name(name);
20973 if (scriptname != NULL)
20974 {
20975 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020976 plen = (int)STRLEN(p);
20977 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020978 if (slen > plen && fnamecmp(p,
20979 sourcing_name + slen - plen) == 0)
20980 j = OK;
20981 vim_free(scriptname);
20982 }
20983 }
20984 if (j == FAIL)
20985 {
20986 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20987 goto erret;
20988 }
20989 }
20990
20991 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992 if (fp == NULL)
20993 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020994
20995 if (fudi.fd_dict != NULL)
20996 {
20997 if (fudi.fd_di == NULL)
20998 {
20999 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021000 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021001 if (fudi.fd_di == NULL)
21002 {
21003 vim_free(fp);
21004 goto erret;
21005 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021006 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21007 {
21008 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021009 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021010 goto erret;
21011 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021012 }
21013 else
21014 /* overwrite existing dict entry */
21015 clear_tv(&fudi.fd_di->di_tv);
21016 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021017 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021018 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021019 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021020
21021 /* behave like "dict" was used */
21022 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021023 }
21024
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021026 STRCPY(fp->uf_name, name);
21027 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021028 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021029 fp->uf_args = newargs;
21030 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021031#ifdef FEAT_PROFILE
21032 fp->uf_tml_count = NULL;
21033 fp->uf_tml_total = NULL;
21034 fp->uf_tml_self = NULL;
21035 fp->uf_profiling = FALSE;
21036 if (prof_def_func())
21037 func_do_profile(fp);
21038#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021039 fp->uf_varargs = varargs;
21040 fp->uf_flags = flags;
21041 fp->uf_calls = 0;
21042 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021043 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021044
21045erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021046 ga_clear_strings(&newargs);
21047 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021048ret_free:
21049 vim_free(skip_until);
21050 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021051 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053}
21054
21055/*
21056 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021057 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021058 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021059 * flags:
21060 * TFN_INT: internal function name OK
21061 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021062 * Advances "pp" to just after the function name (if no error).
21063 */
21064 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021065trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021066 char_u **pp;
21067 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021068 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021069 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021070{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021071 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021072 char_u *start;
21073 char_u *end;
21074 int lead;
21075 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021076 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021077 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021078
21079 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021080 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021081 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021082
21083 /* Check for hard coded <SNR>: already translated function ID (from a user
21084 * command). */
21085 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21086 && (*pp)[2] == (int)KE_SNR)
21087 {
21088 *pp += 3;
21089 len = get_id_len(pp) + 3;
21090 return vim_strnsave(start, len);
21091 }
21092
21093 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21094 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021095 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021096 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021098
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021099 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21100 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021101 if (end == start)
21102 {
21103 if (!skip)
21104 EMSG(_("E129: Function name required"));
21105 goto theend;
21106 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021107 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021108 {
21109 /*
21110 * Report an invalid expression in braces, unless the expression
21111 * evaluation has been cancelled due to an aborting error, an
21112 * interrupt, or an exception.
21113 */
21114 if (!aborting())
21115 {
21116 if (end != NULL)
21117 EMSG2(_(e_invarg2), start);
21118 }
21119 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021120 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021121 goto theend;
21122 }
21123
21124 if (lv.ll_tv != NULL)
21125 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021126 if (fdp != NULL)
21127 {
21128 fdp->fd_dict = lv.ll_dict;
21129 fdp->fd_newkey = lv.ll_newkey;
21130 lv.ll_newkey = NULL;
21131 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021132 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021133 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21134 {
21135 name = vim_strsave(lv.ll_tv->vval.v_string);
21136 *pp = end;
21137 }
21138 else
21139 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021140 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21141 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021142 EMSG(_(e_funcref));
21143 else
21144 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021145 name = NULL;
21146 }
21147 goto theend;
21148 }
21149
21150 if (lv.ll_name == NULL)
21151 {
21152 /* Error found, but continue after the function name. */
21153 *pp = end;
21154 goto theend;
21155 }
21156
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021157 /* Check if the name is a Funcref. If so, use the value. */
21158 if (lv.ll_exp_name != NULL)
21159 {
21160 len = (int)STRLEN(lv.ll_exp_name);
21161 name = deref_func_name(lv.ll_exp_name, &len);
21162 if (name == lv.ll_exp_name)
21163 name = NULL;
21164 }
21165 else
21166 {
21167 len = (int)(end - *pp);
21168 name = deref_func_name(*pp, &len);
21169 if (name == *pp)
21170 name = NULL;
21171 }
21172 if (name != NULL)
21173 {
21174 name = vim_strsave(name);
21175 *pp = end;
21176 goto theend;
21177 }
21178
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021179 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021180 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021181 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021182 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21183 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21184 {
21185 /* When there was "s:" already or the name expanded to get a
21186 * leading "s:" then remove it. */
21187 lv.ll_name += 2;
21188 len -= 2;
21189 lead = 2;
21190 }
21191 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021192 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021193 {
21194 if (lead == 2) /* skip over "s:" */
21195 lv.ll_name += 2;
21196 len = (int)(end - lv.ll_name);
21197 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021198
21199 /*
21200 * Copy the function name to allocated memory.
21201 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21202 * Accept <SNR>123_name() outside a script.
21203 */
21204 if (skip)
21205 lead = 0; /* do nothing */
21206 else if (lead > 0)
21207 {
21208 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021209 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21210 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021211 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021212 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021213 if (current_SID <= 0)
21214 {
21215 EMSG(_(e_usingsid));
21216 goto theend;
21217 }
21218 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21219 lead += (int)STRLEN(sid_buf);
21220 }
21221 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021222 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021223 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021224 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021225 goto theend;
21226 }
21227 name = alloc((unsigned)(len + lead + 1));
21228 if (name != NULL)
21229 {
21230 if (lead > 0)
21231 {
21232 name[0] = K_SPECIAL;
21233 name[1] = KS_EXTRA;
21234 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021235 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021236 STRCPY(name + 3, sid_buf);
21237 }
21238 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21239 name[len + lead] = NUL;
21240 }
21241 *pp = end;
21242
21243theend:
21244 clear_lval(&lv);
21245 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021246}
21247
21248/*
21249 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21250 * Return 2 if "p" starts with "s:".
21251 * Return 0 otherwise.
21252 */
21253 static int
21254eval_fname_script(p)
21255 char_u *p;
21256{
21257 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21258 || STRNICMP(p + 1, "SNR>", 4) == 0))
21259 return 5;
21260 if (p[0] == 's' && p[1] == ':')
21261 return 2;
21262 return 0;
21263}
21264
21265/*
21266 * Return TRUE if "p" starts with "<SID>" or "s:".
21267 * Only works if eval_fname_script() returned non-zero for "p"!
21268 */
21269 static int
21270eval_fname_sid(p)
21271 char_u *p;
21272{
21273 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21274}
21275
21276/*
21277 * List the head of the function: "name(arg1, arg2)".
21278 */
21279 static void
21280list_func_head(fp, indent)
21281 ufunc_T *fp;
21282 int indent;
21283{
21284 int j;
21285
21286 msg_start();
21287 if (indent)
21288 MSG_PUTS(" ");
21289 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021290 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291 {
21292 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021293 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021294 }
21295 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021296 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021298 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021299 {
21300 if (j)
21301 MSG_PUTS(", ");
21302 msg_puts(FUNCARG(fp, j));
21303 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021304 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305 {
21306 if (j)
21307 MSG_PUTS(", ");
21308 MSG_PUTS("...");
21309 }
21310 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021311 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021312 if (p_verbose > 0)
21313 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314}
21315
21316/*
21317 * Find a function by name, return pointer to it in ufuncs.
21318 * Return NULL for unknown function.
21319 */
21320 static ufunc_T *
21321find_func(name)
21322 char_u *name;
21323{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021324 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021325
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021326 hi = hash_find(&func_hashtab, name);
21327 if (!HASHITEM_EMPTY(hi))
21328 return HI2UF(hi);
21329 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021330}
21331
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021332#if defined(EXITFREE) || defined(PROTO)
21333 void
21334free_all_functions()
21335{
21336 hashitem_T *hi;
21337
21338 /* Need to start all over every time, because func_free() may change the
21339 * hash table. */
21340 while (func_hashtab.ht_used > 0)
21341 for (hi = func_hashtab.ht_array; ; ++hi)
21342 if (!HASHITEM_EMPTY(hi))
21343 {
21344 func_free(HI2UF(hi));
21345 break;
21346 }
21347}
21348#endif
21349
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021350/*
21351 * Return TRUE if a function "name" exists.
21352 */
21353 static int
21354function_exists(name)
21355 char_u *name;
21356{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021357 char_u *nm = name;
21358 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021359 int n = FALSE;
21360
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021361 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021362 nm = skipwhite(nm);
21363
21364 /* Only accept "funcname", "funcname ", "funcname (..." and
21365 * "funcname(...", not "funcname!...". */
21366 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021367 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021368 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021369 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021370 else
21371 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021372 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021373 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021374 return n;
21375}
21376
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021377/*
21378 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021379 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021380 */
21381 static int
21382builtin_function(name)
21383 char_u *name;
21384{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021385 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21386 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021387}
21388
Bram Moolenaar05159a02005-02-26 23:04:13 +000021389#if defined(FEAT_PROFILE) || defined(PROTO)
21390/*
21391 * Start profiling function "fp".
21392 */
21393 static void
21394func_do_profile(fp)
21395 ufunc_T *fp;
21396{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021397 int len = fp->uf_lines.ga_len;
21398
21399 if (len == 0)
21400 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021401 fp->uf_tm_count = 0;
21402 profile_zero(&fp->uf_tm_self);
21403 profile_zero(&fp->uf_tm_total);
21404 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021405 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021406 if (fp->uf_tml_total == NULL)
21407 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021408 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021409 if (fp->uf_tml_self == NULL)
21410 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021411 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021412 fp->uf_tml_idx = -1;
21413 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21414 || fp->uf_tml_self == NULL)
21415 return; /* out of memory */
21416
21417 fp->uf_profiling = TRUE;
21418}
21419
21420/*
21421 * Dump the profiling results for all functions in file "fd".
21422 */
21423 void
21424func_dump_profile(fd)
21425 FILE *fd;
21426{
21427 hashitem_T *hi;
21428 int todo;
21429 ufunc_T *fp;
21430 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021431 ufunc_T **sorttab;
21432 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021433
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021434 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021435 if (todo == 0)
21436 return; /* nothing to dump */
21437
Bram Moolenaar73830342005-02-28 22:48:19 +000021438 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21439
Bram Moolenaar05159a02005-02-26 23:04:13 +000021440 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21441 {
21442 if (!HASHITEM_EMPTY(hi))
21443 {
21444 --todo;
21445 fp = HI2UF(hi);
21446 if (fp->uf_profiling)
21447 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021448 if (sorttab != NULL)
21449 sorttab[st_len++] = fp;
21450
Bram Moolenaar05159a02005-02-26 23:04:13 +000021451 if (fp->uf_name[0] == K_SPECIAL)
21452 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21453 else
21454 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21455 if (fp->uf_tm_count == 1)
21456 fprintf(fd, "Called 1 time\n");
21457 else
21458 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21459 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21460 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21461 fprintf(fd, "\n");
21462 fprintf(fd, "count total (s) self (s)\n");
21463
21464 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21465 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021466 if (FUNCLINE(fp, i) == NULL)
21467 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021468 prof_func_line(fd, fp->uf_tml_count[i],
21469 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021470 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21471 }
21472 fprintf(fd, "\n");
21473 }
21474 }
21475 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021476
21477 if (sorttab != NULL && st_len > 0)
21478 {
21479 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21480 prof_total_cmp);
21481 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21482 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21483 prof_self_cmp);
21484 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21485 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021486
21487 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021488}
Bram Moolenaar73830342005-02-28 22:48:19 +000021489
21490 static void
21491prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21492 FILE *fd;
21493 ufunc_T **sorttab;
21494 int st_len;
21495 char *title;
21496 int prefer_self; /* when equal print only self time */
21497{
21498 int i;
21499 ufunc_T *fp;
21500
21501 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21502 fprintf(fd, "count total (s) self (s) function\n");
21503 for (i = 0; i < 20 && i < st_len; ++i)
21504 {
21505 fp = sorttab[i];
21506 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21507 prefer_self);
21508 if (fp->uf_name[0] == K_SPECIAL)
21509 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21510 else
21511 fprintf(fd, " %s()\n", fp->uf_name);
21512 }
21513 fprintf(fd, "\n");
21514}
21515
21516/*
21517 * Print the count and times for one function or function line.
21518 */
21519 static void
21520prof_func_line(fd, count, total, self, prefer_self)
21521 FILE *fd;
21522 int count;
21523 proftime_T *total;
21524 proftime_T *self;
21525 int prefer_self; /* when equal print only self time */
21526{
21527 if (count > 0)
21528 {
21529 fprintf(fd, "%5d ", count);
21530 if (prefer_self && profile_equal(total, self))
21531 fprintf(fd, " ");
21532 else
21533 fprintf(fd, "%s ", profile_msg(total));
21534 if (!prefer_self && profile_equal(total, self))
21535 fprintf(fd, " ");
21536 else
21537 fprintf(fd, "%s ", profile_msg(self));
21538 }
21539 else
21540 fprintf(fd, " ");
21541}
21542
21543/*
21544 * Compare function for total time sorting.
21545 */
21546 static int
21547#ifdef __BORLANDC__
21548_RTLENTRYF
21549#endif
21550prof_total_cmp(s1, s2)
21551 const void *s1;
21552 const void *s2;
21553{
21554 ufunc_T *p1, *p2;
21555
21556 p1 = *(ufunc_T **)s1;
21557 p2 = *(ufunc_T **)s2;
21558 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21559}
21560
21561/*
21562 * Compare function for self time sorting.
21563 */
21564 static int
21565#ifdef __BORLANDC__
21566_RTLENTRYF
21567#endif
21568prof_self_cmp(s1, s2)
21569 const void *s1;
21570 const void *s2;
21571{
21572 ufunc_T *p1, *p2;
21573
21574 p1 = *(ufunc_T **)s1;
21575 p2 = *(ufunc_T **)s2;
21576 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21577}
21578
Bram Moolenaar05159a02005-02-26 23:04:13 +000021579#endif
21580
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021581/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021582 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021583 * Return TRUE if a package was loaded.
21584 */
21585 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021586script_autoload(name, reload)
21587 char_u *name;
21588 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021589{
21590 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021591 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021592 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021593 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021594
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021595 /* Return quickly when autoload disabled. */
21596 if (no_autoload)
21597 return FALSE;
21598
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021599 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021600 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021601 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021602 return FALSE;
21603
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021604 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021605
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021606 /* Find the name in the list of previously loaded package names. Skip
21607 * "autoload/", it's always the same. */
21608 for (i = 0; i < ga_loaded.ga_len; ++i)
21609 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21610 break;
21611 if (!reload && i < ga_loaded.ga_len)
21612 ret = FALSE; /* was loaded already */
21613 else
21614 {
21615 /* Remember the name if it wasn't loaded already. */
21616 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21617 {
21618 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21619 tofree = NULL;
21620 }
21621
21622 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021623 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021624 ret = TRUE;
21625 }
21626
21627 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021628 return ret;
21629}
21630
21631/*
21632 * Return the autoload script name for a function or variable name.
21633 * Returns NULL when out of memory.
21634 */
21635 static char_u *
21636autoload_name(name)
21637 char_u *name;
21638{
21639 char_u *p;
21640 char_u *scriptname;
21641
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021642 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021643 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21644 if (scriptname == NULL)
21645 return FALSE;
21646 STRCPY(scriptname, "autoload/");
21647 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021648 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021649 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021650 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021651 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021652 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021653}
21654
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21656
21657/*
21658 * Function given to ExpandGeneric() to obtain the list of user defined
21659 * function names.
21660 */
21661 char_u *
21662get_user_func_name(xp, idx)
21663 expand_T *xp;
21664 int idx;
21665{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021666 static long_u done;
21667 static hashitem_T *hi;
21668 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669
21670 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021671 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021672 done = 0;
21673 hi = func_hashtab.ht_array;
21674 }
21675 if (done < func_hashtab.ht_used)
21676 {
21677 if (done++ > 0)
21678 ++hi;
21679 while (HASHITEM_EMPTY(hi))
21680 ++hi;
21681 fp = HI2UF(hi);
21682
21683 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21684 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021685
21686 cat_func_name(IObuff, fp);
21687 if (xp->xp_context != EXPAND_USER_FUNC)
21688 {
21689 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021690 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691 STRCAT(IObuff, ")");
21692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693 return IObuff;
21694 }
21695 return NULL;
21696}
21697
21698#endif /* FEAT_CMDL_COMPL */
21699
21700/*
21701 * Copy the function name of "fp" to buffer "buf".
21702 * "buf" must be able to hold the function name plus three bytes.
21703 * Takes care of script-local function names.
21704 */
21705 static void
21706cat_func_name(buf, fp)
21707 char_u *buf;
21708 ufunc_T *fp;
21709{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021710 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021711 {
21712 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021713 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021714 }
21715 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021716 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021717}
21718
21719/*
21720 * ":delfunction {name}"
21721 */
21722 void
21723ex_delfunction(eap)
21724 exarg_T *eap;
21725{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021726 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727 char_u *p;
21728 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021729 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730
21731 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021732 name = trans_function_name(&p, eap->skip, 0, &fudi);
21733 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021735 {
21736 if (fudi.fd_dict != NULL && !eap->skip)
21737 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021738 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 if (!ends_excmd(*skipwhite(p)))
21741 {
21742 vim_free(name);
21743 EMSG(_(e_trailing));
21744 return;
21745 }
21746 eap->nextcmd = check_nextcmd(p);
21747 if (eap->nextcmd != NULL)
21748 *p = NUL;
21749
21750 if (!eap->skip)
21751 fp = find_func(name);
21752 vim_free(name);
21753
21754 if (!eap->skip)
21755 {
21756 if (fp == NULL)
21757 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021758 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 return;
21760 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021761 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021762 {
21763 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21764 return;
21765 }
21766
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021767 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021769 /* Delete the dict item that refers to the function, it will
21770 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021771 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021772 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021773 else
21774 func_free(fp);
21775 }
21776}
21777
21778/*
21779 * Free a function and remove it from the list of functions.
21780 */
21781 static void
21782func_free(fp)
21783 ufunc_T *fp;
21784{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021785 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021786
21787 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021788 ga_clear_strings(&(fp->uf_args));
21789 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021790#ifdef FEAT_PROFILE
21791 vim_free(fp->uf_tml_count);
21792 vim_free(fp->uf_tml_total);
21793 vim_free(fp->uf_tml_self);
21794#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021795
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021796 /* remove the function from the function hashtable */
21797 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21798 if (HASHITEM_EMPTY(hi))
21799 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021800 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021801 hash_remove(&func_hashtab, hi);
21802
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021803 vim_free(fp);
21804}
21805
21806/*
21807 * Unreference a Function: decrement the reference count and free it when it
21808 * becomes zero. Only for numbered functions.
21809 */
21810 static void
21811func_unref(name)
21812 char_u *name;
21813{
21814 ufunc_T *fp;
21815
21816 if (name != NULL && isdigit(*name))
21817 {
21818 fp = find_func(name);
21819 if (fp == NULL)
21820 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021821 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021822 {
21823 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021824 * when "uf_calls" becomes zero. */
21825 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021826 func_free(fp);
21827 }
21828 }
21829}
21830
21831/*
21832 * Count a reference to a Function.
21833 */
21834 static void
21835func_ref(name)
21836 char_u *name;
21837{
21838 ufunc_T *fp;
21839
21840 if (name != NULL && isdigit(*name))
21841 {
21842 fp = find_func(name);
21843 if (fp == NULL)
21844 EMSG2(_(e_intern2), "func_ref()");
21845 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021846 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847 }
21848}
21849
21850/*
21851 * Call a user function.
21852 */
21853 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021854call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021855 ufunc_T *fp; /* pointer to function */
21856 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021857 typval_T *argvars; /* arguments */
21858 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859 linenr_T firstline; /* first line of range */
21860 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021861 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021862{
Bram Moolenaar33570922005-01-25 22:26:29 +000021863 char_u *save_sourcing_name;
21864 linenr_T save_sourcing_lnum;
21865 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021866 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021867 int save_did_emsg;
21868 static int depth = 0;
21869 dictitem_T *v;
21870 int fixvar_idx = 0; /* index in fixvar[] */
21871 int i;
21872 int ai;
21873 char_u numbuf[NUMBUFLEN];
21874 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021875#ifdef FEAT_PROFILE
21876 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021877 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021879
21880 /* If depth of calling is getting too high, don't execute the function */
21881 if (depth >= p_mfd)
21882 {
21883 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021884 rettv->v_type = VAR_NUMBER;
21885 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886 return;
21887 }
21888 ++depth;
21889
21890 line_breakcheck(); /* check for CTRL-C hit */
21891
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021892 fc = (funccall_T *)alloc(sizeof(funccall_T));
21893 fc->caller = current_funccal;
21894 current_funccal = fc;
21895 fc->func = fp;
21896 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021897 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021898 fc->linenr = 0;
21899 fc->returned = FALSE;
21900 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021901 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021902 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21903 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021904
Bram Moolenaar33570922005-01-25 22:26:29 +000021905 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021906 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021907 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21908 * each argument variable and saves a lot of time.
21909 */
21910 /*
21911 * Init l: variables.
21912 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021913 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021914 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021915 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021916 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21917 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021918 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021919 name = v->di_key;
21920 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021921 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021922 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021923 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021924 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021925 v->di_tv.vval.v_dict = selfdict;
21926 ++selfdict->dv_refcount;
21927 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021928
Bram Moolenaar33570922005-01-25 22:26:29 +000021929 /*
21930 * Init a: variables.
21931 * Set a:0 to "argcount".
21932 * Set a:000 to a list with room for the "..." arguments.
21933 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021934 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21935 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021936 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021937 /* Use "name" to avoid a warning from some compiler that checks the
21938 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021939 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021940 name = v->di_key;
21941 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021942 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021943 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021944 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021945 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021946 v->di_tv.vval.v_list = &fc->l_varlist;
21947 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21948 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21949 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021950
21951 /*
21952 * Set a:firstline to "firstline" and a:lastline to "lastline".
21953 * Set a:name to named arguments.
21954 * Set a:N to the "..." arguments.
21955 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021956 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021957 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021958 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021959 (varnumber_T)lastline);
21960 for (i = 0; i < argcount; ++i)
21961 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021962 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021963 if (ai < 0)
21964 /* named argument a:name */
21965 name = FUNCARG(fp, i);
21966 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021967 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021968 /* "..." argument a:1, a:2, etc. */
21969 sprintf((char *)numbuf, "%d", ai + 1);
21970 name = numbuf;
21971 }
21972 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21973 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021974 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021975 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21976 }
21977 else
21978 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021979 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21980 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021981 if (v == NULL)
21982 break;
21983 v->di_flags = DI_FLAGS_RO;
21984 }
21985 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021986 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021987
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021988 /* Note: the values are copied directly to avoid alloc/free.
21989 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021990 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021991 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021992
21993 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21994 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021995 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21996 fc->l_listitems[ai].li_tv = argvars[i];
21997 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021998 }
21999 }
22000
Bram Moolenaar071d4272004-06-13 20:20:40 +000022001 /* Don't redraw while executing the function. */
22002 ++RedrawingDisabled;
22003 save_sourcing_name = sourcing_name;
22004 save_sourcing_lnum = sourcing_lnum;
22005 sourcing_lnum = 1;
22006 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022007 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008 if (sourcing_name != NULL)
22009 {
22010 if (save_sourcing_name != NULL
22011 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22012 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22013 else
22014 STRCPY(sourcing_name, "function ");
22015 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22016
22017 if (p_verbose >= 12)
22018 {
22019 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022020 verbose_enter_scroll();
22021
Bram Moolenaar555b2802005-05-19 21:08:39 +000022022 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023 if (p_verbose >= 14)
22024 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022026 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022027 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022028 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029
22030 msg_puts((char_u *)"(");
22031 for (i = 0; i < argcount; ++i)
22032 {
22033 if (i > 0)
22034 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022035 if (argvars[i].v_type == VAR_NUMBER)
22036 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022037 else
22038 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022039 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22040 if (s != NULL)
22041 {
22042 trunc_string(s, buf, MSG_BUF_CLEN);
22043 msg_puts(buf);
22044 vim_free(tofree);
22045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046 }
22047 }
22048 msg_puts((char_u *)")");
22049 }
22050 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022051
22052 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022053 --no_wait_return;
22054 }
22055 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022056#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022057 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022058 {
22059 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22060 func_do_profile(fp);
22061 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022062 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022063 {
22064 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022065 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022066 profile_zero(&fp->uf_tm_children);
22067 }
22068 script_prof_save(&wait_start);
22069 }
22070#endif
22071
Bram Moolenaar071d4272004-06-13 20:20:40 +000022072 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022073 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074 save_did_emsg = did_emsg;
22075 did_emsg = FALSE;
22076
22077 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022078 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022079 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22080
22081 --RedrawingDisabled;
22082
22083 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022084 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022086 clear_tv(rettv);
22087 rettv->v_type = VAR_NUMBER;
22088 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022089 }
22090
Bram Moolenaar05159a02005-02-26 23:04:13 +000022091#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022092 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022093 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022094 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022095 profile_end(&call_start);
22096 profile_sub_wait(&wait_start, &call_start);
22097 profile_add(&fp->uf_tm_total, &call_start);
22098 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022099 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022100 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022101 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22102 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022103 }
22104 }
22105#endif
22106
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107 /* when being verbose, mention the return value */
22108 if (p_verbose >= 12)
22109 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022110 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022111 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112
Bram Moolenaar071d4272004-06-13 20:20:40 +000022113 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022114 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022115 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022116 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022117 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022118 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022120 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022121 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022122 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022123 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022124
Bram Moolenaar555b2802005-05-19 21:08:39 +000022125 /* The value may be very long. Skip the middle part, so that we
22126 * have some idea how it starts and ends. smsg() would always
22127 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022128 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022129 if (s != NULL)
22130 {
22131 trunc_string(s, buf, MSG_BUF_CLEN);
22132 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
22133 vim_free(tofree);
22134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135 }
22136 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022137
22138 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139 --no_wait_return;
22140 }
22141
22142 vim_free(sourcing_name);
22143 sourcing_name = save_sourcing_name;
22144 sourcing_lnum = save_sourcing_lnum;
22145 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022146#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022147 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022148 script_prof_restore(&wait_start);
22149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150
22151 if (p_verbose >= 12 && sourcing_name != NULL)
22152 {
22153 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022154 verbose_enter_scroll();
22155
Bram Moolenaar555b2802005-05-19 21:08:39 +000022156 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022157 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022158
22159 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022160 --no_wait_return;
22161 }
22162
22163 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022164 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022165 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022166
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022167 /* If the a:000 list and the l: and a: dicts are not referenced we can
22168 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022169 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22170 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22171 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22172 {
22173 free_funccal(fc, FALSE);
22174 }
22175 else
22176 {
22177 hashitem_T *hi;
22178 listitem_T *li;
22179 int todo;
22180
22181 /* "fc" is still in use. This can happen when returning "a:000" or
22182 * assigning "l:" to a global variable.
22183 * Link "fc" in the list for garbage collection later. */
22184 fc->caller = previous_funccal;
22185 previous_funccal = fc;
22186
22187 /* Make a copy of the a: variables, since we didn't do that above. */
22188 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22189 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22190 {
22191 if (!HASHITEM_EMPTY(hi))
22192 {
22193 --todo;
22194 v = HI2DI(hi);
22195 copy_tv(&v->di_tv, &v->di_tv);
22196 }
22197 }
22198
22199 /* Make a copy of the a:000 items, since we didn't do that above. */
22200 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22201 copy_tv(&li->li_tv, &li->li_tv);
22202 }
22203}
22204
22205/*
22206 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022207 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022208 */
22209 static int
22210can_free_funccal(fc, copyID)
22211 funccall_T *fc;
22212 int copyID;
22213{
22214 return (fc->l_varlist.lv_copyID != copyID
22215 && fc->l_vars.dv_copyID != copyID
22216 && fc->l_avars.dv_copyID != copyID);
22217}
22218
22219/*
22220 * Free "fc" and what it contains.
22221 */
22222 static void
22223free_funccal(fc, free_val)
22224 funccall_T *fc;
22225 int free_val; /* a: vars were allocated */
22226{
22227 listitem_T *li;
22228
22229 /* The a: variables typevals may not have been allocated, only free the
22230 * allocated variables. */
22231 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22232
22233 /* free all l: variables */
22234 vars_clear(&fc->l_vars.dv_hashtab);
22235
22236 /* Free the a:000 variables if they were allocated. */
22237 if (free_val)
22238 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22239 clear_tv(&li->li_tv);
22240
22241 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242}
22243
22244/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022245 * Add a number variable "name" to dict "dp" with value "nr".
22246 */
22247 static void
22248add_nr_var(dp, v, name, nr)
22249 dict_T *dp;
22250 dictitem_T *v;
22251 char *name;
22252 varnumber_T nr;
22253{
22254 STRCPY(v->di_key, name);
22255 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22256 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22257 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022258 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022259 v->di_tv.vval.v_number = nr;
22260}
22261
22262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022263 * ":return [expr]"
22264 */
22265 void
22266ex_return(eap)
22267 exarg_T *eap;
22268{
22269 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022270 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022271 int returning = FALSE;
22272
22273 if (current_funccal == NULL)
22274 {
22275 EMSG(_("E133: :return not inside a function"));
22276 return;
22277 }
22278
22279 if (eap->skip)
22280 ++emsg_skip;
22281
22282 eap->nextcmd = NULL;
22283 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022284 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022285 {
22286 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022287 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022289 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290 }
22291 /* It's safer to return also on error. */
22292 else if (!eap->skip)
22293 {
22294 /*
22295 * Return unless the expression evaluation has been cancelled due to an
22296 * aborting error, an interrupt, or an exception.
22297 */
22298 if (!aborting())
22299 returning = do_return(eap, FALSE, TRUE, NULL);
22300 }
22301
22302 /* When skipping or the return gets pending, advance to the next command
22303 * in this line (!returning). Otherwise, ignore the rest of the line.
22304 * Following lines will be ignored by get_func_line(). */
22305 if (returning)
22306 eap->nextcmd = NULL;
22307 else if (eap->nextcmd == NULL) /* no argument */
22308 eap->nextcmd = check_nextcmd(arg);
22309
22310 if (eap->skip)
22311 --emsg_skip;
22312}
22313
22314/*
22315 * Return from a function. Possibly makes the return pending. Also called
22316 * for a pending return at the ":endtry" or after returning from an extra
22317 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022318 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022319 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022320 * FALSE when the return gets pending.
22321 */
22322 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022323do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022324 exarg_T *eap;
22325 int reanimate;
22326 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022327 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022328{
22329 int idx;
22330 struct condstack *cstack = eap->cstack;
22331
22332 if (reanimate)
22333 /* Undo the return. */
22334 current_funccal->returned = FALSE;
22335
22336 /*
22337 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22338 * not in its finally clause (which then is to be executed next) is found.
22339 * In this case, make the ":return" pending for execution at the ":endtry".
22340 * Otherwise, return normally.
22341 */
22342 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22343 if (idx >= 0)
22344 {
22345 cstack->cs_pending[idx] = CSTP_RETURN;
22346
22347 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022348 /* A pending return again gets pending. "rettv" points to an
22349 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022351 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022352 else
22353 {
22354 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022355 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022357 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022358
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022359 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022360 {
22361 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022362 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022363 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022364 else
22365 EMSG(_(e_outofmem));
22366 }
22367 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022368 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369
22370 if (reanimate)
22371 {
22372 /* The pending return value could be overwritten by a ":return"
22373 * without argument in a finally clause; reset the default
22374 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022375 current_funccal->rettv->v_type = VAR_NUMBER;
22376 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022377 }
22378 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022379 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380 }
22381 else
22382 {
22383 current_funccal->returned = TRUE;
22384
22385 /* If the return is carried out now, store the return value. For
22386 * a return immediately after reanimation, the value is already
22387 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022388 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022389 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022390 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022391 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022392 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022393 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022394 }
22395 }
22396
22397 return idx < 0;
22398}
22399
22400/*
22401 * Free the variable with a pending return value.
22402 */
22403 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022404discard_pending_return(rettv)
22405 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406{
Bram Moolenaar33570922005-01-25 22:26:29 +000022407 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408}
22409
22410/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022411 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022412 * is an allocated string. Used by report_pending() for verbose messages.
22413 */
22414 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022415get_return_cmd(rettv)
22416 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022417{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022418 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022419 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022420 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022421
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022422 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022423 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022424 if (s == NULL)
22425 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022426
22427 STRCPY(IObuff, ":return ");
22428 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22429 if (STRLEN(s) + 8 >= IOSIZE)
22430 STRCPY(IObuff + IOSIZE - 4, "...");
22431 vim_free(tofree);
22432 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022433}
22434
22435/*
22436 * Get next function line.
22437 * Called by do_cmdline() to get the next line.
22438 * Returns allocated string, or NULL for end of function.
22439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022440 char_u *
22441get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022442 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022444 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445{
Bram Moolenaar33570922005-01-25 22:26:29 +000022446 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022447 ufunc_T *fp = fcp->func;
22448 char_u *retval;
22449 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450
22451 /* If breakpoints have been added/deleted need to check for it. */
22452 if (fcp->dbg_tick != debug_tick)
22453 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022454 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455 sourcing_lnum);
22456 fcp->dbg_tick = debug_tick;
22457 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022458#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022459 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022460 func_line_end(cookie);
22461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022462
Bram Moolenaar05159a02005-02-26 23:04:13 +000022463 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022464 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22465 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022466 retval = NULL;
22467 else
22468 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022469 /* Skip NULL lines (continuation lines). */
22470 while (fcp->linenr < gap->ga_len
22471 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22472 ++fcp->linenr;
22473 if (fcp->linenr >= gap->ga_len)
22474 retval = NULL;
22475 else
22476 {
22477 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22478 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022479#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022480 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022481 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022482#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484 }
22485
22486 /* Did we encounter a breakpoint? */
22487 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22488 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022489 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022490 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022491 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022492 sourcing_lnum);
22493 fcp->dbg_tick = debug_tick;
22494 }
22495
22496 return retval;
22497}
22498
Bram Moolenaar05159a02005-02-26 23:04:13 +000022499#if defined(FEAT_PROFILE) || defined(PROTO)
22500/*
22501 * Called when starting to read a function line.
22502 * "sourcing_lnum" must be correct!
22503 * When skipping lines it may not actually be executed, but we won't find out
22504 * until later and we need to store the time now.
22505 */
22506 void
22507func_line_start(cookie)
22508 void *cookie;
22509{
22510 funccall_T *fcp = (funccall_T *)cookie;
22511 ufunc_T *fp = fcp->func;
22512
22513 if (fp->uf_profiling && sourcing_lnum >= 1
22514 && sourcing_lnum <= fp->uf_lines.ga_len)
22515 {
22516 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022517 /* Skip continuation lines. */
22518 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22519 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022520 fp->uf_tml_execed = FALSE;
22521 profile_start(&fp->uf_tml_start);
22522 profile_zero(&fp->uf_tml_children);
22523 profile_get_wait(&fp->uf_tml_wait);
22524 }
22525}
22526
22527/*
22528 * Called when actually executing a function line.
22529 */
22530 void
22531func_line_exec(cookie)
22532 void *cookie;
22533{
22534 funccall_T *fcp = (funccall_T *)cookie;
22535 ufunc_T *fp = fcp->func;
22536
22537 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22538 fp->uf_tml_execed = TRUE;
22539}
22540
22541/*
22542 * Called when done with a function line.
22543 */
22544 void
22545func_line_end(cookie)
22546 void *cookie;
22547{
22548 funccall_T *fcp = (funccall_T *)cookie;
22549 ufunc_T *fp = fcp->func;
22550
22551 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22552 {
22553 if (fp->uf_tml_execed)
22554 {
22555 ++fp->uf_tml_count[fp->uf_tml_idx];
22556 profile_end(&fp->uf_tml_start);
22557 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022558 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022559 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22560 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022561 }
22562 fp->uf_tml_idx = -1;
22563 }
22564}
22565#endif
22566
Bram Moolenaar071d4272004-06-13 20:20:40 +000022567/*
22568 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022569 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570 */
22571 int
22572func_has_ended(cookie)
22573 void *cookie;
22574{
Bram Moolenaar33570922005-01-25 22:26:29 +000022575 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576
22577 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22578 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022579 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580 || fcp->returned);
22581}
22582
22583/*
22584 * return TRUE if cookie indicates a function which "abort"s on errors.
22585 */
22586 int
22587func_has_abort(cookie)
22588 void *cookie;
22589{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022590 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022591}
22592
22593#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22594typedef enum
22595{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022596 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22597 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22598 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599} var_flavour_T;
22600
22601static var_flavour_T var_flavour __ARGS((char_u *varname));
22602
22603 static var_flavour_T
22604var_flavour(varname)
22605 char_u *varname;
22606{
22607 char_u *p = varname;
22608
22609 if (ASCII_ISUPPER(*p))
22610 {
22611 while (*(++p))
22612 if (ASCII_ISLOWER(*p))
22613 return VAR_FLAVOUR_SESSION;
22614 return VAR_FLAVOUR_VIMINFO;
22615 }
22616 else
22617 return VAR_FLAVOUR_DEFAULT;
22618}
22619#endif
22620
22621#if defined(FEAT_VIMINFO) || defined(PROTO)
22622/*
22623 * Restore global vars that start with a capital from the viminfo file
22624 */
22625 int
22626read_viminfo_varlist(virp, writing)
22627 vir_T *virp;
22628 int writing;
22629{
22630 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022631 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022632 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633
22634 if (!writing && (find_viminfo_parameter('!') != NULL))
22635 {
22636 tab = vim_strchr(virp->vir_line + 1, '\t');
22637 if (tab != NULL)
22638 {
22639 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022640 switch (*tab)
22641 {
22642 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022643#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022644 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022645#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022646 case 'D': type = VAR_DICT; break;
22647 case 'L': type = VAR_LIST; break;
22648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022649
22650 tab = vim_strchr(tab, '\t');
22651 if (tab != NULL)
22652 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022653 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022654 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022655 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022657#ifdef FEAT_FLOAT
22658 else if (type == VAR_FLOAT)
22659 (void)string2float(tab + 1, &tv.vval.v_float);
22660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022661 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022662 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022663 if (type == VAR_DICT || type == VAR_LIST)
22664 {
22665 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
22666
22667 if (etv == NULL)
22668 /* Failed to parse back the dict or list, use it as a
22669 * string. */
22670 tv.v_type = VAR_STRING;
22671 else
22672 {
22673 vim_free(tv.vval.v_string);
22674 tv = *etv;
22675 }
22676 }
22677
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022678 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022679
22680 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022681 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022682 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
22683 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022684 }
22685 }
22686 }
22687
22688 return viminfo_readline(virp);
22689}
22690
22691/*
22692 * Write global vars that start with a capital to the viminfo file
22693 */
22694 void
22695write_viminfo_varlist(fp)
22696 FILE *fp;
22697{
Bram Moolenaar33570922005-01-25 22:26:29 +000022698 hashitem_T *hi;
22699 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022700 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022701 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022702 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022703 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022704 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705
22706 if (find_viminfo_parameter('!') == NULL)
22707 return;
22708
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022709 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022710
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022711 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022712 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022713 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022714 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022716 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022717 this_var = HI2DI(hi);
22718 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022719 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022720 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022721 {
22722 case VAR_STRING: s = "STR"; break;
22723 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022724#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022725 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022726#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022727 case VAR_DICT: s = "DIC"; break;
22728 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000022729 default: continue;
22730 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022731 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022732 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022733 if (p != NULL)
22734 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022735 vim_free(tofree);
22736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022737 }
22738 }
22739}
22740#endif
22741
22742#if defined(FEAT_SESSION) || defined(PROTO)
22743 int
22744store_session_globals(fd)
22745 FILE *fd;
22746{
Bram Moolenaar33570922005-01-25 22:26:29 +000022747 hashitem_T *hi;
22748 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022749 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750 char_u *p, *t;
22751
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022752 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022753 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022755 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022756 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022757 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022758 this_var = HI2DI(hi);
22759 if ((this_var->di_tv.v_type == VAR_NUMBER
22760 || this_var->di_tv.v_type == VAR_STRING)
22761 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022762 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022763 /* Escape special characters with a backslash. Turn a LF and
22764 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022765 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022766 (char_u *)"\\\"\n\r");
22767 if (p == NULL) /* out of memory */
22768 break;
22769 for (t = p; *t != NUL; ++t)
22770 if (*t == '\n')
22771 *t = 'n';
22772 else if (*t == '\r')
22773 *t = 'r';
22774 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022775 this_var->di_key,
22776 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22777 : ' ',
22778 p,
22779 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22780 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022781 || put_eol(fd) == FAIL)
22782 {
22783 vim_free(p);
22784 return FAIL;
22785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022786 vim_free(p);
22787 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022788#ifdef FEAT_FLOAT
22789 else if (this_var->di_tv.v_type == VAR_FLOAT
22790 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22791 {
22792 float_T f = this_var->di_tv.vval.v_float;
22793 int sign = ' ';
22794
22795 if (f < 0)
22796 {
22797 f = -f;
22798 sign = '-';
22799 }
22800 if ((fprintf(fd, "let %s = %c&%f",
22801 this_var->di_key, sign, f) < 0)
22802 || put_eol(fd) == FAIL)
22803 return FAIL;
22804 }
22805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022806 }
22807 }
22808 return OK;
22809}
22810#endif
22811
Bram Moolenaar661b1822005-07-28 22:36:45 +000022812/*
22813 * Display script name where an item was last set.
22814 * Should only be invoked when 'verbose' is non-zero.
22815 */
22816 void
22817last_set_msg(scriptID)
22818 scid_T scriptID;
22819{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022820 char_u *p;
22821
Bram Moolenaar661b1822005-07-28 22:36:45 +000022822 if (scriptID != 0)
22823 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022824 p = home_replace_save(NULL, get_scriptname(scriptID));
22825 if (p != NULL)
22826 {
22827 verbose_enter();
22828 MSG_PUTS(_("\n\tLast set from "));
22829 MSG_PUTS(p);
22830 vim_free(p);
22831 verbose_leave();
22832 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022833 }
22834}
22835
Bram Moolenaard812df62008-11-09 12:46:09 +000022836/*
22837 * List v:oldfiles in a nice way.
22838 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022839 void
22840ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022841 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022842{
22843 list_T *l = vimvars[VV_OLDFILES].vv_list;
22844 listitem_T *li;
22845 int nr = 0;
22846
22847 if (l == NULL)
22848 msg((char_u *)_("No old files"));
22849 else
22850 {
22851 msg_start();
22852 msg_scroll = TRUE;
22853 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22854 {
22855 msg_outnum((long)++nr);
22856 MSG_PUTS(": ");
22857 msg_outtrans(get_tv_string(&li->li_tv));
22858 msg_putchar('\n');
22859 out_flush(); /* output one line at a time */
22860 ui_breakcheck();
22861 }
22862 /* Assume "got_int" was set to truncate the listing. */
22863 got_int = FALSE;
22864
22865#ifdef FEAT_BROWSE_CMD
22866 if (cmdmod.browse)
22867 {
22868 quit_more = FALSE;
22869 nr = prompt_for_number(FALSE);
22870 msg_starthere();
22871 if (nr > 0)
22872 {
22873 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22874 (long)nr);
22875
22876 if (p != NULL)
22877 {
22878 p = expand_env_save(p);
22879 eap->arg = p;
22880 eap->cmdidx = CMD_edit;
22881 cmdmod.browse = FALSE;
22882 do_exedit(eap, NULL);
22883 vim_free(p);
22884 }
22885 }
22886 }
22887#endif
22888 }
22889}
22890
Bram Moolenaar071d4272004-06-13 20:20:40 +000022891#endif /* FEAT_EVAL */
22892
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022894#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895
22896#ifdef WIN3264
22897/*
22898 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22899 */
22900static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22901static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22902static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22903
22904/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022905 * Get the short path (8.3) for the filename in "fnamep".
22906 * Only works for a valid file name.
22907 * When the path gets longer "fnamep" is changed and the allocated buffer
22908 * is put in "bufp".
22909 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22910 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911 */
22912 static int
22913get_short_pathname(fnamep, bufp, fnamelen)
22914 char_u **fnamep;
22915 char_u **bufp;
22916 int *fnamelen;
22917{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022918 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919 char_u *newbuf;
22920
22921 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022922 l = GetShortPathName(*fnamep, *fnamep, len);
22923 if (l > len - 1)
22924 {
22925 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022926 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927 newbuf = vim_strnsave(*fnamep, l);
22928 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022929 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022930
22931 vim_free(*bufp);
22932 *fnamep = *bufp = newbuf;
22933
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022934 /* Really should always succeed, as the buffer is big enough. */
22935 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936 }
22937
22938 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022939 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022940}
22941
22942/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022943 * Get the short path (8.3) for the filename in "fname". The converted
22944 * path is returned in "bufp".
22945 *
22946 * Some of the directories specified in "fname" may not exist. This function
22947 * will shorten the existing directories at the beginning of the path and then
22948 * append the remaining non-existing path.
22949 *
22950 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022951 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022952 * bufp - Pointer to an allocated buffer for the filename.
22953 * fnamelen - Length of the filename pointed to by fname
22954 *
22955 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022956 */
22957 static int
22958shortpath_for_invalid_fname(fname, bufp, fnamelen)
22959 char_u **fname;
22960 char_u **bufp;
22961 int *fnamelen;
22962{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022963 char_u *short_fname, *save_fname, *pbuf_unused;
22964 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022966 int old_len, len;
22967 int new_len, sfx_len;
22968 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022969
22970 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022971 old_len = *fnamelen;
22972 save_fname = vim_strnsave(*fname, old_len);
22973 pbuf_unused = NULL;
22974 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022976 endp = save_fname + old_len - 1; /* Find the end of the copy */
22977 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022979 /*
22980 * Try shortening the supplied path till it succeeds by removing one
22981 * directory at a time from the tail of the path.
22982 */
22983 len = 0;
22984 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022986 /* go back one path-separator */
22987 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22988 --endp;
22989 if (endp <= save_fname)
22990 break; /* processed the complete path */
22991
22992 /*
22993 * Replace the path separator with a NUL and try to shorten the
22994 * resulting path.
22995 */
22996 ch = *endp;
22997 *endp = 0;
22998 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022999 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023000 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23001 {
23002 retval = FAIL;
23003 goto theend;
23004 }
23005 *endp = ch; /* preserve the string */
23006
23007 if (len > 0)
23008 break; /* successfully shortened the path */
23009
23010 /* failed to shorten the path. Skip the path separator */
23011 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012 }
23013
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023014 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023015 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023016 /*
23017 * Succeeded in shortening the path. Now concatenate the shortened
23018 * path with the remaining path at the tail.
23019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023020
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023021 /* Compute the length of the new path. */
23022 sfx_len = (int)(save_endp - endp) + 1;
23023 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023024
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023025 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023026 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023027 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023028 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023029 /* There is not enough space in the currently allocated string,
23030 * copy it to a buffer big enough. */
23031 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023032 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023033 {
23034 retval = FAIL;
23035 goto theend;
23036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023037 }
23038 else
23039 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023040 /* Transfer short_fname to the main buffer (it's big enough),
23041 * unless get_short_pathname() did its work in-place. */
23042 *fname = *bufp = save_fname;
23043 if (short_fname != save_fname)
23044 vim_strncpy(save_fname, short_fname, len);
23045 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023047
23048 /* concat the not-shortened part of the path */
23049 vim_strncpy(*fname + len, endp, sfx_len);
23050 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023051 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023052
23053theend:
23054 vim_free(pbuf_unused);
23055 vim_free(save_fname);
23056
23057 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023058}
23059
23060/*
23061 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023062 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023063 */
23064 static int
23065shortpath_for_partial(fnamep, bufp, fnamelen)
23066 char_u **fnamep;
23067 char_u **bufp;
23068 int *fnamelen;
23069{
23070 int sepcount, len, tflen;
23071 char_u *p;
23072 char_u *pbuf, *tfname;
23073 int hasTilde;
23074
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023075 /* Count up the path separators from the RHS.. so we know which part
23076 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023077 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023078 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023079 if (vim_ispathsep(*p))
23080 ++sepcount;
23081
23082 /* Need full path first (use expand_env() to remove a "~/") */
23083 hasTilde = (**fnamep == '~');
23084 if (hasTilde)
23085 pbuf = tfname = expand_env_save(*fnamep);
23086 else
23087 pbuf = tfname = FullName_save(*fnamep, FALSE);
23088
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023089 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023090
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023091 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23092 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023093
23094 if (len == 0)
23095 {
23096 /* Don't have a valid filename, so shorten the rest of the
23097 * path if we can. This CAN give us invalid 8.3 filenames, but
23098 * there's not a lot of point in guessing what it might be.
23099 */
23100 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023101 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23102 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103 }
23104
23105 /* Count the paths backward to find the beginning of the desired string. */
23106 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023107 {
23108#ifdef FEAT_MBYTE
23109 if (has_mbyte)
23110 p -= mb_head_off(tfname, p);
23111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023112 if (vim_ispathsep(*p))
23113 {
23114 if (sepcount == 0 || (hasTilde && sepcount == 1))
23115 break;
23116 else
23117 sepcount --;
23118 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023120 if (hasTilde)
23121 {
23122 --p;
23123 if (p >= tfname)
23124 *p = '~';
23125 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023126 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023127 }
23128 else
23129 ++p;
23130
23131 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23132 vim_free(*bufp);
23133 *fnamelen = (int)STRLEN(p);
23134 *bufp = pbuf;
23135 *fnamep = p;
23136
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023137 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023138}
23139#endif /* WIN3264 */
23140
23141/*
23142 * Adjust a filename, according to a string of modifiers.
23143 * *fnamep must be NUL terminated when called. When returning, the length is
23144 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023145 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023146 * When there is an error, *fnamep is set to NULL.
23147 */
23148 int
23149modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23150 char_u *src; /* string with modifiers */
23151 int *usedlen; /* characters after src that are used */
23152 char_u **fnamep; /* file name so far */
23153 char_u **bufp; /* buffer for allocated file name or NULL */
23154 int *fnamelen; /* length of fnamep */
23155{
23156 int valid = 0;
23157 char_u *tail;
23158 char_u *s, *p, *pbuf;
23159 char_u dirname[MAXPATHL];
23160 int c;
23161 int has_fullname = 0;
23162#ifdef WIN3264
23163 int has_shortname = 0;
23164#endif
23165
23166repeat:
23167 /* ":p" - full path/file_name */
23168 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23169 {
23170 has_fullname = 1;
23171
23172 valid |= VALID_PATH;
23173 *usedlen += 2;
23174
23175 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23176 if ((*fnamep)[0] == '~'
23177#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23178 && ((*fnamep)[1] == '/'
23179# ifdef BACKSLASH_IN_FILENAME
23180 || (*fnamep)[1] == '\\'
23181# endif
23182 || (*fnamep)[1] == NUL)
23183
23184#endif
23185 )
23186 {
23187 *fnamep = expand_env_save(*fnamep);
23188 vim_free(*bufp); /* free any allocated file name */
23189 *bufp = *fnamep;
23190 if (*fnamep == NULL)
23191 return -1;
23192 }
23193
23194 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023195 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196 {
23197 if (vim_ispathsep(*p)
23198 && p[1] == '.'
23199 && (p[2] == NUL
23200 || vim_ispathsep(p[2])
23201 || (p[2] == '.'
23202 && (p[3] == NUL || vim_ispathsep(p[3])))))
23203 break;
23204 }
23205
23206 /* FullName_save() is slow, don't use it when not needed. */
23207 if (*p != NUL || !vim_isAbsName(*fnamep))
23208 {
23209 *fnamep = FullName_save(*fnamep, *p != NUL);
23210 vim_free(*bufp); /* free any allocated file name */
23211 *bufp = *fnamep;
23212 if (*fnamep == NULL)
23213 return -1;
23214 }
23215
23216 /* Append a path separator to a directory. */
23217 if (mch_isdir(*fnamep))
23218 {
23219 /* Make room for one or two extra characters. */
23220 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23221 vim_free(*bufp); /* free any allocated file name */
23222 *bufp = *fnamep;
23223 if (*fnamep == NULL)
23224 return -1;
23225 add_pathsep(*fnamep);
23226 }
23227 }
23228
23229 /* ":." - path relative to the current directory */
23230 /* ":~" - path relative to the home directory */
23231 /* ":8" - shortname path - postponed till after */
23232 while (src[*usedlen] == ':'
23233 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23234 {
23235 *usedlen += 2;
23236 if (c == '8')
23237 {
23238#ifdef WIN3264
23239 has_shortname = 1; /* Postpone this. */
23240#endif
23241 continue;
23242 }
23243 pbuf = NULL;
23244 /* Need full path first (use expand_env() to remove a "~/") */
23245 if (!has_fullname)
23246 {
23247 if (c == '.' && **fnamep == '~')
23248 p = pbuf = expand_env_save(*fnamep);
23249 else
23250 p = pbuf = FullName_save(*fnamep, FALSE);
23251 }
23252 else
23253 p = *fnamep;
23254
23255 has_fullname = 0;
23256
23257 if (p != NULL)
23258 {
23259 if (c == '.')
23260 {
23261 mch_dirname(dirname, MAXPATHL);
23262 s = shorten_fname(p, dirname);
23263 if (s != NULL)
23264 {
23265 *fnamep = s;
23266 if (pbuf != NULL)
23267 {
23268 vim_free(*bufp); /* free any allocated file name */
23269 *bufp = pbuf;
23270 pbuf = NULL;
23271 }
23272 }
23273 }
23274 else
23275 {
23276 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23277 /* Only replace it when it starts with '~' */
23278 if (*dirname == '~')
23279 {
23280 s = vim_strsave(dirname);
23281 if (s != NULL)
23282 {
23283 *fnamep = s;
23284 vim_free(*bufp);
23285 *bufp = s;
23286 }
23287 }
23288 }
23289 vim_free(pbuf);
23290 }
23291 }
23292
23293 tail = gettail(*fnamep);
23294 *fnamelen = (int)STRLEN(*fnamep);
23295
23296 /* ":h" - head, remove "/file_name", can be repeated */
23297 /* Don't remove the first "/" or "c:\" */
23298 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23299 {
23300 valid |= VALID_HEAD;
23301 *usedlen += 2;
23302 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023303 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023304 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305 *fnamelen = (int)(tail - *fnamep);
23306#ifdef VMS
23307 if (*fnamelen > 0)
23308 *fnamelen += 1; /* the path separator is part of the path */
23309#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023310 if (*fnamelen == 0)
23311 {
23312 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23313 p = vim_strsave((char_u *)".");
23314 if (p == NULL)
23315 return -1;
23316 vim_free(*bufp);
23317 *bufp = *fnamep = tail = p;
23318 *fnamelen = 1;
23319 }
23320 else
23321 {
23322 while (tail > s && !after_pathsep(s, tail))
23323 mb_ptr_back(*fnamep, tail);
23324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023325 }
23326
23327 /* ":8" - shortname */
23328 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23329 {
23330 *usedlen += 2;
23331#ifdef WIN3264
23332 has_shortname = 1;
23333#endif
23334 }
23335
23336#ifdef WIN3264
23337 /* Check shortname after we have done 'heads' and before we do 'tails'
23338 */
23339 if (has_shortname)
23340 {
23341 pbuf = NULL;
23342 /* Copy the string if it is shortened by :h */
23343 if (*fnamelen < (int)STRLEN(*fnamep))
23344 {
23345 p = vim_strnsave(*fnamep, *fnamelen);
23346 if (p == 0)
23347 return -1;
23348 vim_free(*bufp);
23349 *bufp = *fnamep = p;
23350 }
23351
23352 /* Split into two implementations - makes it easier. First is where
23353 * there isn't a full name already, second is where there is.
23354 */
23355 if (!has_fullname && !vim_isAbsName(*fnamep))
23356 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023357 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023358 return -1;
23359 }
23360 else
23361 {
23362 int l;
23363
23364 /* Simple case, already have the full-name
23365 * Nearly always shorter, so try first time. */
23366 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023367 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023368 return -1;
23369
23370 if (l == 0)
23371 {
23372 /* Couldn't find the filename.. search the paths.
23373 */
23374 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023375 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023376 return -1;
23377 }
23378 *fnamelen = l;
23379 }
23380 }
23381#endif /* WIN3264 */
23382
23383 /* ":t" - tail, just the basename */
23384 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23385 {
23386 *usedlen += 2;
23387 *fnamelen -= (int)(tail - *fnamep);
23388 *fnamep = tail;
23389 }
23390
23391 /* ":e" - extension, can be repeated */
23392 /* ":r" - root, without extension, can be repeated */
23393 while (src[*usedlen] == ':'
23394 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23395 {
23396 /* find a '.' in the tail:
23397 * - for second :e: before the current fname
23398 * - otherwise: The last '.'
23399 */
23400 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23401 s = *fnamep - 2;
23402 else
23403 s = *fnamep + *fnamelen - 1;
23404 for ( ; s > tail; --s)
23405 if (s[0] == '.')
23406 break;
23407 if (src[*usedlen + 1] == 'e') /* :e */
23408 {
23409 if (s > tail)
23410 {
23411 *fnamelen += (int)(*fnamep - (s + 1));
23412 *fnamep = s + 1;
23413#ifdef VMS
23414 /* cut version from the extension */
23415 s = *fnamep + *fnamelen - 1;
23416 for ( ; s > *fnamep; --s)
23417 if (s[0] == ';')
23418 break;
23419 if (s > *fnamep)
23420 *fnamelen = s - *fnamep;
23421#endif
23422 }
23423 else if (*fnamep <= tail)
23424 *fnamelen = 0;
23425 }
23426 else /* :r */
23427 {
23428 if (s > tail) /* remove one extension */
23429 *fnamelen = (int)(s - *fnamep);
23430 }
23431 *usedlen += 2;
23432 }
23433
23434 /* ":s?pat?foo?" - substitute */
23435 /* ":gs?pat?foo?" - global substitute */
23436 if (src[*usedlen] == ':'
23437 && (src[*usedlen + 1] == 's'
23438 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23439 {
23440 char_u *str;
23441 char_u *pat;
23442 char_u *sub;
23443 int sep;
23444 char_u *flags;
23445 int didit = FALSE;
23446
23447 flags = (char_u *)"";
23448 s = src + *usedlen + 2;
23449 if (src[*usedlen + 1] == 'g')
23450 {
23451 flags = (char_u *)"g";
23452 ++s;
23453 }
23454
23455 sep = *s++;
23456 if (sep)
23457 {
23458 /* find end of pattern */
23459 p = vim_strchr(s, sep);
23460 if (p != NULL)
23461 {
23462 pat = vim_strnsave(s, (int)(p - s));
23463 if (pat != NULL)
23464 {
23465 s = p + 1;
23466 /* find end of substitution */
23467 p = vim_strchr(s, sep);
23468 if (p != NULL)
23469 {
23470 sub = vim_strnsave(s, (int)(p - s));
23471 str = vim_strnsave(*fnamep, *fnamelen);
23472 if (sub != NULL && str != NULL)
23473 {
23474 *usedlen = (int)(p + 1 - src);
23475 s = do_string_sub(str, pat, sub, flags);
23476 if (s != NULL)
23477 {
23478 *fnamep = s;
23479 *fnamelen = (int)STRLEN(s);
23480 vim_free(*bufp);
23481 *bufp = s;
23482 didit = TRUE;
23483 }
23484 }
23485 vim_free(sub);
23486 vim_free(str);
23487 }
23488 vim_free(pat);
23489 }
23490 }
23491 /* after using ":s", repeat all the modifiers */
23492 if (didit)
23493 goto repeat;
23494 }
23495 }
23496
23497 return valid;
23498}
23499
23500/*
23501 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23502 * "flags" can be "g" to do a global substitute.
23503 * Returns an allocated string, NULL for error.
23504 */
23505 char_u *
23506do_string_sub(str, pat, sub, flags)
23507 char_u *str;
23508 char_u *pat;
23509 char_u *sub;
23510 char_u *flags;
23511{
23512 int sublen;
23513 regmatch_T regmatch;
23514 int i;
23515 int do_all;
23516 char_u *tail;
23517 garray_T ga;
23518 char_u *ret;
23519 char_u *save_cpo;
23520
23521 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23522 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023523 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023524
23525 ga_init2(&ga, 1, 200);
23526
23527 do_all = (flags[0] == 'g');
23528
23529 regmatch.rm_ic = p_ic;
23530 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23531 if (regmatch.regprog != NULL)
23532 {
23533 tail = str;
23534 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23535 {
23536 /*
23537 * Get some space for a temporary buffer to do the substitution
23538 * into. It will contain:
23539 * - The text up to where the match is.
23540 * - The substituted text.
23541 * - The text after the match.
23542 */
23543 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23544 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23545 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23546 {
23547 ga_clear(&ga);
23548 break;
23549 }
23550
23551 /* copy the text up to where the match is */
23552 i = (int)(regmatch.startp[0] - tail);
23553 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23554 /* add the substituted text */
23555 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23556 + ga.ga_len + i, TRUE, TRUE, FALSE);
23557 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023558 /* avoid getting stuck on a match with an empty string */
23559 if (tail == regmatch.endp[0])
23560 {
23561 if (*tail == NUL)
23562 break;
23563 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23564 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023565 }
23566 else
23567 {
23568 tail = regmatch.endp[0];
23569 if (*tail == NUL)
23570 break;
23571 }
23572 if (!do_all)
23573 break;
23574 }
23575
23576 if (ga.ga_data != NULL)
23577 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23578
23579 vim_free(regmatch.regprog);
23580 }
23581
23582 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23583 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023584 if (p_cpo == empty_option)
23585 p_cpo = save_cpo;
23586 else
23587 /* Darn, evaluating {sub} expression changed the value. */
23588 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023589
23590 return ret;
23591}
23592
23593#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */