blob: d7e8332b05440221174adc13cbf723489517cf28 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000115
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000116/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000117 * All user-defined global variables are stored in dictionary "globvardict".
118 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120static dict_T globvardict;
121static dictitem_T globvars_var;
122#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123
124/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000125 * Old Vim variables such as "v:version" are also available without the "v:".
126 * Also in functions. We need a special hashtable for them.
127 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000128static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000129
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200130/* When using exists() don't auto-load a script. */
131static int no_autoload = FALSE;
132
Bram Moolenaar532c7802005-01-27 14:44:31 +0000133/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000134 * When recursively copying lists and dicts we need to remember which ones we
135 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000136 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137 */
138static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000139#define COPYID_INC 2
140#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000141
142/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000143 * Array to hold the hashtab with variables local to each sourced script.
144 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000146typedef struct
147{
148 dictitem_T sv_var;
149 dict_T sv_dict;
150} scriptvar_T;
151
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200152static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
153#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
154#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
156static int echo_attr = 0; /* attributes used for ":echo" */
157
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000158/* Values for trans_function_name() argument: */
159#define TFN_INT 1 /* internal function name OK */
160#define TFN_QUIET 2 /* no error messages */
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000355 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000361 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200362 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000363};
364
365/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_type vv_di.di_tv.v_type
367#define vv_nr vv_di.di_tv.vval.v_number
368#define vv_float vv_di.di_tv.vval.v_float
369#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000370#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000371#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000372
373/*
374 * The v: variables are stored in dictionary "vimvardict".
375 * "vimvars_var" is the variable that is used for the "l:" scope.
376 */
377static dict_T vimvardict;
378static dictitem_T vimvars_var;
379#define vimvarht vimvardict.dv_hashtab
380
Bram Moolenaara40058a2005-07-11 22:42:07 +0000381static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
382static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
383#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
384static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
385#endif
386static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
387static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
388static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000389static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
390static void list_glob_vars __ARGS((int *first));
391static void list_buf_vars __ARGS((int *first));
392static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000393#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000394static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000395#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000396static void list_vim_vars __ARGS((int *first));
397static void list_script_vars __ARGS((int *first));
398static void list_func_vars __ARGS((int *first));
399static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000400static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
401static int check_changedtick __ARGS((char_u *arg));
402static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
403static void clear_lval __ARGS((lval_T *lp));
404static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
405static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
406static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
407static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
408static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
409static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
410static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
411static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
412static void item_lock __ARGS((typval_T *tv, int deep, int lock));
413static int tv_islocked __ARGS((typval_T *tv));
414
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
416static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000421static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
422static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000423
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000424static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
427static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
428static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000429static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static listitem_T *listitem_alloc __ARGS((void));
431static void listitem_free __ARGS((listitem_T *item));
432static void listitem_remove __ARGS((list_T *l, listitem_T *item));
433static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100434static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
435static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
436static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000438static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000441static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
443static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
444static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000445static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000446static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000447static char_u *list2string __ARGS((typval_T *tv, int copyID));
448static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000449static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000450static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
451static void set_ref_in_list __ARGS((list_T *l, int copyID));
452static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200453static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000454static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000455static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
457static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000458static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000460static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000461static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000462static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
463static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000464static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#ifdef FEAT_FLOAT
466static int string2float __ARGS((char_u *text, float_T *value));
467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
469static int find_internal_func __ARGS((char_u *name));
470static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
471static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200472static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000473static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000474static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000475
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#ifdef FEAT_FLOAT
477static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200478static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200486static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000487static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200488static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000489#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000490static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000501#ifdef FEAT_FLOAT
502static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
503#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000504static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000507static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000508static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000509#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000510static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000511static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
513#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000514static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000516#ifdef FEAT_FLOAT
517static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200518static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000519#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
523static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200534#ifdef FEAT_FLOAT
535static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
536#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000539static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000545#ifdef FEAT_FLOAT
546static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000549#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000550static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000559static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000561static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000567static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000575static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000576static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000577static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000578static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200581static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000582static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000590static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000604static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000610static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000622#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200623static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000624static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
625#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000630static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000631static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000632static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000634static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000638#ifdef vim_mkdir
639static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100642#ifdef FEAT_MZSCHEME
643static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000647static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000648#ifdef FEAT_FLOAT
649static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000652static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000653static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000655static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000656static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
669static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
670#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000672static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000674static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000681static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000682static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000683static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000684static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200686static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000687static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000689static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#ifdef FEAT_FLOAT
692static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200693static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000696static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000697static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000700#ifdef FEAT_FLOAT
701static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
703#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000704static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200705static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706#ifdef HAVE_STRFTIME
707static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
708#endif
709static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200715static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200716static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000722static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200723static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000725static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000726static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000727static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000728static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000729static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000731static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200732#ifdef FEAT_FLOAT
733static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
735#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000739#ifdef FEAT_FLOAT
740static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
741#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200743static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200744static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000754static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000756static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000757static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000759static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000760static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000761static int get_env_len __ARGS((char_u **arg));
762static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000763static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000764static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
765#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
766#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
767 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000768static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000770static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
772static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static typval_T *alloc_tv __ARGS((void));
774static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static void init_tv __ARGS((typval_T *varp));
776static long get_tv_number __ARGS((typval_T *varp));
777static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000778static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000779static char_u *get_tv_string __ARGS((typval_T *varp));
780static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000781static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000782static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000783static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
785static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
786static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000787static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
788static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000789static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
790static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000791static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000792static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000793static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000794static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
795static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
796static int eval_fname_script __ARGS((char_u *p));
797static int eval_fname_sid __ARGS((char_u *p));
798static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000799static ufunc_T *find_func __ARGS((char_u *name));
800static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000801static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000802#ifdef FEAT_PROFILE
803static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000804static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
805static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
806static int
807# ifdef __BORLANDC__
808 _RTLENTRYF
809# endif
810 prof_total_cmp __ARGS((const void *s1, const void *s2));
811static int
812# ifdef __BORLANDC__
813 _RTLENTRYF
814# endif
815 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000816#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000817static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000818static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000819static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000820static void func_free __ARGS((ufunc_T *fp));
821static void func_unref __ARGS((char_u *name));
822static void func_ref __ARGS((char_u *name));
823static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000824static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
825static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000826static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000827static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
828static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000829static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000830static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000831static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000832
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200833
834#ifdef EBCDIC
835static int compare_func_name __ARGS((const void *s1, const void *s2));
836static void sortFunctions __ARGS(());
837#endif
838
839
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000840/* Character used as separated in autoload function/variable names. */
841#define AUTOLOAD_CHAR '#'
842
Bram Moolenaar33570922005-01-25 22:26:29 +0000843/*
844 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000845 */
846 void
847eval_init()
848{
Bram Moolenaar33570922005-01-25 22:26:29 +0000849 int i;
850 struct vimvar *p;
851
852 init_var_dict(&globvardict, &globvars_var);
853 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000854 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000855 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000856
857 for (i = 0; i < VV_LEN; ++i)
858 {
859 p = &vimvars[i];
860 STRCPY(p->vv_di.di_key, p->vv_name);
861 if (p->vv_flags & VV_RO)
862 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
863 else if (p->vv_flags & VV_RO_SBX)
864 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
865 else
866 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000867
868 /* add to v: scope dict, unless the value is not always available */
869 if (p->vv_type != VAR_UNKNOWN)
870 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000871 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000872 /* add to compat scope dict */
873 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000874 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000875 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200876
877#ifdef EBCDIC
878 /*
879 * Sort the function table, to enable binary sort.
880 */
881 sortFunctions();
882#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000883}
884
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000885#if defined(EXITFREE) || defined(PROTO)
886 void
887eval_clear()
888{
889 int i;
890 struct vimvar *p;
891
892 for (i = 0; i < VV_LEN; ++i)
893 {
894 p = &vimvars[i];
895 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000896 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000897 vim_free(p->vv_str);
898 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000899 }
900 else if (p->vv_di.di_tv.v_type == VAR_LIST)
901 {
902 list_unref(p->vv_list);
903 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000904 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000905 }
906 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000907 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908 hash_clear(&compat_hashtab);
909
Bram Moolenaard9fba312005-06-26 22:34:35 +0000910 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911
912 /* global variables */
913 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000914
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000915 /* autoloaded script names */
916 ga_clear_strings(&ga_loaded);
917
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200918 /* script-local variables */
919 for (i = 1; i <= ga_scripts.ga_len; ++i)
920 {
921 vars_clear(&SCRIPT_VARS(i));
922 vim_free(SCRIPT_SV(i));
923 }
924 ga_clear(&ga_scripts);
925
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000926 /* unreferenced lists and dicts */
927 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000928
929 /* functions */
930 free_all_functions();
931 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000932}
933#endif
934
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 * Return the name of the executed function.
937 */
938 char_u *
939func_name(cookie)
940 void *cookie;
941{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000942 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943}
944
945/*
946 * Return the address holding the next breakpoint line for a funccall cookie.
947 */
948 linenr_T *
949func_breakpoint(cookie)
950 void *cookie;
951{
Bram Moolenaar33570922005-01-25 22:26:29 +0000952 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953}
954
955/*
956 * Return the address holding the debug tick for a funccall cookie.
957 */
958 int *
959func_dbg_tick(cookie)
960 void *cookie;
961{
Bram Moolenaar33570922005-01-25 22:26:29 +0000962 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the nesting level for a funccall cookie.
967 */
968 int
969func_level(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000976funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000978/* pointer to list of previously used funccal, still around because some
979 * item in it is still being used. */
980funccall_T *previous_funccal = NULL;
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982/*
983 * Return TRUE when a function was ended by a ":return" command.
984 */
985 int
986current_func_returned()
987{
988 return current_funccal->returned;
989}
990
991
992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 * Set an internal variable to a string value. Creates the variable if it does
994 * not already exist.
995 */
996 void
997set_internal_string_var(name, value)
998 char_u *name;
999 char_u *value;
1000{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001001 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001002 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
1004 val = vim_strsave(value);
1005 if (val != NULL)
1006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001007 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001010 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001011 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
1013 }
1014}
1015
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001017static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001018static char_u *redir_endp = NULL;
1019static char_u *redir_varname = NULL;
1020
1021/*
1022 * Start recording command output to a variable
1023 * Returns OK if successfully completed the setup. FAIL otherwise.
1024 */
1025 int
1026var_redir_start(name, append)
1027 char_u *name;
1028 int append; /* append to an existing variable */
1029{
1030 int save_emsg;
1031 int err;
1032 typval_T tv;
1033
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001034 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001035 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036 {
1037 EMSG(_(e_invarg));
1038 return FAIL;
1039 }
1040
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001041 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001042 redir_varname = vim_strsave(name);
1043 if (redir_varname == NULL)
1044 return FAIL;
1045
1046 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1047 if (redir_lval == NULL)
1048 {
1049 var_redir_stop();
1050 return FAIL;
1051 }
1052
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001053 /* The output is stored in growarray "redir_ga" until redirection ends. */
1054 ga_init2(&redir_ga, (int)sizeof(char), 500);
1055
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001057 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1058 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1060 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001061 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 if (redir_endp != NULL && *redir_endp != NUL)
1063 /* Trailing characters are present after the variable name */
1064 EMSG(_(e_trailing));
1065 else
1066 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001067 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001068 var_redir_stop();
1069 return FAIL;
1070 }
1071
1072 /* check if we can write to the variable: set it to or append an empty
1073 * string */
1074 save_emsg = did_emsg;
1075 did_emsg = FALSE;
1076 tv.v_type = VAR_STRING;
1077 tv.vval.v_string = (char_u *)"";
1078 if (append)
1079 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1080 else
1081 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001082 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001084 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085 if (err)
1086 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001087 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 var_redir_stop();
1089 return FAIL;
1090 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091
1092 return OK;
1093}
1094
1095/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001096 * Append "value[value_len]" to the variable set by var_redir_start().
1097 * The actual appending is postponed until redirection ends, because the value
1098 * appended may in fact be the string we write to, changing it may cause freed
1099 * memory to be used:
1100 * :redir => foo
1101 * :let foo
1102 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 */
1104 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001105var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001107 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001109 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110
1111 if (redir_lval == NULL)
1112 return;
1113
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001115 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001120 {
1121 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001123 }
1124 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126}
1127
1128/*
1129 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001130 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 */
1132 void
1133var_redir_stop()
1134{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001135 typval_T tv;
1136
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 if (redir_lval != NULL)
1138 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001139 /* If there was no error: assign the text to the variable. */
1140 if (redir_endp != NULL)
1141 {
1142 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1143 tv.v_type = VAR_STRING;
1144 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001145 /* Call get_lval() again, if it's inside a Dict or List it may
1146 * have changed. */
1147 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1148 FALSE, FALSE, FALSE, FNE_CHECK_START);
1149 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1150 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1151 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001153
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001154 /* free the collected output */
1155 vim_free(redir_ga.ga_data);
1156 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 vim_free(redir_lval);
1159 redir_lval = NULL;
1160 }
1161 vim_free(redir_varname);
1162 redir_varname = NULL;
1163}
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165# if defined(FEAT_MBYTE) || defined(PROTO)
1166 int
1167eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1168 char_u *enc_from;
1169 char_u *enc_to;
1170 char_u *fname_from;
1171 char_u *fname_to;
1172{
1173 int err = FALSE;
1174
1175 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1176 set_vim_var_string(VV_CC_TO, enc_to, -1);
1177 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1178 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1179 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1180 err = TRUE;
1181 set_vim_var_string(VV_CC_FROM, NULL, -1);
1182 set_vim_var_string(VV_CC_TO, NULL, -1);
1183 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1184 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1185
1186 if (err)
1187 return FAIL;
1188 return OK;
1189}
1190# endif
1191
1192# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1193 int
1194eval_printexpr(fname, args)
1195 char_u *fname;
1196 char_u *args;
1197{
1198 int err = FALSE;
1199
1200 set_vim_var_string(VV_FNAME_IN, fname, -1);
1201 set_vim_var_string(VV_CMDARG, args, -1);
1202 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1203 err = TRUE;
1204 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1205 set_vim_var_string(VV_CMDARG, NULL, -1);
1206
1207 if (err)
1208 {
1209 mch_remove(fname);
1210 return FAIL;
1211 }
1212 return OK;
1213}
1214# endif
1215
1216# if defined(FEAT_DIFF) || defined(PROTO)
1217 void
1218eval_diff(origfile, newfile, outfile)
1219 char_u *origfile;
1220 char_u *newfile;
1221 char_u *outfile;
1222{
1223 int err = FALSE;
1224
1225 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1226 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1227 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1228 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1229 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1230 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1231 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1232}
1233
1234 void
1235eval_patch(origfile, difffile, outfile)
1236 char_u *origfile;
1237 char_u *difffile;
1238 char_u *outfile;
1239{
1240 int err;
1241
1242 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1243 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1244 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1245 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1246 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1247 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1248 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1249}
1250# endif
1251
1252/*
1253 * Top level evaluation function, returning a boolean.
1254 * Sets "error" to TRUE if there was an error.
1255 * Return TRUE or FALSE.
1256 */
1257 int
1258eval_to_bool(arg, error, nextcmd, skip)
1259 char_u *arg;
1260 int *error;
1261 char_u **nextcmd;
1262 int skip; /* only parse, don't execute */
1263{
Bram Moolenaar33570922005-01-25 22:26:29 +00001264 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 int retval = FALSE;
1266
1267 if (skip)
1268 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001269 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 else
1272 {
1273 *error = FALSE;
1274 if (!skip)
1275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001276 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001277 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279 }
1280 if (skip)
1281 --emsg_skip;
1282
1283 return retval;
1284}
1285
1286/*
1287 * Top level evaluation function, returning a string. If "skip" is TRUE,
1288 * only parsing to "nextcmd" is done, without reporting errors. Return
1289 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1290 */
1291 char_u *
1292eval_to_string_skip(arg, nextcmd, skip)
1293 char_u *arg;
1294 char_u **nextcmd;
1295 int skip; /* only parse, don't execute */
1296{
Bram Moolenaar33570922005-01-25 22:26:29 +00001297 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 char_u *retval;
1299
1300 if (skip)
1301 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001302 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 retval = NULL;
1304 else
1305 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 retval = vim_strsave(get_tv_string(&tv));
1307 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 }
1309 if (skip)
1310 --emsg_skip;
1311
1312 return retval;
1313}
1314
1315/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001316 * Skip over an expression at "*pp".
1317 * Return FAIL for an error, OK otherwise.
1318 */
1319 int
1320skip_expr(pp)
1321 char_u **pp;
1322{
Bram Moolenaar33570922005-01-25 22:26:29 +00001323 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001324
1325 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001327}
1328
1329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001331 * When "convert" is TRUE convert a List into a sequence of lines and convert
1332 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 * Return pointer to allocated memory, or NULL for failure.
1334 */
1335 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001336eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 char_u *arg;
1338 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001339 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340{
Bram Moolenaar33570922005-01-25 22:26:29 +00001341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001343 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001344#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001345 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001348 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 retval = NULL;
1350 else
1351 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001353 {
1354 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001355 if (tv.vval.v_list != NULL)
1356 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001357 ga_append(&ga, NUL);
1358 retval = (char_u *)ga.ga_data;
1359 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001360#ifdef FEAT_FLOAT
1361 else if (convert && tv.v_type == VAR_FLOAT)
1362 {
1363 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1364 retval = vim_strsave(numbuf);
1365 }
1366#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001367 else
1368 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001369 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 }
1371
1372 return retval;
1373}
1374
1375/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001376 * Call eval_to_string() without using current local variables and using
1377 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 */
1379 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001380eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 char_u *arg;
1382 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001383 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384{
1385 char_u *retval;
1386 void *save_funccalp;
1387
1388 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001389 if (use_sandbox)
1390 ++sandbox;
1391 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001392 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393 if (use_sandbox)
1394 --sandbox;
1395 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 restore_funccal(save_funccalp);
1397 return retval;
1398}
1399
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400/*
1401 * Top level evaluation function, returning a number.
1402 * Evaluates "expr" silently.
1403 * Returns -1 for an error.
1404 */
1405 int
1406eval_to_number(expr)
1407 char_u *expr;
1408{
Bram Moolenaar33570922005-01-25 22:26:29 +00001409 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001411 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412
1413 ++emsg_off;
1414
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001415 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 retval = -1;
1417 else
1418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001419 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001420 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 }
1422 --emsg_off;
1423
1424 return retval;
1425}
1426
Bram Moolenaara40058a2005-07-11 22:42:07 +00001427/*
1428 * Prepare v: variable "idx" to be used.
1429 * Save the current typeval in "save_tv".
1430 * When not used yet add the variable to the v: hashtable.
1431 */
1432 static void
1433prepare_vimvar(idx, save_tv)
1434 int idx;
1435 typval_T *save_tv;
1436{
1437 *save_tv = vimvars[idx].vv_tv;
1438 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1439 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1440}
1441
1442/*
1443 * Restore v: variable "idx" to typeval "save_tv".
1444 * When no longer defined, remove the variable from the v: hashtable.
1445 */
1446 static void
1447restore_vimvar(idx, save_tv)
1448 int idx;
1449 typval_T *save_tv;
1450{
1451 hashitem_T *hi;
1452
Bram Moolenaara40058a2005-07-11 22:42:07 +00001453 vimvars[idx].vv_tv = *save_tv;
1454 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1455 {
1456 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1457 if (HASHITEM_EMPTY(hi))
1458 EMSG2(_(e_intern2), "restore_vimvar()");
1459 else
1460 hash_remove(&vimvarht, hi);
1461 }
1462}
1463
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001464#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001465/*
1466 * Evaluate an expression to a list with suggestions.
1467 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001468 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001469 */
1470 list_T *
1471eval_spell_expr(badword, expr)
1472 char_u *badword;
1473 char_u *expr;
1474{
1475 typval_T save_val;
1476 typval_T rettv;
1477 list_T *list = NULL;
1478 char_u *p = skipwhite(expr);
1479
1480 /* Set "v:val" to the bad word. */
1481 prepare_vimvar(VV_VAL, &save_val);
1482 vimvars[VV_VAL].vv_type = VAR_STRING;
1483 vimvars[VV_VAL].vv_str = badword;
1484 if (p_verbose == 0)
1485 ++emsg_off;
1486
1487 if (eval1(&p, &rettv, TRUE) == OK)
1488 {
1489 if (rettv.v_type != VAR_LIST)
1490 clear_tv(&rettv);
1491 else
1492 list = rettv.vval.v_list;
1493 }
1494
1495 if (p_verbose == 0)
1496 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001497 restore_vimvar(VV_VAL, &save_val);
1498
1499 return list;
1500}
1501
1502/*
1503 * "list" is supposed to contain two items: a word and a number. Return the
1504 * word in "pp" and the number as the return value.
1505 * Return -1 if anything isn't right.
1506 * Used to get the good word and score from the eval_spell_expr() result.
1507 */
1508 int
1509get_spellword(list, pp)
1510 list_T *list;
1511 char_u **pp;
1512{
1513 listitem_T *li;
1514
1515 li = list->lv_first;
1516 if (li == NULL)
1517 return -1;
1518 *pp = get_tv_string(&li->li_tv);
1519
1520 li = li->li_next;
1521 if (li == NULL)
1522 return -1;
1523 return get_tv_number(&li->li_tv);
1524}
1525#endif
1526
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001527/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001528 * Top level evaluation function.
1529 * Returns an allocated typval_T with the result.
1530 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001531 */
1532 typval_T *
1533eval_expr(arg, nextcmd)
1534 char_u *arg;
1535 char_u **nextcmd;
1536{
1537 typval_T *tv;
1538
1539 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001540 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001541 {
1542 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001543 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001544 }
1545
1546 return tv;
1547}
1548
1549
Bram Moolenaar4f688582007-07-24 12:34:30 +00001550#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1551 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001553 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001554 * Uses argv[argc] for the function arguments. Only Number and String
1555 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001556 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001558 static int
1559call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 char_u *func;
1561 int argc;
1562 char_u **argv;
1563 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565{
Bram Moolenaar33570922005-01-25 22:26:29 +00001566 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 long n;
1568 int len;
1569 int i;
1570 int doesrange;
1571 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001572 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001574 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001576 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
1578 for (i = 0; i < argc; i++)
1579 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001580 /* Pass a NULL or empty argument as an empty string */
1581 if (argv[i] == NULL || *argv[i] == NUL)
1582 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001583 argvars[i].v_type = VAR_STRING;
1584 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001585 continue;
1586 }
1587
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 /* Recognize a number argument, the others must be strings. */
1589 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1590 if (len != 0 && len == (int)STRLEN(argv[i]))
1591 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001592 argvars[i].v_type = VAR_NUMBER;
1593 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 }
1595 else
1596 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001597 argvars[i].v_type = VAR_STRING;
1598 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 }
1600 }
1601
1602 if (safe)
1603 {
1604 save_funccalp = save_funccal();
1605 ++sandbox;
1606 }
1607
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001608 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1609 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (safe)
1613 {
1614 --sandbox;
1615 restore_funccal(save_funccalp);
1616 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 vim_free(argvars);
1618
1619 if (ret == FAIL)
1620 clear_tv(rettv);
1621
1622 return ret;
1623}
1624
Bram Moolenaar4f688582007-07-24 12:34:30 +00001625# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001626/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001627 * Call vimL function "func" and return the result as a string.
1628 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001629 * Uses argv[argc] for the function arguments.
1630 */
1631 void *
1632call_func_retstr(func, argc, argv, safe)
1633 char_u *func;
1634 int argc;
1635 char_u **argv;
1636 int safe; /* use the sandbox */
1637{
1638 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001639 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001640
1641 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1642 return NULL;
1643
1644 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001645 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 return retval;
1647}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001648# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649
Bram Moolenaar4f688582007-07-24 12:34:30 +00001650# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001651/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001652 * Call vimL function "func" and return the result as a number.
1653 * Returns -1 when calling the function fails.
1654 * Uses argv[argc] for the function arguments.
1655 */
1656 long
1657call_func_retnr(func, argc, argv, safe)
1658 char_u *func;
1659 int argc;
1660 char_u **argv;
1661 int safe; /* use the sandbox */
1662{
1663 typval_T rettv;
1664 long retval;
1665
1666 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1667 return -1;
1668
1669 retval = get_tv_number_chk(&rettv, NULL);
1670 clear_tv(&rettv);
1671 return retval;
1672}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001673# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001674
1675/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001676 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001678 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 */
1680 void *
1681call_func_retlist(func, argc, argv, safe)
1682 char_u *func;
1683 int argc;
1684 char_u **argv;
1685 int safe; /* use the sandbox */
1686{
1687 typval_T rettv;
1688
1689 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1690 return NULL;
1691
1692 if (rettv.v_type != VAR_LIST)
1693 {
1694 clear_tv(&rettv);
1695 return NULL;
1696 }
1697
1698 return rettv.vval.v_list;
1699}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700#endif
1701
Bram Moolenaar4f688582007-07-24 12:34:30 +00001702
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703/*
1704 * Save the current function call pointer, and set it to NULL.
1705 * Used when executing autocommands and for ":source".
1706 */
1707 void *
1708save_funccal()
1709{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001710 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 current_funccal = NULL;
1713 return (void *)fc;
1714}
1715
1716 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001717restore_funccal(vfc)
1718 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001720 funccall_T *fc = (funccall_T *)vfc;
1721
1722 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723}
1724
Bram Moolenaar05159a02005-02-26 23:04:13 +00001725#if defined(FEAT_PROFILE) || defined(PROTO)
1726/*
1727 * Prepare profiling for entering a child or something else that is not
1728 * counted for the script/function itself.
1729 * Should always be called in pair with prof_child_exit().
1730 */
1731 void
1732prof_child_enter(tm)
1733 proftime_T *tm; /* place to store waittime */
1734{
1735 funccall_T *fc = current_funccal;
1736
1737 if (fc != NULL && fc->func->uf_profiling)
1738 profile_start(&fc->prof_child);
1739 script_prof_save(tm);
1740}
1741
1742/*
1743 * Take care of time spent in a child.
1744 * Should always be called after prof_child_enter().
1745 */
1746 void
1747prof_child_exit(tm)
1748 proftime_T *tm; /* where waittime was stored */
1749{
1750 funccall_T *fc = current_funccal;
1751
1752 if (fc != NULL && fc->func->uf_profiling)
1753 {
1754 profile_end(&fc->prof_child);
1755 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1756 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1757 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1758 }
1759 script_prof_restore(tm);
1760}
1761#endif
1762
1763
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764#ifdef FEAT_FOLDING
1765/*
1766 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1767 * it in "*cp". Doesn't give error messages.
1768 */
1769 int
1770eval_foldexpr(arg, cp)
1771 char_u *arg;
1772 int *cp;
1773{
Bram Moolenaar33570922005-01-25 22:26:29 +00001774 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 int retval;
1776 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001777 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1778 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
1780 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001781 if (use_sandbox)
1782 ++sandbox;
1783 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001785 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 retval = 0;
1787 else
1788 {
1789 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001790 if (tv.v_type == VAR_NUMBER)
1791 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001792 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 retval = 0;
1794 else
1795 {
1796 /* If the result is a string, check if there is a non-digit before
1797 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001798 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 if (!VIM_ISDIGIT(*s) && *s != '-')
1800 *cp = *s++;
1801 retval = atol((char *)s);
1802 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001803 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 }
1805 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001806 if (use_sandbox)
1807 --sandbox;
1808 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809
1810 return retval;
1811}
1812#endif
1813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001815 * ":let" list all variable values
1816 * ":let var1 var2" list variable values
1817 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001818 * ":let var += expr" assignment command.
1819 * ":let var -= expr" assignment command.
1820 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001821 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 */
1823 void
1824ex_let(eap)
1825 exarg_T *eap;
1826{
1827 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001828 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001829 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 int var_count = 0;
1832 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001833 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001834 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001835 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836
Bram Moolenaardb552d602006-03-23 22:59:57 +00001837 argend = skip_var_list(arg, &var_count, &semicolon);
1838 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001839 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001840 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1841 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001842 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001843 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001845 /*
1846 * ":let" without "=": list variables
1847 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001848 if (*arg == '[')
1849 EMSG(_(e_invarg));
1850 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001852 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001854 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001855 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001856 list_glob_vars(&first);
1857 list_buf_vars(&first);
1858 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001859#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001860 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001861#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001862 list_script_vars(&first);
1863 list_func_vars(&first);
1864 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 eap->nextcmd = check_nextcmd(arg);
1867 }
1868 else
1869 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001870 op[0] = '=';
1871 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001872 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001873 {
1874 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1875 op[0] = expr[-1]; /* +=, -= or .= */
1876 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001877 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 if (eap->skip)
1880 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (eap->skip)
1883 {
1884 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 --emsg_skip;
1887 }
1888 else if (i != FAIL)
1889 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001891 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 }
1894 }
1895}
1896
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897/*
1898 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1899 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 * When "nextchars" is not NULL it points to a string with characters that
1901 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1902 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 * Returns OK or FAIL;
1904 */
1905 static int
1906ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1907 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001908 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909 int copy; /* copy values from "tv", don't move */
1910 int semicolon; /* from skip_var_list() */
1911 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001912 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913{
1914 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001915 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001917 listitem_T *item;
1918 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919
1920 if (*arg != '[')
1921 {
1922 /*
1923 * ":let var = expr" or ":for var in list"
1924 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001925 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001926 return FAIL;
1927 return OK;
1928 }
1929
1930 /*
1931 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1932 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001933 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 {
1935 EMSG(_(e_listreq));
1936 return FAIL;
1937 }
1938
1939 i = list_len(l);
1940 if (semicolon == 0 && var_count < i)
1941 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001942 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943 return FAIL;
1944 }
1945 if (var_count - semicolon > i)
1946 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001947 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 return FAIL;
1949 }
1950
1951 item = l->lv_first;
1952 while (*arg != ']')
1953 {
1954 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001955 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 item = item->li_next;
1957 if (arg == NULL)
1958 return FAIL;
1959
1960 arg = skipwhite(arg);
1961 if (*arg == ';')
1962 {
1963 /* Put the rest of the list (may be empty) in the var after ';'.
1964 * Create a new list for this. */
1965 l = list_alloc();
1966 if (l == NULL)
1967 return FAIL;
1968 while (item != NULL)
1969 {
1970 list_append_tv(l, &item->li_tv);
1971 item = item->li_next;
1972 }
1973
1974 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001975 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001976 ltv.vval.v_list = l;
1977 l->lv_refcount = 1;
1978
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001979 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1980 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981 clear_tv(&ltv);
1982 if (arg == NULL)
1983 return FAIL;
1984 break;
1985 }
1986 else if (*arg != ',' && *arg != ']')
1987 {
1988 EMSG2(_(e_intern2), "ex_let_vars()");
1989 return FAIL;
1990 }
1991 }
1992
1993 return OK;
1994}
1995
1996/*
1997 * Skip over assignable variable "var" or list of variables "[var, var]".
1998 * Used for ":let varvar = expr" and ":for varvar in expr".
1999 * For "[var, var]" increment "*var_count" for each variable.
2000 * for "[var, var; var]" set "semicolon".
2001 * Return NULL for an error.
2002 */
2003 static char_u *
2004skip_var_list(arg, var_count, semicolon)
2005 char_u *arg;
2006 int *var_count;
2007 int *semicolon;
2008{
2009 char_u *p, *s;
2010
2011 if (*arg == '[')
2012 {
2013 /* "[var, var]": find the matching ']'. */
2014 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002015 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002016 {
2017 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2018 s = skip_var_one(p);
2019 if (s == p)
2020 {
2021 EMSG2(_(e_invarg2), p);
2022 return NULL;
2023 }
2024 ++*var_count;
2025
2026 p = skipwhite(s);
2027 if (*p == ']')
2028 break;
2029 else if (*p == ';')
2030 {
2031 if (*semicolon == 1)
2032 {
2033 EMSG(_("Double ; in list of variables"));
2034 return NULL;
2035 }
2036 *semicolon = 1;
2037 }
2038 else if (*p != ',')
2039 {
2040 EMSG2(_(e_invarg2), p);
2041 return NULL;
2042 }
2043 }
2044 return p + 1;
2045 }
2046 else
2047 return skip_var_one(arg);
2048}
2049
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002050/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002051 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002052 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002053 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002054 static char_u *
2055skip_var_one(arg)
2056 char_u *arg;
2057{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002058 if (*arg == '@' && arg[1] != NUL)
2059 return arg + 2;
2060 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2061 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002062}
2063
Bram Moolenaara7043832005-01-21 11:56:39 +00002064/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002065 * List variables for hashtab "ht" with prefix "prefix".
2066 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002067 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002068 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002069list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002070 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002071 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002072 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002073 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002074{
Bram Moolenaar33570922005-01-25 22:26:29 +00002075 hashitem_T *hi;
2076 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002077 int todo;
2078
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002079 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002080 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2081 {
2082 if (!HASHITEM_EMPTY(hi))
2083 {
2084 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002085 di = HI2DI(hi);
2086 if (empty || di->di_tv.v_type != VAR_STRING
2087 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002088 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 }
2090 }
2091}
2092
2093/*
2094 * List global variables.
2095 */
2096 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002097list_glob_vars(first)
2098 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002099{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002100 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002101}
2102
2103/*
2104 * List buffer variables.
2105 */
2106 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002107list_buf_vars(first)
2108 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002109{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002110 char_u numbuf[NUMBUFLEN];
2111
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002112 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2113 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002114
2115 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2117 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002118}
2119
2120/*
2121 * List window variables.
2122 */
2123 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002124list_win_vars(first)
2125 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002126{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002127 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2128 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002129}
2130
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002131#ifdef FEAT_WINDOWS
2132/*
2133 * List tab page variables.
2134 */
2135 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002136list_tab_vars(first)
2137 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002138{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2140 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002141}
2142#endif
2143
Bram Moolenaara7043832005-01-21 11:56:39 +00002144/*
2145 * List Vim variables.
2146 */
2147 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148list_vim_vars(first)
2149 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002150{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152}
2153
2154/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002155 * List script-local variables, if there is a script.
2156 */
2157 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158list_script_vars(first)
2159 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002160{
2161 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2163 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002164}
2165
2166/*
2167 * List function variables, if there is a function.
2168 */
2169 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170list_func_vars(first)
2171 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002172{
2173 if (current_funccal != NULL)
2174 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002175 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002176}
2177
2178/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002179 * List variables in "arg".
2180 */
2181 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002182list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183 exarg_T *eap;
2184 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002185 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186{
2187 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002188 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002190 char_u *name_start;
2191 char_u *arg_subsc;
2192 char_u *tofree;
2193 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194
2195 while (!ends_excmd(*arg) && !got_int)
2196 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002197 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002199 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002200 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2201 {
2202 emsg_severe = TRUE;
2203 EMSG(_(e_trailing));
2204 break;
2205 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002209 /* get_name_len() takes care of expanding curly braces */
2210 name_start = name = arg;
2211 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2212 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002214 /* This is mainly to keep test 49 working: when expanding
2215 * curly braces fails overrule the exception error message. */
2216 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 emsg_severe = TRUE;
2219 EMSG2(_(e_invarg2), arg);
2220 break;
2221 }
2222 error = TRUE;
2223 }
2224 else
2225 {
2226 if (tofree != NULL)
2227 name = tofree;
2228 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 else
2231 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 /* handle d.key, l[idx], f(expr) */
2233 arg_subsc = arg;
2234 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002235 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002237 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002238 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002241 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002242 case 'g': list_glob_vars(first); break;
2243 case 'b': list_buf_vars(first); break;
2244 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002245#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002246 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002247#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002248 case 'v': list_vim_vars(first); break;
2249 case 's': list_script_vars(first); break;
2250 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002251 default:
2252 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002253 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002254 }
2255 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 {
2257 char_u numbuf[NUMBUFLEN];
2258 char_u *tf;
2259 int c;
2260 char_u *s;
2261
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002262 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 c = *arg;
2264 *arg = NUL;
2265 list_one_var_a((char_u *)"",
2266 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002267 tv.v_type,
2268 s == NULL ? (char_u *)"" : s,
2269 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 *arg = c;
2271 vim_free(tf);
2272 }
2273 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002274 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 }
2276 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277
2278 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280
2281 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 }
2283
2284 return arg;
2285}
2286
2287/*
2288 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2289 * Returns a pointer to the char just after the var name.
2290 * Returns NULL if there is an error.
2291 */
2292 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002293ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002295 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002297 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002298 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299{
2300 int c1;
2301 char_u *name;
2302 char_u *p;
2303 char_u *arg_end = NULL;
2304 int len;
2305 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002306 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307
2308 /*
2309 * ":let $VAR = expr": Set environment variable.
2310 */
2311 if (*arg == '$')
2312 {
2313 /* Find the end of the name. */
2314 ++arg;
2315 name = arg;
2316 len = get_env_len(&arg);
2317 if (len == 0)
2318 EMSG2(_(e_invarg2), name - 1);
2319 else
2320 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002321 if (op != NULL && (*op == '+' || *op == '-'))
2322 EMSG2(_(e_letwrong), op);
2323 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002324 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002326 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 {
2328 c1 = name[len];
2329 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002330 p = get_tv_string_chk(tv);
2331 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002332 {
2333 int mustfree = FALSE;
2334 char_u *s = vim_getenv(name, &mustfree);
2335
2336 if (s != NULL)
2337 {
2338 p = tofree = concat_str(s, p);
2339 if (mustfree)
2340 vim_free(s);
2341 }
2342 }
2343 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002344 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002345 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002346 if (STRICMP(name, "HOME") == 0)
2347 init_homedir();
2348 else if (didset_vim && STRICMP(name, "VIM") == 0)
2349 didset_vim = FALSE;
2350 else if (didset_vimruntime
2351 && STRICMP(name, "VIMRUNTIME") == 0)
2352 didset_vimruntime = FALSE;
2353 arg_end = arg;
2354 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002356 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 }
2358 }
2359 }
2360
2361 /*
2362 * ":let &option = expr": Set option value.
2363 * ":let &l:option = expr": Set local option value.
2364 * ":let &g:option = expr": Set global option value.
2365 */
2366 else if (*arg == '&')
2367 {
2368 /* Find the end of the name. */
2369 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002370 if (p == NULL || (endchars != NULL
2371 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372 EMSG(_(e_letunexp));
2373 else
2374 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002375 long n;
2376 int opt_type;
2377 long numval;
2378 char_u *stringval = NULL;
2379 char_u *s;
2380
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 c1 = *p;
2382 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383
2384 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002385 s = get_tv_string_chk(tv); /* != NULL if number or string */
2386 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387 {
2388 opt_type = get_option_value(arg, &numval,
2389 &stringval, opt_flags);
2390 if ((opt_type == 1 && *op == '.')
2391 || (opt_type == 0 && *op != '.'))
2392 EMSG2(_(e_letwrong), op);
2393 else
2394 {
2395 if (opt_type == 1) /* number */
2396 {
2397 if (*op == '+')
2398 n = numval + n;
2399 else
2400 n = numval - n;
2401 }
2402 else if (opt_type == 0 && stringval != NULL) /* string */
2403 {
2404 s = concat_str(stringval, s);
2405 vim_free(stringval);
2406 stringval = s;
2407 }
2408 }
2409 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002410 if (s != NULL)
2411 {
2412 set_option_value(arg, n, s, opt_flags);
2413 arg_end = p;
2414 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002415 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002416 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 }
2418 }
2419
2420 /*
2421 * ":let @r = expr": Set register contents.
2422 */
2423 else if (*arg == '@')
2424 {
2425 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002426 if (op != NULL && (*op == '+' || *op == '-'))
2427 EMSG2(_(e_letwrong), op);
2428 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002429 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002430 EMSG(_(e_letunexp));
2431 else
2432 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002433 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002434 char_u *s;
2435
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002436 p = get_tv_string_chk(tv);
2437 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002439 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 if (s != NULL)
2441 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002442 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 vim_free(s);
2444 }
2445 }
2446 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002447 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002449 arg_end = arg + 1;
2450 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002451 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 }
2453 }
2454
2455 /*
2456 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002458 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002459 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002461 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002463 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2467 EMSG(_(e_letunexp));
2468 else
2469 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 arg_end = p;
2472 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 }
2476
2477 else
2478 EMSG2(_(e_invarg2), arg);
2479
2480 return arg_end;
2481}
2482
2483/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002484 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2485 */
2486 static int
2487check_changedtick(arg)
2488 char_u *arg;
2489{
2490 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2491 {
2492 EMSG2(_(e_readonlyvar), arg);
2493 return TRUE;
2494 }
2495 return FALSE;
2496}
2497
2498/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 * Get an lval: variable, Dict item or List item that can be assigned a value
2500 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2501 * "name.key", "name.key[expr]" etc.
2502 * Indexing only works if "name" is an existing List or Dictionary.
2503 * "name" points to the start of the name.
2504 * If "rettv" is not NULL it points to the value to be assigned.
2505 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2506 * wrong; must end in space or cmd separator.
2507 *
2508 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002509 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 */
2512 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002513get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002515 typval_T *rettv;
2516 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 int unlet;
2518 int skip;
2519 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002520 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 char_u *expr_start, *expr_end;
2524 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002525 dictitem_T *v;
2526 typval_T var1;
2527 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002529 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002530 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002531 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002532 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002533
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536
2537 if (skip)
2538 {
2539 /* When skipping just find the end of the name. */
2540 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002541 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 }
2543
2544 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002545 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 if (expr_start != NULL)
2547 {
2548 /* Don't expand the name when we already know there is an error. */
2549 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2550 && *p != '[' && *p != '.')
2551 {
2552 EMSG(_(e_trailing));
2553 return NULL;
2554 }
2555
2556 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2557 if (lp->ll_exp_name == NULL)
2558 {
2559 /* Report an invalid expression in braces, unless the
2560 * expression evaluation has been cancelled due to an
2561 * aborting error, an interrupt, or an exception. */
2562 if (!aborting() && !quiet)
2563 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002564 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 EMSG2(_(e_invarg2), name);
2566 return NULL;
2567 }
2568 }
2569 lp->ll_name = lp->ll_exp_name;
2570 }
2571 else
2572 lp->ll_name = name;
2573
2574 /* Without [idx] or .key we are done. */
2575 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2576 return p;
2577
2578 cc = *p;
2579 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002580 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 if (v == NULL && !quiet)
2582 EMSG2(_(e_undefvar), lp->ll_name);
2583 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584 if (v == NULL)
2585 return NULL;
2586
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 /*
2588 * Loop until no more [idx] or .key is following.
2589 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002590 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002592 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2594 && !(lp->ll_tv->v_type == VAR_DICT
2595 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 if (!quiet)
2598 EMSG(_("E689: Can only index a List or Dictionary"));
2599 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002602 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 if (!quiet)
2604 EMSG(_("E708: [:] must come last"));
2605 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002607
Bram Moolenaar8c711452005-01-14 21:53:12 +00002608 len = -1;
2609 if (*p == '.')
2610 {
2611 key = p + 1;
2612 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2613 ;
2614 if (len == 0)
2615 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 if (!quiet)
2617 EMSG(_(e_emptykey));
2618 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 }
2620 p = key + len;
2621 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002622 else
2623 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002624 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002625 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002626 if (*p == ':')
2627 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628 else
2629 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002630 empty1 = FALSE;
2631 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002633 if (get_tv_string_chk(&var1) == NULL)
2634 {
2635 /* not a number or string */
2636 clear_tv(&var1);
2637 return NULL;
2638 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 }
2640
2641 /* Optionally get the second index [ :expr]. */
2642 if (*p == ':')
2643 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002647 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002648 if (!empty1)
2649 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002651 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (rettv != NULL && (rettv->v_type != VAR_LIST
2653 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (!quiet)
2656 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 if (!empty1)
2658 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 }
2661 p = skipwhite(p + 1);
2662 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 else
2665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2668 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 if (!empty1)
2670 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002673 if (get_tv_string_chk(&var2) == NULL)
2674 {
2675 /* not a number or string */
2676 if (!empty1)
2677 clear_tv(&var1);
2678 clear_tv(&var2);
2679 return NULL;
2680 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002683 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 if (!quiet)
2690 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 if (!empty1)
2692 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 }
2697
2698 /* Skip to past ']'. */
2699 ++p;
2700 }
2701
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 {
2704 if (len == -1)
2705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002707 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 if (*key == NUL)
2709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (!quiet)
2711 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 }
2715 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002716 lp->ll_list = NULL;
2717 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002718 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002721 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 if (len == -1)
2727 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 }
2730 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 if (len == -1)
2735 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 p = NULL;
2738 break;
2739 }
2740 if (len == -1)
2741 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 }
2744 else
2745 {
2746 /*
2747 * Get the number and item for the only or first index of the List.
2748 */
2749 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 else
2752 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002753 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 clear_tv(&var1);
2755 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002756 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 lp->ll_list = lp->ll_tv->vval.v_list;
2758 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2759 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002761 if (lp->ll_n1 < 0)
2762 {
2763 lp->ll_n1 = 0;
2764 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2765 }
2766 }
2767 if (lp->ll_li == NULL)
2768 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 }
2773
2774 /*
2775 * May need to find the item or absolute index for the second
2776 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 * When no index given: "lp->ll_empty2" is TRUE.
2778 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002782 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 }
2791
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2793 if (lp->ll_n1 < 0)
2794 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2795 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002797 }
2798
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002800 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002801 }
2802
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 return p;
2804}
2805
2806/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002807 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 */
2809 static void
2810clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002811 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812{
2813 vim_free(lp->ll_exp_name);
2814 vim_free(lp->ll_newkey);
2815}
2816
2817/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002818 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002820 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 */
2822 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002823set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002824 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002826 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002828 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829{
2830 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002831 listitem_T *ri;
2832 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833
2834 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002835 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002837 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 cc = *endp;
2839 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002840 if (op != NULL && *op != '=')
2841 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002842 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002843
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002844 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002845 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002846 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002847 {
2848 if (tv_op(&tv, rettv, op) == OK)
2849 set_var(lp->ll_name, &tv, FALSE);
2850 clear_tv(&tv);
2851 }
2852 }
2853 else
2854 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002856 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002858 else if (tv_check_lock(lp->ll_newkey == NULL
2859 ? lp->ll_tv->v_lock
2860 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2861 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 else if (lp->ll_range)
2863 {
2864 /*
2865 * Assign the List values to the list items.
2866 */
2867 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002868 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002869 if (op != NULL && *op != '=')
2870 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2871 else
2872 {
2873 clear_tv(&lp->ll_li->li_tv);
2874 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2875 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 ri = ri->li_next;
2877 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2878 break;
2879 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002880 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002882 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002883 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 ri = NULL;
2885 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002886 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002887 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 lp->ll_li = lp->ll_li->li_next;
2889 ++lp->ll_n1;
2890 }
2891 if (ri != NULL)
2892 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002893 else if (lp->ll_empty2
2894 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 : lp->ll_n1 != lp->ll_n2)
2896 EMSG(_("E711: List value has not enough items"));
2897 }
2898 else
2899 {
2900 /*
2901 * Assign to a List or Dictionary item.
2902 */
2903 if (lp->ll_newkey != NULL)
2904 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002905 if (op != NULL && *op != '=')
2906 {
2907 EMSG2(_(e_letwrong), op);
2908 return;
2909 }
2910
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002912 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 if (di == NULL)
2914 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002915 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2916 {
2917 vim_free(di);
2918 return;
2919 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002921 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002922 else if (op != NULL && *op != '=')
2923 {
2924 tv_op(lp->ll_tv, rettv, op);
2925 return;
2926 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002927 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002929
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 /*
2931 * Assign the value to the variable or list item.
2932 */
2933 if (copy)
2934 copy_tv(rettv, lp->ll_tv);
2935 else
2936 {
2937 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002938 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002940 }
2941 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002942}
2943
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002944/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002945 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2946 * Returns OK or FAIL.
2947 */
2948 static int
2949tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002950 typval_T *tv1;
2951 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002952 char_u *op;
2953{
2954 long n;
2955 char_u numbuf[NUMBUFLEN];
2956 char_u *s;
2957
2958 /* Can't do anything with a Funcref or a Dict on the right. */
2959 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2960 {
2961 switch (tv1->v_type)
2962 {
2963 case VAR_DICT:
2964 case VAR_FUNC:
2965 break;
2966
2967 case VAR_LIST:
2968 if (*op != '+' || tv2->v_type != VAR_LIST)
2969 break;
2970 /* List += List */
2971 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2972 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2973 return OK;
2974
2975 case VAR_NUMBER:
2976 case VAR_STRING:
2977 if (tv2->v_type == VAR_LIST)
2978 break;
2979 if (*op == '+' || *op == '-')
2980 {
2981 /* nr += nr or nr -= nr*/
2982 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002983#ifdef FEAT_FLOAT
2984 if (tv2->v_type == VAR_FLOAT)
2985 {
2986 float_T f = n;
2987
2988 if (*op == '+')
2989 f += tv2->vval.v_float;
2990 else
2991 f -= tv2->vval.v_float;
2992 clear_tv(tv1);
2993 tv1->v_type = VAR_FLOAT;
2994 tv1->vval.v_float = f;
2995 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002996 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002997#endif
2998 {
2999 if (*op == '+')
3000 n += get_tv_number(tv2);
3001 else
3002 n -= get_tv_number(tv2);
3003 clear_tv(tv1);
3004 tv1->v_type = VAR_NUMBER;
3005 tv1->vval.v_number = n;
3006 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003007 }
3008 else
3009 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003010 if (tv2->v_type == VAR_FLOAT)
3011 break;
3012
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003013 /* str .= str */
3014 s = get_tv_string(tv1);
3015 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3016 clear_tv(tv1);
3017 tv1->v_type = VAR_STRING;
3018 tv1->vval.v_string = s;
3019 }
3020 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003021
3022#ifdef FEAT_FLOAT
3023 case VAR_FLOAT:
3024 {
3025 float_T f;
3026
3027 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3028 && tv2->v_type != VAR_NUMBER
3029 && tv2->v_type != VAR_STRING))
3030 break;
3031 if (tv2->v_type == VAR_FLOAT)
3032 f = tv2->vval.v_float;
3033 else
3034 f = get_tv_number(tv2);
3035 if (*op == '+')
3036 tv1->vval.v_float += f;
3037 else
3038 tv1->vval.v_float -= f;
3039 }
3040 return OK;
3041#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003042 }
3043 }
3044
3045 EMSG2(_(e_letwrong), op);
3046 return FAIL;
3047}
3048
3049/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003050 * Add a watcher to a list.
3051 */
3052 static void
3053list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003054 list_T *l;
3055 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003056{
3057 lw->lw_next = l->lv_watch;
3058 l->lv_watch = lw;
3059}
3060
3061/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003062 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003063 * No warning when it isn't found...
3064 */
3065 static void
3066list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003067 list_T *l;
3068 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003069{
Bram Moolenaar33570922005-01-25 22:26:29 +00003070 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003071
3072 lwp = &l->lv_watch;
3073 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3074 {
3075 if (lw == lwrem)
3076 {
3077 *lwp = lw->lw_next;
3078 break;
3079 }
3080 lwp = &lw->lw_next;
3081 }
3082}
3083
3084/*
3085 * Just before removing an item from a list: advance watchers to the next
3086 * item.
3087 */
3088 static void
3089list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003090 list_T *l;
3091 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003092{
Bram Moolenaar33570922005-01-25 22:26:29 +00003093 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003094
3095 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3096 if (lw->lw_item == item)
3097 lw->lw_item = item->li_next;
3098}
3099
3100/*
3101 * Evaluate the expression used in a ":for var in expr" command.
3102 * "arg" points to "var".
3103 * Set "*errp" to TRUE for an error, FALSE otherwise;
3104 * Return a pointer that holds the info. Null when there is an error.
3105 */
3106 void *
3107eval_for_line(arg, errp, nextcmdp, skip)
3108 char_u *arg;
3109 int *errp;
3110 char_u **nextcmdp;
3111 int skip;
3112{
Bram Moolenaar33570922005-01-25 22:26:29 +00003113 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003114 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003115 typval_T tv;
3116 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117
3118 *errp = TRUE; /* default: there is an error */
3119
Bram Moolenaar33570922005-01-25 22:26:29 +00003120 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003121 if (fi == NULL)
3122 return NULL;
3123
3124 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3125 if (expr == NULL)
3126 return fi;
3127
3128 expr = skipwhite(expr);
3129 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3130 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003131 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003132 return fi;
3133 }
3134
3135 if (skip)
3136 ++emsg_skip;
3137 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3138 {
3139 *errp = FALSE;
3140 if (!skip)
3141 {
3142 l = tv.vval.v_list;
3143 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003144 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003145 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003146 clear_tv(&tv);
3147 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003148 else
3149 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003150 /* No need to increment the refcount, it's already set for the
3151 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003152 fi->fi_list = l;
3153 list_add_watch(l, &fi->fi_lw);
3154 fi->fi_lw.lw_item = l->lv_first;
3155 }
3156 }
3157 }
3158 if (skip)
3159 --emsg_skip;
3160
3161 return fi;
3162}
3163
3164/*
3165 * Use the first item in a ":for" list. Advance to the next.
3166 * Assign the values to the variable (list). "arg" points to the first one.
3167 * Return TRUE when a valid item was found, FALSE when at end of list or
3168 * something wrong.
3169 */
3170 int
3171next_for_item(fi_void, arg)
3172 void *fi_void;
3173 char_u *arg;
3174{
Bram Moolenaar33570922005-01-25 22:26:29 +00003175 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003177 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178
3179 item = fi->fi_lw.lw_item;
3180 if (item == NULL)
3181 result = FALSE;
3182 else
3183 {
3184 fi->fi_lw.lw_item = item->li_next;
3185 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3186 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3187 }
3188 return result;
3189}
3190
3191/*
3192 * Free the structure used to store info used by ":for".
3193 */
3194 void
3195free_for_info(fi_void)
3196 void *fi_void;
3197{
Bram Moolenaar33570922005-01-25 22:26:29 +00003198 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003200 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003201 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003203 list_unref(fi->fi_list);
3204 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205 vim_free(fi);
3206}
3207
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3209
3210 void
3211set_context_for_expression(xp, arg, cmdidx)
3212 expand_T *xp;
3213 char_u *arg;
3214 cmdidx_T cmdidx;
3215{
3216 int got_eq = FALSE;
3217 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220 if (cmdidx == CMD_let)
3221 {
3222 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003223 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003224 {
3225 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003226 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227 {
3228 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003229 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003230 if (vim_iswhite(*p))
3231 break;
3232 }
3233 return;
3234 }
3235 }
3236 else
3237 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3238 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 while ((xp->xp_pattern = vim_strpbrk(arg,
3240 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3241 {
3242 c = *xp->xp_pattern;
3243 if (c == '&')
3244 {
3245 c = xp->xp_pattern[1];
3246 if (c == '&')
3247 {
3248 ++xp->xp_pattern;
3249 xp->xp_context = cmdidx != CMD_let || got_eq
3250 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3251 }
3252 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003253 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003255 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3256 xp->xp_pattern += 2;
3257
3258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 }
3260 else if (c == '$')
3261 {
3262 /* environment variable */
3263 xp->xp_context = EXPAND_ENV_VARS;
3264 }
3265 else if (c == '=')
3266 {
3267 got_eq = TRUE;
3268 xp->xp_context = EXPAND_EXPRESSION;
3269 }
3270 else if (c == '<'
3271 && xp->xp_context == EXPAND_FUNCTIONS
3272 && vim_strchr(xp->xp_pattern, '(') == NULL)
3273 {
3274 /* Function name can start with "<SNR>" */
3275 break;
3276 }
3277 else if (cmdidx != CMD_let || got_eq)
3278 {
3279 if (c == '"') /* string */
3280 {
3281 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3282 if (c == '\\' && xp->xp_pattern[1] != NUL)
3283 ++xp->xp_pattern;
3284 xp->xp_context = EXPAND_NOTHING;
3285 }
3286 else if (c == '\'') /* literal string */
3287 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003288 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3290 /* skip */ ;
3291 xp->xp_context = EXPAND_NOTHING;
3292 }
3293 else if (c == '|')
3294 {
3295 if (xp->xp_pattern[1] == '|')
3296 {
3297 ++xp->xp_pattern;
3298 xp->xp_context = EXPAND_EXPRESSION;
3299 }
3300 else
3301 xp->xp_context = EXPAND_COMMANDS;
3302 }
3303 else
3304 xp->xp_context = EXPAND_EXPRESSION;
3305 }
3306 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307 /* Doesn't look like something valid, expand as an expression
3308 * anyway. */
3309 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 arg = xp->xp_pattern;
3311 if (*arg != NUL)
3312 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3313 /* skip */ ;
3314 }
3315 xp->xp_pattern = arg;
3316}
3317
3318#endif /* FEAT_CMDL_COMPL */
3319
3320/*
3321 * ":1,25call func(arg1, arg2)" function call.
3322 */
3323 void
3324ex_call(eap)
3325 exarg_T *eap;
3326{
3327 char_u *arg = eap->arg;
3328 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003330 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003332 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 linenr_T lnum;
3334 int doesrange;
3335 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003336 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003338 if (eap->skip)
3339 {
3340 /* trans_function_name() doesn't work well when skipping, use eval0()
3341 * instead to skip to any following command, e.g. for:
3342 * :if 0 | call dict.foo().bar() | endif */
3343 eval0(eap->arg, &rettv, &eap->nextcmd, FALSE);
3344 return;
3345 }
3346
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003347 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003348 if (fudi.fd_newkey != NULL)
3349 {
3350 /* Still need to give an error message for missing key. */
3351 EMSG2(_(e_dictkey), fudi.fd_newkey);
3352 vim_free(fudi.fd_newkey);
3353 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003354 if (tofree == NULL)
3355 return;
3356
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003357 /* Increase refcount on dictionary, it could get deleted when evaluating
3358 * the arguments. */
3359 if (fudi.fd_dict != NULL)
3360 ++fudi.fd_dict->dv_refcount;
3361
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003362 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003363 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003364 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365
Bram Moolenaar532c7802005-01-27 14:44:31 +00003366 /* Skip white space to allow ":call func ()". Not good, but required for
3367 * backward compatibility. */
3368 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003369 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370
3371 if (*startarg != '(')
3372 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003373 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 goto end;
3375 }
3376
3377 /*
3378 * When skipping, evaluate the function once, to find the end of the
3379 * arguments.
3380 * When the function takes a range, this is discovered after the first
3381 * call, and the loop is broken.
3382 */
3383 if (eap->skip)
3384 {
3385 ++emsg_skip;
3386 lnum = eap->line2; /* do it once, also with an invalid range */
3387 }
3388 else
3389 lnum = eap->line1;
3390 for ( ; lnum <= eap->line2; ++lnum)
3391 {
3392 if (!eap->skip && eap->addr_count > 0)
3393 {
3394 curwin->w_cursor.lnum = lnum;
3395 curwin->w_cursor.col = 0;
3396 }
3397 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003398 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003399 eap->line1, eap->line2, &doesrange,
3400 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 {
3402 failed = TRUE;
3403 break;
3404 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003405
3406 /* Handle a function returning a Funcref, Dictionary or List. */
3407 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3408 {
3409 failed = TRUE;
3410 break;
3411 }
3412
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003413 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (doesrange || eap->skip)
3415 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003416
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003418 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003419 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003420 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 if (aborting())
3422 break;
3423 }
3424 if (eap->skip)
3425 --emsg_skip;
3426
3427 if (!failed)
3428 {
3429 /* Check for trailing illegal characters and a following command. */
3430 if (!ends_excmd(*arg))
3431 {
3432 emsg_severe = TRUE;
3433 EMSG(_(e_trailing));
3434 }
3435 else
3436 eap->nextcmd = check_nextcmd(arg);
3437 }
3438
3439end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003440 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003441 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442}
3443
3444/*
3445 * ":unlet[!] var1 ... " command.
3446 */
3447 void
3448ex_unlet(eap)
3449 exarg_T *eap;
3450{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003451 ex_unletlock(eap, eap->arg, 0);
3452}
3453
3454/*
3455 * ":lockvar" and ":unlockvar" commands
3456 */
3457 void
3458ex_lockvar(eap)
3459 exarg_T *eap;
3460{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003462 int deep = 2;
3463
3464 if (eap->forceit)
3465 deep = -1;
3466 else if (vim_isdigit(*arg))
3467 {
3468 deep = getdigits(&arg);
3469 arg = skipwhite(arg);
3470 }
3471
3472 ex_unletlock(eap, arg, deep);
3473}
3474
3475/*
3476 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3477 */
3478 static void
3479ex_unletlock(eap, argstart, deep)
3480 exarg_T *eap;
3481 char_u *argstart;
3482 int deep;
3483{
3484 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003487 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
3489 do
3490 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003491 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003492 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3493 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003494 if (lv.ll_name == NULL)
3495 error = TRUE; /* error but continue parsing */
3496 if (name_end == NULL || (!vim_iswhite(*name_end)
3497 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003499 if (name_end != NULL)
3500 {
3501 emsg_severe = TRUE;
3502 EMSG(_(e_trailing));
3503 }
3504 if (!(eap->skip || error))
3505 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 break;
3507 }
3508
3509 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003510 {
3511 if (eap->cmdidx == CMD_unlet)
3512 {
3513 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3514 error = TRUE;
3515 }
3516 else
3517 {
3518 if (do_lock_var(&lv, name_end, deep,
3519 eap->cmdidx == CMD_lockvar) == FAIL)
3520 error = TRUE;
3521 }
3522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003524 if (!eap->skip)
3525 clear_lval(&lv);
3526
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 arg = skipwhite(name_end);
3528 } while (!ends_excmd(*arg));
3529
3530 eap->nextcmd = check_nextcmd(arg);
3531}
3532
Bram Moolenaar8c711452005-01-14 21:53:12 +00003533 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003534do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003535 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003536 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003537 int forceit;
3538{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003539 int ret = OK;
3540 int cc;
3541
3542 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003543 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003544 cc = *name_end;
3545 *name_end = NUL;
3546
3547 /* Normal name or expanded name. */
3548 if (check_changedtick(lp->ll_name))
3549 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003550 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003551 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003552 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003553 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003554 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3555 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003556 else if (lp->ll_range)
3557 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003558 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003559
3560 /* Delete a range of List items. */
3561 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3562 {
3563 li = lp->ll_li->li_next;
3564 listitem_remove(lp->ll_list, lp->ll_li);
3565 lp->ll_li = li;
3566 ++lp->ll_n1;
3567 }
3568 }
3569 else
3570 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003571 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003572 /* unlet a List item. */
3573 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003574 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003575 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003576 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003577 }
3578
3579 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003580}
3581
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582/*
3583 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003584 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 */
3586 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003587do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003589 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590{
Bram Moolenaar33570922005-01-25 22:26:29 +00003591 hashtab_T *ht;
3592 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003593 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003594 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
Bram Moolenaar33570922005-01-25 22:26:29 +00003596 ht = find_var_ht(name, &varname);
3597 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003599 hi = hash_find(ht, varname);
3600 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003601 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003602 di = HI2DI(hi);
3603 if (var_check_fixed(di->di_flags, name)
3604 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003605 return FAIL;
3606 delete_var(ht, hi);
3607 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003610 if (forceit)
3611 return OK;
3612 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 return FAIL;
3614}
3615
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003616/*
3617 * Lock or unlock variable indicated by "lp".
3618 * "deep" is the levels to go (-1 for unlimited);
3619 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3620 */
3621 static int
3622do_lock_var(lp, name_end, deep, lock)
3623 lval_T *lp;
3624 char_u *name_end;
3625 int deep;
3626 int lock;
3627{
3628 int ret = OK;
3629 int cc;
3630 dictitem_T *di;
3631
3632 if (deep == 0) /* nothing to do */
3633 return OK;
3634
3635 if (lp->ll_tv == NULL)
3636 {
3637 cc = *name_end;
3638 *name_end = NUL;
3639
3640 /* Normal name or expanded name. */
3641 if (check_changedtick(lp->ll_name))
3642 ret = FAIL;
3643 else
3644 {
3645 di = find_var(lp->ll_name, NULL);
3646 if (di == NULL)
3647 ret = FAIL;
3648 else
3649 {
3650 if (lock)
3651 di->di_flags |= DI_FLAGS_LOCK;
3652 else
3653 di->di_flags &= ~DI_FLAGS_LOCK;
3654 item_lock(&di->di_tv, deep, lock);
3655 }
3656 }
3657 *name_end = cc;
3658 }
3659 else if (lp->ll_range)
3660 {
3661 listitem_T *li = lp->ll_li;
3662
3663 /* (un)lock a range of List items. */
3664 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3665 {
3666 item_lock(&li->li_tv, deep, lock);
3667 li = li->li_next;
3668 ++lp->ll_n1;
3669 }
3670 }
3671 else if (lp->ll_list != NULL)
3672 /* (un)lock a List item. */
3673 item_lock(&lp->ll_li->li_tv, deep, lock);
3674 else
3675 /* un(lock) a Dictionary item. */
3676 item_lock(&lp->ll_di->di_tv, deep, lock);
3677
3678 return ret;
3679}
3680
3681/*
3682 * Lock or unlock an item. "deep" is nr of levels to go.
3683 */
3684 static void
3685item_lock(tv, deep, lock)
3686 typval_T *tv;
3687 int deep;
3688 int lock;
3689{
3690 static int recurse = 0;
3691 list_T *l;
3692 listitem_T *li;
3693 dict_T *d;
3694 hashitem_T *hi;
3695 int todo;
3696
3697 if (recurse >= DICT_MAXNEST)
3698 {
3699 EMSG(_("E743: variable nested too deep for (un)lock"));
3700 return;
3701 }
3702 if (deep == 0)
3703 return;
3704 ++recurse;
3705
3706 /* lock/unlock the item itself */
3707 if (lock)
3708 tv->v_lock |= VAR_LOCKED;
3709 else
3710 tv->v_lock &= ~VAR_LOCKED;
3711
3712 switch (tv->v_type)
3713 {
3714 case VAR_LIST:
3715 if ((l = tv->vval.v_list) != NULL)
3716 {
3717 if (lock)
3718 l->lv_lock |= VAR_LOCKED;
3719 else
3720 l->lv_lock &= ~VAR_LOCKED;
3721 if (deep < 0 || deep > 1)
3722 /* recursive: lock/unlock the items the List contains */
3723 for (li = l->lv_first; li != NULL; li = li->li_next)
3724 item_lock(&li->li_tv, deep - 1, lock);
3725 }
3726 break;
3727 case VAR_DICT:
3728 if ((d = tv->vval.v_dict) != NULL)
3729 {
3730 if (lock)
3731 d->dv_lock |= VAR_LOCKED;
3732 else
3733 d->dv_lock &= ~VAR_LOCKED;
3734 if (deep < 0 || deep > 1)
3735 {
3736 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003737 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003738 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3739 {
3740 if (!HASHITEM_EMPTY(hi))
3741 {
3742 --todo;
3743 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3744 }
3745 }
3746 }
3747 }
3748 }
3749 --recurse;
3750}
3751
Bram Moolenaara40058a2005-07-11 22:42:07 +00003752/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003753 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3754 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003755 */
3756 static int
3757tv_islocked(tv)
3758 typval_T *tv;
3759{
3760 return (tv->v_lock & VAR_LOCKED)
3761 || (tv->v_type == VAR_LIST
3762 && tv->vval.v_list != NULL
3763 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3764 || (tv->v_type == VAR_DICT
3765 && tv->vval.v_dict != NULL
3766 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3767}
3768
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3770/*
3771 * Delete all "menutrans_" variables.
3772 */
3773 void
3774del_menutrans_vars()
3775{
Bram Moolenaar33570922005-01-25 22:26:29 +00003776 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003777 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778
Bram Moolenaar33570922005-01-25 22:26:29 +00003779 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003780 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003781 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003782 {
3783 if (!HASHITEM_EMPTY(hi))
3784 {
3785 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003786 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3787 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003788 }
3789 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003790 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791}
3792#endif
3793
3794#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3795
3796/*
3797 * Local string buffer for the next two functions to store a variable name
3798 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3799 * get_user_var_name().
3800 */
3801
3802static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3803
3804static char_u *varnamebuf = NULL;
3805static int varnamebuflen = 0;
3806
3807/*
3808 * Function to concatenate a prefix and a variable name.
3809 */
3810 static char_u *
3811cat_prefix_varname(prefix, name)
3812 int prefix;
3813 char_u *name;
3814{
3815 int len;
3816
3817 len = (int)STRLEN(name) + 3;
3818 if (len > varnamebuflen)
3819 {
3820 vim_free(varnamebuf);
3821 len += 10; /* some additional space */
3822 varnamebuf = alloc(len);
3823 if (varnamebuf == NULL)
3824 {
3825 varnamebuflen = 0;
3826 return NULL;
3827 }
3828 varnamebuflen = len;
3829 }
3830 *varnamebuf = prefix;
3831 varnamebuf[1] = ':';
3832 STRCPY(varnamebuf + 2, name);
3833 return varnamebuf;
3834}
3835
3836/*
3837 * Function given to ExpandGeneric() to obtain the list of user defined
3838 * (global/buffer/window/built-in) variable names.
3839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 char_u *
3841get_user_var_name(xp, idx)
3842 expand_T *xp;
3843 int idx;
3844{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003845 static long_u gdone;
3846 static long_u bdone;
3847 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003848#ifdef FEAT_WINDOWS
3849 static long_u tdone;
3850#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003851 static int vidx;
3852 static hashitem_T *hi;
3853 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854
3855 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003856 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003857 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003858#ifdef FEAT_WINDOWS
3859 tdone = 0;
3860#endif
3861 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003862
3863 /* Global variables */
3864 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003866 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003867 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003868 else
3869 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003870 while (HASHITEM_EMPTY(hi))
3871 ++hi;
3872 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3873 return cat_prefix_varname('g', hi->hi_key);
3874 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003876
3877 /* b: variables */
3878 ht = &curbuf->b_vars.dv_hashtab;
3879 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003881 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003882 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003883 else
3884 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003885 while (HASHITEM_EMPTY(hi))
3886 ++hi;
3887 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003889 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003891 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 return (char_u *)"b:changedtick";
3893 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003894
3895 /* w: variables */
3896 ht = &curwin->w_vars.dv_hashtab;
3897 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003899 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003900 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003901 else
3902 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003903 while (HASHITEM_EMPTY(hi))
3904 ++hi;
3905 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003907
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003908#ifdef FEAT_WINDOWS
3909 /* t: variables */
3910 ht = &curtab->tp_vars.dv_hashtab;
3911 if (tdone < ht->ht_used)
3912 {
3913 if (tdone++ == 0)
3914 hi = ht->ht_array;
3915 else
3916 ++hi;
3917 while (HASHITEM_EMPTY(hi))
3918 ++hi;
3919 return cat_prefix_varname('t', hi->hi_key);
3920 }
3921#endif
3922
Bram Moolenaar33570922005-01-25 22:26:29 +00003923 /* v: variables */
3924 if (vidx < VV_LEN)
3925 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
3927 vim_free(varnamebuf);
3928 varnamebuf = NULL;
3929 varnamebuflen = 0;
3930 return NULL;
3931}
3932
3933#endif /* FEAT_CMDL_COMPL */
3934
3935/*
3936 * types for expressions.
3937 */
3938typedef enum
3939{
3940 TYPE_UNKNOWN = 0
3941 , TYPE_EQUAL /* == */
3942 , TYPE_NEQUAL /* != */
3943 , TYPE_GREATER /* > */
3944 , TYPE_GEQUAL /* >= */
3945 , TYPE_SMALLER /* < */
3946 , TYPE_SEQUAL /* <= */
3947 , TYPE_MATCH /* =~ */
3948 , TYPE_NOMATCH /* !~ */
3949} exptype_T;
3950
3951/*
3952 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003953 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3955 */
3956
3957/*
3958 * Handle zero level expression.
3959 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003960 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003961 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 * Return OK or FAIL.
3963 */
3964 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003965eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 char_u **nextcmd;
3969 int evaluate;
3970{
3971 int ret;
3972 char_u *p;
3973
3974 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003975 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 if (ret == FAIL || !ends_excmd(*p))
3977 {
3978 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003979 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 /*
3981 * Report the invalid expression unless the expression evaluation has
3982 * been cancelled due to an aborting error, an interrupt, or an
3983 * exception.
3984 */
3985 if (!aborting())
3986 EMSG2(_(e_invexpr2), arg);
3987 ret = FAIL;
3988 }
3989 if (nextcmd != NULL)
3990 *nextcmd = check_nextcmd(p);
3991
3992 return ret;
3993}
3994
3995/*
3996 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003997 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 *
3999 * "arg" must point to the first non-white of the expression.
4000 * "arg" is advanced to the next non-white after the recognized expression.
4001 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004002 * Note: "rettv.v_lock" is not set.
4003 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 * Return OK or FAIL.
4005 */
4006 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004007eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 int evaluate;
4011{
4012 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004013 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014
4015 /*
4016 * Get the first variable.
4017 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004018 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 return FAIL;
4020
4021 if ((*arg)[0] == '?')
4022 {
4023 result = FALSE;
4024 if (evaluate)
4025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004026 int error = FALSE;
4027
4028 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004030 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004031 if (error)
4032 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
4034
4035 /*
4036 * Get the second variable.
4037 */
4038 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004039 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 return FAIL;
4041
4042 /*
4043 * Check for the ":".
4044 */
4045 if ((*arg)[0] != ':')
4046 {
4047 EMSG(_("E109: Missing ':' after '?'"));
4048 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004049 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 return FAIL;
4051 }
4052
4053 /*
4054 * Get the third variable.
4055 */
4056 *arg = skipwhite(*arg + 1);
4057 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4058 {
4059 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004060 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 return FAIL;
4062 }
4063 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004064 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 }
4066
4067 return OK;
4068}
4069
4070/*
4071 * Handle first level expression:
4072 * expr2 || expr2 || expr2 logical OR
4073 *
4074 * "arg" must point to the first non-white of the expression.
4075 * "arg" is advanced to the next non-white after the recognized expression.
4076 *
4077 * Return OK or FAIL.
4078 */
4079 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004080eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 int evaluate;
4084{
Bram Moolenaar33570922005-01-25 22:26:29 +00004085 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 long result;
4087 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004088 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089
4090 /*
4091 * Get the first variable.
4092 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004093 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 return FAIL;
4095
4096 /*
4097 * Repeat until there is no following "||".
4098 */
4099 first = TRUE;
4100 result = FALSE;
4101 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4102 {
4103 if (evaluate && first)
4104 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004105 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004107 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004108 if (error)
4109 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 first = FALSE;
4111 }
4112
4113 /*
4114 * Get the second variable.
4115 */
4116 *arg = skipwhite(*arg + 2);
4117 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4118 return FAIL;
4119
4120 /*
4121 * Compute the result.
4122 */
4123 if (evaluate && !result)
4124 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004125 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004128 if (error)
4129 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 }
4131 if (evaluate)
4132 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 rettv->v_type = VAR_NUMBER;
4134 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 }
4136 }
4137
4138 return OK;
4139}
4140
4141/*
4142 * Handle second level expression:
4143 * expr3 && expr3 && expr3 logical AND
4144 *
4145 * "arg" must point to the first non-white of the expression.
4146 * "arg" is advanced to the next non-white after the recognized expression.
4147 *
4148 * Return OK or FAIL.
4149 */
4150 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 int evaluate;
4155{
Bram Moolenaar33570922005-01-25 22:26:29 +00004156 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 long result;
4158 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004159 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
4161 /*
4162 * Get the first variable.
4163 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 return FAIL;
4166
4167 /*
4168 * Repeat until there is no following "&&".
4169 */
4170 first = TRUE;
4171 result = TRUE;
4172 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4173 {
4174 if (evaluate && first)
4175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004176 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004179 if (error)
4180 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 first = FALSE;
4182 }
4183
4184 /*
4185 * Get the second variable.
4186 */
4187 *arg = skipwhite(*arg + 2);
4188 if (eval4(arg, &var2, evaluate && result) == FAIL)
4189 return FAIL;
4190
4191 /*
4192 * Compute the result.
4193 */
4194 if (evaluate && result)
4195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004196 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004199 if (error)
4200 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 }
4202 if (evaluate)
4203 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 rettv->v_type = VAR_NUMBER;
4205 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 }
4207 }
4208
4209 return OK;
4210}
4211
4212/*
4213 * Handle third level expression:
4214 * var1 == var2
4215 * var1 =~ var2
4216 * var1 != var2
4217 * var1 !~ var2
4218 * var1 > var2
4219 * var1 >= var2
4220 * var1 < var2
4221 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004222 * var1 is var2
4223 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 *
4225 * "arg" must point to the first non-white of the expression.
4226 * "arg" is advanced to the next non-white after the recognized expression.
4227 *
4228 * Return OK or FAIL.
4229 */
4230 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 int evaluate;
4235{
Bram Moolenaar33570922005-01-25 22:26:29 +00004236 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 char_u *p;
4238 int i;
4239 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004240 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 int len = 2;
4242 long n1, n2;
4243 char_u *s1, *s2;
4244 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4245 regmatch_T regmatch;
4246 int ic;
4247 char_u *save_cpo;
4248
4249 /*
4250 * Get the first variable.
4251 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 return FAIL;
4254
4255 p = *arg;
4256 switch (p[0])
4257 {
4258 case '=': if (p[1] == '=')
4259 type = TYPE_EQUAL;
4260 else if (p[1] == '~')
4261 type = TYPE_MATCH;
4262 break;
4263 case '!': if (p[1] == '=')
4264 type = TYPE_NEQUAL;
4265 else if (p[1] == '~')
4266 type = TYPE_NOMATCH;
4267 break;
4268 case '>': if (p[1] != '=')
4269 {
4270 type = TYPE_GREATER;
4271 len = 1;
4272 }
4273 else
4274 type = TYPE_GEQUAL;
4275 break;
4276 case '<': if (p[1] != '=')
4277 {
4278 type = TYPE_SMALLER;
4279 len = 1;
4280 }
4281 else
4282 type = TYPE_SEQUAL;
4283 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004284 case 'i': if (p[1] == 's')
4285 {
4286 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4287 len = 5;
4288 if (!vim_isIDc(p[len]))
4289 {
4290 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4291 type_is = TRUE;
4292 }
4293 }
4294 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296
4297 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004298 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 */
4300 if (type != TYPE_UNKNOWN)
4301 {
4302 /* extra question mark appended: ignore case */
4303 if (p[len] == '?')
4304 {
4305 ic = TRUE;
4306 ++len;
4307 }
4308 /* extra '#' appended: match case */
4309 else if (p[len] == '#')
4310 {
4311 ic = FALSE;
4312 ++len;
4313 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004314 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 else
4316 ic = p_ic;
4317
4318 /*
4319 * Get the second variable.
4320 */
4321 *arg = skipwhite(p + len);
4322 if (eval5(arg, &var2, evaluate) == FAIL)
4323 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 return FAIL;
4326 }
4327
4328 if (evaluate)
4329 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004330 if (type_is && rettv->v_type != var2.v_type)
4331 {
4332 /* For "is" a different type always means FALSE, for "notis"
4333 * it means TRUE. */
4334 n1 = (type == TYPE_NEQUAL);
4335 }
4336 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4337 {
4338 if (type_is)
4339 {
4340 n1 = (rettv->v_type == var2.v_type
4341 && rettv->vval.v_list == var2.vval.v_list);
4342 if (type == TYPE_NEQUAL)
4343 n1 = !n1;
4344 }
4345 else if (rettv->v_type != var2.v_type
4346 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4347 {
4348 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004349 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004350 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004351 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004352 clear_tv(rettv);
4353 clear_tv(&var2);
4354 return FAIL;
4355 }
4356 else
4357 {
4358 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004359 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4360 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004361 if (type == TYPE_NEQUAL)
4362 n1 = !n1;
4363 }
4364 }
4365
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004366 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4367 {
4368 if (type_is)
4369 {
4370 n1 = (rettv->v_type == var2.v_type
4371 && rettv->vval.v_dict == var2.vval.v_dict);
4372 if (type == TYPE_NEQUAL)
4373 n1 = !n1;
4374 }
4375 else if (rettv->v_type != var2.v_type
4376 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4377 {
4378 if (rettv->v_type != var2.v_type)
4379 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4380 else
4381 EMSG(_("E736: Invalid operation for Dictionary"));
4382 clear_tv(rettv);
4383 clear_tv(&var2);
4384 return FAIL;
4385 }
4386 else
4387 {
4388 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004389 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4390 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004391 if (type == TYPE_NEQUAL)
4392 n1 = !n1;
4393 }
4394 }
4395
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004396 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4397 {
4398 if (rettv->v_type != var2.v_type
4399 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4400 {
4401 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004402 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004403 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004404 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004405 clear_tv(rettv);
4406 clear_tv(&var2);
4407 return FAIL;
4408 }
4409 else
4410 {
4411 /* Compare two Funcrefs for being equal or unequal. */
4412 if (rettv->vval.v_string == NULL
4413 || var2.vval.v_string == NULL)
4414 n1 = FALSE;
4415 else
4416 n1 = STRCMP(rettv->vval.v_string,
4417 var2.vval.v_string) == 0;
4418 if (type == TYPE_NEQUAL)
4419 n1 = !n1;
4420 }
4421 }
4422
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004423#ifdef FEAT_FLOAT
4424 /*
4425 * If one of the two variables is a float, compare as a float.
4426 * When using "=~" or "!~", always compare as string.
4427 */
4428 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4429 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4430 {
4431 float_T f1, f2;
4432
4433 if (rettv->v_type == VAR_FLOAT)
4434 f1 = rettv->vval.v_float;
4435 else
4436 f1 = get_tv_number(rettv);
4437 if (var2.v_type == VAR_FLOAT)
4438 f2 = var2.vval.v_float;
4439 else
4440 f2 = get_tv_number(&var2);
4441 n1 = FALSE;
4442 switch (type)
4443 {
4444 case TYPE_EQUAL: n1 = (f1 == f2); break;
4445 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4446 case TYPE_GREATER: n1 = (f1 > f2); break;
4447 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4448 case TYPE_SMALLER: n1 = (f1 < f2); break;
4449 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4450 case TYPE_UNKNOWN:
4451 case TYPE_MATCH:
4452 case TYPE_NOMATCH: break; /* avoid gcc warning */
4453 }
4454 }
4455#endif
4456
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 /*
4458 * If one of the two variables is a number, compare as a number.
4459 * When using "=~" or "!~", always compare as string.
4460 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004461 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4463 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004464 n1 = get_tv_number(rettv);
4465 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 switch (type)
4467 {
4468 case TYPE_EQUAL: n1 = (n1 == n2); break;
4469 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4470 case TYPE_GREATER: n1 = (n1 > n2); break;
4471 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4472 case TYPE_SMALLER: n1 = (n1 < n2); break;
4473 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4474 case TYPE_UNKNOWN:
4475 case TYPE_MATCH:
4476 case TYPE_NOMATCH: break; /* avoid gcc warning */
4477 }
4478 }
4479 else
4480 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004481 s1 = get_tv_string_buf(rettv, buf1);
4482 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4484 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4485 else
4486 i = 0;
4487 n1 = FALSE;
4488 switch (type)
4489 {
4490 case TYPE_EQUAL: n1 = (i == 0); break;
4491 case TYPE_NEQUAL: n1 = (i != 0); break;
4492 case TYPE_GREATER: n1 = (i > 0); break;
4493 case TYPE_GEQUAL: n1 = (i >= 0); break;
4494 case TYPE_SMALLER: n1 = (i < 0); break;
4495 case TYPE_SEQUAL: n1 = (i <= 0); break;
4496
4497 case TYPE_MATCH:
4498 case TYPE_NOMATCH:
4499 /* avoid 'l' flag in 'cpoptions' */
4500 save_cpo = p_cpo;
4501 p_cpo = (char_u *)"";
4502 regmatch.regprog = vim_regcomp(s2,
4503 RE_MAGIC + RE_STRING);
4504 regmatch.rm_ic = ic;
4505 if (regmatch.regprog != NULL)
4506 {
4507 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4508 vim_free(regmatch.regprog);
4509 if (type == TYPE_NOMATCH)
4510 n1 = !n1;
4511 }
4512 p_cpo = save_cpo;
4513 break;
4514
4515 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4516 }
4517 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004518 clear_tv(rettv);
4519 clear_tv(&var2);
4520 rettv->v_type = VAR_NUMBER;
4521 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 }
4523 }
4524
4525 return OK;
4526}
4527
4528/*
4529 * Handle fourth level expression:
4530 * + number addition
4531 * - number subtraction
4532 * . string concatenation
4533 *
4534 * "arg" must point to the first non-white of the expression.
4535 * "arg" is advanced to the next non-white after the recognized expression.
4536 *
4537 * Return OK or FAIL.
4538 */
4539 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004540eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 int evaluate;
4544{
Bram Moolenaar33570922005-01-25 22:26:29 +00004545 typval_T var2;
4546 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 int op;
4548 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004549#ifdef FEAT_FLOAT
4550 float_T f1 = 0, f2 = 0;
4551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 char_u *s1, *s2;
4553 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4554 char_u *p;
4555
4556 /*
4557 * Get the first variable.
4558 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004559 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 return FAIL;
4561
4562 /*
4563 * Repeat computing, until no '+', '-' or '.' is following.
4564 */
4565 for (;;)
4566 {
4567 op = **arg;
4568 if (op != '+' && op != '-' && op != '.')
4569 break;
4570
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004571 if ((op != '+' || rettv->v_type != VAR_LIST)
4572#ifdef FEAT_FLOAT
4573 && (op == '.' || rettv->v_type != VAR_FLOAT)
4574#endif
4575 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004576 {
4577 /* For "list + ...", an illegal use of the first operand as
4578 * a number cannot be determined before evaluating the 2nd
4579 * operand: if this is also a list, all is ok.
4580 * For "something . ...", "something - ..." or "non-list + ...",
4581 * we know that the first operand needs to be a string or number
4582 * without evaluating the 2nd operand. So check before to avoid
4583 * side effects after an error. */
4584 if (evaluate && get_tv_string_chk(rettv) == NULL)
4585 {
4586 clear_tv(rettv);
4587 return FAIL;
4588 }
4589 }
4590
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 /*
4592 * Get the second variable.
4593 */
4594 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004595 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004597 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 return FAIL;
4599 }
4600
4601 if (evaluate)
4602 {
4603 /*
4604 * Compute the result.
4605 */
4606 if (op == '.')
4607 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004608 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4609 s2 = get_tv_string_buf_chk(&var2, buf2);
4610 if (s2 == NULL) /* type error ? */
4611 {
4612 clear_tv(rettv);
4613 clear_tv(&var2);
4614 return FAIL;
4615 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004616 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004617 clear_tv(rettv);
4618 rettv->v_type = VAR_STRING;
4619 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004621 else if (op == '+' && rettv->v_type == VAR_LIST
4622 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004623 {
4624 /* concatenate Lists */
4625 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4626 &var3) == FAIL)
4627 {
4628 clear_tv(rettv);
4629 clear_tv(&var2);
4630 return FAIL;
4631 }
4632 clear_tv(rettv);
4633 *rettv = var3;
4634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 else
4636 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004637 int error = FALSE;
4638
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004639#ifdef FEAT_FLOAT
4640 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004641 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004642 f1 = rettv->vval.v_float;
4643 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004644 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004645 else
4646#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004647 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004648 n1 = get_tv_number_chk(rettv, &error);
4649 if (error)
4650 {
4651 /* This can only happen for "list + non-list". For
4652 * "non-list + ..." or "something - ...", we returned
4653 * before evaluating the 2nd operand. */
4654 clear_tv(rettv);
4655 return FAIL;
4656 }
4657#ifdef FEAT_FLOAT
4658 if (var2.v_type == VAR_FLOAT)
4659 f1 = n1;
4660#endif
4661 }
4662#ifdef FEAT_FLOAT
4663 if (var2.v_type == VAR_FLOAT)
4664 {
4665 f2 = var2.vval.v_float;
4666 n2 = 0;
4667 }
4668 else
4669#endif
4670 {
4671 n2 = get_tv_number_chk(&var2, &error);
4672 if (error)
4673 {
4674 clear_tv(rettv);
4675 clear_tv(&var2);
4676 return FAIL;
4677 }
4678#ifdef FEAT_FLOAT
4679 if (rettv->v_type == VAR_FLOAT)
4680 f2 = n2;
4681#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004682 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004683 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004684
4685#ifdef FEAT_FLOAT
4686 /* If there is a float on either side the result is a float. */
4687 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4688 {
4689 if (op == '+')
4690 f1 = f1 + f2;
4691 else
4692 f1 = f1 - f2;
4693 rettv->v_type = VAR_FLOAT;
4694 rettv->vval.v_float = f1;
4695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004697#endif
4698 {
4699 if (op == '+')
4700 n1 = n1 + n2;
4701 else
4702 n1 = n1 - n2;
4703 rettv->v_type = VAR_NUMBER;
4704 rettv->vval.v_number = n1;
4705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004707 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
4709 }
4710 return OK;
4711}
4712
4713/*
4714 * Handle fifth level expression:
4715 * * number multiplication
4716 * / number division
4717 * % number modulo
4718 *
4719 * "arg" must point to the first non-white of the expression.
4720 * "arg" is advanced to the next non-white after the recognized expression.
4721 *
4722 * Return OK or FAIL.
4723 */
4724 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004725eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004729 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
Bram Moolenaar33570922005-01-25 22:26:29 +00004731 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 int op;
4733 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004734#ifdef FEAT_FLOAT
4735 int use_float = FALSE;
4736 float_T f1 = 0, f2;
4737#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004738 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739
4740 /*
4741 * Get the first variable.
4742 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004743 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 return FAIL;
4745
4746 /*
4747 * Repeat computing, until no '*', '/' or '%' is following.
4748 */
4749 for (;;)
4750 {
4751 op = **arg;
4752 if (op != '*' && op != '/' && op != '%')
4753 break;
4754
4755 if (evaluate)
4756 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004757#ifdef FEAT_FLOAT
4758 if (rettv->v_type == VAR_FLOAT)
4759 {
4760 f1 = rettv->vval.v_float;
4761 use_float = TRUE;
4762 n1 = 0;
4763 }
4764 else
4765#endif
4766 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004767 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004768 if (error)
4769 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 }
4771 else
4772 n1 = 0;
4773
4774 /*
4775 * Get the second variable.
4776 */
4777 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004778 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 return FAIL;
4780
4781 if (evaluate)
4782 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004783#ifdef FEAT_FLOAT
4784 if (var2.v_type == VAR_FLOAT)
4785 {
4786 if (!use_float)
4787 {
4788 f1 = n1;
4789 use_float = TRUE;
4790 }
4791 f2 = var2.vval.v_float;
4792 n2 = 0;
4793 }
4794 else
4795#endif
4796 {
4797 n2 = get_tv_number_chk(&var2, &error);
4798 clear_tv(&var2);
4799 if (error)
4800 return FAIL;
4801#ifdef FEAT_FLOAT
4802 if (use_float)
4803 f2 = n2;
4804#endif
4805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806
4807 /*
4808 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004809 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004811#ifdef FEAT_FLOAT
4812 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004814 if (op == '*')
4815 f1 = f1 * f2;
4816 else if (op == '/')
4817 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004818# ifdef VMS
4819 /* VMS crashes on divide by zero, work around it */
4820 if (f2 == 0.0)
4821 {
4822 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004823 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004824 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004825 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004826 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004827 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004828 }
4829 else
4830 f1 = f1 / f2;
4831# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004832 /* We rely on the floating point library to handle divide
4833 * by zero to result in "inf" and not a crash. */
4834 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004835# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004838 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004839 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004840 return FAIL;
4841 }
4842 rettv->v_type = VAR_FLOAT;
4843 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 }
4845 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004848 if (op == '*')
4849 n1 = n1 * n2;
4850 else if (op == '/')
4851 {
4852 if (n2 == 0) /* give an error message? */
4853 {
4854 if (n1 == 0)
4855 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4856 else if (n1 < 0)
4857 n1 = -0x7fffffffL;
4858 else
4859 n1 = 0x7fffffffL;
4860 }
4861 else
4862 n1 = n1 / n2;
4863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004865 {
4866 if (n2 == 0) /* give an error message? */
4867 n1 = 0;
4868 else
4869 n1 = n1 % n2;
4870 }
4871 rettv->v_type = VAR_NUMBER;
4872 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 }
4875 }
4876
4877 return OK;
4878}
4879
4880/*
4881 * Handle sixth level expression:
4882 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004883 * "string" string constant
4884 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 * &option-name option value
4886 * @r register contents
4887 * identifier variable value
4888 * function() function call
4889 * $VAR environment variable
4890 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004891 * [expr, expr] List
4892 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 *
4894 * Also handle:
4895 * ! in front logical NOT
4896 * - in front unary minus
4897 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004898 * trailing [] subscript in String or List
4899 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 *
4901 * "arg" must point to the first non-white of the expression.
4902 * "arg" is advanced to the next non-white after the recognized expression.
4903 *
4904 * Return OK or FAIL.
4905 */
4906 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004907eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004911 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 long n;
4914 int len;
4915 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 char_u *start_leader, *end_leader;
4917 int ret = OK;
4918 char_u *alias;
4919
4920 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004921 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004922 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004924 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925
4926 /*
4927 * Skip '!' and '-' characters. They are handled later.
4928 */
4929 start_leader = *arg;
4930 while (**arg == '!' || **arg == '-' || **arg == '+')
4931 *arg = skipwhite(*arg + 1);
4932 end_leader = *arg;
4933
4934 switch (**arg)
4935 {
4936 /*
4937 * Number constant.
4938 */
4939 case '0':
4940 case '1':
4941 case '2':
4942 case '3':
4943 case '4':
4944 case '5':
4945 case '6':
4946 case '7':
4947 case '8':
4948 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004949 {
4950#ifdef FEAT_FLOAT
4951 char_u *p = skipdigits(*arg + 1);
4952 int get_float = FALSE;
4953
4954 /* We accept a float when the format matches
4955 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004956 * strict to avoid backwards compatibility problems.
4957 * Don't look for a float after the "." operator, so that
4958 * ":let vers = 1.2.3" doesn't fail. */
4959 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004961 get_float = TRUE;
4962 p = skipdigits(p + 2);
4963 if (*p == 'e' || *p == 'E')
4964 {
4965 ++p;
4966 if (*p == '-' || *p == '+')
4967 ++p;
4968 if (!vim_isdigit(*p))
4969 get_float = FALSE;
4970 else
4971 p = skipdigits(p + 1);
4972 }
4973 if (ASCII_ISALPHA(*p) || *p == '.')
4974 get_float = FALSE;
4975 }
4976 if (get_float)
4977 {
4978 float_T f;
4979
4980 *arg += string2float(*arg, &f);
4981 if (evaluate)
4982 {
4983 rettv->v_type = VAR_FLOAT;
4984 rettv->vval.v_float = f;
4985 }
4986 }
4987 else
4988#endif
4989 {
4990 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4991 *arg += len;
4992 if (evaluate)
4993 {
4994 rettv->v_type = VAR_NUMBER;
4995 rettv->vval.v_number = n;
4996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 }
4998 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000
5001 /*
5002 * String constant: "string".
5003 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005004 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 break;
5006
5007 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005008 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005010 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005011 break;
5012
5013 /*
5014 * List: [expr, expr]
5015 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005016 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 break;
5018
5019 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005020 * Dictionary: {key: val, key: val}
5021 */
5022 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5023 break;
5024
5025 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005026 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005028 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 break;
5030
5031 /*
5032 * Environment variable: $VAR.
5033 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005034 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 break;
5036
5037 /*
5038 * Register contents: @r.
5039 */
5040 case '@': ++*arg;
5041 if (evaluate)
5042 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005043 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005044 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
5046 if (**arg != NUL)
5047 ++*arg;
5048 break;
5049
5050 /*
5051 * nested expression: (expression).
5052 */
5053 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005054 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 if (**arg == ')')
5056 ++*arg;
5057 else if (ret == OK)
5058 {
5059 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005060 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 ret = FAIL;
5062 }
5063 break;
5064
Bram Moolenaar8c711452005-01-14 21:53:12 +00005065 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 break;
5067 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005068
5069 if (ret == NOTDONE)
5070 {
5071 /*
5072 * Must be a variable or function name.
5073 * Can also be a curly-braces kind of name: {expr}.
5074 */
5075 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005076 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 if (alias != NULL)
5078 s = alias;
5079
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005080 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 ret = FAIL;
5082 else
5083 {
5084 if (**arg == '(') /* recursive! */
5085 {
5086 /* If "s" is the name of a variable of type VAR_FUNC
5087 * use its contents. */
5088 s = deref_func_name(s, &len);
5089
5090 /* Invoke the function. */
5091 ret = get_func_tv(s, len, rettv, arg,
5092 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005093 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 /* Stop the expression evaluation when immediately
5095 * aborting on error, or when an interrupt occurred or
5096 * an exception was thrown but not caught. */
5097 if (aborting())
5098 {
5099 if (ret == OK)
5100 clear_tv(rettv);
5101 ret = FAIL;
5102 }
5103 }
5104 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005105 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005106 else
5107 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005109 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005110 }
5111
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 *arg = skipwhite(*arg);
5113
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005114 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5115 * expr(expr). */
5116 if (ret == OK)
5117 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118
5119 /*
5120 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5121 */
5122 if (ret == OK && evaluate && end_leader > start_leader)
5123 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005124 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005125 int val = 0;
5126#ifdef FEAT_FLOAT
5127 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005128
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005129 if (rettv->v_type == VAR_FLOAT)
5130 f = rettv->vval.v_float;
5131 else
5132#endif
5133 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005134 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005136 clear_tv(rettv);
5137 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005139 else
5140 {
5141 while (end_leader > start_leader)
5142 {
5143 --end_leader;
5144 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005145 {
5146#ifdef FEAT_FLOAT
5147 if (rettv->v_type == VAR_FLOAT)
5148 f = !f;
5149 else
5150#endif
5151 val = !val;
5152 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005153 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005154 {
5155#ifdef FEAT_FLOAT
5156 if (rettv->v_type == VAR_FLOAT)
5157 f = -f;
5158 else
5159#endif
5160 val = -val;
5161 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005162 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005163#ifdef FEAT_FLOAT
5164 if (rettv->v_type == VAR_FLOAT)
5165 {
5166 clear_tv(rettv);
5167 rettv->vval.v_float = f;
5168 }
5169 else
5170#endif
5171 {
5172 clear_tv(rettv);
5173 rettv->v_type = VAR_NUMBER;
5174 rettv->vval.v_number = val;
5175 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
5178
5179 return ret;
5180}
5181
5182/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005183 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5184 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005185 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5186 */
5187 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005188eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005189 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005190 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005191 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005192 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005193{
5194 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005195 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005196 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005197 long len = -1;
5198 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005199 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005201
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005202 if (rettv->v_type == VAR_FUNC
5203#ifdef FEAT_FLOAT
5204 || rettv->v_type == VAR_FLOAT
5205#endif
5206 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005207 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005208 if (verbose)
5209 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005210 return FAIL;
5211 }
5212
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005214 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215 /*
5216 * dict.name
5217 */
5218 key = *arg + 1;
5219 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5220 ;
5221 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005222 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005224 }
5225 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005227 /*
5228 * something[idx]
5229 *
5230 * Get the (first) variable from inside the [].
5231 */
5232 *arg = skipwhite(*arg + 1);
5233 if (**arg == ':')
5234 empty1 = TRUE;
5235 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5236 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005237 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5238 {
5239 /* not a number or string */
5240 clear_tv(&var1);
5241 return FAIL;
5242 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005243
5244 /*
5245 * Get the second variable from inside the [:].
5246 */
5247 if (**arg == ':')
5248 {
5249 range = TRUE;
5250 *arg = skipwhite(*arg + 1);
5251 if (**arg == ']')
5252 empty2 = TRUE;
5253 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005255 if (!empty1)
5256 clear_tv(&var1);
5257 return FAIL;
5258 }
5259 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5260 {
5261 /* not a number or string */
5262 if (!empty1)
5263 clear_tv(&var1);
5264 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 return FAIL;
5266 }
5267 }
5268
5269 /* Check for the ']'. */
5270 if (**arg != ']')
5271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005272 if (verbose)
5273 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005274 clear_tv(&var1);
5275 if (range)
5276 clear_tv(&var2);
5277 return FAIL;
5278 }
5279 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 }
5281
5282 if (evaluate)
5283 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 n1 = 0;
5285 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005287 n1 = get_tv_number(&var1);
5288 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289 }
5290 if (range)
5291 {
5292 if (empty2)
5293 n2 = -1;
5294 else
5295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005296 n2 = get_tv_number(&var2);
5297 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 }
5299 }
5300
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005301 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302 {
5303 case VAR_NUMBER:
5304 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005305 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306 len = (long)STRLEN(s);
5307 if (range)
5308 {
5309 /* The resulting variable is a substring. If the indexes
5310 * are out of range the result is empty. */
5311 if (n1 < 0)
5312 {
5313 n1 = len + n1;
5314 if (n1 < 0)
5315 n1 = 0;
5316 }
5317 if (n2 < 0)
5318 n2 = len + n2;
5319 else if (n2 >= len)
5320 n2 = len;
5321 if (n1 >= len || n2 < 0 || n1 > n2)
5322 s = NULL;
5323 else
5324 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5325 }
5326 else
5327 {
5328 /* The resulting variable is a string of a single
5329 * character. If the index is too big or negative the
5330 * result is empty. */
5331 if (n1 >= len || n1 < 0)
5332 s = NULL;
5333 else
5334 s = vim_strnsave(s + n1, 1);
5335 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005336 clear_tv(rettv);
5337 rettv->v_type = VAR_STRING;
5338 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005339 break;
5340
5341 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005342 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343 if (n1 < 0)
5344 n1 = len + n1;
5345 if (!empty1 && (n1 < 0 || n1 >= len))
5346 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005347 /* For a range we allow invalid values and return an empty
5348 * list. A list index out of range is an error. */
5349 if (!range)
5350 {
5351 if (verbose)
5352 EMSGN(_(e_listidx), n1);
5353 return FAIL;
5354 }
5355 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 }
5357 if (range)
5358 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005359 list_T *l;
5360 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361
5362 if (n2 < 0)
5363 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005364 else if (n2 >= len)
5365 n2 = len - 1;
5366 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005367 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368 l = list_alloc();
5369 if (l == NULL)
5370 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005371 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372 n1 <= n2; ++n1)
5373 {
5374 if (list_append_tv(l, &item->li_tv) == FAIL)
5375 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005376 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 return FAIL;
5378 }
5379 item = item->li_next;
5380 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005381 clear_tv(rettv);
5382 rettv->v_type = VAR_LIST;
5383 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005384 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 }
5386 else
5387 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005388 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005389 clear_tv(rettv);
5390 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 }
5392 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393
5394 case VAR_DICT:
5395 if (range)
5396 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005397 if (verbose)
5398 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005399 if (len == -1)
5400 clear_tv(&var1);
5401 return FAIL;
5402 }
5403 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005404 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005405
5406 if (len == -1)
5407 {
5408 key = get_tv_string(&var1);
5409 if (*key == NUL)
5410 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005411 if (verbose)
5412 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005413 clear_tv(&var1);
5414 return FAIL;
5415 }
5416 }
5417
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005418 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005419
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005420 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005421 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005422 if (len == -1)
5423 clear_tv(&var1);
5424 if (item == NULL)
5425 return FAIL;
5426
5427 copy_tv(&item->di_tv, &var1);
5428 clear_tv(rettv);
5429 *rettv = var1;
5430 }
5431 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 }
5433 }
5434
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 return OK;
5436}
5437
5438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 * Get an option value.
5440 * "arg" points to the '&' or '+' before the option name.
5441 * "arg" is advanced to character after the option name.
5442 * Return OK or FAIL.
5443 */
5444 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005445get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005447 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 int evaluate;
5449{
5450 char_u *option_end;
5451 long numval;
5452 char_u *stringval;
5453 int opt_type;
5454 int c;
5455 int working = (**arg == '+'); /* has("+option") */
5456 int ret = OK;
5457 int opt_flags;
5458
5459 /*
5460 * Isolate the option name and find its value.
5461 */
5462 option_end = find_option_end(arg, &opt_flags);
5463 if (option_end == NULL)
5464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005465 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 EMSG2(_("E112: Option name missing: %s"), *arg);
5467 return FAIL;
5468 }
5469
5470 if (!evaluate)
5471 {
5472 *arg = option_end;
5473 return OK;
5474 }
5475
5476 c = *option_end;
5477 *option_end = NUL;
5478 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005479 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480
5481 if (opt_type == -3) /* invalid name */
5482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005483 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 EMSG2(_("E113: Unknown option: %s"), *arg);
5485 ret = FAIL;
5486 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005487 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 {
5489 if (opt_type == -2) /* hidden string option */
5490 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005491 rettv->v_type = VAR_STRING;
5492 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 }
5494 else if (opt_type == -1) /* hidden number option */
5495 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005496 rettv->v_type = VAR_NUMBER;
5497 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 }
5499 else if (opt_type == 1) /* number option */
5500 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005501 rettv->v_type = VAR_NUMBER;
5502 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 }
5504 else /* string option */
5505 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005506 rettv->v_type = VAR_STRING;
5507 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 }
5509 }
5510 else if (working && (opt_type == -2 || opt_type == -1))
5511 ret = FAIL;
5512
5513 *option_end = c; /* put back for error messages */
5514 *arg = option_end;
5515
5516 return ret;
5517}
5518
5519/*
5520 * Allocate a variable for a string constant.
5521 * Return OK or FAIL.
5522 */
5523 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005524get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005526 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 int evaluate;
5528{
5529 char_u *p;
5530 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 int extra = 0;
5532
5533 /*
5534 * Find the end of the string, skipping backslashed characters.
5535 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005536 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 {
5538 if (*p == '\\' && p[1] != NUL)
5539 {
5540 ++p;
5541 /* A "\<x>" form occupies at least 4 characters, and produces up
5542 * to 6 characters: reserve space for 2 extra */
5543 if (*p == '<')
5544 extra += 2;
5545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 }
5547
5548 if (*p != '"')
5549 {
5550 EMSG2(_("E114: Missing quote: %s"), *arg);
5551 return FAIL;
5552 }
5553
5554 /* If only parsing, set *arg and return here */
5555 if (!evaluate)
5556 {
5557 *arg = p + 1;
5558 return OK;
5559 }
5560
5561 /*
5562 * Copy the string into allocated memory, handling backslashed
5563 * characters.
5564 */
5565 name = alloc((unsigned)(p - *arg + extra));
5566 if (name == NULL)
5567 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005568 rettv->v_type = VAR_STRING;
5569 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570
Bram Moolenaar8c711452005-01-14 21:53:12 +00005571 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 {
5573 if (*p == '\\')
5574 {
5575 switch (*++p)
5576 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005577 case 'b': *name++ = BS; ++p; break;
5578 case 'e': *name++ = ESC; ++p; break;
5579 case 'f': *name++ = FF; ++p; break;
5580 case 'n': *name++ = NL; ++p; break;
5581 case 'r': *name++ = CAR; ++p; break;
5582 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583
5584 case 'X': /* hex: "\x1", "\x12" */
5585 case 'x':
5586 case 'u': /* Unicode: "\u0023" */
5587 case 'U':
5588 if (vim_isxdigit(p[1]))
5589 {
5590 int n, nr;
5591 int c = toupper(*p);
5592
5593 if (c == 'X')
5594 n = 2;
5595 else
5596 n = 4;
5597 nr = 0;
5598 while (--n >= 0 && vim_isxdigit(p[1]))
5599 {
5600 ++p;
5601 nr = (nr << 4) + hex2nr(*p);
5602 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005603 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604#ifdef FEAT_MBYTE
5605 /* For "\u" store the number according to
5606 * 'encoding'. */
5607 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005608 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 else
5610#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005611 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 break;
5614
5615 /* octal: "\1", "\12", "\123" */
5616 case '0':
5617 case '1':
5618 case '2':
5619 case '3':
5620 case '4':
5621 case '5':
5622 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005623 case '7': *name = *p++ - '0';
5624 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005626 *name = (*name << 3) + *p++ - '0';
5627 if (*p >= '0' && *p <= '7')
5628 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005630 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 break;
5632
5633 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005634 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 if (extra != 0)
5636 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005637 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 break;
5639 }
5640 /* FALLTHROUGH */
5641
Bram Moolenaar8c711452005-01-14 21:53:12 +00005642 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 break;
5644 }
5645 }
5646 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005647 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005650 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 *arg = p + 1;
5652
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 return OK;
5654}
5655
5656/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005657 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 * Return OK or FAIL.
5659 */
5660 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005661get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 int evaluate;
5665{
5666 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005667 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005668 int reduce = 0;
5669
5670 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005671 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005672 */
5673 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5674 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005678 break;
5679 ++reduce;
5680 ++p;
5681 }
5682 }
5683
Bram Moolenaar8c711452005-01-14 21:53:12 +00005684 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005685 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005686 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005687 return FAIL;
5688 }
5689
Bram Moolenaar8c711452005-01-14 21:53:12 +00005690 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005691 if (!evaluate)
5692 {
5693 *arg = p + 1;
5694 return OK;
5695 }
5696
5697 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005698 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005699 */
5700 str = alloc((unsigned)((p - *arg) - reduce));
5701 if (str == NULL)
5702 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005703 rettv->v_type = VAR_STRING;
5704 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005705
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005707 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005708 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005709 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005710 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005711 break;
5712 ++p;
5713 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005714 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005715 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005717 *arg = p + 1;
5718
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005719 return OK;
5720}
5721
5722/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005723 * Allocate a variable for a List and fill it from "*arg".
5724 * Return OK or FAIL.
5725 */
5726 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005727get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005729 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005730 int evaluate;
5731{
Bram Moolenaar33570922005-01-25 22:26:29 +00005732 list_T *l = NULL;
5733 typval_T tv;
5734 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005735
5736 if (evaluate)
5737 {
5738 l = list_alloc();
5739 if (l == NULL)
5740 return FAIL;
5741 }
5742
5743 *arg = skipwhite(*arg + 1);
5744 while (**arg != ']' && **arg != NUL)
5745 {
5746 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5747 goto failret;
5748 if (evaluate)
5749 {
5750 item = listitem_alloc();
5751 if (item != NULL)
5752 {
5753 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005754 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005755 list_append(l, item);
5756 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005757 else
5758 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759 }
5760
5761 if (**arg == ']')
5762 break;
5763 if (**arg != ',')
5764 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005766 goto failret;
5767 }
5768 *arg = skipwhite(*arg + 1);
5769 }
5770
5771 if (**arg != ']')
5772 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005774failret:
5775 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005776 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005777 return FAIL;
5778 }
5779
5780 *arg = skipwhite(*arg + 1);
5781 if (evaluate)
5782 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005783 rettv->v_type = VAR_LIST;
5784 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005785 ++l->lv_refcount;
5786 }
5787
5788 return OK;
5789}
5790
5791/*
5792 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005793 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005794 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005795 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005796list_alloc()
5797{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005798 list_T *l;
5799
5800 l = (list_T *)alloc_clear(sizeof(list_T));
5801 if (l != NULL)
5802 {
5803 /* Prepend the list to the list of lists for garbage collection. */
5804 if (first_list != NULL)
5805 first_list->lv_used_prev = l;
5806 l->lv_used_prev = NULL;
5807 l->lv_used_next = first_list;
5808 first_list = l;
5809 }
5810 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811}
5812
5813/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005814 * Allocate an empty list for a return value.
5815 * Returns OK or FAIL.
5816 */
5817 static int
5818rettv_list_alloc(rettv)
5819 typval_T *rettv;
5820{
5821 list_T *l = list_alloc();
5822
5823 if (l == NULL)
5824 return FAIL;
5825
5826 rettv->vval.v_list = l;
5827 rettv->v_type = VAR_LIST;
5828 ++l->lv_refcount;
5829 return OK;
5830}
5831
5832/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833 * Unreference a list: decrement the reference count and free it when it
5834 * becomes zero.
5835 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005836 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005837list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005838 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005839{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005840 if (l != NULL && --l->lv_refcount <= 0)
5841 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005842}
5843
5844/*
5845 * Free a list, including all items it points to.
5846 * Ignores the reference count.
5847 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005848 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005849list_free(l, recurse)
5850 list_T *l;
5851 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852{
Bram Moolenaar33570922005-01-25 22:26:29 +00005853 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005855 /* Remove the list from the list of lists for garbage collection. */
5856 if (l->lv_used_prev == NULL)
5857 first_list = l->lv_used_next;
5858 else
5859 l->lv_used_prev->lv_used_next = l->lv_used_next;
5860 if (l->lv_used_next != NULL)
5861 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5862
Bram Moolenaard9fba312005-06-26 22:34:35 +00005863 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005865 /* Remove the item before deleting it. */
5866 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005867 if (recurse || (item->li_tv.v_type != VAR_LIST
5868 && item->li_tv.v_type != VAR_DICT))
5869 clear_tv(&item->li_tv);
5870 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005871 }
5872 vim_free(l);
5873}
5874
5875/*
5876 * Allocate a list item.
5877 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005878 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879listitem_alloc()
5880{
Bram Moolenaar33570922005-01-25 22:26:29 +00005881 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005882}
5883
5884/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005885 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886 */
5887 static void
5888listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005889 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005891 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892 vim_free(item);
5893}
5894
5895/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005896 * Remove a list item from a List and free it. Also clears the value.
5897 */
5898 static void
5899listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005900 list_T *l;
5901 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005902{
5903 list_remove(l, item, item);
5904 listitem_free(item);
5905}
5906
5907/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908 * Get the number of items in a list.
5909 */
5910 static long
5911list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005912 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914 if (l == NULL)
5915 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005916 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917}
5918
5919/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005920 * Return TRUE when two lists have exactly the same values.
5921 */
5922 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005923list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005924 list_T *l1;
5925 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005926 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005927 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005928{
Bram Moolenaar33570922005-01-25 22:26:29 +00005929 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005930
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005931 if (l1 == NULL || l2 == NULL)
5932 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005933 if (l1 == l2)
5934 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005935 if (list_len(l1) != list_len(l2))
5936 return FALSE;
5937
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005938 for (item1 = l1->lv_first, item2 = l2->lv_first;
5939 item1 != NULL && item2 != NULL;
5940 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005941 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005942 return FALSE;
5943 return item1 == NULL && item2 == NULL;
5944}
5945
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005946#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5947 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005948/*
5949 * Return the dictitem that an entry in a hashtable points to.
5950 */
5951 dictitem_T *
5952dict_lookup(hi)
5953 hashitem_T *hi;
5954{
5955 return HI2DI(hi);
5956}
5957#endif
5958
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005959/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005960 * Return TRUE when two dictionaries have exactly the same key/values.
5961 */
5962 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005963dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005964 dict_T *d1;
5965 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005966 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005967 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005968{
Bram Moolenaar33570922005-01-25 22:26:29 +00005969 hashitem_T *hi;
5970 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005971 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005972
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005973 if (d1 == NULL || d2 == NULL)
5974 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005975 if (d1 == d2)
5976 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005977 if (dict_len(d1) != dict_len(d2))
5978 return FALSE;
5979
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005980 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005981 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005982 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005983 if (!HASHITEM_EMPTY(hi))
5984 {
5985 item2 = dict_find(d2, hi->hi_key, -1);
5986 if (item2 == NULL)
5987 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005988 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005989 return FALSE;
5990 --todo;
5991 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005992 }
5993 return TRUE;
5994}
5995
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005996static int tv_equal_recurse_limit;
5997
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005998/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005999 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006000 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006001 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006002 */
6003 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006004tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 typval_T *tv1;
6006 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006007 int ic; /* ignore case */
6008 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006009{
6010 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006011 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006012 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006013 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006014
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006015 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006016 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006017
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006018 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006019 * recursiveness to a limit. We guess they are equal then.
6020 * A fixed limit has the problem of still taking an awful long time.
6021 * Reduce the limit every time running into it. That should work fine for
6022 * deeply linked structures that are not recursively linked and catch
6023 * recursiveness quickly. */
6024 if (!recursive)
6025 tv_equal_recurse_limit = 1000;
6026 if (recursive_cnt >= tv_equal_recurse_limit)
6027 {
6028 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006029 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006030 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006031
6032 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006033 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006034 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006035 ++recursive_cnt;
6036 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6037 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006038 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006039
6040 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006041 ++recursive_cnt;
6042 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6043 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006044 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006045
6046 case VAR_FUNC:
6047 return (tv1->vval.v_string != NULL
6048 && tv2->vval.v_string != NULL
6049 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6050
6051 case VAR_NUMBER:
6052 return tv1->vval.v_number == tv2->vval.v_number;
6053
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006054#ifdef FEAT_FLOAT
6055 case VAR_FLOAT:
6056 return tv1->vval.v_float == tv2->vval.v_float;
6057#endif
6058
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006059 case VAR_STRING:
6060 s1 = get_tv_string_buf(tv1, buf1);
6061 s2 = get_tv_string_buf(tv2, buf2);
6062 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006063 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006064
6065 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006066 return TRUE;
6067}
6068
6069/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070 * Locate item with index "n" in list "l" and return it.
6071 * A negative index is counted from the end; -1 is the last item.
6072 * Returns NULL when "n" is out of range.
6073 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006074 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006075list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006076 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077 long n;
6078{
Bram Moolenaar33570922005-01-25 22:26:29 +00006079 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006080 long idx;
6081
6082 if (l == NULL)
6083 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006084
6085 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006087 n = l->lv_len + n;
6088
6089 /* Check for index out of range. */
6090 if (n < 0 || n >= l->lv_len)
6091 return NULL;
6092
6093 /* When there is a cached index may start search from there. */
6094 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006095 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006096 if (n < l->lv_idx / 2)
6097 {
6098 /* closest to the start of the list */
6099 item = l->lv_first;
6100 idx = 0;
6101 }
6102 else if (n > (l->lv_idx + l->lv_len) / 2)
6103 {
6104 /* closest to the end of the list */
6105 item = l->lv_last;
6106 idx = l->lv_len - 1;
6107 }
6108 else
6109 {
6110 /* closest to the cached index */
6111 item = l->lv_idx_item;
6112 idx = l->lv_idx;
6113 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006114 }
6115 else
6116 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006117 if (n < l->lv_len / 2)
6118 {
6119 /* closest to the start of the list */
6120 item = l->lv_first;
6121 idx = 0;
6122 }
6123 else
6124 {
6125 /* closest to the end of the list */
6126 item = l->lv_last;
6127 idx = l->lv_len - 1;
6128 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006129 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006130
6131 while (n > idx)
6132 {
6133 /* search forward */
6134 item = item->li_next;
6135 ++idx;
6136 }
6137 while (n < idx)
6138 {
6139 /* search backward */
6140 item = item->li_prev;
6141 --idx;
6142 }
6143
6144 /* cache the used index */
6145 l->lv_idx = idx;
6146 l->lv_idx_item = item;
6147
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006148 return item;
6149}
6150
6151/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006152 * Get list item "l[idx]" as a number.
6153 */
6154 static long
6155list_find_nr(l, idx, errorp)
6156 list_T *l;
6157 long idx;
6158 int *errorp; /* set to TRUE when something wrong */
6159{
6160 listitem_T *li;
6161
6162 li = list_find(l, idx);
6163 if (li == NULL)
6164 {
6165 if (errorp != NULL)
6166 *errorp = TRUE;
6167 return -1L;
6168 }
6169 return get_tv_number_chk(&li->li_tv, errorp);
6170}
6171
6172/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006173 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6174 */
6175 char_u *
6176list_find_str(l, idx)
6177 list_T *l;
6178 long idx;
6179{
6180 listitem_T *li;
6181
6182 li = list_find(l, idx - 1);
6183 if (li == NULL)
6184 {
6185 EMSGN(_(e_listidx), idx);
6186 return NULL;
6187 }
6188 return get_tv_string(&li->li_tv);
6189}
6190
6191/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006192 * Locate "item" list "l" and return its index.
6193 * Returns -1 when "item" is not in the list.
6194 */
6195 static long
6196list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006197 list_T *l;
6198 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006199{
6200 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006201 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006202
6203 if (l == NULL)
6204 return -1;
6205 idx = 0;
6206 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6207 ++idx;
6208 if (li == NULL)
6209 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006210 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006211}
6212
6213/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006214 * Append item "item" to the end of list "l".
6215 */
6216 static void
6217list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006218 list_T *l;
6219 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006220{
6221 if (l->lv_last == NULL)
6222 {
6223 /* empty list */
6224 l->lv_first = item;
6225 l->lv_last = item;
6226 item->li_prev = NULL;
6227 }
6228 else
6229 {
6230 l->lv_last->li_next = item;
6231 item->li_prev = l->lv_last;
6232 l->lv_last = item;
6233 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006234 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006235 item->li_next = NULL;
6236}
6237
6238/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006239 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006240 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006241 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006242 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006243list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006244 list_T *l;
6245 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006246{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006247 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006248
Bram Moolenaar05159a02005-02-26 23:04:13 +00006249 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006250 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006251 copy_tv(tv, &li->li_tv);
6252 list_append(l, li);
6253 return OK;
6254}
6255
6256/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006257 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006258 * Return FAIL when out of memory.
6259 */
6260 int
6261list_append_dict(list, dict)
6262 list_T *list;
6263 dict_T *dict;
6264{
6265 listitem_T *li = listitem_alloc();
6266
6267 if (li == NULL)
6268 return FAIL;
6269 li->li_tv.v_type = VAR_DICT;
6270 li->li_tv.v_lock = 0;
6271 li->li_tv.vval.v_dict = dict;
6272 list_append(list, li);
6273 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006274 return OK;
6275}
6276
6277/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006278 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006279 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006280 * Returns FAIL when out of memory.
6281 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006282 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006283list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006284 list_T *l;
6285 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006286 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006287{
6288 listitem_T *li = listitem_alloc();
6289
6290 if (li == NULL)
6291 return FAIL;
6292 list_append(l, li);
6293 li->li_tv.v_type = VAR_STRING;
6294 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006295 if (str == NULL)
6296 li->li_tv.vval.v_string = NULL;
6297 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006298 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006299 return FAIL;
6300 return OK;
6301}
6302
6303/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006304 * Append "n" to list "l".
6305 * Returns FAIL when out of memory.
6306 */
6307 static int
6308list_append_number(l, n)
6309 list_T *l;
6310 varnumber_T n;
6311{
6312 listitem_T *li;
6313
6314 li = listitem_alloc();
6315 if (li == NULL)
6316 return FAIL;
6317 li->li_tv.v_type = VAR_NUMBER;
6318 li->li_tv.v_lock = 0;
6319 li->li_tv.vval.v_number = n;
6320 list_append(l, li);
6321 return OK;
6322}
6323
6324/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006325 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006326 * If "item" is NULL append at the end.
6327 * Return FAIL when out of memory.
6328 */
6329 static int
6330list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006331 list_T *l;
6332 typval_T *tv;
6333 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006334{
Bram Moolenaar33570922005-01-25 22:26:29 +00006335 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006336
6337 if (ni == NULL)
6338 return FAIL;
6339 copy_tv(tv, &ni->li_tv);
6340 if (item == NULL)
6341 /* Append new item at end of list. */
6342 list_append(l, ni);
6343 else
6344 {
6345 /* Insert new item before existing item. */
6346 ni->li_prev = item->li_prev;
6347 ni->li_next = item;
6348 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006349 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006350 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006351 ++l->lv_idx;
6352 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006353 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006354 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006355 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006356 l->lv_idx_item = NULL;
6357 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006358 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006359 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006360 }
6361 return OK;
6362}
6363
6364/*
6365 * Extend "l1" with "l2".
6366 * If "bef" is NULL append at the end, otherwise insert before this item.
6367 * Returns FAIL when out of memory.
6368 */
6369 static int
6370list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006371 list_T *l1;
6372 list_T *l2;
6373 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006374{
Bram Moolenaar33570922005-01-25 22:26:29 +00006375 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006376 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006377
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006378 /* We also quit the loop when we have inserted the original item count of
6379 * the list, avoid a hang when we extend a list with itself. */
6380 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006381 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6382 return FAIL;
6383 return OK;
6384}
6385
6386/*
6387 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6388 * Return FAIL when out of memory.
6389 */
6390 static int
6391list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006392 list_T *l1;
6393 list_T *l2;
6394 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006395{
Bram Moolenaar33570922005-01-25 22:26:29 +00006396 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006397
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006398 if (l1 == NULL || l2 == NULL)
6399 return FAIL;
6400
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006401 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006402 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403 if (l == NULL)
6404 return FAIL;
6405 tv->v_type = VAR_LIST;
6406 tv->vval.v_list = l;
6407
6408 /* append all items from the second list */
6409 return list_extend(l, l2, NULL);
6410}
6411
6412/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006413 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006415 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416 * Returns NULL when out of memory.
6417 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006418 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006419list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006420 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006421 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006422 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423{
Bram Moolenaar33570922005-01-25 22:26:29 +00006424 list_T *copy;
6425 listitem_T *item;
6426 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006427
6428 if (orig == NULL)
6429 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006430
6431 copy = list_alloc();
6432 if (copy != NULL)
6433 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006434 if (copyID != 0)
6435 {
6436 /* Do this before adding the items, because one of the items may
6437 * refer back to this list. */
6438 orig->lv_copyID = copyID;
6439 orig->lv_copylist = copy;
6440 }
6441 for (item = orig->lv_first; item != NULL && !got_int;
6442 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006443 {
6444 ni = listitem_alloc();
6445 if (ni == NULL)
6446 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006447 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006448 {
6449 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6450 {
6451 vim_free(ni);
6452 break;
6453 }
6454 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006455 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006456 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006457 list_append(copy, ni);
6458 }
6459 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006460 if (item != NULL)
6461 {
6462 list_unref(copy);
6463 copy = NULL;
6464 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006465 }
6466
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006467 return copy;
6468}
6469
6470/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006471 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006472 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006473 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006474 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006475list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006476 list_T *l;
6477 listitem_T *item;
6478 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006479{
Bram Moolenaar33570922005-01-25 22:26:29 +00006480 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006481
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006482 /* notify watchers */
6483 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006484 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006485 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006486 list_fix_watch(l, ip);
6487 if (ip == item2)
6488 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006489 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006490
6491 if (item2->li_next == NULL)
6492 l->lv_last = item->li_prev;
6493 else
6494 item2->li_next->li_prev = item->li_prev;
6495 if (item->li_prev == NULL)
6496 l->lv_first = item2->li_next;
6497 else
6498 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006499 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006500}
6501
6502/*
6503 * Return an allocated string with the string representation of a list.
6504 * May return NULL.
6505 */
6506 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006507list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006508 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006509 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006510{
6511 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006512
6513 if (tv->vval.v_list == NULL)
6514 return NULL;
6515 ga_init2(&ga, (int)sizeof(char), 80);
6516 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006517 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006518 {
6519 vim_free(ga.ga_data);
6520 return NULL;
6521 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006522 ga_append(&ga, ']');
6523 ga_append(&ga, NUL);
6524 return (char_u *)ga.ga_data;
6525}
6526
6527/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006528 * Join list "l" into a string in "*gap", using separator "sep".
6529 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006530 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006531 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006532 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006533list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006534 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006535 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006536 char_u *sep;
6537 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006538 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006539{
6540 int first = TRUE;
6541 char_u *tofree;
6542 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006543 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006544 char_u *s;
6545
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006546 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006547 {
6548 if (first)
6549 first = FALSE;
6550 else
6551 ga_concat(gap, sep);
6552
6553 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006554 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006555 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006556 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006557 if (s != NULL)
6558 ga_concat(gap, s);
6559 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006560 if (s == NULL)
6561 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006562 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006563 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006564 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006565}
6566
6567/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006568 * Garbage collection for lists and dictionaries.
6569 *
6570 * We use reference counts to be able to free most items right away when they
6571 * are no longer used. But for composite items it's possible that it becomes
6572 * unused while the reference count is > 0: When there is a recursive
6573 * reference. Example:
6574 * :let l = [1, 2, 3]
6575 * :let d = {9: l}
6576 * :let l[1] = d
6577 *
6578 * Since this is quite unusual we handle this with garbage collection: every
6579 * once in a while find out which lists and dicts are not referenced from any
6580 * variable.
6581 *
6582 * Here is a good reference text about garbage collection (refers to Python
6583 * but it applies to all reference-counting mechanisms):
6584 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006585 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006586
6587/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006588 * Do garbage collection for lists and dicts.
6589 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006590 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006591 int
6592garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006593{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006594 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006595 buf_T *buf;
6596 win_T *wp;
6597 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006598 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006599 int did_free;
6600 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006601#ifdef FEAT_WINDOWS
6602 tabpage_T *tp;
6603#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006604
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006605 /* Only do this once. */
6606 want_garbage_collect = FALSE;
6607 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006608 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006609
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006610 /* We advance by two because we add one for items referenced through
6611 * previous_funccal. */
6612 current_copyID += COPYID_INC;
6613 copyID = current_copyID;
6614
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006615 /*
6616 * 1. Go through all accessible variables and mark all lists and dicts
6617 * with copyID.
6618 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006619
6620 /* Don't free variables in the previous_funccal list unless they are only
6621 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006622 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006623 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6624 {
6625 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6626 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6627 }
6628
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006629 /* script-local variables */
6630 for (i = 1; i <= ga_scripts.ga_len; ++i)
6631 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6632
6633 /* buffer-local variables */
6634 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6635 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6636
6637 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006638 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006639 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6640
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006641#ifdef FEAT_WINDOWS
6642 /* tabpage-local variables */
6643 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6644 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6645#endif
6646
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006647 /* global variables */
6648 set_ref_in_ht(&globvarht, copyID);
6649
6650 /* function-local variables */
6651 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6652 {
6653 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6654 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6655 }
6656
Bram Moolenaard812df62008-11-09 12:46:09 +00006657 /* v: vars */
6658 set_ref_in_ht(&vimvarht, copyID);
6659
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006660 /*
6661 * 2. Free lists and dictionaries that are not referenced.
6662 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006663 did_free = free_unref_items(copyID);
6664
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006665 /*
6666 * 3. Check if any funccal can be freed now.
6667 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006668 for (pfc = &previous_funccal; *pfc != NULL; )
6669 {
6670 if (can_free_funccal(*pfc, copyID))
6671 {
6672 fc = *pfc;
6673 *pfc = fc->caller;
6674 free_funccal(fc, TRUE);
6675 did_free = TRUE;
6676 did_free_funccal = TRUE;
6677 }
6678 else
6679 pfc = &(*pfc)->caller;
6680 }
6681 if (did_free_funccal)
6682 /* When a funccal was freed some more items might be garbage
6683 * collected, so run again. */
6684 (void)garbage_collect();
6685
6686 return did_free;
6687}
6688
6689/*
6690 * Free lists and dictionaries that are no longer referenced.
6691 */
6692 static int
6693free_unref_items(copyID)
6694 int copyID;
6695{
6696 dict_T *dd;
6697 list_T *ll;
6698 int did_free = FALSE;
6699
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006700 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006701 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006702 */
6703 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006704 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006705 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006706 /* Free the Dictionary and ordinary items it contains, but don't
6707 * recurse into Lists and Dictionaries, they will be in the list
6708 * of dicts or list of lists. */
6709 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006710 did_free = TRUE;
6711
6712 /* restart, next dict may also have been freed */
6713 dd = first_dict;
6714 }
6715 else
6716 dd = dd->dv_used_next;
6717
6718 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006719 * Go through the list of lists and free items without the copyID.
6720 * But don't free a list that has a watcher (used in a for loop), these
6721 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006722 */
6723 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006724 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6725 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006726 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006727 /* Free the List and ordinary items it contains, but don't recurse
6728 * into Lists and Dictionaries, they will be in the list of dicts
6729 * or list of lists. */
6730 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006731 did_free = TRUE;
6732
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006733 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006734 ll = first_list;
6735 }
6736 else
6737 ll = ll->lv_used_next;
6738
6739 return did_free;
6740}
6741
6742/*
6743 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6744 */
6745 static void
6746set_ref_in_ht(ht, copyID)
6747 hashtab_T *ht;
6748 int copyID;
6749{
6750 int todo;
6751 hashitem_T *hi;
6752
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006753 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006754 for (hi = ht->ht_array; todo > 0; ++hi)
6755 if (!HASHITEM_EMPTY(hi))
6756 {
6757 --todo;
6758 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6759 }
6760}
6761
6762/*
6763 * Mark all lists and dicts referenced through list "l" with "copyID".
6764 */
6765 static void
6766set_ref_in_list(l, copyID)
6767 list_T *l;
6768 int copyID;
6769{
6770 listitem_T *li;
6771
6772 for (li = l->lv_first; li != NULL; li = li->li_next)
6773 set_ref_in_item(&li->li_tv, copyID);
6774}
6775
6776/*
6777 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6778 */
6779 static void
6780set_ref_in_item(tv, copyID)
6781 typval_T *tv;
6782 int copyID;
6783{
6784 dict_T *dd;
6785 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006786
6787 switch (tv->v_type)
6788 {
6789 case VAR_DICT:
6790 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006791 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006792 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006793 /* Didn't see this dict yet. */
6794 dd->dv_copyID = copyID;
6795 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006796 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006797 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006798
6799 case VAR_LIST:
6800 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006801 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006802 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006803 /* Didn't see this list yet. */
6804 ll->lv_copyID = copyID;
6805 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006806 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006807 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006808 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006809 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006810}
6811
6812/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006813 * Allocate an empty header for a dictionary.
6814 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006815 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006816dict_alloc()
6817{
Bram Moolenaar33570922005-01-25 22:26:29 +00006818 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006819
Bram Moolenaar33570922005-01-25 22:26:29 +00006820 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006821 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006822 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006823 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006824 if (first_dict != NULL)
6825 first_dict->dv_used_prev = d;
6826 d->dv_used_next = first_dict;
6827 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006828 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006829
Bram Moolenaar33570922005-01-25 22:26:29 +00006830 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006831 d->dv_lock = 0;
6832 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006833 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006834 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006835 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006836}
6837
6838/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006839 * Allocate an empty dict for a return value.
6840 * Returns OK or FAIL.
6841 */
6842 static int
6843rettv_dict_alloc(rettv)
6844 typval_T *rettv;
6845{
6846 dict_T *d = dict_alloc();
6847
6848 if (d == NULL)
6849 return FAIL;
6850
6851 rettv->vval.v_dict = d;
6852 rettv->v_type = VAR_DICT;
6853 ++d->dv_refcount;
6854 return OK;
6855}
6856
6857
6858/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006859 * Unreference a Dictionary: decrement the reference count and free it when it
6860 * becomes zero.
6861 */
6862 static void
6863dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006864 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006865{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006866 if (d != NULL && --d->dv_refcount <= 0)
6867 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006868}
6869
6870/*
6871 * Free a Dictionary, including all items it contains.
6872 * Ignores the reference count.
6873 */
6874 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006875dict_free(d, recurse)
6876 dict_T *d;
6877 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006878{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006879 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006880 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006881 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006882
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006883 /* Remove the dict from the list of dicts for garbage collection. */
6884 if (d->dv_used_prev == NULL)
6885 first_dict = d->dv_used_next;
6886 else
6887 d->dv_used_prev->dv_used_next = d->dv_used_next;
6888 if (d->dv_used_next != NULL)
6889 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6890
6891 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006892 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006893 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006894 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006895 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006896 if (!HASHITEM_EMPTY(hi))
6897 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006898 /* Remove the item before deleting it, just in case there is
6899 * something recursive causing trouble. */
6900 di = HI2DI(hi);
6901 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006902 if (recurse || (di->di_tv.v_type != VAR_LIST
6903 && di->di_tv.v_type != VAR_DICT))
6904 clear_tv(&di->di_tv);
6905 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006906 --todo;
6907 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006908 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006909 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006910 vim_free(d);
6911}
6912
6913/*
6914 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006915 * The "key" is copied to the new item.
6916 * Note that the value of the item "di_tv" still needs to be initialized!
6917 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006918 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006919 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006920dictitem_alloc(key)
6921 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006922{
Bram Moolenaar33570922005-01-25 22:26:29 +00006923 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006924
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006925 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006926 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006927 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006928 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006929 di->di_flags = 0;
6930 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006931 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006932}
6933
6934/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006935 * Make a copy of a Dictionary item.
6936 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006937 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006938dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006939 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006940{
Bram Moolenaar33570922005-01-25 22:26:29 +00006941 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006942
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006943 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6944 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006945 if (di != NULL)
6946 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006947 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006948 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006949 copy_tv(&org->di_tv, &di->di_tv);
6950 }
6951 return di;
6952}
6953
6954/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006955 * Remove item "item" from Dictionary "dict" and free it.
6956 */
6957 static void
6958dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006959 dict_T *dict;
6960 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006961{
Bram Moolenaar33570922005-01-25 22:26:29 +00006962 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006963
Bram Moolenaar33570922005-01-25 22:26:29 +00006964 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006965 if (HASHITEM_EMPTY(hi))
6966 EMSG2(_(e_intern2), "dictitem_remove()");
6967 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006968 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006969 dictitem_free(item);
6970}
6971
6972/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006973 * Free a dict item. Also clears the value.
6974 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006975 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006976dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006977 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006978{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006979 clear_tv(&item->di_tv);
6980 vim_free(item);
6981}
6982
6983/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006984 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6985 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006986 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006987 * Returns NULL when out of memory.
6988 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006989 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006990dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006991 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006992 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006993 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006994{
Bram Moolenaar33570922005-01-25 22:26:29 +00006995 dict_T *copy;
6996 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006997 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006998 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006999
7000 if (orig == NULL)
7001 return NULL;
7002
7003 copy = dict_alloc();
7004 if (copy != NULL)
7005 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007006 if (copyID != 0)
7007 {
7008 orig->dv_copyID = copyID;
7009 orig->dv_copydict = copy;
7010 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007011 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007012 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007013 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007014 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007015 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007016 --todo;
7017
7018 di = dictitem_alloc(hi->hi_key);
7019 if (di == NULL)
7020 break;
7021 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007022 {
7023 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7024 copyID) == FAIL)
7025 {
7026 vim_free(di);
7027 break;
7028 }
7029 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007030 else
7031 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7032 if (dict_add(copy, di) == FAIL)
7033 {
7034 dictitem_free(di);
7035 break;
7036 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007037 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007038 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007039
Bram Moolenaare9a41262005-01-15 22:18:47 +00007040 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007041 if (todo > 0)
7042 {
7043 dict_unref(copy);
7044 copy = NULL;
7045 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007046 }
7047
7048 return copy;
7049}
7050
7051/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007052 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007053 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007054 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007055 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007056dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007057 dict_T *d;
7058 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007059{
Bram Moolenaar33570922005-01-25 22:26:29 +00007060 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007061}
7062
Bram Moolenaar8c711452005-01-14 21:53:12 +00007063/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007064 * Add a number or string entry to dictionary "d".
7065 * When "str" is NULL use number "nr", otherwise use "str".
7066 * Returns FAIL when out of memory and when key already exists.
7067 */
7068 int
7069dict_add_nr_str(d, key, nr, str)
7070 dict_T *d;
7071 char *key;
7072 long nr;
7073 char_u *str;
7074{
7075 dictitem_T *item;
7076
7077 item = dictitem_alloc((char_u *)key);
7078 if (item == NULL)
7079 return FAIL;
7080 item->di_tv.v_lock = 0;
7081 if (str == NULL)
7082 {
7083 item->di_tv.v_type = VAR_NUMBER;
7084 item->di_tv.vval.v_number = nr;
7085 }
7086 else
7087 {
7088 item->di_tv.v_type = VAR_STRING;
7089 item->di_tv.vval.v_string = vim_strsave(str);
7090 }
7091 if (dict_add(d, item) == FAIL)
7092 {
7093 dictitem_free(item);
7094 return FAIL;
7095 }
7096 return OK;
7097}
7098
7099/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007100 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007101 * Returns FAIL when out of memory and when key already exists.
7102 */
7103 int
7104dict_add_list(d, key, list)
7105 dict_T *d;
7106 char *key;
7107 list_T *list;
7108{
7109 dictitem_T *item;
7110
7111 item = dictitem_alloc((char_u *)key);
7112 if (item == NULL)
7113 return FAIL;
7114 item->di_tv.v_lock = 0;
7115 item->di_tv.v_type = VAR_LIST;
7116 item->di_tv.vval.v_list = list;
7117 if (dict_add(d, item) == FAIL)
7118 {
7119 dictitem_free(item);
7120 return FAIL;
7121 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007122 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007123 return OK;
7124}
7125
7126/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007127 * Get the number of items in a Dictionary.
7128 */
7129 static long
7130dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007131 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007132{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007133 if (d == NULL)
7134 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007135 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007136}
7137
7138/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007139 * Find item "key[len]" in Dictionary "d".
7140 * If "len" is negative use strlen(key).
7141 * Returns NULL when not found.
7142 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007143 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007144dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007146 char_u *key;
7147 int len;
7148{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007149#define AKEYLEN 200
7150 char_u buf[AKEYLEN];
7151 char_u *akey;
7152 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007153 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007154
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007155 if (len < 0)
7156 akey = key;
7157 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007158 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007159 tofree = akey = vim_strnsave(key, len);
7160 if (akey == NULL)
7161 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007162 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007163 else
7164 {
7165 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007166 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007167 akey = buf;
7168 }
7169
Bram Moolenaar33570922005-01-25 22:26:29 +00007170 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007171 vim_free(tofree);
7172 if (HASHITEM_EMPTY(hi))
7173 return NULL;
7174 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175}
7176
7177/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007178 * Get a string item from a dictionary.
7179 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007180 * Returns NULL if the entry doesn't exist or out of memory.
7181 */
7182 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007183get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007184 dict_T *d;
7185 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007186 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007187{
7188 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007189 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007190
7191 di = dict_find(d, key, -1);
7192 if (di == NULL)
7193 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007194 s = get_tv_string(&di->di_tv);
7195 if (save && s != NULL)
7196 s = vim_strsave(s);
7197 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007198}
7199
7200/*
7201 * Get a number item from a dictionary.
7202 * Returns 0 if the entry doesn't exist or out of memory.
7203 */
7204 long
7205get_dict_number(d, key)
7206 dict_T *d;
7207 char_u *key;
7208{
7209 dictitem_T *di;
7210
7211 di = dict_find(d, key, -1);
7212 if (di == NULL)
7213 return 0;
7214 return get_tv_number(&di->di_tv);
7215}
7216
7217/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218 * Return an allocated string with the string representation of a Dictionary.
7219 * May return NULL.
7220 */
7221 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007222dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007223 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007224 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225{
7226 garray_T ga;
7227 int first = TRUE;
7228 char_u *tofree;
7229 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007230 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007232 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007233 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007234
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007235 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007236 return NULL;
7237 ga_init2(&ga, (int)sizeof(char), 80);
7238 ga_append(&ga, '{');
7239
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007240 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007241 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007242 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007243 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007245 --todo;
7246
7247 if (first)
7248 first = FALSE;
7249 else
7250 ga_concat(&ga, (char_u *)", ");
7251
7252 tofree = string_quote(hi->hi_key, FALSE);
7253 if (tofree != NULL)
7254 {
7255 ga_concat(&ga, tofree);
7256 vim_free(tofree);
7257 }
7258 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007259 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007260 if (s != NULL)
7261 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007263 if (s == NULL)
7264 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007267 if (todo > 0)
7268 {
7269 vim_free(ga.ga_data);
7270 return NULL;
7271 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272
7273 ga_append(&ga, '}');
7274 ga_append(&ga, NUL);
7275 return (char_u *)ga.ga_data;
7276}
7277
7278/*
7279 * Allocate a variable for a Dictionary and fill it from "*arg".
7280 * Return OK or FAIL. Returns NOTDONE for {expr}.
7281 */
7282 static int
7283get_dict_tv(arg, rettv, evaluate)
7284 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007285 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007286 int evaluate;
7287{
Bram Moolenaar33570922005-01-25 22:26:29 +00007288 dict_T *d = NULL;
7289 typval_T tvkey;
7290 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007291 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007292 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007293 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007294 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007295
7296 /*
7297 * First check if it's not a curly-braces thing: {expr}.
7298 * Must do this without evaluating, otherwise a function may be called
7299 * twice. Unfortunately this means we need to call eval1() twice for the
7300 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007301 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007303 if (*start != '}')
7304 {
7305 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7306 return FAIL;
7307 if (*start == '}')
7308 return NOTDONE;
7309 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007310
7311 if (evaluate)
7312 {
7313 d = dict_alloc();
7314 if (d == NULL)
7315 return FAIL;
7316 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317 tvkey.v_type = VAR_UNKNOWN;
7318 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007319
7320 *arg = skipwhite(*arg + 1);
7321 while (**arg != '}' && **arg != NUL)
7322 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007324 goto failret;
7325 if (**arg != ':')
7326 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007327 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007328 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007329 goto failret;
7330 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007331 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007332 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007333 key = get_tv_string_buf_chk(&tvkey, buf);
7334 if (key == NULL || *key == NUL)
7335 {
7336 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7337 if (key != NULL)
7338 EMSG(_(e_emptykey));
7339 clear_tv(&tvkey);
7340 goto failret;
7341 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007343
7344 *arg = skipwhite(*arg + 1);
7345 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7346 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007347 if (evaluate)
7348 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007349 goto failret;
7350 }
7351 if (evaluate)
7352 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007354 if (item != NULL)
7355 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007356 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007357 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007358 clear_tv(&tv);
7359 goto failret;
7360 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007361 item = dictitem_alloc(key);
7362 clear_tv(&tvkey);
7363 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007364 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007365 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007366 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007367 if (dict_add(d, item) == FAIL)
7368 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007369 }
7370 }
7371
7372 if (**arg == '}')
7373 break;
7374 if (**arg != ',')
7375 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007376 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007377 goto failret;
7378 }
7379 *arg = skipwhite(*arg + 1);
7380 }
7381
7382 if (**arg != '}')
7383 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007384 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007385failret:
7386 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007387 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007388 return FAIL;
7389 }
7390
7391 *arg = skipwhite(*arg + 1);
7392 if (evaluate)
7393 {
7394 rettv->v_type = VAR_DICT;
7395 rettv->vval.v_dict = d;
7396 ++d->dv_refcount;
7397 }
7398
7399 return OK;
7400}
7401
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007403 * Return a string with the string representation of a variable.
7404 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007405 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007406 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007407 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007408 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007409 */
7410 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007411echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007412 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007413 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007414 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007415 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007416{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007417 static int recurse = 0;
7418 char_u *r = NULL;
7419
Bram Moolenaar33570922005-01-25 22:26:29 +00007420 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007421 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007422 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007423 *tofree = NULL;
7424 return NULL;
7425 }
7426 ++recurse;
7427
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007428 switch (tv->v_type)
7429 {
7430 case VAR_FUNC:
7431 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007432 r = tv->vval.v_string;
7433 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007434
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007435 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007436 if (tv->vval.v_list == NULL)
7437 {
7438 *tofree = NULL;
7439 r = NULL;
7440 }
7441 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7442 {
7443 *tofree = NULL;
7444 r = (char_u *)"[...]";
7445 }
7446 else
7447 {
7448 tv->vval.v_list->lv_copyID = copyID;
7449 *tofree = list2string(tv, copyID);
7450 r = *tofree;
7451 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007452 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007453
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007455 if (tv->vval.v_dict == NULL)
7456 {
7457 *tofree = NULL;
7458 r = NULL;
7459 }
7460 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7461 {
7462 *tofree = NULL;
7463 r = (char_u *)"{...}";
7464 }
7465 else
7466 {
7467 tv->vval.v_dict->dv_copyID = copyID;
7468 *tofree = dict2string(tv, copyID);
7469 r = *tofree;
7470 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007471 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007472
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007473 case VAR_STRING:
7474 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007475 *tofree = NULL;
7476 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007477 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007478
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007479#ifdef FEAT_FLOAT
7480 case VAR_FLOAT:
7481 *tofree = NULL;
7482 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7483 r = numbuf;
7484 break;
7485#endif
7486
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007487 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007488 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007489 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007490 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007491
7492 --recurse;
7493 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007494}
7495
7496/*
7497 * Return a string with the string representation of a variable.
7498 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7499 * "numbuf" is used for a number.
7500 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007501 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007502 */
7503 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007504tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007505 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007506 char_u **tofree;
7507 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007508 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007509{
7510 switch (tv->v_type)
7511 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007512 case VAR_FUNC:
7513 *tofree = string_quote(tv->vval.v_string, TRUE);
7514 return *tofree;
7515 case VAR_STRING:
7516 *tofree = string_quote(tv->vval.v_string, FALSE);
7517 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007518#ifdef FEAT_FLOAT
7519 case VAR_FLOAT:
7520 *tofree = NULL;
7521 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7522 return numbuf;
7523#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007524 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007525 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007526 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007527 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007528 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007529 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007530 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007531 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007532}
7533
7534/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007535 * Return string "str" in ' quotes, doubling ' characters.
7536 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007538 */
7539 static char_u *
7540string_quote(str, function)
7541 char_u *str;
7542 int function;
7543{
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007545 char_u *p, *r, *s;
7546
Bram Moolenaar33570922005-01-25 22:26:29 +00007547 len = (function ? 13 : 3);
7548 if (str != NULL)
7549 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007550 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007551 for (p = str; *p != NUL; mb_ptr_adv(p))
7552 if (*p == '\'')
7553 ++len;
7554 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007555 s = r = alloc(len);
7556 if (r != NULL)
7557 {
7558 if (function)
7559 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007561 r += 10;
7562 }
7563 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007564 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007565 if (str != NULL)
7566 for (p = str; *p != NUL; )
7567 {
7568 if (*p == '\'')
7569 *r++ = '\'';
7570 MB_COPY_CHAR(p, r);
7571 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007572 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007573 if (function)
7574 *r++ = ')';
7575 *r++ = NUL;
7576 }
7577 return s;
7578}
7579
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007580#ifdef FEAT_FLOAT
7581/*
7582 * Convert the string "text" to a floating point number.
7583 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7584 * this always uses a decimal point.
7585 * Returns the length of the text that was consumed.
7586 */
7587 static int
7588string2float(text, value)
7589 char_u *text;
7590 float_T *value; /* result stored here */
7591{
7592 char *s = (char *)text;
7593 float_T f;
7594
7595 f = strtod(s, &s);
7596 *value = f;
7597 return (int)((char_u *)s - text);
7598}
7599#endif
7600
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 * Get the value of an environment variable.
7603 * "arg" is pointing to the '$'. It is advanced to after the name.
7604 * If the environment variable was not set, silently assume it is empty.
7605 * Always return OK.
7606 */
7607 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007608get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007610 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 int evaluate;
7612{
7613 char_u *string = NULL;
7614 int len;
7615 int cc;
7616 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007617 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618
7619 ++*arg;
7620 name = *arg;
7621 len = get_env_len(arg);
7622 if (evaluate)
7623 {
7624 if (len != 0)
7625 {
7626 cc = name[len];
7627 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007628 /* first try vim_getenv(), fast for normal environment vars */
7629 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007631 {
7632 if (!mustfree)
7633 string = vim_strsave(string);
7634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 else
7636 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007637 if (mustfree)
7638 vim_free(string);
7639
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 /* next try expanding things like $VIM and ${HOME} */
7641 string = expand_env_save(name - 1);
7642 if (string != NULL && *string == '$')
7643 {
7644 vim_free(string);
7645 string = NULL;
7646 }
7647 }
7648 name[len] = cc;
7649 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007650 rettv->v_type = VAR_STRING;
7651 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 }
7653
7654 return OK;
7655}
7656
7657/*
7658 * Array with names and number of arguments of all internal functions
7659 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7660 */
7661static struct fst
7662{
7663 char *f_name; /* function name */
7664 char f_min_argc; /* minimal number of arguments */
7665 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007666 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007667 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668} functions[] =
7669{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007670#ifdef FEAT_FLOAT
7671 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007672 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007673#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007674 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 {"append", 2, 2, f_append},
7676 {"argc", 0, 0, f_argc},
7677 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007678 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007679#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007680 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007681 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007682 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007685 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 {"bufexists", 1, 1, f_bufexists},
7687 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7688 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7689 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7690 {"buflisted", 1, 1, f_buflisted},
7691 {"bufloaded", 1, 1, f_bufloaded},
7692 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007693 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 {"bufwinnr", 1, 1, f_bufwinnr},
7695 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007696 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007697 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007698#ifdef FEAT_FLOAT
7699 {"ceil", 1, 1, f_ceil},
7700#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007701 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 {"char2nr", 1, 1, f_char2nr},
7703 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007704 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007706#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007707 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007708 {"complete_add", 1, 1, f_complete_add},
7709 {"complete_check", 0, 0, f_complete_check},
7710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007712 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007713#ifdef FEAT_FLOAT
7714 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007715 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007716#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007717 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007719 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007720 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 {"delete", 1, 1, f_delete},
7722 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007723 {"diff_filler", 1, 1, f_diff_filler},
7724 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007725 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007727 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 {"eventhandler", 0, 0, f_eventhandler},
7729 {"executable", 1, 1, f_executable},
7730 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007731#ifdef FEAT_FLOAT
7732 {"exp", 1, 1, f_exp},
7733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007735 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007736 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7738 {"filereadable", 1, 1, f_filereadable},
7739 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007740 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007741 {"finddir", 1, 3, f_finddir},
7742 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007743#ifdef FEAT_FLOAT
7744 {"float2nr", 1, 1, f_float2nr},
7745 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007746 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007747#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007748 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 {"fnamemodify", 2, 2, f_fnamemodify},
7750 {"foldclosed", 1, 1, f_foldclosed},
7751 {"foldclosedend", 1, 1, f_foldclosedend},
7752 {"foldlevel", 1, 1, f_foldlevel},
7753 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007754 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007756 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007757 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007758 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007759 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 {"getbufvar", 2, 2, f_getbufvar},
7761 {"getchar", 0, 1, f_getchar},
7762 {"getcharmod", 0, 0, f_getcharmod},
7763 {"getcmdline", 0, 0, f_getcmdline},
7764 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007765 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007767 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007768 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 {"getfsize", 1, 1, f_getfsize},
7770 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007771 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007772 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007773 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007774 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007775 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007776 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007777 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007778 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007780 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007781 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 {"getwinposx", 0, 0, f_getwinposx},
7783 {"getwinposy", 0, 0, f_getwinposy},
7784 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007785 {"glob", 1, 2, f_glob},
7786 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007788 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007789 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007790 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7792 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7793 {"histadd", 2, 2, f_histadd},
7794 {"histdel", 1, 2, f_histdel},
7795 {"histget", 1, 2, f_histget},
7796 {"histnr", 1, 1, f_histnr},
7797 {"hlID", 1, 1, f_hlID},
7798 {"hlexists", 1, 1, f_hlexists},
7799 {"hostname", 0, 0, f_hostname},
7800 {"iconv", 3, 3, f_iconv},
7801 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007802 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007803 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007805 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 {"inputrestore", 0, 0, f_inputrestore},
7807 {"inputsave", 0, 0, f_inputsave},
7808 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007809 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007811 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007812 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007813 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007814 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007816 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 {"libcall", 3, 3, f_libcall},
7818 {"libcallnr", 3, 3, f_libcallnr},
7819 {"line", 1, 1, f_line},
7820 {"line2byte", 1, 1, f_line2byte},
7821 {"lispindent", 1, 1, f_lispindent},
7822 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007823#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007824 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007825 {"log10", 1, 1, f_log10},
7826#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007827 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007828 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007829 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007830 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007831 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007832 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007833 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007834 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007835 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007836 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007837 {"max", 1, 1, f_max},
7838 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007839#ifdef vim_mkdir
7840 {"mkdir", 1, 3, f_mkdir},
7841#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007842 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007843#ifdef FEAT_MZSCHEME
7844 {"mzeval", 1, 1, f_mzeval},
7845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 {"nextnonblank", 1, 1, f_nextnonblank},
7847 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007848 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007849#ifdef FEAT_FLOAT
7850 {"pow", 2, 2, f_pow},
7851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007853 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007854 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007855 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007856 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007857 {"reltime", 0, 2, f_reltime},
7858 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 {"remote_expr", 2, 3, f_remote_expr},
7860 {"remote_foreground", 1, 1, f_remote_foreground},
7861 {"remote_peek", 1, 2, f_remote_peek},
7862 {"remote_read", 1, 1, f_remote_read},
7863 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007864 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007866 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007868 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007869#ifdef FEAT_FLOAT
7870 {"round", 1, 1, f_round},
7871#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007872 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007873 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007874 {"searchpair", 3, 7, f_searchpair},
7875 {"searchpairpos", 3, 7, f_searchpairpos},
7876 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 {"server2client", 2, 2, f_server2client},
7878 {"serverlist", 0, 0, f_serverlist},
7879 {"setbufvar", 3, 3, f_setbufvar},
7880 {"setcmdpos", 1, 1, f_setcmdpos},
7881 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007882 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007883 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007884 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007885 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007887 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007888 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007890 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007892#ifdef FEAT_FLOAT
7893 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007894 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007895#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007896 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007897 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007898 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007899 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007900 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007901#ifdef FEAT_FLOAT
7902 {"sqrt", 1, 1, f_sqrt},
7903 {"str2float", 1, 1, f_str2float},
7904#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007905 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007906 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007907 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908#ifdef HAVE_STRFTIME
7909 {"strftime", 1, 2, f_strftime},
7910#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007911 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007912 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {"strlen", 1, 1, f_strlen},
7914 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007915 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007917 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {"submatch", 1, 1, f_submatch},
7919 {"substitute", 4, 4, f_substitute},
7920 {"synID", 3, 3, f_synID},
7921 {"synIDattr", 2, 3, f_synIDattr},
7922 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007923 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007924 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007925 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007926 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007927 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007928 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007929 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007930 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007931#ifdef FEAT_FLOAT
7932 {"tan", 1, 1, f_tan},
7933 {"tanh", 1, 1, f_tanh},
7934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007936 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 {"tolower", 1, 1, f_tolower},
7938 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007939 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007940#ifdef FEAT_FLOAT
7941 {"trunc", 1, 1, f_trunc},
7942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007944 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007945 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007946 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 {"virtcol", 1, 1, f_virtcol},
7948 {"visualmode", 0, 1, f_visualmode},
7949 {"winbufnr", 1, 1, f_winbufnr},
7950 {"wincol", 0, 0, f_wincol},
7951 {"winheight", 1, 1, f_winheight},
7952 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007953 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007955 {"winrestview", 1, 1, f_winrestview},
7956 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007958 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959};
7960
7961#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7962
7963/*
7964 * Function given to ExpandGeneric() to obtain the list of internal
7965 * or user defined function names.
7966 */
7967 char_u *
7968get_function_name(xp, idx)
7969 expand_T *xp;
7970 int idx;
7971{
7972 static int intidx = -1;
7973 char_u *name;
7974
7975 if (idx == 0)
7976 intidx = -1;
7977 if (intidx < 0)
7978 {
7979 name = get_user_func_name(xp, idx);
7980 if (name != NULL)
7981 return name;
7982 }
7983 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7984 {
7985 STRCPY(IObuff, functions[intidx].f_name);
7986 STRCAT(IObuff, "(");
7987 if (functions[intidx].f_max_argc == 0)
7988 STRCAT(IObuff, ")");
7989 return IObuff;
7990 }
7991
7992 return NULL;
7993}
7994
7995/*
7996 * Function given to ExpandGeneric() to obtain the list of internal or
7997 * user defined variable or function names.
7998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 char_u *
8000get_expr_name(xp, idx)
8001 expand_T *xp;
8002 int idx;
8003{
8004 static int intidx = -1;
8005 char_u *name;
8006
8007 if (idx == 0)
8008 intidx = -1;
8009 if (intidx < 0)
8010 {
8011 name = get_function_name(xp, idx);
8012 if (name != NULL)
8013 return name;
8014 }
8015 return get_user_var_name(xp, ++intidx);
8016}
8017
8018#endif /* FEAT_CMDL_COMPL */
8019
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008020#if defined(EBCDIC) || defined(PROTO)
8021/*
8022 * Compare struct fst by function name.
8023 */
8024 static int
8025compare_func_name(s1, s2)
8026 const void *s1;
8027 const void *s2;
8028{
8029 struct fst *p1 = (struct fst *)s1;
8030 struct fst *p2 = (struct fst *)s2;
8031
8032 return STRCMP(p1->f_name, p2->f_name);
8033}
8034
8035/*
8036 * Sort the function table by function name.
8037 * The sorting of the table above is ASCII dependant.
8038 * On machines using EBCDIC we have to sort it.
8039 */
8040 static void
8041sortFunctions()
8042{
8043 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8044
8045 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8046}
8047#endif
8048
8049
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050/*
8051 * Find internal function in table above.
8052 * Return index, or -1 if not found
8053 */
8054 static int
8055find_internal_func(name)
8056 char_u *name; /* name of the function */
8057{
8058 int first = 0;
8059 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8060 int cmp;
8061 int x;
8062
8063 /*
8064 * Find the function name in the table. Binary search.
8065 */
8066 while (first <= last)
8067 {
8068 x = first + ((unsigned)(last - first) >> 1);
8069 cmp = STRCMP(name, functions[x].f_name);
8070 if (cmp < 0)
8071 last = x - 1;
8072 else if (cmp > 0)
8073 first = x + 1;
8074 else
8075 return x;
8076 }
8077 return -1;
8078}
8079
8080/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008081 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8082 * name it contains, otherwise return "name".
8083 */
8084 static char_u *
8085deref_func_name(name, lenp)
8086 char_u *name;
8087 int *lenp;
8088{
Bram Moolenaar33570922005-01-25 22:26:29 +00008089 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008090 int cc;
8091
8092 cc = name[*lenp];
8093 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008094 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008095 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008096 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008097 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008098 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008099 {
8100 *lenp = 0;
8101 return (char_u *)""; /* just in case */
8102 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008103 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008104 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008105 }
8106
8107 return name;
8108}
8109
8110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 * Allocate a variable for the result of a function.
8112 * Return OK or FAIL.
8113 */
8114 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008115get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8116 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 char_u *name; /* name of the function */
8118 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 char_u **arg; /* argument, pointing to the '(' */
8121 linenr_T firstline; /* first line of range */
8122 linenr_T lastline; /* last line of range */
8123 int *doesrange; /* return: function handled range */
8124 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008125 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126{
8127 char_u *argp;
8128 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008129 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 int argcount = 0; /* number of arguments found */
8131
8132 /*
8133 * Get the arguments.
8134 */
8135 argp = *arg;
8136 while (argcount < MAX_FUNC_ARGS)
8137 {
8138 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8139 if (*argp == ')' || *argp == ',' || *argp == NUL)
8140 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8142 {
8143 ret = FAIL;
8144 break;
8145 }
8146 ++argcount;
8147 if (*argp != ',')
8148 break;
8149 }
8150 if (*argp == ')')
8151 ++argp;
8152 else
8153 ret = FAIL;
8154
8155 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008156 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008157 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008159 {
8160 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008161 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008162 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008163 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165
8166 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008167 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168
8169 *arg = skipwhite(argp);
8170 return ret;
8171}
8172
8173
8174/*
8175 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008176 * Return OK when the function can't be called, FAIL otherwise.
8177 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 */
8179 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008180call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008181 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008182 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008184 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008186 typval_T *argvars; /* vars for arguments, must have "argcount"
8187 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 linenr_T firstline; /* first line of range */
8189 linenr_T lastline; /* last line of range */
8190 int *doesrange; /* return: function handled range */
8191 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008192 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193{
8194 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195#define ERROR_UNKNOWN 0
8196#define ERROR_TOOMANY 1
8197#define ERROR_TOOFEW 2
8198#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008199#define ERROR_DICT 4
8200#define ERROR_NONE 5
8201#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 int error = ERROR_NONE;
8203 int i;
8204 int llen;
8205 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206#define FLEN_FIXED 40
8207 char_u fname_buf[FLEN_FIXED + 1];
8208 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008209 char_u *name;
8210
8211 /* Make a copy of the name, if it comes from a funcref variable it could
8212 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008213 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008214 if (name == NULL)
8215 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216
8217 /*
8218 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8219 * Change <SNR>123_name() to K_SNR 123_name().
8220 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8221 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 llen = eval_fname_script(name);
8223 if (llen > 0)
8224 {
8225 fname_buf[0] = K_SPECIAL;
8226 fname_buf[1] = KS_EXTRA;
8227 fname_buf[2] = (int)KE_SNR;
8228 i = 3;
8229 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8230 {
8231 if (current_SID <= 0)
8232 error = ERROR_SCRIPT;
8233 else
8234 {
8235 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8236 i = (int)STRLEN(fname_buf);
8237 }
8238 }
8239 if (i + STRLEN(name + llen) < FLEN_FIXED)
8240 {
8241 STRCPY(fname_buf + i, name + llen);
8242 fname = fname_buf;
8243 }
8244 else
8245 {
8246 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8247 if (fname == NULL)
8248 error = ERROR_OTHER;
8249 else
8250 {
8251 mch_memmove(fname, fname_buf, (size_t)i);
8252 STRCPY(fname + i, name + llen);
8253 }
8254 }
8255 }
8256 else
8257 fname = name;
8258
8259 *doesrange = FALSE;
8260
8261
8262 /* execute the function if no errors detected and executing */
8263 if (evaluate && error == ERROR_NONE)
8264 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008265 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8266 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 error = ERROR_UNKNOWN;
8268
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008269 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {
8271 /*
8272 * User defined function.
8273 */
8274 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008275
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008277 /* Trigger FuncUndefined event, may load the function. */
8278 if (fp == NULL
8279 && apply_autocmds(EVENT_FUNCUNDEFINED,
8280 fname, fname, TRUE, NULL)
8281 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008283 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 fp = find_func(fname);
8285 }
8286#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008287 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008288 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008289 {
8290 /* loaded a package, search for the function again */
8291 fp = find_func(fname);
8292 }
8293
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 if (fp != NULL)
8295 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008296 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008298 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008300 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008302 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008303 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 else
8305 {
8306 /*
8307 * Call the user function.
8308 * Save and restore search patterns, script variables and
8309 * redo buffer.
8310 */
8311 save_search_patterns();
8312 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008313 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008314 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008315 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008316 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8317 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8318 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008319 /* Function was unreferenced while being used, free it
8320 * now. */
8321 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 restoreRedobuff();
8323 restore_search_patterns();
8324 error = ERROR_NONE;
8325 }
8326 }
8327 }
8328 else
8329 {
8330 /*
8331 * Find the function name in the table, call its implementation.
8332 */
8333 i = find_internal_func(fname);
8334 if (i >= 0)
8335 {
8336 if (argcount < functions[i].f_min_argc)
8337 error = ERROR_TOOFEW;
8338 else if (argcount > functions[i].f_max_argc)
8339 error = ERROR_TOOMANY;
8340 else
8341 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008342 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008343 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 error = ERROR_NONE;
8345 }
8346 }
8347 }
8348 /*
8349 * The function call (or "FuncUndefined" autocommand sequence) might
8350 * have been aborted by an error, an interrupt, or an explicitly thrown
8351 * exception that has not been caught so far. This situation can be
8352 * tested for by calling aborting(). For an error in an internal
8353 * function or for the "E132" error in call_user_func(), however, the
8354 * throw point at which the "force_abort" flag (temporarily reset by
8355 * emsg()) is normally updated has not been reached yet. We need to
8356 * update that flag first to make aborting() reliable.
8357 */
8358 update_force_abort();
8359 }
8360 if (error == ERROR_NONE)
8361 ret = OK;
8362
8363 /*
8364 * Report an error unless the argument evaluation or function call has been
8365 * cancelled due to an aborting error, an interrupt, or an exception.
8366 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008367 if (!aborting())
8368 {
8369 switch (error)
8370 {
8371 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008372 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008373 break;
8374 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008375 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008376 break;
8377 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008378 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008379 name);
8380 break;
8381 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008382 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008383 name);
8384 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008385 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008386 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008387 name);
8388 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008389 }
8390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 if (fname != name && fname != fname_buf)
8393 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008394 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395
8396 return ret;
8397}
8398
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008399/*
8400 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008401 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008402 */
8403 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008404emsg_funcname(ermsg, name)
8405 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008406 char_u *name;
8407{
8408 char_u *p;
8409
8410 if (*name == K_SPECIAL)
8411 p = concat_str((char_u *)"<SNR>", name + 3);
8412 else
8413 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008414 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008415 if (p != name)
8416 vim_free(p);
8417}
8418
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008419/*
8420 * Return TRUE for a non-zero Number and a non-empty String.
8421 */
8422 static int
8423non_zero_arg(argvars)
8424 typval_T *argvars;
8425{
8426 return ((argvars[0].v_type == VAR_NUMBER
8427 && argvars[0].vval.v_number != 0)
8428 || (argvars[0].v_type == VAR_STRING
8429 && argvars[0].vval.v_string != NULL
8430 && *argvars[0].vval.v_string != NUL));
8431}
8432
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433/*********************************************
8434 * Implementation of the built-in functions
8435 */
8436
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008437#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008438static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8439
8440/*
8441 * Get the float value of "argvars[0]" into "f".
8442 * Returns FAIL when the argument is not a Number or Float.
8443 */
8444 static int
8445get_float_arg(argvars, f)
8446 typval_T *argvars;
8447 float_T *f;
8448{
8449 if (argvars[0].v_type == VAR_FLOAT)
8450 {
8451 *f = argvars[0].vval.v_float;
8452 return OK;
8453 }
8454 if (argvars[0].v_type == VAR_NUMBER)
8455 {
8456 *f = (float_T)argvars[0].vval.v_number;
8457 return OK;
8458 }
8459 EMSG(_("E808: Number or Float required"));
8460 return FAIL;
8461}
8462
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008463/*
8464 * "abs(expr)" function
8465 */
8466 static void
8467f_abs(argvars, rettv)
8468 typval_T *argvars;
8469 typval_T *rettv;
8470{
8471 if (argvars[0].v_type == VAR_FLOAT)
8472 {
8473 rettv->v_type = VAR_FLOAT;
8474 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8475 }
8476 else
8477 {
8478 varnumber_T n;
8479 int error = FALSE;
8480
8481 n = get_tv_number_chk(&argvars[0], &error);
8482 if (error)
8483 rettv->vval.v_number = -1;
8484 else if (n > 0)
8485 rettv->vval.v_number = n;
8486 else
8487 rettv->vval.v_number = -n;
8488 }
8489}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008490
8491/*
8492 * "acos()" function
8493 */
8494 static void
8495f_acos(argvars, rettv)
8496 typval_T *argvars;
8497 typval_T *rettv;
8498{
8499 float_T f;
8500
8501 rettv->v_type = VAR_FLOAT;
8502 if (get_float_arg(argvars, &f) == OK)
8503 rettv->vval.v_float = acos(f);
8504 else
8505 rettv->vval.v_float = 0.0;
8506}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008507#endif
8508
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008510 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 */
8512 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008513f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008514 typval_T *argvars;
8515 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516{
Bram Moolenaar33570922005-01-25 22:26:29 +00008517 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008519 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008520 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008522 if ((l = argvars[0].vval.v_list) != NULL
8523 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8524 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008525 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008526 }
8527 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008528 EMSG(_(e_listreq));
8529}
8530
8531/*
8532 * "append(lnum, string/list)" function
8533 */
8534 static void
8535f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008536 typval_T *argvars;
8537 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008538{
8539 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008540 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008541 list_T *l = NULL;
8542 listitem_T *li = NULL;
8543 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008544 long added = 0;
8545
Bram Moolenaar0d660222005-01-07 21:51:51 +00008546 lnum = get_tv_lnum(argvars);
8547 if (lnum >= 0
8548 && lnum <= curbuf->b_ml.ml_line_count
8549 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008550 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008551 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008552 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008553 l = argvars[1].vval.v_list;
8554 if (l == NULL)
8555 return;
8556 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008557 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008558 for (;;)
8559 {
8560 if (l == NULL)
8561 tv = &argvars[1]; /* append a string */
8562 else if (li == NULL)
8563 break; /* end of list */
8564 else
8565 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008566 line = get_tv_string_chk(tv);
8567 if (line == NULL) /* type error */
8568 {
8569 rettv->vval.v_number = 1; /* Failed */
8570 break;
8571 }
8572 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008573 ++added;
8574 if (l == NULL)
8575 break;
8576 li = li->li_next;
8577 }
8578
8579 appended_lines_mark(lnum, added);
8580 if (curwin->w_cursor.lnum > lnum)
8581 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008583 else
8584 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585}
8586
8587/*
8588 * "argc()" function
8589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008591f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008592 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008595 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596}
8597
8598/*
8599 * "argidx()" function
8600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008602f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008603 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008606 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607}
8608
8609/*
8610 * "argv(nr)" function
8611 */
8612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008613f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008614 typval_T *argvars;
8615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616{
8617 int idx;
8618
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008619 if (argvars[0].v_type != VAR_UNKNOWN)
8620 {
8621 idx = get_tv_number_chk(&argvars[0], NULL);
8622 if (idx >= 0 && idx < ARGCOUNT)
8623 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8624 else
8625 rettv->vval.v_string = NULL;
8626 rettv->v_type = VAR_STRING;
8627 }
8628 else if (rettv_list_alloc(rettv) == OK)
8629 for (idx = 0; idx < ARGCOUNT; ++idx)
8630 list_append_string(rettv->vval.v_list,
8631 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632}
8633
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008634#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008635/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008636 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008637 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008638 static void
8639f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008640 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008641 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008642{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008643 float_T f;
8644
8645 rettv->v_type = VAR_FLOAT;
8646 if (get_float_arg(argvars, &f) == OK)
8647 rettv->vval.v_float = asin(f);
8648 else
8649 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008650}
8651
8652/*
8653 * "atan()" function
8654 */
8655 static void
8656f_atan(argvars, rettv)
8657 typval_T *argvars;
8658 typval_T *rettv;
8659{
8660 float_T f;
8661
8662 rettv->v_type = VAR_FLOAT;
8663 if (get_float_arg(argvars, &f) == OK)
8664 rettv->vval.v_float = atan(f);
8665 else
8666 rettv->vval.v_float = 0.0;
8667}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008668
8669/*
8670 * "atan2()" function
8671 */
8672 static void
8673f_atan2(argvars, rettv)
8674 typval_T *argvars;
8675 typval_T *rettv;
8676{
8677 float_T fx, fy;
8678
8679 rettv->v_type = VAR_FLOAT;
8680 if (get_float_arg(argvars, &fx) == OK
8681 && get_float_arg(&argvars[1], &fy) == OK)
8682 rettv->vval.v_float = atan2(fx, fy);
8683 else
8684 rettv->vval.v_float = 0.0;
8685}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008686#endif
8687
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688/*
8689 * "browse(save, title, initdir, default)" function
8690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008692f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008693 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695{
8696#ifdef FEAT_BROWSE
8697 int save;
8698 char_u *title;
8699 char_u *initdir;
8700 char_u *defname;
8701 char_u buf[NUMBUFLEN];
8702 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008703 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008705 save = get_tv_number_chk(&argvars[0], &error);
8706 title = get_tv_string_chk(&argvars[1]);
8707 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8708 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008710 if (error || title == NULL || initdir == NULL || defname == NULL)
8711 rettv->vval.v_string = NULL;
8712 else
8713 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008714 do_browse(save ? BROWSE_SAVE : 0,
8715 title, defname, NULL, initdir, NULL, curbuf);
8716#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008717 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008718#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008719 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008720}
8721
8722/*
8723 * "browsedir(title, initdir)" function
8724 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008726f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008727 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008728 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008729{
8730#ifdef FEAT_BROWSE
8731 char_u *title;
8732 char_u *initdir;
8733 char_u buf[NUMBUFLEN];
8734
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008735 title = get_tv_string_chk(&argvars[0]);
8736 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008737
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008738 if (title == NULL || initdir == NULL)
8739 rettv->vval.v_string = NULL;
8740 else
8741 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008742 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008744 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008746 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747}
8748
Bram Moolenaar33570922005-01-25 22:26:29 +00008749static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008750
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751/*
8752 * Find a buffer by number or exact name.
8753 */
8754 static buf_T *
8755find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008756 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757{
8758 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008760 if (avar->v_type == VAR_NUMBER)
8761 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008762 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008764 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008765 if (buf == NULL)
8766 {
8767 /* No full path name match, try a match with a URL or a "nofile"
8768 * buffer, these don't use the full path. */
8769 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8770 if (buf->b_fname != NULL
8771 && (path_with_url(buf->b_fname)
8772#ifdef FEAT_QUICKFIX
8773 || bt_nofile(buf)
8774#endif
8775 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008776 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008777 break;
8778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 }
8780 return buf;
8781}
8782
8783/*
8784 * "bufexists(expr)" function
8785 */
8786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008787f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008788 typval_T *argvars;
8789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008791 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792}
8793
8794/*
8795 * "buflisted(expr)" function
8796 */
8797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008798f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008799 typval_T *argvars;
8800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801{
8802 buf_T *buf;
8803
8804 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008805 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806}
8807
8808/*
8809 * "bufloaded(expr)" function
8810 */
8811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008812f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008813 typval_T *argvars;
8814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815{
8816 buf_T *buf;
8817
8818 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008819 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820}
8821
Bram Moolenaar33570922005-01-25 22:26:29 +00008822static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008823
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824/*
8825 * Get buffer by number or pattern.
8826 */
8827 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008828get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008829 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 int save_magic;
8833 char_u *save_cpo;
8834 buf_T *buf;
8835
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008836 if (tv->v_type == VAR_NUMBER)
8837 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008838 if (tv->v_type != VAR_STRING)
8839 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 if (name == NULL || *name == NUL)
8841 return curbuf;
8842 if (name[0] == '$' && name[1] == NUL)
8843 return lastbuf;
8844
8845 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8846 save_magic = p_magic;
8847 p_magic = TRUE;
8848 save_cpo = p_cpo;
8849 p_cpo = (char_u *)"";
8850
8851 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8852 TRUE, FALSE));
8853
8854 p_magic = save_magic;
8855 p_cpo = save_cpo;
8856
8857 /* If not found, try expanding the name, like done for bufexists(). */
8858 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008859 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860
8861 return buf;
8862}
8863
8864/*
8865 * "bufname(expr)" function
8866 */
8867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008868f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008869 typval_T *argvars;
8870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871{
8872 buf_T *buf;
8873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008874 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008876 buf = get_buf_tv(&argvars[0]);
8877 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008879 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008881 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 --emsg_off;
8883}
8884
8885/*
8886 * "bufnr(expr)" function
8887 */
8888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008889f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008890 typval_T *argvars;
8891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892{
8893 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008894 int error = FALSE;
8895 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008897 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008899 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008900 --emsg_off;
8901
8902 /* If the buffer isn't found and the second argument is not zero create a
8903 * new buffer. */
8904 if (buf == NULL
8905 && argvars[1].v_type != VAR_UNKNOWN
8906 && get_tv_number_chk(&argvars[1], &error) != 0
8907 && !error
8908 && (name = get_tv_string_chk(&argvars[0])) != NULL
8909 && !error)
8910 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8911
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008913 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008915 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916}
8917
8918/*
8919 * "bufwinnr(nr)" function
8920 */
8921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008922f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008923 typval_T *argvars;
8924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925{
8926#ifdef FEAT_WINDOWS
8927 win_T *wp;
8928 int winnr = 0;
8929#endif
8930 buf_T *buf;
8931
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008932 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008934 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935#ifdef FEAT_WINDOWS
8936 for (wp = firstwin; wp; wp = wp->w_next)
8937 {
8938 ++winnr;
8939 if (wp->w_buffer == buf)
8940 break;
8941 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008942 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945#endif
8946 --emsg_off;
8947}
8948
8949/*
8950 * "byte2line(byte)" function
8951 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008953f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008954 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956{
8957#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959#else
8960 long boff = 0;
8961
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008962 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008964 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 (linenr_T)0, &boff);
8968#endif
8969}
8970
8971/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008972 * "byteidx()" function
8973 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008975f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008976 typval_T *argvars;
8977 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008978{
8979#ifdef FEAT_MBYTE
8980 char_u *t;
8981#endif
8982 char_u *str;
8983 long idx;
8984
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008985 str = get_tv_string_chk(&argvars[0]);
8986 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008987 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008988 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008989 return;
8990
8991#ifdef FEAT_MBYTE
8992 t = str;
8993 for ( ; idx > 0; idx--)
8994 {
8995 if (*t == NUL) /* EOL reached */
8996 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008997 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008998 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008999 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009000#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009001 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009002 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009003#endif
9004}
9005
9006/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009007 * "call(func, arglist)" function
9008 */
9009 static void
9010f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009011 typval_T *argvars;
9012 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009013{
9014 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009015 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009016 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00009017 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009018 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00009019 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009020
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009021 if (argvars[1].v_type != VAR_LIST)
9022 {
9023 EMSG(_(e_listreq));
9024 return;
9025 }
9026 if (argvars[1].vval.v_list == NULL)
9027 return;
9028
9029 if (argvars[0].v_type == VAR_FUNC)
9030 func = argvars[0].vval.v_string;
9031 else
9032 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009033 if (*func == NUL)
9034 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009035
Bram Moolenaare9a41262005-01-15 22:18:47 +00009036 if (argvars[2].v_type != VAR_UNKNOWN)
9037 {
9038 if (argvars[2].v_type != VAR_DICT)
9039 {
9040 EMSG(_(e_dictreq));
9041 return;
9042 }
9043 selfdict = argvars[2].vval.v_dict;
9044 }
9045
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009046 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9047 item = item->li_next)
9048 {
9049 if (argc == MAX_FUNC_ARGS)
9050 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009051 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009052 break;
9053 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009054 /* Make a copy of each argument. This is needed to be able to set
9055 * v_lock to VAR_FIXED in the copy without changing the original list.
9056 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009057 copy_tv(&item->li_tv, &argv[argc++]);
9058 }
9059
9060 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009061 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009062 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9063 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009064
9065 /* Free the arguments. */
9066 while (argc > 0)
9067 clear_tv(&argv[--argc]);
9068}
9069
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009070#ifdef FEAT_FLOAT
9071/*
9072 * "ceil({float})" function
9073 */
9074 static void
9075f_ceil(argvars, rettv)
9076 typval_T *argvars;
9077 typval_T *rettv;
9078{
9079 float_T f;
9080
9081 rettv->v_type = VAR_FLOAT;
9082 if (get_float_arg(argvars, &f) == OK)
9083 rettv->vval.v_float = ceil(f);
9084 else
9085 rettv->vval.v_float = 0.0;
9086}
9087#endif
9088
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009089/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009090 * "changenr()" function
9091 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009092 static void
9093f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009094 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009095 typval_T *rettv;
9096{
9097 rettv->vval.v_number = curbuf->b_u_seq_cur;
9098}
9099
9100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 * "char2nr(string)" function
9102 */
9103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009104f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009105 typval_T *argvars;
9106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107{
9108#ifdef FEAT_MBYTE
9109 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009110 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 else
9112#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009113 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114}
9115
9116/*
9117 * "cindent(lnum)" function
9118 */
9119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009121 typval_T *argvars;
9122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123{
9124#ifdef FEAT_CINDENT
9125 pos_T pos;
9126 linenr_T lnum;
9127
9128 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009129 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9131 {
9132 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009133 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 curwin->w_cursor = pos;
9135 }
9136 else
9137#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009138 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139}
9140
9141/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009142 * "clearmatches()" function
9143 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009144 static void
9145f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009146 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009147 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009148{
9149#ifdef FEAT_SEARCH_EXTRA
9150 clear_matches(curwin);
9151#endif
9152}
9153
9154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 * "col(string)" function
9156 */
9157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009158f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009159 typval_T *argvars;
9160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161{
9162 colnr_T col = 0;
9163 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009164 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009166 fp = var2fpos(&argvars[0], FALSE, &fnum);
9167 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 {
9169 if (fp->col == MAXCOL)
9170 {
9171 /* '> can be MAXCOL, get the length of the line then */
9172 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009173 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 else
9175 col = MAXCOL;
9176 }
9177 else
9178 {
9179 col = fp->col + 1;
9180#ifdef FEAT_VIRTUALEDIT
9181 /* col(".") when the cursor is on the NUL at the end of the line
9182 * because of "coladd" can be seen as an extra column. */
9183 if (virtual_active() && fp == &curwin->w_cursor)
9184 {
9185 char_u *p = ml_get_cursor();
9186
9187 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9188 curwin->w_virtcol - curwin->w_cursor.coladd))
9189 {
9190# ifdef FEAT_MBYTE
9191 int l;
9192
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009193 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 col += l;
9195# else
9196 if (*p != NUL && p[1] == NUL)
9197 ++col;
9198# endif
9199 }
9200 }
9201#endif
9202 }
9203 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009204 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205}
9206
Bram Moolenaar572cb562005-08-05 21:35:02 +00009207#if defined(FEAT_INS_EXPAND)
9208/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009209 * "complete()" function
9210 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009211 static void
9212f_complete(argvars, rettv)
9213 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009214 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009215{
9216 int startcol;
9217
9218 if ((State & INSERT) == 0)
9219 {
9220 EMSG(_("E785: complete() can only be used in Insert mode"));
9221 return;
9222 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009223
9224 /* Check for undo allowed here, because if something was already inserted
9225 * the line was already saved for undo and this check isn't done. */
9226 if (!undo_allowed())
9227 return;
9228
Bram Moolenaarade00832006-03-10 21:46:58 +00009229 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9230 {
9231 EMSG(_(e_invarg));
9232 return;
9233 }
9234
9235 startcol = get_tv_number_chk(&argvars[0], NULL);
9236 if (startcol <= 0)
9237 return;
9238
9239 set_completion(startcol - 1, argvars[1].vval.v_list);
9240}
9241
9242/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009243 * "complete_add()" function
9244 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009245 static void
9246f_complete_add(argvars, rettv)
9247 typval_T *argvars;
9248 typval_T *rettv;
9249{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009250 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009251}
9252
9253/*
9254 * "complete_check()" function
9255 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009256 static void
9257f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009258 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009259 typval_T *rettv;
9260{
9261 int saved = RedrawingDisabled;
9262
9263 RedrawingDisabled = 0;
9264 ins_compl_check_keys(0);
9265 rettv->vval.v_number = compl_interrupted;
9266 RedrawingDisabled = saved;
9267}
9268#endif
9269
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270/*
9271 * "confirm(message, buttons[, default [, type]])" function
9272 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009274f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009275 typval_T *argvars UNUSED;
9276 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277{
9278#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9279 char_u *message;
9280 char_u *buttons = NULL;
9281 char_u buf[NUMBUFLEN];
9282 char_u buf2[NUMBUFLEN];
9283 int def = 1;
9284 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009285 char_u *typestr;
9286 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009288 message = get_tv_string_chk(&argvars[0]);
9289 if (message == NULL)
9290 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009291 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009293 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9294 if (buttons == NULL)
9295 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009296 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009298 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009299 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009301 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9302 if (typestr == NULL)
9303 error = TRUE;
9304 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009306 switch (TOUPPER_ASC(*typestr))
9307 {
9308 case 'E': type = VIM_ERROR; break;
9309 case 'Q': type = VIM_QUESTION; break;
9310 case 'I': type = VIM_INFO; break;
9311 case 'W': type = VIM_WARNING; break;
9312 case 'G': type = VIM_GENERIC; break;
9313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 }
9315 }
9316 }
9317 }
9318
9319 if (buttons == NULL || *buttons == NUL)
9320 buttons = (char_u *)_("&Ok");
9321
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009322 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009323 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009324 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325#endif
9326}
9327
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009328/*
9329 * "copy()" function
9330 */
9331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009332f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009333 typval_T *argvars;
9334 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009335{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009336 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009337}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009339#ifdef FEAT_FLOAT
9340/*
9341 * "cos()" function
9342 */
9343 static void
9344f_cos(argvars, rettv)
9345 typval_T *argvars;
9346 typval_T *rettv;
9347{
9348 float_T f;
9349
9350 rettv->v_type = VAR_FLOAT;
9351 if (get_float_arg(argvars, &f) == OK)
9352 rettv->vval.v_float = cos(f);
9353 else
9354 rettv->vval.v_float = 0.0;
9355}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009356
9357/*
9358 * "cosh()" function
9359 */
9360 static void
9361f_cosh(argvars, rettv)
9362 typval_T *argvars;
9363 typval_T *rettv;
9364{
9365 float_T f;
9366
9367 rettv->v_type = VAR_FLOAT;
9368 if (get_float_arg(argvars, &f) == OK)
9369 rettv->vval.v_float = cosh(f);
9370 else
9371 rettv->vval.v_float = 0.0;
9372}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009373#endif
9374
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009376 * "count()" function
9377 */
9378 static void
9379f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009380 typval_T *argvars;
9381 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009382{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009383 long n = 0;
9384 int ic = FALSE;
9385
Bram Moolenaare9a41262005-01-15 22:18:47 +00009386 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009387 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009388 listitem_T *li;
9389 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009390 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009391
Bram Moolenaare9a41262005-01-15 22:18:47 +00009392 if ((l = argvars[0].vval.v_list) != NULL)
9393 {
9394 li = l->lv_first;
9395 if (argvars[2].v_type != VAR_UNKNOWN)
9396 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009397 int error = FALSE;
9398
9399 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009400 if (argvars[3].v_type != VAR_UNKNOWN)
9401 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009402 idx = get_tv_number_chk(&argvars[3], &error);
9403 if (!error)
9404 {
9405 li = list_find(l, idx);
9406 if (li == NULL)
9407 EMSGN(_(e_listidx), idx);
9408 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009409 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009410 if (error)
9411 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009412 }
9413
9414 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009415 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009416 ++n;
9417 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009418 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009419 else if (argvars[0].v_type == VAR_DICT)
9420 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009421 int todo;
9422 dict_T *d;
9423 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009424
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009425 if ((d = argvars[0].vval.v_dict) != NULL)
9426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009427 int error = FALSE;
9428
Bram Moolenaare9a41262005-01-15 22:18:47 +00009429 if (argvars[2].v_type != VAR_UNKNOWN)
9430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009431 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009432 if (argvars[3].v_type != VAR_UNKNOWN)
9433 EMSG(_(e_invarg));
9434 }
9435
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009436 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009437 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009438 {
9439 if (!HASHITEM_EMPTY(hi))
9440 {
9441 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009442 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009443 ++n;
9444 }
9445 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009446 }
9447 }
9448 else
9449 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009450 rettv->vval.v_number = n;
9451}
9452
9453/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9455 *
9456 * Checks the existence of a cscope connection.
9457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009459f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009460 typval_T *argvars UNUSED;
9461 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462{
9463#ifdef FEAT_CSCOPE
9464 int num = 0;
9465 char_u *dbpath = NULL;
9466 char_u *prepend = NULL;
9467 char_u buf[NUMBUFLEN];
9468
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009469 if (argvars[0].v_type != VAR_UNKNOWN
9470 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009472 num = (int)get_tv_number(&argvars[0]);
9473 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009474 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009475 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 }
9477
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009478 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479#endif
9480}
9481
9482/*
9483 * "cursor(lnum, col)" function
9484 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009485 * Moves the cursor to the specified line and column.
9486 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009489f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009490 typval_T *argvars;
9491 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492{
9493 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009494#ifdef FEAT_VIRTUALEDIT
9495 long coladd = 0;
9496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009498 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009499 if (argvars[1].v_type == VAR_UNKNOWN)
9500 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009501 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009502
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009503 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009504 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009505 line = pos.lnum;
9506 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009507#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009508 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009509#endif
9510 }
9511 else
9512 {
9513 line = get_tv_lnum(argvars);
9514 col = get_tv_number_chk(&argvars[1], NULL);
9515#ifdef FEAT_VIRTUALEDIT
9516 if (argvars[2].v_type != VAR_UNKNOWN)
9517 coladd = get_tv_number_chk(&argvars[2], NULL);
9518#endif
9519 }
9520 if (line < 0 || col < 0
9521#ifdef FEAT_VIRTUALEDIT
9522 || coladd < 0
9523#endif
9524 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009525 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 if (line > 0)
9527 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 if (col > 0)
9529 curwin->w_cursor.col = col - 1;
9530#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009531 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532#endif
9533
9534 /* Make sure the cursor is in a valid position. */
9535 check_cursor();
9536#ifdef FEAT_MBYTE
9537 /* Correct cursor for multi-byte character. */
9538 if (has_mbyte)
9539 mb_adjust_cursor();
9540#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009541
9542 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009543 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544}
9545
9546/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009547 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 */
9549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009550f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009551 typval_T *argvars;
9552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009554 int noref = 0;
9555
9556 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009557 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009558 if (noref < 0 || noref > 1)
9559 EMSG(_(e_invarg));
9560 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009561 {
9562 current_copyID += COPYID_INC;
9563 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565}
9566
9567/*
9568 * "delete()" function
9569 */
9570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009571f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009572 typval_T *argvars;
9573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574{
9575 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009576 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009578 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579}
9580
9581/*
9582 * "did_filetype()" function
9583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009585f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009586 typval_T *argvars UNUSED;
9587 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588{
9589#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009590 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591#endif
9592}
9593
9594/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009595 * "diff_filler()" function
9596 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009599 typval_T *argvars UNUSED;
9600 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009601{
9602#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009603 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009604#endif
9605}
9606
9607/*
9608 * "diff_hlID()" function
9609 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009611f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009612 typval_T *argvars UNUSED;
9613 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009614{
9615#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009617 static linenr_T prev_lnum = 0;
9618 static int changedtick = 0;
9619 static int fnum = 0;
9620 static int change_start = 0;
9621 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009622 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009623 int filler_lines;
9624 int col;
9625
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009626 if (lnum < 0) /* ignore type error in {lnum} arg */
9627 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009628 if (lnum != prev_lnum
9629 || changedtick != curbuf->b_changedtick
9630 || fnum != curbuf->b_fnum)
9631 {
9632 /* New line, buffer, change: need to get the values. */
9633 filler_lines = diff_check(curwin, lnum);
9634 if (filler_lines < 0)
9635 {
9636 if (filler_lines == -1)
9637 {
9638 change_start = MAXCOL;
9639 change_end = -1;
9640 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9641 hlID = HLF_ADD; /* added line */
9642 else
9643 hlID = HLF_CHD; /* changed line */
9644 }
9645 else
9646 hlID = HLF_ADD; /* added line */
9647 }
9648 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009649 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009650 prev_lnum = lnum;
9651 changedtick = curbuf->b_changedtick;
9652 fnum = curbuf->b_fnum;
9653 }
9654
9655 if (hlID == HLF_CHD || hlID == HLF_TXD)
9656 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009657 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009658 if (col >= change_start && col <= change_end)
9659 hlID = HLF_TXD; /* changed text */
9660 else
9661 hlID = HLF_CHD; /* changed line */
9662 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009663 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009664#endif
9665}
9666
9667/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009668 * "empty({expr})" function
9669 */
9670 static void
9671f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009672 typval_T *argvars;
9673 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009674{
9675 int n;
9676
9677 switch (argvars[0].v_type)
9678 {
9679 case VAR_STRING:
9680 case VAR_FUNC:
9681 n = argvars[0].vval.v_string == NULL
9682 || *argvars[0].vval.v_string == NUL;
9683 break;
9684 case VAR_NUMBER:
9685 n = argvars[0].vval.v_number == 0;
9686 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009687#ifdef FEAT_FLOAT
9688 case VAR_FLOAT:
9689 n = argvars[0].vval.v_float == 0.0;
9690 break;
9691#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009692 case VAR_LIST:
9693 n = argvars[0].vval.v_list == NULL
9694 || argvars[0].vval.v_list->lv_first == NULL;
9695 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009696 case VAR_DICT:
9697 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009698 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009699 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009700 default:
9701 EMSG2(_(e_intern2), "f_empty()");
9702 n = 0;
9703 }
9704
9705 rettv->vval.v_number = n;
9706}
9707
9708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 * "escape({string}, {chars})" function
9710 */
9711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009712f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009713 typval_T *argvars;
9714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715{
9716 char_u buf[NUMBUFLEN];
9717
Bram Moolenaar758711c2005-02-02 23:11:38 +00009718 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9719 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009720 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721}
9722
9723/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009724 * "eval()" function
9725 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009726 static void
9727f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009728 typval_T *argvars;
9729 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009730{
9731 char_u *s;
9732
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009733 s = get_tv_string_chk(&argvars[0]);
9734 if (s != NULL)
9735 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009736
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009737 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9738 {
9739 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009740 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009741 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009742 else if (*s != NUL)
9743 EMSG(_(e_trailing));
9744}
9745
9746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 * "eventhandler()" function
9748 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009751 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009754 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755}
9756
9757/*
9758 * "executable()" function
9759 */
9760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009761f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009762 typval_T *argvars;
9763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009765 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766}
9767
9768/*
9769 * "exists()" function
9770 */
9771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009772f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009773 typval_T *argvars;
9774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775{
9776 char_u *p;
9777 char_u *name;
9778 int n = FALSE;
9779 int len = 0;
9780
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009781 no_autoload = TRUE;
9782
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009783 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 if (*p == '$') /* environment variable */
9785 {
9786 /* first try "normal" environment variables (fast) */
9787 if (mch_getenv(p + 1) != NULL)
9788 n = TRUE;
9789 else
9790 {
9791 /* try expanding things like $VIM and ${HOME} */
9792 p = expand_env_save(p);
9793 if (p != NULL && *p != '$')
9794 n = TRUE;
9795 vim_free(p);
9796 }
9797 }
9798 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009799 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009800 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009801 if (*skipwhite(p) != NUL)
9802 n = FALSE; /* trailing garbage */
9803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 else if (*p == '*') /* internal or user defined function */
9805 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009806 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 }
9808 else if (*p == ':')
9809 {
9810 n = cmd_exists(p + 1);
9811 }
9812 else if (*p == '#')
9813 {
9814#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009815 if (p[1] == '#')
9816 n = autocmd_supported(p + 2);
9817 else
9818 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819#endif
9820 }
9821 else /* internal variable */
9822 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009823 char_u *tofree;
9824 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009826 /* get_name_len() takes care of expanding curly braces */
9827 name = p;
9828 len = get_name_len(&p, &tofree, TRUE, FALSE);
9829 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009831 if (tofree != NULL)
9832 name = tofree;
9833 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9834 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009836 /* handle d.key, l[idx], f(expr) */
9837 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9838 if (n)
9839 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 }
9841 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009842 if (*p != NUL)
9843 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009845 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 }
9847
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009848 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009849
9850 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851}
9852
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009853#ifdef FEAT_FLOAT
9854/*
9855 * "exp()" function
9856 */
9857 static void
9858f_exp(argvars, rettv)
9859 typval_T *argvars;
9860 typval_T *rettv;
9861{
9862 float_T f;
9863
9864 rettv->v_type = VAR_FLOAT;
9865 if (get_float_arg(argvars, &f) == OK)
9866 rettv->vval.v_float = exp(f);
9867 else
9868 rettv->vval.v_float = 0.0;
9869}
9870#endif
9871
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872/*
9873 * "expand()" function
9874 */
9875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009877 typval_T *argvars;
9878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879{
9880 char_u *s;
9881 int len;
9882 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009883 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009885 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009887 rettv->v_type = VAR_STRING;
9888 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 if (*s == '%' || *s == '#' || *s == '<')
9890 {
9891 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009892 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 --emsg_off;
9894 }
9895 else
9896 {
9897 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009898 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009899 if (argvars[1].v_type != VAR_UNKNOWN
9900 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009901 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009902 if (!error)
9903 {
9904 ExpandInit(&xpc);
9905 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009906 if (p_wic)
9907 options += WILD_ICASE;
9908 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, options, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009909 }
9910 else
9911 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912 }
9913}
9914
9915/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009916 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009917 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009918 */
9919 static void
9920f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009921 typval_T *argvars;
9922 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009923{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009924 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009925 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009926 list_T *l1, *l2;
9927 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009928 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009929 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009930
Bram Moolenaare9a41262005-01-15 22:18:47 +00009931 l1 = argvars[0].vval.v_list;
9932 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009933 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9934 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009935 {
9936 if (argvars[2].v_type != VAR_UNKNOWN)
9937 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009938 before = get_tv_number_chk(&argvars[2], &error);
9939 if (error)
9940 return; /* type error; errmsg already given */
9941
Bram Moolenaar758711c2005-02-02 23:11:38 +00009942 if (before == l1->lv_len)
9943 item = NULL;
9944 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009945 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009946 item = list_find(l1, before);
9947 if (item == NULL)
9948 {
9949 EMSGN(_(e_listidx), before);
9950 return;
9951 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009952 }
9953 }
9954 else
9955 item = NULL;
9956 list_extend(l1, l2, item);
9957
Bram Moolenaare9a41262005-01-15 22:18:47 +00009958 copy_tv(&argvars[0], rettv);
9959 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009960 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009961 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9962 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009963 dict_T *d1, *d2;
9964 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009965 char_u *action;
9966 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009967 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009968 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009969
9970 d1 = argvars[0].vval.v_dict;
9971 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009972 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9973 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009974 {
9975 /* Check the third argument. */
9976 if (argvars[2].v_type != VAR_UNKNOWN)
9977 {
9978 static char *(av[]) = {"keep", "force", "error"};
9979
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009980 action = get_tv_string_chk(&argvars[2]);
9981 if (action == NULL)
9982 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009983 for (i = 0; i < 3; ++i)
9984 if (STRCMP(action, av[i]) == 0)
9985 break;
9986 if (i == 3)
9987 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009988 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009989 return;
9990 }
9991 }
9992 else
9993 action = (char_u *)"force";
9994
9995 /* Go over all entries in the second dict and add them to the
9996 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009997 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009998 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009999 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010000 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010001 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010002 --todo;
10003 di1 = dict_find(d1, hi2->hi_key, -1);
10004 if (di1 == NULL)
10005 {
10006 di1 = dictitem_copy(HI2DI(hi2));
10007 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10008 dictitem_free(di1);
10009 }
10010 else if (*action == 'e')
10011 {
10012 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10013 break;
10014 }
10015 else if (*action == 'f')
10016 {
10017 clear_tv(&di1->di_tv);
10018 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10019 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010020 }
10021 }
10022
Bram Moolenaare9a41262005-01-15 22:18:47 +000010023 copy_tv(&argvars[0], rettv);
10024 }
10025 }
10026 else
10027 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010028}
10029
10030/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010031 * "feedkeys()" function
10032 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010033 static void
10034f_feedkeys(argvars, rettv)
10035 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010036 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010037{
10038 int remap = TRUE;
10039 char_u *keys, *flags;
10040 char_u nbuf[NUMBUFLEN];
10041 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010042 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010043
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010044 /* This is not allowed in the sandbox. If the commands would still be
10045 * executed in the sandbox it would be OK, but it probably happens later,
10046 * when "sandbox" is no longer set. */
10047 if (check_secure())
10048 return;
10049
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010050 keys = get_tv_string(&argvars[0]);
10051 if (*keys != NUL)
10052 {
10053 if (argvars[1].v_type != VAR_UNKNOWN)
10054 {
10055 flags = get_tv_string_buf(&argvars[1], nbuf);
10056 for ( ; *flags != NUL; ++flags)
10057 {
10058 switch (*flags)
10059 {
10060 case 'n': remap = FALSE; break;
10061 case 'm': remap = TRUE; break;
10062 case 't': typed = TRUE; break;
10063 }
10064 }
10065 }
10066
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010067 /* Need to escape K_SPECIAL and CSI before putting the string in the
10068 * typeahead buffer. */
10069 keys_esc = vim_strsave_escape_csi(keys);
10070 if (keys_esc != NULL)
10071 {
10072 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010073 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010074 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010075 if (vgetc_busy)
10076 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010077 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010078 }
10079}
10080
10081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 * "filereadable()" function
10083 */
10084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010085f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010086 typval_T *argvars;
10087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010089 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 char_u *p;
10091 int n;
10092
Bram Moolenaarc236c162008-07-13 17:41:49 +000010093#ifndef O_NONBLOCK
10094# define O_NONBLOCK 0
10095#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010096 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010097 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10098 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 {
10100 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010101 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 }
10103 else
10104 n = FALSE;
10105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010106 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107}
10108
10109/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010110 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 * rights to write into.
10112 */
10113 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010114f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010115 typval_T *argvars;
10116 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010118 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119}
10120
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010121static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010122
10123 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010124findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010125 typval_T *argvars;
10126 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010127 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010128{
10129#ifdef FEAT_SEARCHPATH
10130 char_u *fname;
10131 char_u *fresult = NULL;
10132 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10133 char_u *p;
10134 char_u pathbuf[NUMBUFLEN];
10135 int count = 1;
10136 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010137 int error = FALSE;
10138#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010139
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010140 rettv->vval.v_string = NULL;
10141 rettv->v_type = VAR_STRING;
10142
10143#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010144 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010145
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010146 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010147 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010148 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10149 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010150 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010151 else
10152 {
10153 if (*p != NUL)
10154 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010155
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010156 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010157 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010158 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010159 }
10160
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010161 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10162 error = TRUE;
10163
10164 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010165 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010166 do
10167 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010168 if (rettv->v_type == VAR_STRING)
10169 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010170 fresult = find_file_in_path_option(first ? fname : NULL,
10171 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010172 0, first, path,
10173 find_what,
10174 curbuf->b_ffname,
10175 find_what == FINDFILE_DIR
10176 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010177 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010178
10179 if (fresult != NULL && rettv->v_type == VAR_LIST)
10180 list_append_string(rettv->vval.v_list, fresult, -1);
10181
10182 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010183 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010184
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010185 if (rettv->v_type == VAR_STRING)
10186 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010187#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010188}
10189
Bram Moolenaar33570922005-01-25 22:26:29 +000010190static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10191static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010192
10193/*
10194 * Implementation of map() and filter().
10195 */
10196 static void
10197filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010198 typval_T *argvars;
10199 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010200 int map;
10201{
10202 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010203 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010204 listitem_T *li, *nli;
10205 list_T *l = NULL;
10206 dictitem_T *di;
10207 hashtab_T *ht;
10208 hashitem_T *hi;
10209 dict_T *d = NULL;
10210 typval_T save_val;
10211 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010212 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010213 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010214 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010215 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010216 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010217
Bram Moolenaare9a41262005-01-15 22:18:47 +000010218 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010219 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010220 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010221 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010222 return;
10223 }
10224 else if (argvars[0].v_type == VAR_DICT)
10225 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010226 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010227 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010228 return;
10229 }
10230 else
10231 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010232 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010233 return;
10234 }
10235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010236 expr = get_tv_string_buf_chk(&argvars[1], buf);
10237 /* On type errors, the preceding call has already displayed an error
10238 * message. Avoid a misleading error message for an empty string that
10239 * was not passed as argument. */
10240 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010242 prepare_vimvar(VV_VAL, &save_val);
10243 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010244
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010245 /* We reset "did_emsg" to be able to detect whether an error
10246 * occurred during evaluation of the expression. */
10247 save_did_emsg = did_emsg;
10248 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010249
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010250 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010251 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010253 vimvars[VV_KEY].vv_type = VAR_STRING;
10254
10255 ht = &d->dv_hashtab;
10256 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010257 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010258 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010260 if (!HASHITEM_EMPTY(hi))
10261 {
10262 --todo;
10263 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010264 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010265 break;
10266 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010267 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010268 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010269 break;
10270 if (!map && rem)
10271 dictitem_remove(d, di);
10272 clear_tv(&vimvars[VV_KEY].vv_tv);
10273 }
10274 }
10275 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010276 }
10277 else
10278 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010279 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10280
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010281 for (li = l->lv_first; li != NULL; li = nli)
10282 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010283 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010284 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010285 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010286 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010287 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010288 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010289 break;
10290 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010291 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010292 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010293 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010294 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010295
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010296 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010297 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010298
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010299 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010300 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010301
10302 copy_tv(&argvars[0], rettv);
10303}
10304
10305 static int
10306filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010307 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010308 char_u *expr;
10309 int map;
10310 int *remp;
10311{
Bram Moolenaar33570922005-01-25 22:26:29 +000010312 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010313 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010314 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010315
Bram Moolenaar33570922005-01-25 22:26:29 +000010316 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010317 s = expr;
10318 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010319 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010320 if (*s != NUL) /* check for trailing chars after expr */
10321 {
10322 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010323 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010324 }
10325 if (map)
10326 {
10327 /* map(): replace the list item value */
10328 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010329 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010330 *tv = rettv;
10331 }
10332 else
10333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010334 int error = FALSE;
10335
Bram Moolenaare9a41262005-01-15 22:18:47 +000010336 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010337 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010338 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010339 /* On type error, nothing has been removed; return FAIL to stop the
10340 * loop. The error message was given by get_tv_number_chk(). */
10341 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010342 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010343 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010344 retval = OK;
10345theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010346 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010347 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010348}
10349
10350/*
10351 * "filter()" function
10352 */
10353 static void
10354f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010355 typval_T *argvars;
10356 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010357{
10358 filter_map(argvars, rettv, FALSE);
10359}
10360
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010361/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010362 * "finddir({fname}[, {path}[, {count}]])" function
10363 */
10364 static void
10365f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010366 typval_T *argvars;
10367 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010368{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010369 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010370}
10371
10372/*
10373 * "findfile({fname}[, {path}[, {count}]])" function
10374 */
10375 static void
10376f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010377 typval_T *argvars;
10378 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010379{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010380 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010381}
10382
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010383#ifdef FEAT_FLOAT
10384/*
10385 * "float2nr({float})" function
10386 */
10387 static void
10388f_float2nr(argvars, rettv)
10389 typval_T *argvars;
10390 typval_T *rettv;
10391{
10392 float_T f;
10393
10394 if (get_float_arg(argvars, &f) == OK)
10395 {
10396 if (f < -0x7fffffff)
10397 rettv->vval.v_number = -0x7fffffff;
10398 else if (f > 0x7fffffff)
10399 rettv->vval.v_number = 0x7fffffff;
10400 else
10401 rettv->vval.v_number = (varnumber_T)f;
10402 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010403}
10404
10405/*
10406 * "floor({float})" function
10407 */
10408 static void
10409f_floor(argvars, rettv)
10410 typval_T *argvars;
10411 typval_T *rettv;
10412{
10413 float_T f;
10414
10415 rettv->v_type = VAR_FLOAT;
10416 if (get_float_arg(argvars, &f) == OK)
10417 rettv->vval.v_float = floor(f);
10418 else
10419 rettv->vval.v_float = 0.0;
10420}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010421
10422/*
10423 * "fmod()" function
10424 */
10425 static void
10426f_fmod(argvars, rettv)
10427 typval_T *argvars;
10428 typval_T *rettv;
10429{
10430 float_T fx, fy;
10431
10432 rettv->v_type = VAR_FLOAT;
10433 if (get_float_arg(argvars, &fx) == OK
10434 && get_float_arg(&argvars[1], &fy) == OK)
10435 rettv->vval.v_float = fmod(fx, fy);
10436 else
10437 rettv->vval.v_float = 0.0;
10438}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010439#endif
10440
Bram Moolenaar0d660222005-01-07 21:51:51 +000010441/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010442 * "fnameescape({string})" function
10443 */
10444 static void
10445f_fnameescape(argvars, rettv)
10446 typval_T *argvars;
10447 typval_T *rettv;
10448{
10449 rettv->vval.v_string = vim_strsave_fnameescape(
10450 get_tv_string(&argvars[0]), FALSE);
10451 rettv->v_type = VAR_STRING;
10452}
10453
10454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 * "fnamemodify({fname}, {mods})" function
10456 */
10457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010458f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010459 typval_T *argvars;
10460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461{
10462 char_u *fname;
10463 char_u *mods;
10464 int usedlen = 0;
10465 int len;
10466 char_u *fbuf = NULL;
10467 char_u buf[NUMBUFLEN];
10468
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010469 fname = get_tv_string_chk(&argvars[0]);
10470 mods = get_tv_string_buf_chk(&argvars[1], buf);
10471 if (fname == NULL || mods == NULL)
10472 fname = NULL;
10473 else
10474 {
10475 len = (int)STRLEN(fname);
10476 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010479 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010481 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010483 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 vim_free(fbuf);
10485}
10486
Bram Moolenaar33570922005-01-25 22:26:29 +000010487static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488
10489/*
10490 * "foldclosed()" function
10491 */
10492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010493foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010494 typval_T *argvars;
10495 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 int end;
10497{
10498#ifdef FEAT_FOLDING
10499 linenr_T lnum;
10500 linenr_T first, last;
10501
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010502 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10504 {
10505 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10506 {
10507 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010508 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010510 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 return;
10512 }
10513 }
10514#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010515 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516}
10517
10518/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010519 * "foldclosed()" function
10520 */
10521 static void
10522f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010523 typval_T *argvars;
10524 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010525{
10526 foldclosed_both(argvars, rettv, FALSE);
10527}
10528
10529/*
10530 * "foldclosedend()" function
10531 */
10532 static void
10533f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010534 typval_T *argvars;
10535 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010536{
10537 foldclosed_both(argvars, rettv, TRUE);
10538}
10539
10540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 * "foldlevel()" function
10542 */
10543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010544f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010545 typval_T *argvars;
10546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547{
10548#ifdef FEAT_FOLDING
10549 linenr_T lnum;
10550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010551 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010553 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555}
10556
10557/*
10558 * "foldtext()" function
10559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010561f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010562 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564{
10565#ifdef FEAT_FOLDING
10566 linenr_T lnum;
10567 char_u *s;
10568 char_u *r;
10569 int len;
10570 char *txt;
10571#endif
10572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010573 rettv->v_type = VAR_STRING;
10574 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10577 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10578 <= curbuf->b_ml.ml_line_count
10579 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 {
10581 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010582 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10583 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584 {
10585 if (!linewhite(lnum))
10586 break;
10587 ++lnum;
10588 }
10589
10590 /* Find interesting text in this line. */
10591 s = skipwhite(ml_get(lnum));
10592 /* skip C comment-start */
10593 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010594 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010596 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010597 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010598 {
10599 s = skipwhite(ml_get(lnum + 1));
10600 if (*s == '*')
10601 s = skipwhite(s + 1);
10602 }
10603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604 txt = _("+-%s%3ld lines: ");
10605 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010606 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607 + 20 /* for %3ld */
10608 + STRLEN(s))); /* concatenated */
10609 if (r != NULL)
10610 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010611 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10612 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10613 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 len = (int)STRLEN(r);
10615 STRCAT(r, s);
10616 /* remove 'foldmarker' and 'commentstring' */
10617 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010618 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619 }
10620 }
10621#endif
10622}
10623
10624/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010625 * "foldtextresult(lnum)" function
10626 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010628f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010629 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010630 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010631{
10632#ifdef FEAT_FOLDING
10633 linenr_T lnum;
10634 char_u *text;
10635 char_u buf[51];
10636 foldinfo_T foldinfo;
10637 int fold_count;
10638#endif
10639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010640 rettv->v_type = VAR_STRING;
10641 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010642#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010643 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010644 /* treat illegal types and illegal string values for {lnum} the same */
10645 if (lnum < 0)
10646 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010647 fold_count = foldedCount(curwin, lnum, &foldinfo);
10648 if (fold_count > 0)
10649 {
10650 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10651 &foldinfo, buf);
10652 if (text == buf)
10653 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010654 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010655 }
10656#endif
10657}
10658
10659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 * "foreground()" function
10661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010663f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010664 typval_T *argvars UNUSED;
10665 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667#ifdef FEAT_GUI
10668 if (gui.in_use)
10669 gui_mch_set_foreground();
10670#else
10671# ifdef WIN32
10672 win32_set_foreground();
10673# endif
10674#endif
10675}
10676
10677/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010678 * "function()" function
10679 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010681f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010682 typval_T *argvars;
10683 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010684{
10685 char_u *s;
10686
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010687 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010688 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010689 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010690 /* Don't check an autoload name for existence here. */
10691 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010692 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010693 else
10694 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695 rettv->vval.v_string = vim_strsave(s);
10696 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010697 }
10698}
10699
10700/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010701 * "garbagecollect()" function
10702 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010703 static void
10704f_garbagecollect(argvars, rettv)
10705 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010706 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010707{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010708 /* This is postponed until we are back at the toplevel, because we may be
10709 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10710 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010711
10712 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10713 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010714}
10715
10716/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010717 * "get()" function
10718 */
10719 static void
10720f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010721 typval_T *argvars;
10722 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010723{
Bram Moolenaar33570922005-01-25 22:26:29 +000010724 listitem_T *li;
10725 list_T *l;
10726 dictitem_T *di;
10727 dict_T *d;
10728 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010729
Bram Moolenaare9a41262005-01-15 22:18:47 +000010730 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010731 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010732 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010733 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010734 int error = FALSE;
10735
10736 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10737 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010738 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010739 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010740 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010741 else if (argvars[0].v_type == VAR_DICT)
10742 {
10743 if ((d = argvars[0].vval.v_dict) != NULL)
10744 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010745 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010746 if (di != NULL)
10747 tv = &di->di_tv;
10748 }
10749 }
10750 else
10751 EMSG2(_(e_listdictarg), "get()");
10752
10753 if (tv == NULL)
10754 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010755 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010756 copy_tv(&argvars[2], rettv);
10757 }
10758 else
10759 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010760}
10761
Bram Moolenaar342337a2005-07-21 21:11:17 +000010762static 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 +000010763
10764/*
10765 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010766 * Return a range (from start to end) of lines in rettv from the specified
10767 * buffer.
10768 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010769 */
10770 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010771get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010772 buf_T *buf;
10773 linenr_T start;
10774 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010775 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010776 typval_T *rettv;
10777{
10778 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010779
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010780 if (retlist && rettv_list_alloc(rettv) == FAIL)
10781 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010782
10783 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10784 return;
10785
10786 if (!retlist)
10787 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010788 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10789 p = ml_get_buf(buf, start, FALSE);
10790 else
10791 p = (char_u *)"";
10792
10793 rettv->v_type = VAR_STRING;
10794 rettv->vval.v_string = vim_strsave(p);
10795 }
10796 else
10797 {
10798 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010799 return;
10800
10801 if (start < 1)
10802 start = 1;
10803 if (end > buf->b_ml.ml_line_count)
10804 end = buf->b_ml.ml_line_count;
10805 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010806 if (list_append_string(rettv->vval.v_list,
10807 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010808 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010809 }
10810}
10811
10812/*
10813 * "getbufline()" function
10814 */
10815 static void
10816f_getbufline(argvars, rettv)
10817 typval_T *argvars;
10818 typval_T *rettv;
10819{
10820 linenr_T lnum;
10821 linenr_T end;
10822 buf_T *buf;
10823
10824 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10825 ++emsg_off;
10826 buf = get_buf_tv(&argvars[0]);
10827 --emsg_off;
10828
Bram Moolenaar661b1822005-07-28 22:36:45 +000010829 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010830 if (argvars[2].v_type == VAR_UNKNOWN)
10831 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010832 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010833 end = get_tv_lnum_buf(&argvars[2], buf);
10834
Bram Moolenaar342337a2005-07-21 21:11:17 +000010835 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010836}
10837
Bram Moolenaar0d660222005-01-07 21:51:51 +000010838/*
10839 * "getbufvar()" function
10840 */
10841 static void
10842f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010843 typval_T *argvars;
10844 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010845{
10846 buf_T *buf;
10847 buf_T *save_curbuf;
10848 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010849 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010851 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10852 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010853 ++emsg_off;
10854 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010855
10856 rettv->v_type = VAR_STRING;
10857 rettv->vval.v_string = NULL;
10858
10859 if (buf != NULL && varname != NULL)
10860 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010861 /* set curbuf to be our buf, temporarily */
10862 save_curbuf = curbuf;
10863 curbuf = buf;
10864
Bram Moolenaar0d660222005-01-07 21:51:51 +000010865 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010866 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010010867 else if (STRCMP(varname, "changedtick") == 0)
10868 {
10869 rettv->v_type = VAR_NUMBER;
10870 rettv->vval.v_number = curbuf->b_changedtick;
10871 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010872 else
10873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010874 if (*varname == NUL)
10875 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10876 * scope prefix before the NUL byte is required by
10877 * find_var_in_ht(). */
10878 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010879 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010880 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010881 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010882 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010883 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010884
10885 /* restore previous notion of curbuf */
10886 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010887 }
10888
10889 --emsg_off;
10890}
10891
10892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 * "getchar()" function
10894 */
10895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010896f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010897 typval_T *argvars;
10898 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899{
10900 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010901 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010903 /* Position the cursor. Needed after a message that ends in a space. */
10904 windgoto(msg_row, msg_col);
10905
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 ++no_mapping;
10907 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010908 for (;;)
10909 {
10910 if (argvars[0].v_type == VAR_UNKNOWN)
10911 /* getchar(): blocking wait. */
10912 n = safe_vgetc();
10913 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10914 /* getchar(1): only check if char avail */
10915 n = vpeekc();
10916 else if (error || vpeekc() == NUL)
10917 /* illegal argument or getchar(0) and no char avail: return zero */
10918 n = 0;
10919 else
10920 /* getchar(0) and char avail: return char */
10921 n = safe_vgetc();
10922 if (n == K_IGNORE)
10923 continue;
10924 break;
10925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 --no_mapping;
10927 --allow_keys;
10928
Bram Moolenaar219b8702006-11-01 14:32:36 +000010929 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10930 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10931 vimvars[VV_MOUSE_COL].vv_nr = 0;
10932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010933 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 if (IS_SPECIAL(n) || mod_mask != 0)
10935 {
10936 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10937 int i = 0;
10938
10939 /* Turn a special key into three bytes, plus modifier. */
10940 if (mod_mask != 0)
10941 {
10942 temp[i++] = K_SPECIAL;
10943 temp[i++] = KS_MODIFIER;
10944 temp[i++] = mod_mask;
10945 }
10946 if (IS_SPECIAL(n))
10947 {
10948 temp[i++] = K_SPECIAL;
10949 temp[i++] = K_SECOND(n);
10950 temp[i++] = K_THIRD(n);
10951 }
10952#ifdef FEAT_MBYTE
10953 else if (has_mbyte)
10954 i += (*mb_char2bytes)(n, temp + i);
10955#endif
10956 else
10957 temp[i++] = n;
10958 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010959 rettv->v_type = VAR_STRING;
10960 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010961
10962#ifdef FEAT_MOUSE
10963 if (n == K_LEFTMOUSE
10964 || n == K_LEFTMOUSE_NM
10965 || n == K_LEFTDRAG
10966 || n == K_LEFTRELEASE
10967 || n == K_LEFTRELEASE_NM
10968 || n == K_MIDDLEMOUSE
10969 || n == K_MIDDLEDRAG
10970 || n == K_MIDDLERELEASE
10971 || n == K_RIGHTMOUSE
10972 || n == K_RIGHTDRAG
10973 || n == K_RIGHTRELEASE
10974 || n == K_X1MOUSE
10975 || n == K_X1DRAG
10976 || n == K_X1RELEASE
10977 || n == K_X2MOUSE
10978 || n == K_X2DRAG
10979 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020010980 || n == K_MOUSELEFT
10981 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000010982 || n == K_MOUSEDOWN
10983 || n == K_MOUSEUP)
10984 {
10985 int row = mouse_row;
10986 int col = mouse_col;
10987 win_T *win;
10988 linenr_T lnum;
10989# ifdef FEAT_WINDOWS
10990 win_T *wp;
10991# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010992 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010993
10994 if (row >= 0 && col >= 0)
10995 {
10996 /* Find the window at the mouse coordinates and compute the
10997 * text position. */
10998 win = mouse_find_win(&row, &col);
10999 (void)mouse_comp_pos(win, &row, &col, &lnum);
11000# ifdef FEAT_WINDOWS
11001 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011002 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011003# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011004 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011005 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11006 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11007 }
11008 }
11009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 }
11011}
11012
11013/*
11014 * "getcharmod()" function
11015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011017f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011018 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011021 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022}
11023
11024/*
11025 * "getcmdline()" function
11026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011028f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011029 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011032 rettv->v_type = VAR_STRING;
11033 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034}
11035
11036/*
11037 * "getcmdpos()" function
11038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011040f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011041 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045}
11046
11047/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011048 * "getcmdtype()" function
11049 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011050 static void
11051f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011052 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011053 typval_T *rettv;
11054{
11055 rettv->v_type = VAR_STRING;
11056 rettv->vval.v_string = alloc(2);
11057 if (rettv->vval.v_string != NULL)
11058 {
11059 rettv->vval.v_string[0] = get_cmdline_type();
11060 rettv->vval.v_string[1] = NUL;
11061 }
11062}
11063
11064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 * "getcwd()" function
11066 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011068f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011069 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071{
11072 char_u cwd[MAXPATHL];
11073
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011074 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011076 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 else
11078 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011079 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000011081 if (rettv->vval.v_string != NULL)
11082 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083#endif
11084 }
11085}
11086
11087/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011088 * "getfontname()" function
11089 */
11090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011091f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011092 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011093 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011094{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011095 rettv->v_type = VAR_STRING;
11096 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011097#ifdef FEAT_GUI
11098 if (gui.in_use)
11099 {
11100 GuiFont font;
11101 char_u *name = NULL;
11102
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011103 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011104 {
11105 /* Get the "Normal" font. Either the name saved by
11106 * hl_set_font_name() or from the font ID. */
11107 font = gui.norm_font;
11108 name = hl_get_font_name();
11109 }
11110 else
11111 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011113 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11114 return;
11115 font = gui_mch_get_font(name, FALSE);
11116 if (font == NOFONT)
11117 return; /* Invalid font name, return empty string. */
11118 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011119 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011120 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011121 gui_mch_free_font(font);
11122 }
11123#endif
11124}
11125
11126/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011127 * "getfperm({fname})" function
11128 */
11129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011130f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011131 typval_T *argvars;
11132 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011133{
11134 char_u *fname;
11135 struct stat st;
11136 char_u *perm = NULL;
11137 char_u flags[] = "rwx";
11138 int i;
11139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011141
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011142 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011143 if (mch_stat((char *)fname, &st) >= 0)
11144 {
11145 perm = vim_strsave((char_u *)"---------");
11146 if (perm != NULL)
11147 {
11148 for (i = 0; i < 9; i++)
11149 {
11150 if (st.st_mode & (1 << (8 - i)))
11151 perm[i] = flags[i % 3];
11152 }
11153 }
11154 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011155 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011156}
11157
11158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159 * "getfsize({fname})" function
11160 */
11161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011162f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011163 typval_T *argvars;
11164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165{
11166 char_u *fname;
11167 struct stat st;
11168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011169 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011171 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172
11173 if (mch_stat((char *)fname, &st) >= 0)
11174 {
11175 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011176 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011178 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011179 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011180
11181 /* non-perfect check for overflow */
11182 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11183 rettv->vval.v_number = -2;
11184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185 }
11186 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011187 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188}
11189
11190/*
11191 * "getftime({fname})" function
11192 */
11193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011194f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011195 typval_T *argvars;
11196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197{
11198 char_u *fname;
11199 struct stat st;
11200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011201 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202
11203 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011204 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011206 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207}
11208
11209/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011210 * "getftype({fname})" function
11211 */
11212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011213f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011214 typval_T *argvars;
11215 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011216{
11217 char_u *fname;
11218 struct stat st;
11219 char_u *type = NULL;
11220 char *t;
11221
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011222 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011223
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011224 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011225 if (mch_lstat((char *)fname, &st) >= 0)
11226 {
11227#ifdef S_ISREG
11228 if (S_ISREG(st.st_mode))
11229 t = "file";
11230 else if (S_ISDIR(st.st_mode))
11231 t = "dir";
11232# ifdef S_ISLNK
11233 else if (S_ISLNK(st.st_mode))
11234 t = "link";
11235# endif
11236# ifdef S_ISBLK
11237 else if (S_ISBLK(st.st_mode))
11238 t = "bdev";
11239# endif
11240# ifdef S_ISCHR
11241 else if (S_ISCHR(st.st_mode))
11242 t = "cdev";
11243# endif
11244# ifdef S_ISFIFO
11245 else if (S_ISFIFO(st.st_mode))
11246 t = "fifo";
11247# endif
11248# ifdef S_ISSOCK
11249 else if (S_ISSOCK(st.st_mode))
11250 t = "fifo";
11251# endif
11252 else
11253 t = "other";
11254#else
11255# ifdef S_IFMT
11256 switch (st.st_mode & S_IFMT)
11257 {
11258 case S_IFREG: t = "file"; break;
11259 case S_IFDIR: t = "dir"; break;
11260# ifdef S_IFLNK
11261 case S_IFLNK: t = "link"; break;
11262# endif
11263# ifdef S_IFBLK
11264 case S_IFBLK: t = "bdev"; break;
11265# endif
11266# ifdef S_IFCHR
11267 case S_IFCHR: t = "cdev"; break;
11268# endif
11269# ifdef S_IFIFO
11270 case S_IFIFO: t = "fifo"; break;
11271# endif
11272# ifdef S_IFSOCK
11273 case S_IFSOCK: t = "socket"; break;
11274# endif
11275 default: t = "other";
11276 }
11277# else
11278 if (mch_isdir(fname))
11279 t = "dir";
11280 else
11281 t = "file";
11282# endif
11283#endif
11284 type = vim_strsave((char_u *)t);
11285 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011287}
11288
11289/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011290 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011291 */
11292 static void
11293f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011294 typval_T *argvars;
11295 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011296{
11297 linenr_T lnum;
11298 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011299 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011300
11301 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011302 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011303 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011304 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011305 retlist = FALSE;
11306 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011307 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011308 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011309 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011310 retlist = TRUE;
11311 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011312
Bram Moolenaar342337a2005-07-21 21:11:17 +000011313 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011314}
11315
11316/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011317 * "getmatches()" function
11318 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011319 static void
11320f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011321 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011322 typval_T *rettv;
11323{
11324#ifdef FEAT_SEARCH_EXTRA
11325 dict_T *dict;
11326 matchitem_T *cur = curwin->w_match_head;
11327
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011328 if (rettv_list_alloc(rettv) == OK)
11329 {
11330 while (cur != NULL)
11331 {
11332 dict = dict_alloc();
11333 if (dict == NULL)
11334 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011335 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11336 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11337 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11338 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11339 list_append_dict(rettv->vval.v_list, dict);
11340 cur = cur->next;
11341 }
11342 }
11343#endif
11344}
11345
11346/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011347 * "getpid()" function
11348 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011349 static void
11350f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011351 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011352 typval_T *rettv;
11353{
11354 rettv->vval.v_number = mch_get_pid();
11355}
11356
11357/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011358 * "getpos(string)" function
11359 */
11360 static void
11361f_getpos(argvars, rettv)
11362 typval_T *argvars;
11363 typval_T *rettv;
11364{
11365 pos_T *fp;
11366 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011367 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011368
11369 if (rettv_list_alloc(rettv) == OK)
11370 {
11371 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011372 fp = var2fpos(&argvars[0], TRUE, &fnum);
11373 if (fnum != -1)
11374 list_append_number(l, (varnumber_T)fnum);
11375 else
11376 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011377 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11378 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011379 list_append_number(l, (fp != NULL)
11380 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011381 : (varnumber_T)0);
11382 list_append_number(l,
11383#ifdef FEAT_VIRTUALEDIT
11384 (fp != NULL) ? (varnumber_T)fp->coladd :
11385#endif
11386 (varnumber_T)0);
11387 }
11388 else
11389 rettv->vval.v_number = FALSE;
11390}
11391
11392/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011393 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011394 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011395 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011396f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011397 typval_T *argvars UNUSED;
11398 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011399{
11400#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011401 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011402#endif
11403
Bram Moolenaar2641f772005-03-25 21:58:17 +000011404#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011405 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011406 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011407 wp = NULL;
11408 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11409 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011410 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011411 if (wp == NULL)
11412 return;
11413 }
11414
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011415 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011416 }
11417#endif
11418}
11419
11420/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421 * "getreg()" function
11422 */
11423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011424f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011425 typval_T *argvars;
11426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427{
11428 char_u *strregname;
11429 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011430 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011431 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011433 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011434 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011435 strregname = get_tv_string_chk(&argvars[0]);
11436 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011437 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011438 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011441 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442 regname = (strregname == NULL ? '"' : *strregname);
11443 if (regname == 0)
11444 regname = '"';
11445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011447 rettv->vval.v_string = error ? NULL :
11448 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449}
11450
11451/*
11452 * "getregtype()" function
11453 */
11454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011455f_getregtype(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;
11461 char_u buf[NUMBUFLEN + 2];
11462 long reglen = 0;
11463
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011464 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011465 {
11466 strregname = get_tv_string_chk(&argvars[0]);
11467 if (strregname == NULL) /* type error; errmsg already given */
11468 {
11469 rettv->v_type = VAR_STRING;
11470 rettv->vval.v_string = NULL;
11471 return;
11472 }
11473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474 else
11475 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011476 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477
11478 regname = (strregname == NULL ? '"' : *strregname);
11479 if (regname == 0)
11480 regname = '"';
11481
11482 buf[0] = NUL;
11483 buf[1] = NUL;
11484 switch (get_reg_type(regname, &reglen))
11485 {
11486 case MLINE: buf[0] = 'V'; break;
11487 case MCHAR: buf[0] = 'v'; break;
11488#ifdef FEAT_VISUAL
11489 case MBLOCK:
11490 buf[0] = Ctrl_V;
11491 sprintf((char *)buf + 1, "%ld", reglen + 1);
11492 break;
11493#endif
11494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011495 rettv->v_type = VAR_STRING;
11496 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497}
11498
11499/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011500 * "gettabvar()" function
11501 */
11502 static void
11503f_gettabvar(argvars, rettv)
11504 typval_T *argvars;
11505 typval_T *rettv;
11506{
11507 tabpage_T *tp;
11508 dictitem_T *v;
11509 char_u *varname;
11510
11511 rettv->v_type = VAR_STRING;
11512 rettv->vval.v_string = NULL;
11513
11514 varname = get_tv_string_chk(&argvars[1]);
11515 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11516 if (tp != NULL && varname != NULL)
11517 {
11518 /* look up the variable */
11519 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11520 if (v != NULL)
11521 copy_tv(&v->di_tv, rettv);
11522 }
11523}
11524
11525/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011526 * "gettabwinvar()" function
11527 */
11528 static void
11529f_gettabwinvar(argvars, rettv)
11530 typval_T *argvars;
11531 typval_T *rettv;
11532{
11533 getwinvar(argvars, rettv, 1);
11534}
11535
11536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537 * "getwinposx()" function
11538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011540f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011541 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011544 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545#ifdef FEAT_GUI
11546 if (gui.in_use)
11547 {
11548 int x, y;
11549
11550 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011551 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552 }
11553#endif
11554}
11555
11556/*
11557 * "getwinposy()" function
11558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011561 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011564 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565#ifdef FEAT_GUI
11566 if (gui.in_use)
11567 {
11568 int x, y;
11569
11570 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011571 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572 }
11573#endif
11574}
11575
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011576/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011577 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011578 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011579 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011580find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011581 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011582 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011583{
11584#ifdef FEAT_WINDOWS
11585 win_T *wp;
11586#endif
11587 int nr;
11588
11589 nr = get_tv_number_chk(vp, NULL);
11590
11591#ifdef FEAT_WINDOWS
11592 if (nr < 0)
11593 return NULL;
11594 if (nr == 0)
11595 return curwin;
11596
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011597 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11598 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011599 if (--nr <= 0)
11600 break;
11601 return wp;
11602#else
11603 if (nr == 0 || nr == 1)
11604 return curwin;
11605 return NULL;
11606#endif
11607}
11608
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609/*
11610 * "getwinvar()" function
11611 */
11612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011613f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011614 typval_T *argvars;
11615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011617 getwinvar(argvars, rettv, 0);
11618}
11619
11620/*
11621 * getwinvar() and gettabwinvar()
11622 */
11623 static void
11624getwinvar(argvars, rettv, off)
11625 typval_T *argvars;
11626 typval_T *rettv;
11627 int off; /* 1 for gettabwinvar() */
11628{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011629 win_T *win, *oldcurwin;
11630 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011631 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011632 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011634#ifdef FEAT_WINDOWS
11635 if (off == 1)
11636 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11637 else
11638 tp = curtab;
11639#endif
11640 win = find_win_by_nr(&argvars[off], tp);
11641 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011642 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011644 rettv->v_type = VAR_STRING;
11645 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646
11647 if (win != NULL && varname != NULL)
11648 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011649 /* Set curwin to be our win, temporarily. Also set curbuf, so
11650 * that we can get buffer-local options. */
11651 oldcurwin = curwin;
11652 curwin = win;
11653 curbuf = win->w_buffer;
11654
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011656 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657 else
11658 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011659 if (*varname == NUL)
11660 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11661 * scope prefix before the NUL byte is required by
11662 * find_var_in_ht(). */
11663 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011665 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011667 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011669
11670 /* restore previous notion of curwin */
11671 curwin = oldcurwin;
11672 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673 }
11674
11675 --emsg_off;
11676}
11677
11678/*
11679 * "glob()" function
11680 */
11681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011682f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011683 typval_T *argvars;
11684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011686 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011688 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011690 /* When the optional second argument is non-zero, don't remove matches
11691 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11692 if (argvars[1].v_type != VAR_UNKNOWN
11693 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011694 options |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011695 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011696 if (!error)
11697 {
11698 ExpandInit(&xpc);
11699 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011700 if (p_wic)
11701 options += WILD_ICASE;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011702 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011703 NULL, options, WILD_ALL);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011704 }
11705 else
11706 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707}
11708
11709/*
11710 * "globpath()" function
11711 */
11712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011713f_globpath(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 Moolenaarbb5ddda2008-11-28 10:01:10 +000011717 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011719 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011720 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011722 /* When the optional second argument is non-zero, don't remove matches
11723 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11724 if (argvars[2].v_type != VAR_UNKNOWN
11725 && get_tv_number_chk(&argvars[2], &error))
11726 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011727 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011728 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011729 rettv->vval.v_string = NULL;
11730 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011731 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11732 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733}
11734
11735/*
11736 * "has()" function
11737 */
11738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011739f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011740 typval_T *argvars;
11741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011742{
11743 int i;
11744 char_u *name;
11745 int n = FALSE;
11746 static char *(has_list[]) =
11747 {
11748#ifdef AMIGA
11749 "amiga",
11750# ifdef FEAT_ARP
11751 "arp",
11752# endif
11753#endif
11754#ifdef __BEOS__
11755 "beos",
11756#endif
11757#ifdef MSDOS
11758# ifdef DJGPP
11759 "dos32",
11760# else
11761 "dos16",
11762# endif
11763#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011764#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765 "mac",
11766#endif
11767#if defined(MACOS_X_UNIX)
11768 "macunix",
11769#endif
11770#ifdef OS2
11771 "os2",
11772#endif
11773#ifdef __QNX__
11774 "qnx",
11775#endif
11776#ifdef RISCOS
11777 "riscos",
11778#endif
11779#ifdef UNIX
11780 "unix",
11781#endif
11782#ifdef VMS
11783 "vms",
11784#endif
11785#ifdef WIN16
11786 "win16",
11787#endif
11788#ifdef WIN32
11789 "win32",
11790#endif
11791#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11792 "win32unix",
11793#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011794#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795 "win64",
11796#endif
11797#ifdef EBCDIC
11798 "ebcdic",
11799#endif
11800#ifndef CASE_INSENSITIVE_FILENAME
11801 "fname_case",
11802#endif
11803#ifdef FEAT_ARABIC
11804 "arabic",
11805#endif
11806#ifdef FEAT_AUTOCMD
11807 "autocmd",
11808#endif
11809#ifdef FEAT_BEVAL
11810 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011811# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11812 "balloon_multiline",
11813# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814#endif
11815#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11816 "builtin_terms",
11817# ifdef ALL_BUILTIN_TCAPS
11818 "all_builtin_terms",
11819# endif
11820#endif
11821#ifdef FEAT_BYTEOFF
11822 "byte_offset",
11823#endif
11824#ifdef FEAT_CINDENT
11825 "cindent",
11826#endif
11827#ifdef FEAT_CLIENTSERVER
11828 "clientserver",
11829#endif
11830#ifdef FEAT_CLIPBOARD
11831 "clipboard",
11832#endif
11833#ifdef FEAT_CMDL_COMPL
11834 "cmdline_compl",
11835#endif
11836#ifdef FEAT_CMDHIST
11837 "cmdline_hist",
11838#endif
11839#ifdef FEAT_COMMENTS
11840 "comments",
11841#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011842#ifdef FEAT_CONCEAL
11843 "conceal",
11844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845#ifdef FEAT_CRYPT
11846 "cryptv",
11847#endif
11848#ifdef FEAT_CSCOPE
11849 "cscope",
11850#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011851#ifdef FEAT_CURSORBIND
11852 "cursorbind",
11853#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011854#ifdef CURSOR_SHAPE
11855 "cursorshape",
11856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857#ifdef DEBUG
11858 "debug",
11859#endif
11860#ifdef FEAT_CON_DIALOG
11861 "dialog_con",
11862#endif
11863#ifdef FEAT_GUI_DIALOG
11864 "dialog_gui",
11865#endif
11866#ifdef FEAT_DIFF
11867 "diff",
11868#endif
11869#ifdef FEAT_DIGRAPHS
11870 "digraphs",
11871#endif
11872#ifdef FEAT_DND
11873 "dnd",
11874#endif
11875#ifdef FEAT_EMACS_TAGS
11876 "emacs_tags",
11877#endif
11878 "eval", /* always present, of course! */
11879#ifdef FEAT_EX_EXTRA
11880 "ex_extra",
11881#endif
11882#ifdef FEAT_SEARCH_EXTRA
11883 "extra_search",
11884#endif
11885#ifdef FEAT_FKMAP
11886 "farsi",
11887#endif
11888#ifdef FEAT_SEARCHPATH
11889 "file_in_path",
11890#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011891#if defined(UNIX) && !defined(USE_SYSTEM)
11892 "filterpipe",
11893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894#ifdef FEAT_FIND_ID
11895 "find_in_path",
11896#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011897#ifdef FEAT_FLOAT
11898 "float",
11899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900#ifdef FEAT_FOLDING
11901 "folding",
11902#endif
11903#ifdef FEAT_FOOTER
11904 "footer",
11905#endif
11906#if !defined(USE_SYSTEM) && defined(UNIX)
11907 "fork",
11908#endif
11909#ifdef FEAT_GETTEXT
11910 "gettext",
11911#endif
11912#ifdef FEAT_GUI
11913 "gui",
11914#endif
11915#ifdef FEAT_GUI_ATHENA
11916# ifdef FEAT_GUI_NEXTAW
11917 "gui_neXtaw",
11918# else
11919 "gui_athena",
11920# endif
11921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922#ifdef FEAT_GUI_GTK
11923 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011926#ifdef FEAT_GUI_GNOME
11927 "gui_gnome",
11928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929#ifdef FEAT_GUI_MAC
11930 "gui_mac",
11931#endif
11932#ifdef FEAT_GUI_MOTIF
11933 "gui_motif",
11934#endif
11935#ifdef FEAT_GUI_PHOTON
11936 "gui_photon",
11937#endif
11938#ifdef FEAT_GUI_W16
11939 "gui_win16",
11940#endif
11941#ifdef FEAT_GUI_W32
11942 "gui_win32",
11943#endif
11944#ifdef FEAT_HANGULIN
11945 "hangul_input",
11946#endif
11947#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11948 "iconv",
11949#endif
11950#ifdef FEAT_INS_EXPAND
11951 "insert_expand",
11952#endif
11953#ifdef FEAT_JUMPLIST
11954 "jumplist",
11955#endif
11956#ifdef FEAT_KEYMAP
11957 "keymap",
11958#endif
11959#ifdef FEAT_LANGMAP
11960 "langmap",
11961#endif
11962#ifdef FEAT_LIBCALL
11963 "libcall",
11964#endif
11965#ifdef FEAT_LINEBREAK
11966 "linebreak",
11967#endif
11968#ifdef FEAT_LISP
11969 "lispindent",
11970#endif
11971#ifdef FEAT_LISTCMDS
11972 "listcmds",
11973#endif
11974#ifdef FEAT_LOCALMAP
11975 "localmap",
11976#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020011977#ifdef FEAT_LUA
11978# ifndef DYNAMIC_LUA
11979 "lua",
11980# endif
11981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982#ifdef FEAT_MENU
11983 "menu",
11984#endif
11985#ifdef FEAT_SESSION
11986 "mksession",
11987#endif
11988#ifdef FEAT_MODIFY_FNAME
11989 "modify_fname",
11990#endif
11991#ifdef FEAT_MOUSE
11992 "mouse",
11993#endif
11994#ifdef FEAT_MOUSESHAPE
11995 "mouseshape",
11996#endif
11997#if defined(UNIX) || defined(VMS)
11998# ifdef FEAT_MOUSE_DEC
11999 "mouse_dec",
12000# endif
12001# ifdef FEAT_MOUSE_GPM
12002 "mouse_gpm",
12003# endif
12004# ifdef FEAT_MOUSE_JSB
12005 "mouse_jsbterm",
12006# endif
12007# ifdef FEAT_MOUSE_NET
12008 "mouse_netterm",
12009# endif
12010# ifdef FEAT_MOUSE_PTERM
12011 "mouse_pterm",
12012# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012013# ifdef FEAT_SYSMOUSE
12014 "mouse_sysmouse",
12015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016# ifdef FEAT_MOUSE_XTERM
12017 "mouse_xterm",
12018# endif
12019#endif
12020#ifdef FEAT_MBYTE
12021 "multi_byte",
12022#endif
12023#ifdef FEAT_MBYTE_IME
12024 "multi_byte_ime",
12025#endif
12026#ifdef FEAT_MULTI_LANG
12027 "multi_lang",
12028#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012029#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012030#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012031 "mzscheme",
12032#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034#ifdef FEAT_OLE
12035 "ole",
12036#endif
12037#ifdef FEAT_OSFILETYPE
12038 "osfiletype",
12039#endif
12040#ifdef FEAT_PATH_EXTRA
12041 "path_extra",
12042#endif
12043#ifdef FEAT_PERL
12044#ifndef DYNAMIC_PERL
12045 "perl",
12046#endif
12047#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012048#ifdef FEAT_PERSISTENT_UNDO
12049 "persistent_undo",
12050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051#ifdef FEAT_PYTHON
12052#ifndef DYNAMIC_PYTHON
12053 "python",
12054#endif
12055#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012056#ifdef FEAT_PYTHON3
12057#ifndef DYNAMIC_PYTHON3
12058 "python3",
12059#endif
12060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061#ifdef FEAT_POSTSCRIPT
12062 "postscript",
12063#endif
12064#ifdef FEAT_PRINTER
12065 "printer",
12066#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012067#ifdef FEAT_PROFILE
12068 "profile",
12069#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012070#ifdef FEAT_RELTIME
12071 "reltime",
12072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073#ifdef FEAT_QUICKFIX
12074 "quickfix",
12075#endif
12076#ifdef FEAT_RIGHTLEFT
12077 "rightleft",
12078#endif
12079#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12080 "ruby",
12081#endif
12082#ifdef FEAT_SCROLLBIND
12083 "scrollbind",
12084#endif
12085#ifdef FEAT_CMDL_INFO
12086 "showcmd",
12087 "cmdline_info",
12088#endif
12089#ifdef FEAT_SIGNS
12090 "signs",
12091#endif
12092#ifdef FEAT_SMARTINDENT
12093 "smartindent",
12094#endif
12095#ifdef FEAT_SNIFF
12096 "sniff",
12097#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012098#ifdef STARTUPTIME
12099 "startuptime",
12100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101#ifdef FEAT_STL_OPT
12102 "statusline",
12103#endif
12104#ifdef FEAT_SUN_WORKSHOP
12105 "sun_workshop",
12106#endif
12107#ifdef FEAT_NETBEANS_INTG
12108 "netbeans_intg",
12109#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012110#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012111 "spell",
12112#endif
12113#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114 "syntax",
12115#endif
12116#if defined(USE_SYSTEM) || !defined(UNIX)
12117 "system",
12118#endif
12119#ifdef FEAT_TAG_BINS
12120 "tag_binary",
12121#endif
12122#ifdef FEAT_TAG_OLDSTATIC
12123 "tag_old_static",
12124#endif
12125#ifdef FEAT_TAG_ANYWHITE
12126 "tag_any_white",
12127#endif
12128#ifdef FEAT_TCL
12129# ifndef DYNAMIC_TCL
12130 "tcl",
12131# endif
12132#endif
12133#ifdef TERMINFO
12134 "terminfo",
12135#endif
12136#ifdef FEAT_TERMRESPONSE
12137 "termresponse",
12138#endif
12139#ifdef FEAT_TEXTOBJ
12140 "textobjects",
12141#endif
12142#ifdef HAVE_TGETENT
12143 "tgetent",
12144#endif
12145#ifdef FEAT_TITLE
12146 "title",
12147#endif
12148#ifdef FEAT_TOOLBAR
12149 "toolbar",
12150#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012151#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12152 "unnamedplus",
12153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154#ifdef FEAT_USR_CMDS
12155 "user-commands", /* was accidentally included in 5.4 */
12156 "user_commands",
12157#endif
12158#ifdef FEAT_VIMINFO
12159 "viminfo",
12160#endif
12161#ifdef FEAT_VERTSPLIT
12162 "vertsplit",
12163#endif
12164#ifdef FEAT_VIRTUALEDIT
12165 "virtualedit",
12166#endif
12167#ifdef FEAT_VISUAL
12168 "visual",
12169#endif
12170#ifdef FEAT_VISUALEXTRA
12171 "visualextra",
12172#endif
12173#ifdef FEAT_VREPLACE
12174 "vreplace",
12175#endif
12176#ifdef FEAT_WILDIGN
12177 "wildignore",
12178#endif
12179#ifdef FEAT_WILDMENU
12180 "wildmenu",
12181#endif
12182#ifdef FEAT_WINDOWS
12183 "windows",
12184#endif
12185#ifdef FEAT_WAK
12186 "winaltkeys",
12187#endif
12188#ifdef FEAT_WRITEBACKUP
12189 "writebackup",
12190#endif
12191#ifdef FEAT_XIM
12192 "xim",
12193#endif
12194#ifdef FEAT_XFONTSET
12195 "xfontset",
12196#endif
12197#ifdef USE_XSMP
12198 "xsmp",
12199#endif
12200#ifdef USE_XSMP_INTERACT
12201 "xsmp_interact",
12202#endif
12203#ifdef FEAT_XCLIPBOARD
12204 "xterm_clipboard",
12205#endif
12206#ifdef FEAT_XTERM_SAVE
12207 "xterm_save",
12208#endif
12209#if defined(UNIX) && defined(FEAT_X11)
12210 "X11",
12211#endif
12212 NULL
12213 };
12214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012215 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216 for (i = 0; has_list[i] != NULL; ++i)
12217 if (STRICMP(name, has_list[i]) == 0)
12218 {
12219 n = TRUE;
12220 break;
12221 }
12222
12223 if (n == FALSE)
12224 {
12225 if (STRNICMP(name, "patch", 5) == 0)
12226 n = has_patch(atoi((char *)name + 5));
12227 else if (STRICMP(name, "vim_starting") == 0)
12228 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012229#ifdef FEAT_MBYTE
12230 else if (STRICMP(name, "multi_byte_encoding") == 0)
12231 n = has_mbyte;
12232#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012233#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12234 else if (STRICMP(name, "balloon_multiline") == 0)
12235 n = multiline_balloon_available();
12236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012237#ifdef DYNAMIC_TCL
12238 else if (STRICMP(name, "tcl") == 0)
12239 n = tcl_enabled(FALSE);
12240#endif
12241#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12242 else if (STRICMP(name, "iconv") == 0)
12243 n = iconv_enabled(FALSE);
12244#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012245#ifdef DYNAMIC_LUA
12246 else if (STRICMP(name, "lua") == 0)
12247 n = lua_enabled(FALSE);
12248#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012249#ifdef DYNAMIC_MZSCHEME
12250 else if (STRICMP(name, "mzscheme") == 0)
12251 n = mzscheme_enabled(FALSE);
12252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253#ifdef DYNAMIC_RUBY
12254 else if (STRICMP(name, "ruby") == 0)
12255 n = ruby_enabled(FALSE);
12256#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012257#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258#ifdef DYNAMIC_PYTHON
12259 else if (STRICMP(name, "python") == 0)
12260 n = python_enabled(FALSE);
12261#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012262#endif
12263#ifdef FEAT_PYTHON3
12264#ifdef DYNAMIC_PYTHON3
12265 else if (STRICMP(name, "python3") == 0)
12266 n = python3_enabled(FALSE);
12267#endif
12268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269#ifdef DYNAMIC_PERL
12270 else if (STRICMP(name, "perl") == 0)
12271 n = perl_enabled(FALSE);
12272#endif
12273#ifdef FEAT_GUI
12274 else if (STRICMP(name, "gui_running") == 0)
12275 n = (gui.in_use || gui.starting);
12276# ifdef FEAT_GUI_W32
12277 else if (STRICMP(name, "gui_win32s") == 0)
12278 n = gui_is_win32s();
12279# endif
12280# ifdef FEAT_BROWSE
12281 else if (STRICMP(name, "browse") == 0)
12282 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12283# endif
12284#endif
12285#ifdef FEAT_SYN_HL
12286 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012287 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288#endif
12289#if defined(WIN3264)
12290 else if (STRICMP(name, "win95") == 0)
12291 n = mch_windows95();
12292#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012293#ifdef FEAT_NETBEANS_INTG
12294 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012295 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297 }
12298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012299 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300}
12301
12302/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012303 * "has_key()" function
12304 */
12305 static void
12306f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012307 typval_T *argvars;
12308 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012309{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012310 if (argvars[0].v_type != VAR_DICT)
12311 {
12312 EMSG(_(e_dictreq));
12313 return;
12314 }
12315 if (argvars[0].vval.v_dict == NULL)
12316 return;
12317
12318 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012319 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012320}
12321
12322/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012323 * "haslocaldir()" function
12324 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012325 static void
12326f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012327 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012328 typval_T *rettv;
12329{
12330 rettv->vval.v_number = (curwin->w_localdir != NULL);
12331}
12332
12333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334 * "hasmapto()" function
12335 */
12336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012337f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012338 typval_T *argvars;
12339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340{
12341 char_u *name;
12342 char_u *mode;
12343 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012344 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012346 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012347 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348 mode = (char_u *)"nvo";
12349 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012350 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012351 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012352 if (argvars[2].v_type != VAR_UNKNOWN)
12353 abbr = get_tv_number(&argvars[2]);
12354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355
Bram Moolenaar2c932302006-03-18 21:42:09 +000012356 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012357 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012359 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360}
12361
12362/*
12363 * "histadd()" function
12364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012366f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012367 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369{
12370#ifdef FEAT_CMDHIST
12371 int histype;
12372 char_u *str;
12373 char_u buf[NUMBUFLEN];
12374#endif
12375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012376 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377 if (check_restricted() || check_secure())
12378 return;
12379#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012380 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12381 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382 if (histype >= 0)
12383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012384 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385 if (*str != NUL)
12386 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012387 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012389 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390 return;
12391 }
12392 }
12393#endif
12394}
12395
12396/*
12397 * "histdel()" function
12398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012400f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012401 typval_T *argvars UNUSED;
12402 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403{
12404#ifdef FEAT_CMDHIST
12405 int n;
12406 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012407 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012409 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12410 if (str == NULL)
12411 n = 0;
12412 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012414 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012415 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012417 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012418 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419 else
12420 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012421 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012422 get_tv_string_buf(&argvars[1], buf));
12423 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424#endif
12425}
12426
12427/*
12428 * "histget()" function
12429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012431f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012432 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434{
12435#ifdef FEAT_CMDHIST
12436 int type;
12437 int idx;
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 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012443 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012444 {
12445 type = get_histtype(str);
12446 if (argvars[1].v_type == VAR_UNKNOWN)
12447 idx = get_history_idx(type);
12448 else
12449 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12450 /* -1 on type error */
12451 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012454 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012456 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457}
12458
12459/*
12460 * "histnr()" function
12461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012463f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012464 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012466{
12467 int i;
12468
12469#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012470 char_u *history = get_tv_string_chk(&argvars[0]);
12471
12472 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473 if (i >= HIST_CMD && i < HIST_COUNT)
12474 i = get_history_idx(i);
12475 else
12476#endif
12477 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012478 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479}
12480
12481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482 * "highlightID(name)" function
12483 */
12484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012485f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012486 typval_T *argvars;
12487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012489 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490}
12491
12492/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012493 * "highlight_exists()" function
12494 */
12495 static void
12496f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012497 typval_T *argvars;
12498 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012499{
12500 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12501}
12502
12503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504 * "hostname()" function
12505 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012507f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012508 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510{
12511 char_u hostname[256];
12512
12513 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012514 rettv->v_type = VAR_STRING;
12515 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012516}
12517
12518/*
12519 * iconv() function
12520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012522f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012523 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012525{
12526#ifdef FEAT_MBYTE
12527 char_u buf1[NUMBUFLEN];
12528 char_u buf2[NUMBUFLEN];
12529 char_u *from, *to, *str;
12530 vimconv_T vimconv;
12531#endif
12532
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012533 rettv->v_type = VAR_STRING;
12534 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535
12536#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012537 str = get_tv_string(&argvars[0]);
12538 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12539 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540 vimconv.vc_type = CONV_NONE;
12541 convert_setup(&vimconv, from, to);
12542
12543 /* If the encodings are equal, no conversion needed. */
12544 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012545 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012546 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012547 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548
12549 convert_setup(&vimconv, NULL, NULL);
12550 vim_free(from);
12551 vim_free(to);
12552#endif
12553}
12554
12555/*
12556 * "indent()" function
12557 */
12558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012559f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012560 typval_T *argvars;
12561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562{
12563 linenr_T lnum;
12564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012565 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012566 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012567 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012568 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012569 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012570}
12571
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012572/*
12573 * "index()" function
12574 */
12575 static void
12576f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012577 typval_T *argvars;
12578 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012579{
Bram Moolenaar33570922005-01-25 22:26:29 +000012580 list_T *l;
12581 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012582 long idx = 0;
12583 int ic = FALSE;
12584
12585 rettv->vval.v_number = -1;
12586 if (argvars[0].v_type != VAR_LIST)
12587 {
12588 EMSG(_(e_listreq));
12589 return;
12590 }
12591 l = argvars[0].vval.v_list;
12592 if (l != NULL)
12593 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012594 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012595 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012596 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012597 int error = FALSE;
12598
Bram Moolenaar758711c2005-02-02 23:11:38 +000012599 /* Start at specified item. Use the cached index that list_find()
12600 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012601 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012602 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012603 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012604 ic = get_tv_number_chk(&argvars[3], &error);
12605 if (error)
12606 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012607 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012608
Bram Moolenaar758711c2005-02-02 23:11:38 +000012609 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012610 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012611 {
12612 rettv->vval.v_number = idx;
12613 break;
12614 }
12615 }
12616}
12617
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618static int inputsecret_flag = 0;
12619
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012620static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12621
Bram Moolenaar071d4272004-06-13 20:20:40 +000012622/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012623 * This function is used by f_input() and f_inputdialog() functions. The third
12624 * argument to f_input() specifies the type of completion to use at the
12625 * prompt. The third argument to f_inputdialog() specifies the value to return
12626 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012627 */
12628 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012629get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012630 typval_T *argvars;
12631 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012632 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012634 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635 char_u *p = NULL;
12636 int c;
12637 char_u buf[NUMBUFLEN];
12638 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012639 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012640 int xp_type = EXPAND_NOTHING;
12641 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012643 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012644 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645
12646#ifdef NO_CONSOLE_INPUT
12647 /* While starting up, there is no place to enter text. */
12648 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650#endif
12651
12652 cmd_silent = FALSE; /* Want to see the prompt. */
12653 if (prompt != NULL)
12654 {
12655 /* Only the part of the message after the last NL is considered as
12656 * prompt for the command line */
12657 p = vim_strrchr(prompt, '\n');
12658 if (p == NULL)
12659 p = prompt;
12660 else
12661 {
12662 ++p;
12663 c = *p;
12664 *p = NUL;
12665 msg_start();
12666 msg_clr_eos();
12667 msg_puts_attr(prompt, echo_attr);
12668 msg_didout = FALSE;
12669 msg_starthere();
12670 *p = c;
12671 }
12672 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012673
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012674 if (argvars[1].v_type != VAR_UNKNOWN)
12675 {
12676 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12677 if (defstr != NULL)
12678 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012680 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012681 {
12682 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012683 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012684 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012685
Bram Moolenaar4463f292005-09-25 22:20:24 +000012686 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012687
Bram Moolenaar4463f292005-09-25 22:20:24 +000012688 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12689 if (xp_name == NULL)
12690 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012691
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012692 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012693
Bram Moolenaar4463f292005-09-25 22:20:24 +000012694 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12695 &xp_arg) == FAIL)
12696 return;
12697 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012698 }
12699
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012700 if (defstr != NULL)
12701 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012702 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12703 xp_type, xp_arg);
12704
12705 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012707 /* since the user typed this, no need to wait for return */
12708 need_wait_return = FALSE;
12709 msg_didout = FALSE;
12710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711 cmd_silent = cmd_silent_save;
12712}
12713
12714/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012715 * "input()" function
12716 * Also handles inputsecret() when inputsecret is set.
12717 */
12718 static void
12719f_input(argvars, rettv)
12720 typval_T *argvars;
12721 typval_T *rettv;
12722{
12723 get_user_input(argvars, rettv, FALSE);
12724}
12725
12726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727 * "inputdialog()" function
12728 */
12729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012730f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012731 typval_T *argvars;
12732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733{
12734#if defined(FEAT_GUI_TEXTDIALOG)
12735 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12736 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12737 {
12738 char_u *message;
12739 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012740 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012742 message = get_tv_string_chk(&argvars[0]);
12743 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012744 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012745 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746 else
12747 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012748 if (message != NULL && defstr != NULL
12749 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010012750 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012751 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752 else
12753 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012754 if (message != NULL && defstr != NULL
12755 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012756 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012757 rettv->vval.v_string = vim_strsave(
12758 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012760 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763 }
12764 else
12765#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012766 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012767}
12768
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012769/*
12770 * "inputlist()" function
12771 */
12772 static void
12773f_inputlist(argvars, rettv)
12774 typval_T *argvars;
12775 typval_T *rettv;
12776{
12777 listitem_T *li;
12778 int selected;
12779 int mouse_used;
12780
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012781#ifdef NO_CONSOLE_INPUT
12782 /* While starting up, there is no place to enter text. */
12783 if (no_console_input())
12784 return;
12785#endif
12786 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12787 {
12788 EMSG2(_(e_listarg), "inputlist()");
12789 return;
12790 }
12791
12792 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012793 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012794 lines_left = Rows; /* avoid more prompt */
12795 msg_scroll = TRUE;
12796 msg_clr_eos();
12797
12798 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12799 {
12800 msg_puts(get_tv_string(&li->li_tv));
12801 msg_putchar('\n');
12802 }
12803
12804 /* Ask for choice. */
12805 selected = prompt_for_number(&mouse_used);
12806 if (mouse_used)
12807 selected -= lines_left;
12808
12809 rettv->vval.v_number = selected;
12810}
12811
12812
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12814
12815/*
12816 * "inputrestore()" function
12817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012819f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012820 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822{
12823 if (ga_userinput.ga_len > 0)
12824 {
12825 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12827 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012828 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829 }
12830 else if (p_verbose > 1)
12831 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012832 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012833 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834 }
12835}
12836
12837/*
12838 * "inputsave()" function
12839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012841f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012842 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012845 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012846 if (ga_grow(&ga_userinput, 1) == OK)
12847 {
12848 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12849 + ga_userinput.ga_len);
12850 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012851 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 }
12853 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012854 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855}
12856
12857/*
12858 * "inputsecret()" function
12859 */
12860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012861f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012862 typval_T *argvars;
12863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864{
12865 ++cmdline_star;
12866 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012867 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868 --cmdline_star;
12869 --inputsecret_flag;
12870}
12871
12872/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012873 * "insert()" function
12874 */
12875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012876f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012877 typval_T *argvars;
12878 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012879{
12880 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012881 listitem_T *item;
12882 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012883 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012884
12885 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012886 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012887 else if ((l = argvars[0].vval.v_list) != NULL
12888 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012889 {
12890 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012891 before = get_tv_number_chk(&argvars[2], &error);
12892 if (error)
12893 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012894
Bram Moolenaar758711c2005-02-02 23:11:38 +000012895 if (before == l->lv_len)
12896 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012897 else
12898 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012899 item = list_find(l, before);
12900 if (item == NULL)
12901 {
12902 EMSGN(_(e_listidx), before);
12903 l = NULL;
12904 }
12905 }
12906 if (l != NULL)
12907 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012908 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012909 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012910 }
12911 }
12912}
12913
12914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915 * "isdirectory()" function
12916 */
12917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012918f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012919 typval_T *argvars;
12920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012922 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923}
12924
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012925/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012926 * "islocked()" function
12927 */
12928 static void
12929f_islocked(argvars, rettv)
12930 typval_T *argvars;
12931 typval_T *rettv;
12932{
12933 lval_T lv;
12934 char_u *end;
12935 dictitem_T *di;
12936
12937 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012938 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12939 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012940 if (end != NULL && lv.ll_name != NULL)
12941 {
12942 if (*end != NUL)
12943 EMSG(_(e_trailing));
12944 else
12945 {
12946 if (lv.ll_tv == NULL)
12947 {
12948 if (check_changedtick(lv.ll_name))
12949 rettv->vval.v_number = 1; /* always locked */
12950 else
12951 {
12952 di = find_var(lv.ll_name, NULL);
12953 if (di != NULL)
12954 {
12955 /* Consider a variable locked when:
12956 * 1. the variable itself is locked
12957 * 2. the value of the variable is locked.
12958 * 3. the List or Dict value is locked.
12959 */
12960 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12961 || tv_islocked(&di->di_tv));
12962 }
12963 }
12964 }
12965 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012966 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012967 else if (lv.ll_newkey != NULL)
12968 EMSG2(_(e_dictkey), lv.ll_newkey);
12969 else if (lv.ll_list != NULL)
12970 /* List item. */
12971 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12972 else
12973 /* Dictionary item. */
12974 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12975 }
12976 }
12977
12978 clear_lval(&lv);
12979}
12980
Bram Moolenaar33570922005-01-25 22:26:29 +000012981static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012982
12983/*
12984 * Turn a dict into a list:
12985 * "what" == 0: list of keys
12986 * "what" == 1: list of values
12987 * "what" == 2: list of items
12988 */
12989 static void
12990dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012991 typval_T *argvars;
12992 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012993 int what;
12994{
Bram Moolenaar33570922005-01-25 22:26:29 +000012995 list_T *l2;
12996 dictitem_T *di;
12997 hashitem_T *hi;
12998 listitem_T *li;
12999 listitem_T *li2;
13000 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013001 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013002
Bram Moolenaar8c711452005-01-14 21:53:12 +000013003 if (argvars[0].v_type != VAR_DICT)
13004 {
13005 EMSG(_(e_dictreq));
13006 return;
13007 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013008 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013009 return;
13010
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013011 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013012 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013013
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013014 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013015 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013016 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013017 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013018 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013019 --todo;
13020 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013021
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013022 li = listitem_alloc();
13023 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013024 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013025 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013026
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013027 if (what == 0)
13028 {
13029 /* keys() */
13030 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013031 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013032 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13033 }
13034 else if (what == 1)
13035 {
13036 /* values() */
13037 copy_tv(&di->di_tv, &li->li_tv);
13038 }
13039 else
13040 {
13041 /* items() */
13042 l2 = list_alloc();
13043 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013044 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013045 li->li_tv.vval.v_list = l2;
13046 if (l2 == NULL)
13047 break;
13048 ++l2->lv_refcount;
13049
13050 li2 = listitem_alloc();
13051 if (li2 == NULL)
13052 break;
13053 list_append(l2, li2);
13054 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013055 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013056 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13057
13058 li2 = listitem_alloc();
13059 if (li2 == NULL)
13060 break;
13061 list_append(l2, li2);
13062 copy_tv(&di->di_tv, &li2->li_tv);
13063 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013064 }
13065 }
13066}
13067
13068/*
13069 * "items(dict)" function
13070 */
13071 static void
13072f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013073 typval_T *argvars;
13074 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013075{
13076 dict_list(argvars, rettv, 2);
13077}
13078
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013080 * "join()" function
13081 */
13082 static void
13083f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013084 typval_T *argvars;
13085 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013086{
13087 garray_T ga;
13088 char_u *sep;
13089
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013090 if (argvars[0].v_type != VAR_LIST)
13091 {
13092 EMSG(_(e_listreq));
13093 return;
13094 }
13095 if (argvars[0].vval.v_list == NULL)
13096 return;
13097 if (argvars[1].v_type == VAR_UNKNOWN)
13098 sep = (char_u *)" ";
13099 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013100 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013101
13102 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013103
13104 if (sep != NULL)
13105 {
13106 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013107 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013108 ga_append(&ga, NUL);
13109 rettv->vval.v_string = (char_u *)ga.ga_data;
13110 }
13111 else
13112 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013113}
13114
13115/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013116 * "keys()" function
13117 */
13118 static void
13119f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013120 typval_T *argvars;
13121 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013122{
13123 dict_list(argvars, rettv, 0);
13124}
13125
13126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127 * "last_buffer_nr()" function.
13128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013130f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013131 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133{
13134 int n = 0;
13135 buf_T *buf;
13136
13137 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13138 if (n < buf->b_fnum)
13139 n = buf->b_fnum;
13140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013141 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013142}
13143
13144/*
13145 * "len()" function
13146 */
13147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013148f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013149 typval_T *argvars;
13150 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013151{
13152 switch (argvars[0].v_type)
13153 {
13154 case VAR_STRING:
13155 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013156 rettv->vval.v_number = (varnumber_T)STRLEN(
13157 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013158 break;
13159 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013160 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013161 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013162 case VAR_DICT:
13163 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13164 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013165 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013166 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013167 break;
13168 }
13169}
13170
Bram Moolenaar33570922005-01-25 22:26:29 +000013171static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013172
13173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013174libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013175 typval_T *argvars;
13176 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013177 int type;
13178{
13179#ifdef FEAT_LIBCALL
13180 char_u *string_in;
13181 char_u **string_result;
13182 int nr_result;
13183#endif
13184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013185 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013186 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013187 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013188
13189 if (check_restricted() || check_secure())
13190 return;
13191
13192#ifdef FEAT_LIBCALL
13193 /* The first two args must be strings, otherwise its meaningless */
13194 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13195 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013196 string_in = NULL;
13197 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013198 string_in = argvars[2].vval.v_string;
13199 if (type == VAR_NUMBER)
13200 string_result = NULL;
13201 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013202 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013203 if (mch_libcall(argvars[0].vval.v_string,
13204 argvars[1].vval.v_string,
13205 string_in,
13206 argvars[2].vval.v_number,
13207 string_result,
13208 &nr_result) == OK
13209 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013210 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013211 }
13212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213}
13214
13215/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013216 * "libcall()" function
13217 */
13218 static void
13219f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013220 typval_T *argvars;
13221 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013222{
13223 libcall_common(argvars, rettv, VAR_STRING);
13224}
13225
13226/*
13227 * "libcallnr()" function
13228 */
13229 static void
13230f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013231 typval_T *argvars;
13232 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013233{
13234 libcall_common(argvars, rettv, VAR_NUMBER);
13235}
13236
13237/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238 * "line(string)" function
13239 */
13240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013241f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013242 typval_T *argvars;
13243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244{
13245 linenr_T lnum = 0;
13246 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013247 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013249 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250 if (fp != NULL)
13251 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013252 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253}
13254
13255/*
13256 * "line2byte(lnum)" function
13257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013259f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013260 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262{
13263#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013264 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265#else
13266 linenr_T lnum;
13267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013268 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013269 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013270 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013272 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13273 if (rettv->vval.v_number >= 0)
13274 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275#endif
13276}
13277
13278/*
13279 * "lispindent(lnum)" function
13280 */
13281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013282f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013283 typval_T *argvars;
13284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285{
13286#ifdef FEAT_LISP
13287 pos_T pos;
13288 linenr_T lnum;
13289
13290 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013291 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13293 {
13294 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013295 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296 curwin->w_cursor = pos;
13297 }
13298 else
13299#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013300 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301}
13302
13303/*
13304 * "localtime()" function
13305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013307f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013308 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013311 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312}
13313
Bram Moolenaar33570922005-01-25 22:26:29 +000013314static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315
13316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013317get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013318 typval_T *argvars;
13319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320 int exact;
13321{
13322 char_u *keys;
13323 char_u *which;
13324 char_u buf[NUMBUFLEN];
13325 char_u *keys_buf = NULL;
13326 char_u *rhs;
13327 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013328 int abbr = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013329 int get_dict = FALSE;
13330 mapblock_T *mp;
13331 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332
13333 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013334 rettv->v_type = VAR_STRING;
13335 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013337 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338 if (*keys == NUL)
13339 return;
13340
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013341 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013343 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013344 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013345 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013346 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013347 if (argvars[3].v_type != VAR_UNKNOWN)
13348 get_dict = get_tv_number(&argvars[3]);
13349 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351 else
13352 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013353 if (which == NULL)
13354 return;
13355
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356 mode = get_map_mode(&which, 0);
13357
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013358 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013359 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013361
13362 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013364 /* Return a string. */
13365 if (rhs != NULL)
13366 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367
Bram Moolenaarbd743252010-10-20 21:23:33 +020013368 }
13369 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13370 {
13371 /* Return a dictionary. */
13372 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13373 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13374 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375
Bram Moolenaarbd743252010-10-20 21:23:33 +020013376 dict_add_nr_str(dict, "lhs", 0L, lhs);
13377 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13378 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13379 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13380 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13381 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13382 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13383 dict_add_nr_str(dict, "mode", 0L, mapmode);
13384
13385 vim_free(lhs);
13386 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387 }
13388}
13389
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013390#ifdef FEAT_FLOAT
13391/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013392 * "log()" function
13393 */
13394 static void
13395f_log(argvars, rettv)
13396 typval_T *argvars;
13397 typval_T *rettv;
13398{
13399 float_T f;
13400
13401 rettv->v_type = VAR_FLOAT;
13402 if (get_float_arg(argvars, &f) == OK)
13403 rettv->vval.v_float = log(f);
13404 else
13405 rettv->vval.v_float = 0.0;
13406}
13407
13408/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013409 * "log10()" function
13410 */
13411 static void
13412f_log10(argvars, rettv)
13413 typval_T *argvars;
13414 typval_T *rettv;
13415{
13416 float_T f;
13417
13418 rettv->v_type = VAR_FLOAT;
13419 if (get_float_arg(argvars, &f) == OK)
13420 rettv->vval.v_float = log10(f);
13421 else
13422 rettv->vval.v_float = 0.0;
13423}
13424#endif
13425
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013427 * "map()" function
13428 */
13429 static void
13430f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013431 typval_T *argvars;
13432 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013433{
13434 filter_map(argvars, rettv, TRUE);
13435}
13436
13437/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013438 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439 */
13440 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013441f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013442 typval_T *argvars;
13443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013445 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446}
13447
13448/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013449 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450 */
13451 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013452f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013453 typval_T *argvars;
13454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013456 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457}
13458
Bram Moolenaar33570922005-01-25 22:26:29 +000013459static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460
13461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013462find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013463 typval_T *argvars;
13464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465 int type;
13466{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013467 char_u *str = NULL;
13468 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469 char_u *pat;
13470 regmatch_T regmatch;
13471 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013472 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473 char_u *save_cpo;
13474 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013475 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013476 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013477 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013478 list_T *l = NULL;
13479 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013480 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013481 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482
13483 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13484 save_cpo = p_cpo;
13485 p_cpo = (char_u *)"";
13486
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013487 rettv->vval.v_number = -1;
13488 if (type == 3)
13489 {
13490 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013491 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013492 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013493 }
13494 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013496 rettv->v_type = VAR_STRING;
13497 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013500 if (argvars[0].v_type == VAR_LIST)
13501 {
13502 if ((l = argvars[0].vval.v_list) == NULL)
13503 goto theend;
13504 li = l->lv_first;
13505 }
13506 else
13507 expr = str = get_tv_string(&argvars[0]);
13508
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013509 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13510 if (pat == NULL)
13511 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013512
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013513 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013515 int error = FALSE;
13516
13517 start = get_tv_number_chk(&argvars[2], &error);
13518 if (error)
13519 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013520 if (l != NULL)
13521 {
13522 li = list_find(l, start);
13523 if (li == NULL)
13524 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013525 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013526 }
13527 else
13528 {
13529 if (start < 0)
13530 start = 0;
13531 if (start > (long)STRLEN(str))
13532 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013533 /* When "count" argument is there ignore matches before "start",
13534 * otherwise skip part of the string. Differs when pattern is "^"
13535 * or "\<". */
13536 if (argvars[3].v_type != VAR_UNKNOWN)
13537 startcol = start;
13538 else
13539 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013540 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013541
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013542 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013543 nth = get_tv_number_chk(&argvars[3], &error);
13544 if (error)
13545 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546 }
13547
13548 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13549 if (regmatch.regprog != NULL)
13550 {
13551 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013552
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013553 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013554 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013555 if (l != NULL)
13556 {
13557 if (li == NULL)
13558 {
13559 match = FALSE;
13560 break;
13561 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013562 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013563 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013564 if (str == NULL)
13565 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013566 }
13567
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013568 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013569
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013570 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013571 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013572 if (l == NULL && !match)
13573 break;
13574
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013575 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013576 if (l != NULL)
13577 {
13578 li = li->li_next;
13579 ++idx;
13580 }
13581 else
13582 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013583#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013584 startcol = (colnr_T)(regmatch.startp[0]
13585 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013586#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013587 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013588#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013589 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013590 }
13591
13592 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013594 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013595 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013596 int i;
13597
13598 /* return list with matched string and submatches */
13599 for (i = 0; i < NSUBEXP; ++i)
13600 {
13601 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013602 {
13603 if (list_append_string(rettv->vval.v_list,
13604 (char_u *)"", 0) == FAIL)
13605 break;
13606 }
13607 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013608 regmatch.startp[i],
13609 (int)(regmatch.endp[i] - regmatch.startp[i]))
13610 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013611 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013612 }
13613 }
13614 else if (type == 2)
13615 {
13616 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013617 if (l != NULL)
13618 copy_tv(&li->li_tv, rettv);
13619 else
13620 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013622 }
13623 else if (l != NULL)
13624 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625 else
13626 {
13627 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013628 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629 (varnumber_T)(regmatch.startp[0] - str);
13630 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013631 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013633 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634 }
13635 }
13636 vim_free(regmatch.regprog);
13637 }
13638
13639theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013640 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 p_cpo = save_cpo;
13642}
13643
13644/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013645 * "match()" function
13646 */
13647 static void
13648f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013649 typval_T *argvars;
13650 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013651{
13652 find_some_match(argvars, rettv, 1);
13653}
13654
13655/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013656 * "matchadd()" function
13657 */
13658 static void
13659f_matchadd(argvars, rettv)
13660 typval_T *argvars;
13661 typval_T *rettv;
13662{
13663#ifdef FEAT_SEARCH_EXTRA
13664 char_u buf[NUMBUFLEN];
13665 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13666 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13667 int prio = 10; /* default priority */
13668 int id = -1;
13669 int error = FALSE;
13670
13671 rettv->vval.v_number = -1;
13672
13673 if (grp == NULL || pat == NULL)
13674 return;
13675 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013676 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013677 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013678 if (argvars[3].v_type != VAR_UNKNOWN)
13679 id = get_tv_number_chk(&argvars[3], &error);
13680 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013681 if (error == TRUE)
13682 return;
13683 if (id >= 1 && id <= 3)
13684 {
13685 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13686 return;
13687 }
13688
13689 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13690#endif
13691}
13692
13693/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013694 * "matcharg()" function
13695 */
13696 static void
13697f_matcharg(argvars, rettv)
13698 typval_T *argvars;
13699 typval_T *rettv;
13700{
13701 if (rettv_list_alloc(rettv) == OK)
13702 {
13703#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013704 int id = get_tv_number(&argvars[0]);
13705 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013706
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013707 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013708 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013709 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13710 {
13711 list_append_string(rettv->vval.v_list,
13712 syn_id2name(m->hlg_id), -1);
13713 list_append_string(rettv->vval.v_list, m->pattern, -1);
13714 }
13715 else
13716 {
13717 list_append_string(rettv->vval.v_list, NUL, -1);
13718 list_append_string(rettv->vval.v_list, NUL, -1);
13719 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013720 }
13721#endif
13722 }
13723}
13724
13725/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013726 * "matchdelete()" function
13727 */
13728 static void
13729f_matchdelete(argvars, rettv)
13730 typval_T *argvars;
13731 typval_T *rettv;
13732{
13733#ifdef FEAT_SEARCH_EXTRA
13734 rettv->vval.v_number = match_delete(curwin,
13735 (int)get_tv_number(&argvars[0]), TRUE);
13736#endif
13737}
13738
13739/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013740 * "matchend()" function
13741 */
13742 static void
13743f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013744 typval_T *argvars;
13745 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013746{
13747 find_some_match(argvars, rettv, 0);
13748}
13749
13750/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013751 * "matchlist()" function
13752 */
13753 static void
13754f_matchlist(argvars, rettv)
13755 typval_T *argvars;
13756 typval_T *rettv;
13757{
13758 find_some_match(argvars, rettv, 3);
13759}
13760
13761/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013762 * "matchstr()" function
13763 */
13764 static void
13765f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013766 typval_T *argvars;
13767 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013768{
13769 find_some_match(argvars, rettv, 2);
13770}
13771
Bram Moolenaar33570922005-01-25 22:26:29 +000013772static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013773
13774 static void
13775max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013776 typval_T *argvars;
13777 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013778 int domax;
13779{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013780 long n = 0;
13781 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013782 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013783
13784 if (argvars[0].v_type == VAR_LIST)
13785 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013786 list_T *l;
13787 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013788
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013789 l = argvars[0].vval.v_list;
13790 if (l != NULL)
13791 {
13792 li = l->lv_first;
13793 if (li != NULL)
13794 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013795 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013796 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013797 {
13798 li = li->li_next;
13799 if (li == NULL)
13800 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013801 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013802 if (domax ? i > n : i < n)
13803 n = i;
13804 }
13805 }
13806 }
13807 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013808 else if (argvars[0].v_type == VAR_DICT)
13809 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013810 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013811 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013812 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013813 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013814
13815 d = argvars[0].vval.v_dict;
13816 if (d != NULL)
13817 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013818 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013819 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013820 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013821 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013822 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013823 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013824 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013825 if (first)
13826 {
13827 n = i;
13828 first = FALSE;
13829 }
13830 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013831 n = i;
13832 }
13833 }
13834 }
13835 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013836 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013837 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013838 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013839}
13840
13841/*
13842 * "max()" function
13843 */
13844 static void
13845f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013846 typval_T *argvars;
13847 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013848{
13849 max_min(argvars, rettv, TRUE);
13850}
13851
13852/*
13853 * "min()" function
13854 */
13855 static void
13856f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013857 typval_T *argvars;
13858 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013859{
13860 max_min(argvars, rettv, FALSE);
13861}
13862
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013863static int mkdir_recurse __ARGS((char_u *dir, int prot));
13864
13865/*
13866 * Create the directory in which "dir" is located, and higher levels when
13867 * needed.
13868 */
13869 static int
13870mkdir_recurse(dir, prot)
13871 char_u *dir;
13872 int prot;
13873{
13874 char_u *p;
13875 char_u *updir;
13876 int r = FAIL;
13877
13878 /* Get end of directory name in "dir".
13879 * We're done when it's "/" or "c:/". */
13880 p = gettail_sep(dir);
13881 if (p <= get_past_head(dir))
13882 return OK;
13883
13884 /* If the directory exists we're done. Otherwise: create it.*/
13885 updir = vim_strnsave(dir, (int)(p - dir));
13886 if (updir == NULL)
13887 return FAIL;
13888 if (mch_isdir(updir))
13889 r = OK;
13890 else if (mkdir_recurse(updir, prot) == OK)
13891 r = vim_mkdir_emsg(updir, prot);
13892 vim_free(updir);
13893 return r;
13894}
13895
13896#ifdef vim_mkdir
13897/*
13898 * "mkdir()" function
13899 */
13900 static void
13901f_mkdir(argvars, rettv)
13902 typval_T *argvars;
13903 typval_T *rettv;
13904{
13905 char_u *dir;
13906 char_u buf[NUMBUFLEN];
13907 int prot = 0755;
13908
13909 rettv->vval.v_number = FAIL;
13910 if (check_restricted() || check_secure())
13911 return;
13912
13913 dir = get_tv_string_buf(&argvars[0], buf);
13914 if (argvars[1].v_type != VAR_UNKNOWN)
13915 {
13916 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013917 prot = get_tv_number_chk(&argvars[2], NULL);
13918 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013919 mkdir_recurse(dir, prot);
13920 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013921 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013922}
13923#endif
13924
Bram Moolenaar0d660222005-01-07 21:51:51 +000013925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013926 * "mode()" function
13927 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013929f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013930 typval_T *argvars;
13931 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013933 char_u buf[3];
13934
13935 buf[1] = NUL;
13936 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937
13938#ifdef FEAT_VISUAL
13939 if (VIsual_active)
13940 {
13941 if (VIsual_select)
13942 buf[0] = VIsual_mode + 's' - 'v';
13943 else
13944 buf[0] = VIsual_mode;
13945 }
13946 else
13947#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013948 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13949 || State == CONFIRM)
13950 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013952 if (State == ASKMORE)
13953 buf[1] = 'm';
13954 else if (State == CONFIRM)
13955 buf[1] = '?';
13956 }
13957 else if (State == EXTERNCMD)
13958 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959 else if (State & INSERT)
13960 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013961#ifdef FEAT_VREPLACE
13962 if (State & VREPLACE_FLAG)
13963 {
13964 buf[0] = 'R';
13965 buf[1] = 'v';
13966 }
13967 else
13968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969 if (State & REPLACE_FLAG)
13970 buf[0] = 'R';
13971 else
13972 buf[0] = 'i';
13973 }
13974 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013975 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013977 if (exmode_active)
13978 buf[1] = 'v';
13979 }
13980 else if (exmode_active)
13981 {
13982 buf[0] = 'c';
13983 buf[1] = 'e';
13984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013986 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013988 if (finish_op)
13989 buf[1] = 'o';
13990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013992 /* Clear out the minor mode when the argument is not a non-zero number or
13993 * non-empty string. */
13994 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013995 buf[1] = NUL;
13996
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013997 rettv->vval.v_string = vim_strsave(buf);
13998 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999}
14000
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014001#ifdef FEAT_MZSCHEME
14002/*
14003 * "mzeval()" function
14004 */
14005 static void
14006f_mzeval(argvars, rettv)
14007 typval_T *argvars;
14008 typval_T *rettv;
14009{
14010 char_u *str;
14011 char_u buf[NUMBUFLEN];
14012
14013 str = get_tv_string_buf(&argvars[0], buf);
14014 do_mzeval(str, rettv);
14015}
14016#endif
14017
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014019 * "nextnonblank()" function
14020 */
14021 static void
14022f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014023 typval_T *argvars;
14024 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014025{
14026 linenr_T lnum;
14027
14028 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14029 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014030 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014031 {
14032 lnum = 0;
14033 break;
14034 }
14035 if (*skipwhite(ml_get(lnum)) != NUL)
14036 break;
14037 }
14038 rettv->vval.v_number = lnum;
14039}
14040
14041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042 * "nr2char()" function
14043 */
14044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014045f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014046 typval_T *argvars;
14047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048{
14049 char_u buf[NUMBUFLEN];
14050
14051#ifdef FEAT_MBYTE
14052 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014053 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054 else
14055#endif
14056 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014057 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058 buf[1] = NUL;
14059 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014060 rettv->v_type = VAR_STRING;
14061 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014062}
14063
14064/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014065 * "pathshorten()" function
14066 */
14067 static void
14068f_pathshorten(argvars, rettv)
14069 typval_T *argvars;
14070 typval_T *rettv;
14071{
14072 char_u *p;
14073
14074 rettv->v_type = VAR_STRING;
14075 p = get_tv_string_chk(&argvars[0]);
14076 if (p == NULL)
14077 rettv->vval.v_string = NULL;
14078 else
14079 {
14080 p = vim_strsave(p);
14081 rettv->vval.v_string = p;
14082 if (p != NULL)
14083 shorten_dir(p);
14084 }
14085}
14086
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014087#ifdef FEAT_FLOAT
14088/*
14089 * "pow()" function
14090 */
14091 static void
14092f_pow(argvars, rettv)
14093 typval_T *argvars;
14094 typval_T *rettv;
14095{
14096 float_T fx, fy;
14097
14098 rettv->v_type = VAR_FLOAT;
14099 if (get_float_arg(argvars, &fx) == OK
14100 && get_float_arg(&argvars[1], &fy) == OK)
14101 rettv->vval.v_float = pow(fx, fy);
14102 else
14103 rettv->vval.v_float = 0.0;
14104}
14105#endif
14106
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014107/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014108 * "prevnonblank()" function
14109 */
14110 static void
14111f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014112 typval_T *argvars;
14113 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014114{
14115 linenr_T lnum;
14116
14117 lnum = get_tv_lnum(argvars);
14118 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14119 lnum = 0;
14120 else
14121 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14122 --lnum;
14123 rettv->vval.v_number = lnum;
14124}
14125
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014126#ifdef HAVE_STDARG_H
14127/* This dummy va_list is here because:
14128 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14129 * - locally in the function results in a "used before set" warning
14130 * - using va_start() to initialize it gives "function with fixed args" error */
14131static va_list ap;
14132#endif
14133
Bram Moolenaar8c711452005-01-14 21:53:12 +000014134/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014135 * "printf()" function
14136 */
14137 static void
14138f_printf(argvars, rettv)
14139 typval_T *argvars;
14140 typval_T *rettv;
14141{
14142 rettv->v_type = VAR_STRING;
14143 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014144#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014145 {
14146 char_u buf[NUMBUFLEN];
14147 int len;
14148 char_u *s;
14149 int saved_did_emsg = did_emsg;
14150 char *fmt;
14151
14152 /* Get the required length, allocate the buffer and do it for real. */
14153 did_emsg = FALSE;
14154 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014155 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014156 if (!did_emsg)
14157 {
14158 s = alloc(len + 1);
14159 if (s != NULL)
14160 {
14161 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014162 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014163 }
14164 }
14165 did_emsg |= saved_did_emsg;
14166 }
14167#endif
14168}
14169
14170/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014171 * "pumvisible()" function
14172 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014173 static void
14174f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014175 typval_T *argvars UNUSED;
14176 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014177{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014178#ifdef FEAT_INS_EXPAND
14179 if (pum_visible())
14180 rettv->vval.v_number = 1;
14181#endif
14182}
14183
14184/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014185 * "range()" function
14186 */
14187 static void
14188f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014189 typval_T *argvars;
14190 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014191{
14192 long start;
14193 long end;
14194 long stride = 1;
14195 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014196 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014197
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014198 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014199 if (argvars[1].v_type == VAR_UNKNOWN)
14200 {
14201 end = start - 1;
14202 start = 0;
14203 }
14204 else
14205 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014206 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014207 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014208 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014209 }
14210
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014211 if (error)
14212 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014213 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014214 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014215 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014216 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014217 else
14218 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014219 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014220 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014221 if (list_append_number(rettv->vval.v_list,
14222 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014223 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014224 }
14225}
14226
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014227/*
14228 * "readfile()" function
14229 */
14230 static void
14231f_readfile(argvars, rettv)
14232 typval_T *argvars;
14233 typval_T *rettv;
14234{
14235 int binary = FALSE;
14236 char_u *fname;
14237 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014238 listitem_T *li;
14239#define FREAD_SIZE 200 /* optimized for text lines */
14240 char_u buf[FREAD_SIZE];
14241 int readlen; /* size of last fread() */
14242 int buflen; /* nr of valid chars in buf[] */
14243 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14244 int tolist; /* first byte in buf[] still to be put in list */
14245 int chop; /* how many CR to chop off */
14246 char_u *prev = NULL; /* previously read bytes, if any */
14247 int prevlen = 0; /* length of "prev" if not NULL */
14248 char_u *s;
14249 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014250 long maxline = MAXLNUM;
14251 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014252
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014253 if (argvars[1].v_type != VAR_UNKNOWN)
14254 {
14255 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14256 binary = TRUE;
14257 if (argvars[2].v_type != VAR_UNKNOWN)
14258 maxline = get_tv_number(&argvars[2]);
14259 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014260
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014261 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014262 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014263
14264 /* Always open the file in binary mode, library functions have a mind of
14265 * their own about CR-LF conversion. */
14266 fname = get_tv_string(&argvars[0]);
14267 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14268 {
14269 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14270 return;
14271 }
14272
14273 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014274 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014275 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014276 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014277 buflen = filtd + readlen;
14278 tolist = 0;
14279 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14280 {
14281 if (buf[filtd] == '\n' || readlen <= 0)
14282 {
14283 /* Only when in binary mode add an empty list item when the
14284 * last line ends in a '\n'. */
14285 if (!binary && readlen == 0 && filtd == 0)
14286 break;
14287
14288 /* Found end-of-line or end-of-file: add a text line to the
14289 * list. */
14290 chop = 0;
14291 if (!binary)
14292 while (filtd - chop - 1 >= tolist
14293 && buf[filtd - chop - 1] == '\r')
14294 ++chop;
14295 len = filtd - tolist - chop;
14296 if (prev == NULL)
14297 s = vim_strnsave(buf + tolist, len);
14298 else
14299 {
14300 s = alloc((unsigned)(prevlen + len + 1));
14301 if (s != NULL)
14302 {
14303 mch_memmove(s, prev, prevlen);
14304 vim_free(prev);
14305 prev = NULL;
14306 mch_memmove(s + prevlen, buf + tolist, len);
14307 s[prevlen + len] = NUL;
14308 }
14309 }
14310 tolist = filtd + 1;
14311
14312 li = listitem_alloc();
14313 if (li == NULL)
14314 {
14315 vim_free(s);
14316 break;
14317 }
14318 li->li_tv.v_type = VAR_STRING;
14319 li->li_tv.v_lock = 0;
14320 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014321 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014322
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014323 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014324 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014325 if (readlen <= 0)
14326 break;
14327 }
14328 else if (buf[filtd] == NUL)
14329 buf[filtd] = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014330#ifdef FEAT_MBYTE
14331 else if (buf[filtd] == 0xef
14332 && enc_utf8
14333 && filtd + 2 < buflen
14334 && !binary
14335 && buf[filtd + 1] == 0xbb
14336 && buf[filtd + 2] == 0xbf)
14337 {
14338 /* remove utf-8 byte order mark */
14339 mch_memmove(buf + filtd, buf + filtd + 3, buflen - filtd - 3);
14340 --filtd;
14341 buflen -= 3;
14342 }
14343#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014344 }
14345 if (readlen <= 0)
14346 break;
14347
14348 if (tolist == 0)
14349 {
14350 /* "buf" is full, need to move text to an allocated buffer */
14351 if (prev == NULL)
14352 {
14353 prev = vim_strnsave(buf, buflen);
14354 prevlen = buflen;
14355 }
14356 else
14357 {
14358 s = alloc((unsigned)(prevlen + buflen));
14359 if (s != NULL)
14360 {
14361 mch_memmove(s, prev, prevlen);
14362 mch_memmove(s + prevlen, buf, buflen);
14363 vim_free(prev);
14364 prev = s;
14365 prevlen += buflen;
14366 }
14367 }
14368 filtd = 0;
14369 }
14370 else
14371 {
14372 mch_memmove(buf, buf + tolist, buflen - tolist);
14373 filtd -= tolist;
14374 }
14375 }
14376
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014377 /*
14378 * For a negative line count use only the lines at the end of the file,
14379 * free the rest.
14380 */
14381 if (maxline < 0)
14382 while (cnt > -maxline)
14383 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014384 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014385 --cnt;
14386 }
14387
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014388 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014389 fclose(fd);
14390}
14391
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014392#if defined(FEAT_RELTIME)
14393static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14394
14395/*
14396 * Convert a List to proftime_T.
14397 * Return FAIL when there is something wrong.
14398 */
14399 static int
14400list2proftime(arg, tm)
14401 typval_T *arg;
14402 proftime_T *tm;
14403{
14404 long n1, n2;
14405 int error = FALSE;
14406
14407 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14408 || arg->vval.v_list->lv_len != 2)
14409 return FAIL;
14410 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14411 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14412# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014413 tm->HighPart = n1;
14414 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014415# else
14416 tm->tv_sec = n1;
14417 tm->tv_usec = n2;
14418# endif
14419 return error ? FAIL : OK;
14420}
14421#endif /* FEAT_RELTIME */
14422
14423/*
14424 * "reltime()" function
14425 */
14426 static void
14427f_reltime(argvars, rettv)
14428 typval_T *argvars;
14429 typval_T *rettv;
14430{
14431#ifdef FEAT_RELTIME
14432 proftime_T res;
14433 proftime_T start;
14434
14435 if (argvars[0].v_type == VAR_UNKNOWN)
14436 {
14437 /* No arguments: get current time. */
14438 profile_start(&res);
14439 }
14440 else if (argvars[1].v_type == VAR_UNKNOWN)
14441 {
14442 if (list2proftime(&argvars[0], &res) == FAIL)
14443 return;
14444 profile_end(&res);
14445 }
14446 else
14447 {
14448 /* Two arguments: compute the difference. */
14449 if (list2proftime(&argvars[0], &start) == FAIL
14450 || list2proftime(&argvars[1], &res) == FAIL)
14451 return;
14452 profile_sub(&res, &start);
14453 }
14454
14455 if (rettv_list_alloc(rettv) == OK)
14456 {
14457 long n1, n2;
14458
14459# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014460 n1 = res.HighPart;
14461 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014462# else
14463 n1 = res.tv_sec;
14464 n2 = res.tv_usec;
14465# endif
14466 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14467 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14468 }
14469#endif
14470}
14471
14472/*
14473 * "reltimestr()" function
14474 */
14475 static void
14476f_reltimestr(argvars, rettv)
14477 typval_T *argvars;
14478 typval_T *rettv;
14479{
14480#ifdef FEAT_RELTIME
14481 proftime_T tm;
14482#endif
14483
14484 rettv->v_type = VAR_STRING;
14485 rettv->vval.v_string = NULL;
14486#ifdef FEAT_RELTIME
14487 if (list2proftime(&argvars[0], &tm) == OK)
14488 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14489#endif
14490}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014491
Bram Moolenaar0d660222005-01-07 21:51:51 +000014492#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14493static void make_connection __ARGS((void));
14494static int check_connection __ARGS((void));
14495
14496 static void
14497make_connection()
14498{
14499 if (X_DISPLAY == NULL
14500# ifdef FEAT_GUI
14501 && !gui.in_use
14502# endif
14503 )
14504 {
14505 x_force_connect = TRUE;
14506 setup_term_clip();
14507 x_force_connect = FALSE;
14508 }
14509}
14510
14511 static int
14512check_connection()
14513{
14514 make_connection();
14515 if (X_DISPLAY == NULL)
14516 {
14517 EMSG(_("E240: No connection to Vim server"));
14518 return FAIL;
14519 }
14520 return OK;
14521}
14522#endif
14523
14524#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014525static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014526
14527 static void
14528remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014529 typval_T *argvars;
14530 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014531 int expr;
14532{
14533 char_u *server_name;
14534 char_u *keys;
14535 char_u *r = NULL;
14536 char_u buf[NUMBUFLEN];
14537# ifdef WIN32
14538 HWND w;
14539# else
14540 Window w;
14541# endif
14542
14543 if (check_restricted() || check_secure())
14544 return;
14545
14546# ifdef FEAT_X11
14547 if (check_connection() == FAIL)
14548 return;
14549# endif
14550
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014551 server_name = get_tv_string_chk(&argvars[0]);
14552 if (server_name == NULL)
14553 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014554 keys = get_tv_string_buf(&argvars[1], buf);
14555# ifdef WIN32
14556 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14557# else
14558 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14559 < 0)
14560# endif
14561 {
14562 if (r != NULL)
14563 EMSG(r); /* sending worked but evaluation failed */
14564 else
14565 EMSG2(_("E241: Unable to send to %s"), server_name);
14566 return;
14567 }
14568
14569 rettv->vval.v_string = r;
14570
14571 if (argvars[2].v_type != VAR_UNKNOWN)
14572 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014573 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014574 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014575 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014576
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014577 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014578 v.di_tv.v_type = VAR_STRING;
14579 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014580 idvar = get_tv_string_chk(&argvars[2]);
14581 if (idvar != NULL)
14582 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014583 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014584 }
14585}
14586#endif
14587
14588/*
14589 * "remote_expr()" function
14590 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014591 static void
14592f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014593 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014594 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014595{
14596 rettv->v_type = VAR_STRING;
14597 rettv->vval.v_string = NULL;
14598#ifdef FEAT_CLIENTSERVER
14599 remote_common(argvars, rettv, TRUE);
14600#endif
14601}
14602
14603/*
14604 * "remote_foreground()" function
14605 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014606 static void
14607f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014608 typval_T *argvars UNUSED;
14609 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014610{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014611#ifdef FEAT_CLIENTSERVER
14612# ifdef WIN32
14613 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014614 {
14615 char_u *server_name = get_tv_string_chk(&argvars[0]);
14616
14617 if (server_name != NULL)
14618 serverForeground(server_name);
14619 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014620# else
14621 /* Send a foreground() expression to the server. */
14622 argvars[1].v_type = VAR_STRING;
14623 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14624 argvars[2].v_type = VAR_UNKNOWN;
14625 remote_common(argvars, rettv, TRUE);
14626 vim_free(argvars[1].vval.v_string);
14627# endif
14628#endif
14629}
14630
Bram Moolenaar0d660222005-01-07 21:51:51 +000014631 static void
14632f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014633 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014634 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014635{
14636#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014637 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014638 char_u *s = NULL;
14639# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014640 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014641# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014642 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014643
14644 if (check_restricted() || check_secure())
14645 {
14646 rettv->vval.v_number = -1;
14647 return;
14648 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014649 serverid = get_tv_string_chk(&argvars[0]);
14650 if (serverid == NULL)
14651 {
14652 rettv->vval.v_number = -1;
14653 return; /* type error; errmsg already given */
14654 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014655# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014656 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014657 if (n == 0)
14658 rettv->vval.v_number = -1;
14659 else
14660 {
14661 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14662 rettv->vval.v_number = (s != NULL);
14663 }
14664# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014665 if (check_connection() == FAIL)
14666 return;
14667
14668 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014669 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014670# endif
14671
14672 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14673 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014674 char_u *retvar;
14675
Bram Moolenaar33570922005-01-25 22:26:29 +000014676 v.di_tv.v_type = VAR_STRING;
14677 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014678 retvar = get_tv_string_chk(&argvars[1]);
14679 if (retvar != NULL)
14680 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014681 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014682 }
14683#else
14684 rettv->vval.v_number = -1;
14685#endif
14686}
14687
Bram Moolenaar0d660222005-01-07 21:51:51 +000014688 static void
14689f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014690 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014691 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014692{
14693 char_u *r = NULL;
14694
14695#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014696 char_u *serverid = get_tv_string_chk(&argvars[0]);
14697
14698 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014699 {
14700# ifdef WIN32
14701 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014702 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014703
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014704 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014705 if (n != 0)
14706 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14707 if (r == NULL)
14708# else
14709 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014710 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014711# endif
14712 EMSG(_("E277: Unable to read a server reply"));
14713 }
14714#endif
14715 rettv->v_type = VAR_STRING;
14716 rettv->vval.v_string = r;
14717}
14718
14719/*
14720 * "remote_send()" function
14721 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014722 static void
14723f_remote_send(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 rettv->v_type = VAR_STRING;
14728 rettv->vval.v_string = NULL;
14729#ifdef FEAT_CLIENTSERVER
14730 remote_common(argvars, rettv, FALSE);
14731#endif
14732}
14733
14734/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014735 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014736 */
14737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014738f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014739 typval_T *argvars;
14740 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014741{
Bram Moolenaar33570922005-01-25 22:26:29 +000014742 list_T *l;
14743 listitem_T *item, *item2;
14744 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014745 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014746 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014747 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014748 dict_T *d;
14749 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014750
Bram Moolenaar8c711452005-01-14 21:53:12 +000014751 if (argvars[0].v_type == VAR_DICT)
14752 {
14753 if (argvars[2].v_type != VAR_UNKNOWN)
14754 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014755 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014756 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014757 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014758 key = get_tv_string_chk(&argvars[1]);
14759 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014760 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014761 di = dict_find(d, key, -1);
14762 if (di == NULL)
14763 EMSG2(_(e_dictkey), key);
14764 else
14765 {
14766 *rettv = di->di_tv;
14767 init_tv(&di->di_tv);
14768 dictitem_remove(d, di);
14769 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014770 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014771 }
14772 }
14773 else if (argvars[0].v_type != VAR_LIST)
14774 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014775 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014776 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014778 int error = FALSE;
14779
14780 idx = get_tv_number_chk(&argvars[1], &error);
14781 if (error)
14782 ; /* type error: do nothing, errmsg already given */
14783 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014784 EMSGN(_(e_listidx), idx);
14785 else
14786 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014787 if (argvars[2].v_type == VAR_UNKNOWN)
14788 {
14789 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014790 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014791 *rettv = item->li_tv;
14792 vim_free(item);
14793 }
14794 else
14795 {
14796 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014797 end = get_tv_number_chk(&argvars[2], &error);
14798 if (error)
14799 ; /* type error: do nothing */
14800 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014801 EMSGN(_(e_listidx), end);
14802 else
14803 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014804 int cnt = 0;
14805
14806 for (li = item; li != NULL; li = li->li_next)
14807 {
14808 ++cnt;
14809 if (li == item2)
14810 break;
14811 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014812 if (li == NULL) /* didn't find "item2" after "item" */
14813 EMSG(_(e_invrange));
14814 else
14815 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014816 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014817 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014818 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014819 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014820 l->lv_first = item;
14821 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014822 item->li_prev = NULL;
14823 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014824 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014825 }
14826 }
14827 }
14828 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014829 }
14830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014831}
14832
14833/*
14834 * "rename({from}, {to})" function
14835 */
14836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014837f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014838 typval_T *argvars;
14839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840{
14841 char_u buf[NUMBUFLEN];
14842
14843 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014844 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014846 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14847 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014848}
14849
14850/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014851 * "repeat()" function
14852 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014853 static void
14854f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014855 typval_T *argvars;
14856 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014857{
14858 char_u *p;
14859 int n;
14860 int slen;
14861 int len;
14862 char_u *r;
14863 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014864
14865 n = get_tv_number(&argvars[1]);
14866 if (argvars[0].v_type == VAR_LIST)
14867 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014868 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014869 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014870 if (list_extend(rettv->vval.v_list,
14871 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014872 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014873 }
14874 else
14875 {
14876 p = get_tv_string(&argvars[0]);
14877 rettv->v_type = VAR_STRING;
14878 rettv->vval.v_string = NULL;
14879
14880 slen = (int)STRLEN(p);
14881 len = slen * n;
14882 if (len <= 0)
14883 return;
14884
14885 r = alloc(len + 1);
14886 if (r != NULL)
14887 {
14888 for (i = 0; i < n; i++)
14889 mch_memmove(r + i * slen, p, (size_t)slen);
14890 r[len] = NUL;
14891 }
14892
14893 rettv->vval.v_string = r;
14894 }
14895}
14896
14897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014898 * "resolve()" function
14899 */
14900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014901f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014902 typval_T *argvars;
14903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904{
14905 char_u *p;
14906
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014907 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908#ifdef FEAT_SHORTCUT
14909 {
14910 char_u *v = NULL;
14911
14912 v = mch_resolve_shortcut(p);
14913 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014914 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014915 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014916 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014917 }
14918#else
14919# ifdef HAVE_READLINK
14920 {
14921 char_u buf[MAXPATHL + 1];
14922 char_u *cpy;
14923 int len;
14924 char_u *remain = NULL;
14925 char_u *q;
14926 int is_relative_to_current = FALSE;
14927 int has_trailing_pathsep = FALSE;
14928 int limit = 100;
14929
14930 p = vim_strsave(p);
14931
14932 if (p[0] == '.' && (vim_ispathsep(p[1])
14933 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14934 is_relative_to_current = TRUE;
14935
14936 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014937 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014938 has_trailing_pathsep = TRUE;
14939
14940 q = getnextcomp(p);
14941 if (*q != NUL)
14942 {
14943 /* Separate the first path component in "p", and keep the
14944 * remainder (beginning with the path separator). */
14945 remain = vim_strsave(q - 1);
14946 q[-1] = NUL;
14947 }
14948
14949 for (;;)
14950 {
14951 for (;;)
14952 {
14953 len = readlink((char *)p, (char *)buf, MAXPATHL);
14954 if (len <= 0)
14955 break;
14956 buf[len] = NUL;
14957
14958 if (limit-- == 0)
14959 {
14960 vim_free(p);
14961 vim_free(remain);
14962 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014963 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964 goto fail;
14965 }
14966
14967 /* Ensure that the result will have a trailing path separator
14968 * if the argument has one. */
14969 if (remain == NULL && has_trailing_pathsep)
14970 add_pathsep(buf);
14971
14972 /* Separate the first path component in the link value and
14973 * concatenate the remainders. */
14974 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14975 if (*q != NUL)
14976 {
14977 if (remain == NULL)
14978 remain = vim_strsave(q - 1);
14979 else
14980 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014981 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014982 if (cpy != NULL)
14983 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984 vim_free(remain);
14985 remain = cpy;
14986 }
14987 }
14988 q[-1] = NUL;
14989 }
14990
14991 q = gettail(p);
14992 if (q > p && *q == NUL)
14993 {
14994 /* Ignore trailing path separator. */
14995 q[-1] = NUL;
14996 q = gettail(p);
14997 }
14998 if (q > p && !mch_isFullName(buf))
14999 {
15000 /* symlink is relative to directory of argument */
15001 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15002 if (cpy != NULL)
15003 {
15004 STRCPY(cpy, p);
15005 STRCPY(gettail(cpy), buf);
15006 vim_free(p);
15007 p = cpy;
15008 }
15009 }
15010 else
15011 {
15012 vim_free(p);
15013 p = vim_strsave(buf);
15014 }
15015 }
15016
15017 if (remain == NULL)
15018 break;
15019
15020 /* Append the first path component of "remain" to "p". */
15021 q = getnextcomp(remain + 1);
15022 len = q - remain - (*q != NUL);
15023 cpy = vim_strnsave(p, STRLEN(p) + len);
15024 if (cpy != NULL)
15025 {
15026 STRNCAT(cpy, remain, len);
15027 vim_free(p);
15028 p = cpy;
15029 }
15030 /* Shorten "remain". */
15031 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015032 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015033 else
15034 {
15035 vim_free(remain);
15036 remain = NULL;
15037 }
15038 }
15039
15040 /* If the result is a relative path name, make it explicitly relative to
15041 * the current directory if and only if the argument had this form. */
15042 if (!vim_ispathsep(*p))
15043 {
15044 if (is_relative_to_current
15045 && *p != NUL
15046 && !(p[0] == '.'
15047 && (p[1] == NUL
15048 || vim_ispathsep(p[1])
15049 || (p[1] == '.'
15050 && (p[2] == NUL
15051 || vim_ispathsep(p[2]))))))
15052 {
15053 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015054 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055 if (cpy != NULL)
15056 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015057 vim_free(p);
15058 p = cpy;
15059 }
15060 }
15061 else if (!is_relative_to_current)
15062 {
15063 /* Strip leading "./". */
15064 q = p;
15065 while (q[0] == '.' && vim_ispathsep(q[1]))
15066 q += 2;
15067 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015068 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015069 }
15070 }
15071
15072 /* Ensure that the result will have no trailing path separator
15073 * if the argument had none. But keep "/" or "//". */
15074 if (!has_trailing_pathsep)
15075 {
15076 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015077 if (after_pathsep(p, q))
15078 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015079 }
15080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015081 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082 }
15083# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015084 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015085# endif
15086#endif
15087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015088 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015089
15090#ifdef HAVE_READLINK
15091fail:
15092#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015093 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015094}
15095
15096/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015097 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098 */
15099 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015100f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015101 typval_T *argvars;
15102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103{
Bram Moolenaar33570922005-01-25 22:26:29 +000015104 list_T *l;
15105 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015106
Bram Moolenaar0d660222005-01-07 21:51:51 +000015107 if (argvars[0].v_type != VAR_LIST)
15108 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015109 else if ((l = argvars[0].vval.v_list) != NULL
15110 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015111 {
15112 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015113 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015114 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015115 while (li != NULL)
15116 {
15117 ni = li->li_prev;
15118 list_append(l, li);
15119 li = ni;
15120 }
15121 rettv->vval.v_list = l;
15122 rettv->v_type = VAR_LIST;
15123 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015124 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126}
15127
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015128#define SP_NOMOVE 0x01 /* don't move cursor */
15129#define SP_REPEAT 0x02 /* repeat to find outer pair */
15130#define SP_RETCOUNT 0x04 /* return matchcount */
15131#define SP_SETPCMARK 0x08 /* set previous context mark */
15132#define SP_START 0x10 /* accept match at start position */
15133#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15134#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015135
Bram Moolenaar33570922005-01-25 22:26:29 +000015136static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015137
15138/*
15139 * Get flags for a search function.
15140 * Possibly sets "p_ws".
15141 * Returns BACKWARD, FORWARD or zero (for an error).
15142 */
15143 static int
15144get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015145 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015146 int *flagsp;
15147{
15148 int dir = FORWARD;
15149 char_u *flags;
15150 char_u nbuf[NUMBUFLEN];
15151 int mask;
15152
15153 if (varp->v_type != VAR_UNKNOWN)
15154 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015155 flags = get_tv_string_buf_chk(varp, nbuf);
15156 if (flags == NULL)
15157 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015158 while (*flags != NUL)
15159 {
15160 switch (*flags)
15161 {
15162 case 'b': dir = BACKWARD; break;
15163 case 'w': p_ws = TRUE; break;
15164 case 'W': p_ws = FALSE; break;
15165 default: mask = 0;
15166 if (flagsp != NULL)
15167 switch (*flags)
15168 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015169 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015170 case 'e': mask = SP_END; break;
15171 case 'm': mask = SP_RETCOUNT; break;
15172 case 'n': mask = SP_NOMOVE; break;
15173 case 'p': mask = SP_SUBPAT; break;
15174 case 'r': mask = SP_REPEAT; break;
15175 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015176 }
15177 if (mask == 0)
15178 {
15179 EMSG2(_(e_invarg2), flags);
15180 dir = 0;
15181 }
15182 else
15183 *flagsp |= mask;
15184 }
15185 if (dir == 0)
15186 break;
15187 ++flags;
15188 }
15189 }
15190 return dir;
15191}
15192
Bram Moolenaar071d4272004-06-13 20:20:40 +000015193/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015194 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015195 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015196 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015197search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015198 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015199 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015200 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015201{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015202 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015203 char_u *pat;
15204 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015205 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206 int save_p_ws = p_ws;
15207 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015208 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015209 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015210 proftime_T tm;
15211#ifdef FEAT_RELTIME
15212 long time_limit = 0;
15213#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015214 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015215 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015217 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015218 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015219 if (dir == 0)
15220 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015221 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015222 if (flags & SP_START)
15223 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015224 if (flags & SP_END)
15225 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015226
Bram Moolenaar76929292008-01-06 19:07:36 +000015227 /* Optional arguments: line number to stop searching and timeout. */
15228 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015229 {
15230 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15231 if (lnum_stop < 0)
15232 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015233#ifdef FEAT_RELTIME
15234 if (argvars[3].v_type != VAR_UNKNOWN)
15235 {
15236 time_limit = get_tv_number_chk(&argvars[3], NULL);
15237 if (time_limit < 0)
15238 goto theend;
15239 }
15240#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015241 }
15242
Bram Moolenaar76929292008-01-06 19:07:36 +000015243#ifdef FEAT_RELTIME
15244 /* Set the time limit, if there is one. */
15245 profile_setlimit(time_limit, &tm);
15246#endif
15247
Bram Moolenaar231334e2005-07-25 20:46:57 +000015248 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015249 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015250 * Check to make sure only those flags are set.
15251 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15252 * flags cannot be set. Check for that condition also.
15253 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015254 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015255 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015256 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015257 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015258 goto theend;
15259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015260
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015261 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015262 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015263 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015264 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015265 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015266 if (flags & SP_SUBPAT)
15267 retval = subpatnum;
15268 else
15269 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015270 if (flags & SP_SETPCMARK)
15271 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015273 if (match_pos != NULL)
15274 {
15275 /* Store the match cursor position */
15276 match_pos->lnum = pos.lnum;
15277 match_pos->col = pos.col + 1;
15278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015279 /* "/$" will put the cursor after the end of the line, may need to
15280 * correct that here */
15281 check_cursor();
15282 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015283
15284 /* If 'n' flag is used: restore cursor position. */
15285 if (flags & SP_NOMOVE)
15286 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015287 else
15288 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015289theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015290 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015291
15292 return retval;
15293}
15294
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015295#ifdef FEAT_FLOAT
15296/*
15297 * "round({float})" function
15298 */
15299 static void
15300f_round(argvars, rettv)
15301 typval_T *argvars;
15302 typval_T *rettv;
15303{
15304 float_T f;
15305
15306 rettv->v_type = VAR_FLOAT;
15307 if (get_float_arg(argvars, &f) == OK)
15308 /* round() is not in C90, use ceil() or floor() instead. */
15309 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15310 else
15311 rettv->vval.v_float = 0.0;
15312}
15313#endif
15314
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015315/*
15316 * "search()" function
15317 */
15318 static void
15319f_search(argvars, rettv)
15320 typval_T *argvars;
15321 typval_T *rettv;
15322{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015323 int flags = 0;
15324
15325 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326}
15327
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015329 * "searchdecl()" function
15330 */
15331 static void
15332f_searchdecl(argvars, rettv)
15333 typval_T *argvars;
15334 typval_T *rettv;
15335{
15336 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015337 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015338 int error = FALSE;
15339 char_u *name;
15340
15341 rettv->vval.v_number = 1; /* default: FAIL */
15342
15343 name = get_tv_string_chk(&argvars[0]);
15344 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015345 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015346 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015347 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15348 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15349 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015350 if (!error && name != NULL)
15351 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015352 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015353}
15354
15355/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015356 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015357 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015358 static int
15359searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015360 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015361 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362{
15363 char_u *spat, *mpat, *epat;
15364 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366 int dir;
15367 int flags = 0;
15368 char_u nbuf1[NUMBUFLEN];
15369 char_u nbuf2[NUMBUFLEN];
15370 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015371 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015372 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015373 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015374
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015376 spat = get_tv_string_chk(&argvars[0]);
15377 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15378 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15379 if (spat == NULL || mpat == NULL || epat == NULL)
15380 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381
Bram Moolenaar071d4272004-06-13 20:20:40 +000015382 /* Handle the optional fourth argument: flags */
15383 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015384 if (dir == 0)
15385 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015386
15387 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015388 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15389 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015390 if ((flags & (SP_END | SP_SUBPAT)) != 0
15391 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015392 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015393 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015394 goto theend;
15395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015397 /* Using 'r' implies 'W', otherwise it doesn't work. */
15398 if (flags & SP_REPEAT)
15399 p_ws = FALSE;
15400
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015401 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015402 if (argvars[3].v_type == VAR_UNKNOWN
15403 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015404 skip = (char_u *)"";
15405 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015407 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015408 if (argvars[5].v_type != VAR_UNKNOWN)
15409 {
15410 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15411 if (lnum_stop < 0)
15412 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015413#ifdef FEAT_RELTIME
15414 if (argvars[6].v_type != VAR_UNKNOWN)
15415 {
15416 time_limit = get_tv_number_chk(&argvars[6], NULL);
15417 if (time_limit < 0)
15418 goto theend;
15419 }
15420#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015421 }
15422 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015423 if (skip == NULL)
15424 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015425
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015426 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015427 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015428
15429theend:
15430 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015431
15432 return retval;
15433}
15434
15435/*
15436 * "searchpair()" function
15437 */
15438 static void
15439f_searchpair(argvars, rettv)
15440 typval_T *argvars;
15441 typval_T *rettv;
15442{
15443 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15444}
15445
15446/*
15447 * "searchpairpos()" function
15448 */
15449 static void
15450f_searchpairpos(argvars, rettv)
15451 typval_T *argvars;
15452 typval_T *rettv;
15453{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015454 pos_T match_pos;
15455 int lnum = 0;
15456 int col = 0;
15457
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015458 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015459 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015460
15461 if (searchpair_cmn(argvars, &match_pos) > 0)
15462 {
15463 lnum = match_pos.lnum;
15464 col = match_pos.col;
15465 }
15466
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015467 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15468 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015469}
15470
15471/*
15472 * Search for a start/middle/end thing.
15473 * Used by searchpair(), see its documentation for the details.
15474 * Returns 0 or -1 for no match,
15475 */
15476 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015477do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15478 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015479 char_u *spat; /* start pattern */
15480 char_u *mpat; /* middle pattern */
15481 char_u *epat; /* end pattern */
15482 int dir; /* BACKWARD or FORWARD */
15483 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015484 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015485 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015486 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015487 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015488{
15489 char_u *save_cpo;
15490 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15491 long retval = 0;
15492 pos_T pos;
15493 pos_T firstpos;
15494 pos_T foundpos;
15495 pos_T save_cursor;
15496 pos_T save_pos;
15497 int n;
15498 int r;
15499 int nest = 1;
15500 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015501 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015502 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015503
15504 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15505 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015506 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015507
Bram Moolenaar76929292008-01-06 19:07:36 +000015508#ifdef FEAT_RELTIME
15509 /* Set the time limit, if there is one. */
15510 profile_setlimit(time_limit, &tm);
15511#endif
15512
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015513 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15514 * start/middle/end (pat3, for the top pair). */
15515 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15516 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15517 if (pat2 == NULL || pat3 == NULL)
15518 goto theend;
15519 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15520 if (*mpat == NUL)
15521 STRCPY(pat3, pat2);
15522 else
15523 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15524 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015525 if (flags & SP_START)
15526 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015527
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528 save_cursor = curwin->w_cursor;
15529 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015530 clearpos(&firstpos);
15531 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532 pat = pat3;
15533 for (;;)
15534 {
15535 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015536 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15538 /* didn't find it or found the first match again: FAIL */
15539 break;
15540
15541 if (firstpos.lnum == 0)
15542 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015543 if (equalpos(pos, foundpos))
15544 {
15545 /* Found the same position again. Can happen with a pattern that
15546 * has "\zs" at the end and searching backwards. Advance one
15547 * character and try again. */
15548 if (dir == BACKWARD)
15549 decl(&pos);
15550 else
15551 incl(&pos);
15552 }
15553 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015555 /* clear the start flag to avoid getting stuck here */
15556 options &= ~SEARCH_START;
15557
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558 /* If the skip pattern matches, ignore this match. */
15559 if (*skip != NUL)
15560 {
15561 save_pos = curwin->w_cursor;
15562 curwin->w_cursor = pos;
15563 r = eval_to_bool(skip, &err, NULL, FALSE);
15564 curwin->w_cursor = save_pos;
15565 if (err)
15566 {
15567 /* Evaluating {skip} caused an error, break here. */
15568 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015569 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015570 break;
15571 }
15572 if (r)
15573 continue;
15574 }
15575
15576 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15577 {
15578 /* Found end when searching backwards or start when searching
15579 * forward: nested pair. */
15580 ++nest;
15581 pat = pat2; /* nested, don't search for middle */
15582 }
15583 else
15584 {
15585 /* Found end when searching forward or start when searching
15586 * backward: end of (nested) pair; or found middle in outer pair. */
15587 if (--nest == 1)
15588 pat = pat3; /* outer level, search for middle */
15589 }
15590
15591 if (nest == 0)
15592 {
15593 /* Found the match: return matchcount or line number. */
15594 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015595 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015596 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015597 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015598 if (flags & SP_SETPCMARK)
15599 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015600 curwin->w_cursor = pos;
15601 if (!(flags & SP_REPEAT))
15602 break;
15603 nest = 1; /* search for next unmatched */
15604 }
15605 }
15606
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015607 if (match_pos != NULL)
15608 {
15609 /* Store the match cursor position */
15610 match_pos->lnum = curwin->w_cursor.lnum;
15611 match_pos->col = curwin->w_cursor.col + 1;
15612 }
15613
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015615 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015616 curwin->w_cursor = save_cursor;
15617
15618theend:
15619 vim_free(pat2);
15620 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015621 if (p_cpo == empty_option)
15622 p_cpo = save_cpo;
15623 else
15624 /* Darn, evaluating the {skip} expression changed the value. */
15625 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015626
15627 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628}
15629
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015630/*
15631 * "searchpos()" function
15632 */
15633 static void
15634f_searchpos(argvars, rettv)
15635 typval_T *argvars;
15636 typval_T *rettv;
15637{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015638 pos_T match_pos;
15639 int lnum = 0;
15640 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015641 int n;
15642 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015643
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015644 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015645 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015646
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015647 n = search_cmn(argvars, &match_pos, &flags);
15648 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015649 {
15650 lnum = match_pos.lnum;
15651 col = match_pos.col;
15652 }
15653
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015654 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15655 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015656 if (flags & SP_SUBPAT)
15657 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015658}
15659
15660
Bram Moolenaar0d660222005-01-07 21:51:51 +000015661 static void
15662f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015663 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015666#ifdef FEAT_CLIENTSERVER
15667 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015668 char_u *server = get_tv_string_chk(&argvars[0]);
15669 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670
Bram Moolenaar0d660222005-01-07 21:51:51 +000015671 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015672 if (server == NULL || reply == NULL)
15673 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015674 if (check_restricted() || check_secure())
15675 return;
15676# ifdef FEAT_X11
15677 if (check_connection() == FAIL)
15678 return;
15679# endif
15680
15681 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015683 EMSG(_("E258: Unable to send to client"));
15684 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015686 rettv->vval.v_number = 0;
15687#else
15688 rettv->vval.v_number = -1;
15689#endif
15690}
15691
Bram Moolenaar0d660222005-01-07 21:51:51 +000015692 static void
15693f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015694 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015695 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015696{
15697 char_u *r = NULL;
15698
15699#ifdef FEAT_CLIENTSERVER
15700# ifdef WIN32
15701 r = serverGetVimNames();
15702# else
15703 make_connection();
15704 if (X_DISPLAY != NULL)
15705 r = serverGetVimNames(X_DISPLAY);
15706# endif
15707#endif
15708 rettv->v_type = VAR_STRING;
15709 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710}
15711
15712/*
15713 * "setbufvar()" function
15714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015716f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015717 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015718 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719{
15720 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015723 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724 char_u nbuf[NUMBUFLEN];
15725
15726 if (check_restricted() || check_secure())
15727 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015728 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15729 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015730 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731 varp = &argvars[2];
15732
15733 if (buf != NULL && varname != NULL && varp != NULL)
15734 {
15735 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737
15738 if (*varname == '&')
15739 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015740 long numval;
15741 char_u *strval;
15742 int error = FALSE;
15743
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015745 numval = get_tv_number_chk(varp, &error);
15746 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015747 if (!error && strval != NULL)
15748 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749 }
15750 else
15751 {
15752 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15753 if (bufvarname != NULL)
15754 {
15755 STRCPY(bufvarname, "b:");
15756 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015757 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758 vim_free(bufvarname);
15759 }
15760 }
15761
15762 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765}
15766
15767/*
15768 * "setcmdpos()" function
15769 */
15770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015771f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015772 typval_T *argvars;
15773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015774{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015775 int pos = (int)get_tv_number(&argvars[0]) - 1;
15776
15777 if (pos >= 0)
15778 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779}
15780
15781/*
15782 * "setline()" function
15783 */
15784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015785f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015786 typval_T *argvars;
15787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788{
15789 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015790 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015791 list_T *l = NULL;
15792 listitem_T *li = NULL;
15793 long added = 0;
15794 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015795
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015796 lnum = get_tv_lnum(&argvars[0]);
15797 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015798 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015799 l = argvars[1].vval.v_list;
15800 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015801 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015802 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015803 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015804
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015805 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015806 for (;;)
15807 {
15808 if (l != NULL)
15809 {
15810 /* list argument, get next string */
15811 if (li == NULL)
15812 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015813 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015814 li = li->li_next;
15815 }
15816
15817 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015818 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015819 break;
15820 if (lnum <= curbuf->b_ml.ml_line_count)
15821 {
15822 /* existing line, replace it */
15823 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15824 {
15825 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015826 if (lnum == curwin->w_cursor.lnum)
15827 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015828 rettv->vval.v_number = 0; /* OK */
15829 }
15830 }
15831 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15832 {
15833 /* lnum is one past the last line, append the line */
15834 ++added;
15835 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15836 rettv->vval.v_number = 0; /* OK */
15837 }
15838
15839 if (l == NULL) /* only one string argument */
15840 break;
15841 ++lnum;
15842 }
15843
15844 if (added > 0)
15845 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846}
15847
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015848static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15849
Bram Moolenaar071d4272004-06-13 20:20:40 +000015850/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015851 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015852 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015853 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015854set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015855 win_T *wp UNUSED;
15856 typval_T *list_arg UNUSED;
15857 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015858 typval_T *rettv;
15859{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015860#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015861 char_u *act;
15862 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015863#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015864
Bram Moolenaar2641f772005-03-25 21:58:17 +000015865 rettv->vval.v_number = -1;
15866
15867#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015868 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015869 EMSG(_(e_listreq));
15870 else
15871 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015872 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015873
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015874 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015875 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015876 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015877 if (act == NULL)
15878 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015879 if (*act == 'a' || *act == 'r')
15880 action = *act;
15881 }
15882
Bram Moolenaarbc226b62010-08-09 22:14:48 +020015883 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015884 rettv->vval.v_number = 0;
15885 }
15886#endif
15887}
15888
15889/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015890 * "setloclist()" function
15891 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015892 static void
15893f_setloclist(argvars, rettv)
15894 typval_T *argvars;
15895 typval_T *rettv;
15896{
15897 win_T *win;
15898
15899 rettv->vval.v_number = -1;
15900
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015901 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015902 if (win != NULL)
15903 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15904}
15905
15906/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015907 * "setmatches()" function
15908 */
15909 static void
15910f_setmatches(argvars, rettv)
15911 typval_T *argvars;
15912 typval_T *rettv;
15913{
15914#ifdef FEAT_SEARCH_EXTRA
15915 list_T *l;
15916 listitem_T *li;
15917 dict_T *d;
15918
15919 rettv->vval.v_number = -1;
15920 if (argvars[0].v_type != VAR_LIST)
15921 {
15922 EMSG(_(e_listreq));
15923 return;
15924 }
15925 if ((l = argvars[0].vval.v_list) != NULL)
15926 {
15927
15928 /* To some extent make sure that we are dealing with a list from
15929 * "getmatches()". */
15930 li = l->lv_first;
15931 while (li != NULL)
15932 {
15933 if (li->li_tv.v_type != VAR_DICT
15934 || (d = li->li_tv.vval.v_dict) == NULL)
15935 {
15936 EMSG(_(e_invarg));
15937 return;
15938 }
15939 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15940 && dict_find(d, (char_u *)"pattern", -1) != NULL
15941 && dict_find(d, (char_u *)"priority", -1) != NULL
15942 && dict_find(d, (char_u *)"id", -1) != NULL))
15943 {
15944 EMSG(_(e_invarg));
15945 return;
15946 }
15947 li = li->li_next;
15948 }
15949
15950 clear_matches(curwin);
15951 li = l->lv_first;
15952 while (li != NULL)
15953 {
15954 d = li->li_tv.vval.v_dict;
15955 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15956 get_dict_string(d, (char_u *)"pattern", FALSE),
15957 (int)get_dict_number(d, (char_u *)"priority"),
15958 (int)get_dict_number(d, (char_u *)"id"));
15959 li = li->li_next;
15960 }
15961 rettv->vval.v_number = 0;
15962 }
15963#endif
15964}
15965
15966/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015967 * "setpos()" function
15968 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015969 static void
15970f_setpos(argvars, rettv)
15971 typval_T *argvars;
15972 typval_T *rettv;
15973{
15974 pos_T pos;
15975 int fnum;
15976 char_u *name;
15977
Bram Moolenaar08250432008-02-13 11:42:46 +000015978 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015979 name = get_tv_string_chk(argvars);
15980 if (name != NULL)
15981 {
15982 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15983 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015984 if (--pos.col < 0)
15985 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015986 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015987 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015988 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015989 if (fnum == curbuf->b_fnum)
15990 {
15991 curwin->w_cursor = pos;
15992 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015993 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015994 }
15995 else
15996 EMSG(_(e_invarg));
15997 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015998 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15999 {
16000 /* set mark */
16001 if (setmark_pos(name[1], &pos, fnum) == OK)
16002 rettv->vval.v_number = 0;
16003 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016004 else
16005 EMSG(_(e_invarg));
16006 }
16007 }
16008}
16009
16010/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016011 * "setqflist()" function
16012 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016013 static void
16014f_setqflist(argvars, rettv)
16015 typval_T *argvars;
16016 typval_T *rettv;
16017{
16018 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16019}
16020
16021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016022 * "setreg()" function
16023 */
16024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016025f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016026 typval_T *argvars;
16027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028{
16029 int regname;
16030 char_u *strregname;
16031 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016032 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033 int append;
16034 char_u yank_type;
16035 long block_len;
16036
16037 block_len = -1;
16038 yank_type = MAUTO;
16039 append = FALSE;
16040
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016041 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016042 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016044 if (strregname == NULL)
16045 return; /* type error; errmsg already given */
16046 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047 if (regname == 0 || regname == '@')
16048 regname = '"';
16049 else if (regname == '=')
16050 return;
16051
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016052 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016053 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016054 stropt = get_tv_string_chk(&argvars[2]);
16055 if (stropt == NULL)
16056 return; /* type error */
16057 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016058 switch (*stropt)
16059 {
16060 case 'a': case 'A': /* append */
16061 append = TRUE;
16062 break;
16063 case 'v': case 'c': /* character-wise selection */
16064 yank_type = MCHAR;
16065 break;
16066 case 'V': case 'l': /* line-wise selection */
16067 yank_type = MLINE;
16068 break;
16069#ifdef FEAT_VISUAL
16070 case 'b': case Ctrl_V: /* block-wise selection */
16071 yank_type = MBLOCK;
16072 if (VIM_ISDIGIT(stropt[1]))
16073 {
16074 ++stropt;
16075 block_len = getdigits(&stropt) - 1;
16076 --stropt;
16077 }
16078 break;
16079#endif
16080 }
16081 }
16082
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016083 strval = get_tv_string_chk(&argvars[1]);
16084 if (strval != NULL)
16085 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016087 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088}
16089
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016090/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016091 * "settabvar()" function
16092 */
16093 static void
16094f_settabvar(argvars, rettv)
16095 typval_T *argvars;
16096 typval_T *rettv;
16097{
16098 tabpage_T *save_curtab;
16099 char_u *varname, *tabvarname;
16100 typval_T *varp;
16101 tabpage_T *tp;
16102
16103 rettv->vval.v_number = 0;
16104
16105 if (check_restricted() || check_secure())
16106 return;
16107
16108 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16109 varname = get_tv_string_chk(&argvars[1]);
16110 varp = &argvars[2];
16111
16112 if (tp != NULL && varname != NULL && varp != NULL)
16113 {
16114 save_curtab = curtab;
16115 goto_tabpage_tp(tp);
16116
16117 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16118 if (tabvarname != NULL)
16119 {
16120 STRCPY(tabvarname, "t:");
16121 STRCPY(tabvarname + 2, varname);
16122 set_var(tabvarname, varp, TRUE);
16123 vim_free(tabvarname);
16124 }
16125
16126 /* Restore current tabpage */
16127 if (valid_tabpage(save_curtab))
16128 goto_tabpage_tp(save_curtab);
16129 }
16130}
16131
16132/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016133 * "settabwinvar()" function
16134 */
16135 static void
16136f_settabwinvar(argvars, rettv)
16137 typval_T *argvars;
16138 typval_T *rettv;
16139{
16140 setwinvar(argvars, rettv, 1);
16141}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142
16143/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016144 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016147f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016148 typval_T *argvars;
16149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016150{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016151 setwinvar(argvars, rettv, 0);
16152}
16153
16154/*
16155 * "setwinvar()" and "settabwinvar()" functions
16156 */
16157 static void
16158setwinvar(argvars, rettv, off)
16159 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016160 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016161 int off;
16162{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163 win_T *win;
16164#ifdef FEAT_WINDOWS
16165 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016166 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167#endif
16168 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016169 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016171 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172
16173 if (check_restricted() || check_secure())
16174 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016175
16176#ifdef FEAT_WINDOWS
16177 if (off == 1)
16178 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16179 else
16180 tp = curtab;
16181#endif
16182 win = find_win_by_nr(&argvars[off], tp);
16183 varname = get_tv_string_chk(&argvars[off + 1]);
16184 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185
16186 if (win != NULL && varname != NULL && varp != NULL)
16187 {
16188#ifdef FEAT_WINDOWS
16189 /* set curwin to be our win, temporarily */
16190 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016191 save_curtab = curtab;
16192 goto_tabpage_tp(tp);
16193 if (!win_valid(win))
16194 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195 curwin = win;
16196 curbuf = curwin->w_buffer;
16197#endif
16198
16199 if (*varname == '&')
16200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016201 long numval;
16202 char_u *strval;
16203 int error = FALSE;
16204
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016206 numval = get_tv_number_chk(varp, &error);
16207 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016208 if (!error && strval != NULL)
16209 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 }
16211 else
16212 {
16213 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16214 if (winvarname != NULL)
16215 {
16216 STRCPY(winvarname, "w:");
16217 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016218 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016219 vim_free(winvarname);
16220 }
16221 }
16222
16223#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016224 /* Restore current tabpage and window, if still valid (autocomands can
16225 * make them invalid). */
16226 if (valid_tabpage(save_curtab))
16227 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228 if (win_valid(save_curwin))
16229 {
16230 curwin = save_curwin;
16231 curbuf = curwin->w_buffer;
16232 }
16233#endif
16234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016235}
16236
16237/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016238 * "shellescape({string})" function
16239 */
16240 static void
16241f_shellescape(argvars, rettv)
16242 typval_T *argvars;
16243 typval_T *rettv;
16244{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016245 rettv->vval.v_string = vim_strsave_shellescape(
16246 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016247 rettv->v_type = VAR_STRING;
16248}
16249
16250/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016251 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252 */
16253 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016254f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016255 typval_T *argvars;
16256 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016257{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016258 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259
Bram Moolenaar0d660222005-01-07 21:51:51 +000016260 p = get_tv_string(&argvars[0]);
16261 rettv->vval.v_string = vim_strsave(p);
16262 simplify_filename(rettv->vval.v_string); /* simplify in place */
16263 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264}
16265
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016266#ifdef FEAT_FLOAT
16267/*
16268 * "sin()" function
16269 */
16270 static void
16271f_sin(argvars, rettv)
16272 typval_T *argvars;
16273 typval_T *rettv;
16274{
16275 float_T f;
16276
16277 rettv->v_type = VAR_FLOAT;
16278 if (get_float_arg(argvars, &f) == OK)
16279 rettv->vval.v_float = sin(f);
16280 else
16281 rettv->vval.v_float = 0.0;
16282}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016283
16284/*
16285 * "sinh()" function
16286 */
16287 static void
16288f_sinh(argvars, rettv)
16289 typval_T *argvars;
16290 typval_T *rettv;
16291{
16292 float_T f;
16293
16294 rettv->v_type = VAR_FLOAT;
16295 if (get_float_arg(argvars, &f) == OK)
16296 rettv->vval.v_float = sinh(f);
16297 else
16298 rettv->vval.v_float = 0.0;
16299}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016300#endif
16301
Bram Moolenaar0d660222005-01-07 21:51:51 +000016302static int
16303#ifdef __BORLANDC__
16304 _RTLENTRYF
16305#endif
16306 item_compare __ARGS((const void *s1, const void *s2));
16307static int
16308#ifdef __BORLANDC__
16309 _RTLENTRYF
16310#endif
16311 item_compare2 __ARGS((const void *s1, const void *s2));
16312
16313static int item_compare_ic;
16314static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016315static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016316#define ITEM_COMPARE_FAIL 999
16317
Bram Moolenaar071d4272004-06-13 20:20:40 +000016318/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016319 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016321 static int
16322#ifdef __BORLANDC__
16323_RTLENTRYF
16324#endif
16325item_compare(s1, s2)
16326 const void *s1;
16327 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016328{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016329 char_u *p1, *p2;
16330 char_u *tofree1, *tofree2;
16331 int res;
16332 char_u numbuf1[NUMBUFLEN];
16333 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016335 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16336 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016337 if (p1 == NULL)
16338 p1 = (char_u *)"";
16339 if (p2 == NULL)
16340 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016341 if (item_compare_ic)
16342 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016343 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016344 res = STRCMP(p1, p2);
16345 vim_free(tofree1);
16346 vim_free(tofree2);
16347 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348}
16349
16350 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016351#ifdef __BORLANDC__
16352_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016353#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016354item_compare2(s1, s2)
16355 const void *s1;
16356 const void *s2;
16357{
16358 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016359 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016360 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016361 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016363 /* shortcut after failure in previous call; compare all items equal */
16364 if (item_compare_func_err)
16365 return 0;
16366
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016367 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16368 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016369 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16370 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016371
16372 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016373 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016374 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016375 clear_tv(&argv[0]);
16376 clear_tv(&argv[1]);
16377
16378 if (res == FAIL)
16379 res = ITEM_COMPARE_FAIL;
16380 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016381 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16382 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016383 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016384 clear_tv(&rettv);
16385 return res;
16386}
16387
16388/*
16389 * "sort({list})" function
16390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016392f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016393 typval_T *argvars;
16394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016395{
Bram Moolenaar33570922005-01-25 22:26:29 +000016396 list_T *l;
16397 listitem_T *li;
16398 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016399 long len;
16400 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016401
Bram Moolenaar0d660222005-01-07 21:51:51 +000016402 if (argvars[0].v_type != VAR_LIST)
16403 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404 else
16405 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016406 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016407 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016408 return;
16409 rettv->vval.v_list = l;
16410 rettv->v_type = VAR_LIST;
16411 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016412
Bram Moolenaar0d660222005-01-07 21:51:51 +000016413 len = list_len(l);
16414 if (len <= 1)
16415 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416
Bram Moolenaar0d660222005-01-07 21:51:51 +000016417 item_compare_ic = FALSE;
16418 item_compare_func = NULL;
16419 if (argvars[1].v_type != VAR_UNKNOWN)
16420 {
16421 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016422 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016423 else
16424 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016425 int error = FALSE;
16426
16427 i = get_tv_number_chk(&argvars[1], &error);
16428 if (error)
16429 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016430 if (i == 1)
16431 item_compare_ic = TRUE;
16432 else
16433 item_compare_func = get_tv_string(&argvars[1]);
16434 }
16435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436
Bram Moolenaar0d660222005-01-07 21:51:51 +000016437 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016438 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016439 if (ptrs == NULL)
16440 return;
16441 i = 0;
16442 for (li = l->lv_first; li != NULL; li = li->li_next)
16443 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016445 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016446 /* test the compare function */
16447 if (item_compare_func != NULL
16448 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16449 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016450 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016452 {
16453 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016454 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016455 item_compare_func == NULL ? item_compare : item_compare2);
16456
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016457 if (!item_compare_func_err)
16458 {
16459 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016460 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016461 l->lv_len = 0;
16462 for (i = 0; i < len; ++i)
16463 list_append(l, ptrs[i]);
16464 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016465 }
16466
16467 vim_free(ptrs);
16468 }
16469}
16470
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016471/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016472 * "soundfold({word})" function
16473 */
16474 static void
16475f_soundfold(argvars, rettv)
16476 typval_T *argvars;
16477 typval_T *rettv;
16478{
16479 char_u *s;
16480
16481 rettv->v_type = VAR_STRING;
16482 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016483#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016484 rettv->vval.v_string = eval_soundfold(s);
16485#else
16486 rettv->vval.v_string = vim_strsave(s);
16487#endif
16488}
16489
16490/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016491 * "spellbadword()" function
16492 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016493 static void
16494f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016495 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016496 typval_T *rettv;
16497{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016498 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016499 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016500 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016501
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016502 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016503 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016504
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016505#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016506 if (argvars[0].v_type == VAR_UNKNOWN)
16507 {
16508 /* Find the start and length of the badly spelled word. */
16509 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16510 if (len != 0)
16511 word = ml_get_cursor();
16512 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016513 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016514 {
16515 char_u *str = get_tv_string_chk(&argvars[0]);
16516 int capcol = -1;
16517
16518 if (str != NULL)
16519 {
16520 /* Check the argument for spelling. */
16521 while (*str != NUL)
16522 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016523 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016524 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016525 {
16526 word = str;
16527 break;
16528 }
16529 str += len;
16530 }
16531 }
16532 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016533#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016534
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016535 list_append_string(rettv->vval.v_list, word, len);
16536 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016537 attr == HLF_SPB ? "bad" :
16538 attr == HLF_SPR ? "rare" :
16539 attr == HLF_SPL ? "local" :
16540 attr == HLF_SPC ? "caps" :
16541 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016542}
16543
16544/*
16545 * "spellsuggest()" function
16546 */
16547 static void
16548f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016549 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016550 typval_T *rettv;
16551{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016552#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016553 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016554 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016555 int maxcount;
16556 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016557 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016558 listitem_T *li;
16559 int need_capital = FALSE;
16560#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016561
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016562 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016563 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016564
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016565#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016566 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016567 {
16568 str = get_tv_string(&argvars[0]);
16569 if (argvars[1].v_type != VAR_UNKNOWN)
16570 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016571 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016572 if (maxcount <= 0)
16573 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016574 if (argvars[2].v_type != VAR_UNKNOWN)
16575 {
16576 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16577 if (typeerr)
16578 return;
16579 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016580 }
16581 else
16582 maxcount = 25;
16583
Bram Moolenaar4770d092006-01-12 23:22:24 +000016584 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016585
16586 for (i = 0; i < ga.ga_len; ++i)
16587 {
16588 str = ((char_u **)ga.ga_data)[i];
16589
16590 li = listitem_alloc();
16591 if (li == NULL)
16592 vim_free(str);
16593 else
16594 {
16595 li->li_tv.v_type = VAR_STRING;
16596 li->li_tv.v_lock = 0;
16597 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016598 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016599 }
16600 }
16601 ga_clear(&ga);
16602 }
16603#endif
16604}
16605
Bram Moolenaar0d660222005-01-07 21:51:51 +000016606 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016607f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016608 typval_T *argvars;
16609 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016610{
16611 char_u *str;
16612 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016613 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016614 regmatch_T regmatch;
16615 char_u patbuf[NUMBUFLEN];
16616 char_u *save_cpo;
16617 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016618 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016619 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016620 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016621
16622 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16623 save_cpo = p_cpo;
16624 p_cpo = (char_u *)"";
16625
16626 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016627 if (argvars[1].v_type != VAR_UNKNOWN)
16628 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016629 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16630 if (pat == NULL)
16631 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016632 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016633 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016634 }
16635 if (pat == NULL || *pat == NUL)
16636 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016637
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016638 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016640 if (typeerr)
16641 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016642
Bram Moolenaar0d660222005-01-07 21:51:51 +000016643 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16644 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016645 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016646 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016647 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016648 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016649 if (*str == NUL)
16650 match = FALSE; /* empty item at the end */
16651 else
16652 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016653 if (match)
16654 end = regmatch.startp[0];
16655 else
16656 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016657 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16658 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016659 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016660 if (list_append_string(rettv->vval.v_list, str,
16661 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016662 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016663 }
16664 if (!match)
16665 break;
16666 /* Advance to just after the match. */
16667 if (regmatch.endp[0] > str)
16668 col = 0;
16669 else
16670 {
16671 /* Don't get stuck at the same match. */
16672#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016673 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016674#else
16675 col = 1;
16676#endif
16677 }
16678 str = regmatch.endp[0];
16679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680
Bram Moolenaar0d660222005-01-07 21:51:51 +000016681 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016683
Bram Moolenaar0d660222005-01-07 21:51:51 +000016684 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016685}
16686
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016687#ifdef FEAT_FLOAT
16688/*
16689 * "sqrt()" function
16690 */
16691 static void
16692f_sqrt(argvars, rettv)
16693 typval_T *argvars;
16694 typval_T *rettv;
16695{
16696 float_T f;
16697
16698 rettv->v_type = VAR_FLOAT;
16699 if (get_float_arg(argvars, &f) == OK)
16700 rettv->vval.v_float = sqrt(f);
16701 else
16702 rettv->vval.v_float = 0.0;
16703}
16704
16705/*
16706 * "str2float()" function
16707 */
16708 static void
16709f_str2float(argvars, rettv)
16710 typval_T *argvars;
16711 typval_T *rettv;
16712{
16713 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16714
16715 if (*p == '+')
16716 p = skipwhite(p + 1);
16717 (void)string2float(p, &rettv->vval.v_float);
16718 rettv->v_type = VAR_FLOAT;
16719}
16720#endif
16721
Bram Moolenaar2c932302006-03-18 21:42:09 +000016722/*
16723 * "str2nr()" function
16724 */
16725 static void
16726f_str2nr(argvars, rettv)
16727 typval_T *argvars;
16728 typval_T *rettv;
16729{
16730 int base = 10;
16731 char_u *p;
16732 long n;
16733
16734 if (argvars[1].v_type != VAR_UNKNOWN)
16735 {
16736 base = get_tv_number(&argvars[1]);
16737 if (base != 8 && base != 10 && base != 16)
16738 {
16739 EMSG(_(e_invarg));
16740 return;
16741 }
16742 }
16743
16744 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016745 if (*p == '+')
16746 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016747 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16748 rettv->vval.v_number = n;
16749}
16750
Bram Moolenaar071d4272004-06-13 20:20:40 +000016751#ifdef HAVE_STRFTIME
16752/*
16753 * "strftime({format}[, {time}])" function
16754 */
16755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016756f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016757 typval_T *argvars;
16758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759{
16760 char_u result_buf[256];
16761 struct tm *curtime;
16762 time_t seconds;
16763 char_u *p;
16764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016765 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016767 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016768 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016769 seconds = time(NULL);
16770 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016771 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772 curtime = localtime(&seconds);
16773 /* MSVC returns NULL for an invalid value of seconds. */
16774 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016775 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016776 else
16777 {
16778# ifdef FEAT_MBYTE
16779 vimconv_T conv;
16780 char_u *enc;
16781
16782 conv.vc_type = CONV_NONE;
16783 enc = enc_locale();
16784 convert_setup(&conv, p_enc, enc);
16785 if (conv.vc_type != CONV_NONE)
16786 p = string_convert(&conv, p, NULL);
16787# endif
16788 if (p != NULL)
16789 (void)strftime((char *)result_buf, sizeof(result_buf),
16790 (char *)p, curtime);
16791 else
16792 result_buf[0] = NUL;
16793
16794# ifdef FEAT_MBYTE
16795 if (conv.vc_type != CONV_NONE)
16796 vim_free(p);
16797 convert_setup(&conv, enc, p_enc);
16798 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016799 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800 else
16801# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016802 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803
16804# ifdef FEAT_MBYTE
16805 /* Release conversion descriptors */
16806 convert_setup(&conv, NULL, NULL);
16807 vim_free(enc);
16808# endif
16809 }
16810}
16811#endif
16812
16813/*
16814 * "stridx()" function
16815 */
16816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016817f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016818 typval_T *argvars;
16819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820{
16821 char_u buf[NUMBUFLEN];
16822 char_u *needle;
16823 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016824 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016826 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016828 needle = get_tv_string_chk(&argvars[1]);
16829 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016830 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016831 if (needle == NULL || haystack == NULL)
16832 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016833
Bram Moolenaar33570922005-01-25 22:26:29 +000016834 if (argvars[2].v_type != VAR_UNKNOWN)
16835 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016836 int error = FALSE;
16837
16838 start_idx = get_tv_number_chk(&argvars[2], &error);
16839 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016840 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016841 if (start_idx >= 0)
16842 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016843 }
16844
16845 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16846 if (pos != NULL)
16847 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848}
16849
16850/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016851 * "string()" function
16852 */
16853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016854f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016855 typval_T *argvars;
16856 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016857{
16858 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016859 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016861 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016862 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016863 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016864 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016865 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866}
16867
16868/*
16869 * "strlen()" function
16870 */
16871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016872f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016873 typval_T *argvars;
16874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016876 rettv->vval.v_number = (varnumber_T)(STRLEN(
16877 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878}
16879
16880/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016881 * "strchars()" function
16882 */
16883 static void
16884f_strchars(argvars, rettv)
16885 typval_T *argvars;
16886 typval_T *rettv;
16887{
16888 char_u *s = get_tv_string(&argvars[0]);
16889#ifdef FEAT_MBYTE
16890 varnumber_T len = 0;
16891
16892 while (*s != NUL)
16893 {
16894 mb_cptr2char_adv(&s);
16895 ++len;
16896 }
16897 rettv->vval.v_number = len;
16898#else
16899 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16900#endif
16901}
16902
16903/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016904 * "strdisplaywidth()" function
16905 */
16906 static void
16907f_strdisplaywidth(argvars, rettv)
16908 typval_T *argvars;
16909 typval_T *rettv;
16910{
16911 char_u *s = get_tv_string(&argvars[0]);
16912 int col = 0;
16913
16914 if (argvars[1].v_type != VAR_UNKNOWN)
16915 col = get_tv_number(&argvars[1]);
16916
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016917 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016918}
16919
16920/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016921 * "strwidth()" function
16922 */
16923 static void
16924f_strwidth(argvars, rettv)
16925 typval_T *argvars;
16926 typval_T *rettv;
16927{
16928 char_u *s = get_tv_string(&argvars[0]);
16929
16930 rettv->vval.v_number = (varnumber_T)(
16931#ifdef FEAT_MBYTE
16932 mb_string2cells(s, -1)
16933#else
16934 STRLEN(s)
16935#endif
16936 );
16937}
16938
16939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940 * "strpart()" function
16941 */
16942 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016943f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016944 typval_T *argvars;
16945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946{
16947 char_u *p;
16948 int n;
16949 int len;
16950 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016951 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016953 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954 slen = (int)STRLEN(p);
16955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016956 n = get_tv_number_chk(&argvars[1], &error);
16957 if (error)
16958 len = 0;
16959 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016960 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961 else
16962 len = slen - n; /* default len: all bytes that are available. */
16963
16964 /*
16965 * Only return the overlap between the specified part and the actual
16966 * string.
16967 */
16968 if (n < 0)
16969 {
16970 len += n;
16971 n = 0;
16972 }
16973 else if (n > slen)
16974 n = slen;
16975 if (len < 0)
16976 len = 0;
16977 else if (n + len > slen)
16978 len = slen - n;
16979
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016980 rettv->v_type = VAR_STRING;
16981 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982}
16983
16984/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016985 * "strridx()" function
16986 */
16987 static void
16988f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016989 typval_T *argvars;
16990 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016991{
16992 char_u buf[NUMBUFLEN];
16993 char_u *needle;
16994 char_u *haystack;
16995 char_u *rest;
16996 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016997 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016998
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016999 needle = get_tv_string_chk(&argvars[1]);
17000 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017001
17002 rettv->vval.v_number = -1;
17003 if (needle == NULL || haystack == NULL)
17004 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017005
17006 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017007 if (argvars[2].v_type != VAR_UNKNOWN)
17008 {
17009 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017010 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017011 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017012 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017013 }
17014 else
17015 end_idx = haystack_len;
17016
Bram Moolenaar0d660222005-01-07 21:51:51 +000017017 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017018 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017019 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017020 lastmatch = haystack + end_idx;
17021 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017022 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017023 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017024 for (rest = haystack; *rest != '\0'; ++rest)
17025 {
17026 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017027 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017028 break;
17029 lastmatch = rest;
17030 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017031 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017032
17033 if (lastmatch == NULL)
17034 rettv->vval.v_number = -1;
17035 else
17036 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17037}
17038
17039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040 * "strtrans()" function
17041 */
17042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017043f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017044 typval_T *argvars;
17045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017046{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017047 rettv->v_type = VAR_STRING;
17048 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017049}
17050
17051/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017052 * "submatch()" function
17053 */
17054 static void
17055f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017056 typval_T *argvars;
17057 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017058{
17059 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017060 rettv->vval.v_string =
17061 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017062}
17063
17064/*
17065 * "substitute()" function
17066 */
17067 static void
17068f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017069 typval_T *argvars;
17070 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017071{
17072 char_u patbuf[NUMBUFLEN];
17073 char_u subbuf[NUMBUFLEN];
17074 char_u flagsbuf[NUMBUFLEN];
17075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017076 char_u *str = get_tv_string_chk(&argvars[0]);
17077 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17078 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17079 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17080
Bram Moolenaar0d660222005-01-07 21:51:51 +000017081 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017082 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17083 rettv->vval.v_string = NULL;
17084 else
17085 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017086}
17087
17088/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017089 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017092f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017093 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095{
17096 int id = 0;
17097#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017098 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017099 long col;
17100 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017101 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017103 lnum = get_tv_lnum(argvars); /* -1 on type error */
17104 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17105 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017107 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017108 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017109 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110#endif
17111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017112 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017113}
17114
17115/*
17116 * "synIDattr(id, what [, mode])" function
17117 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017119f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017120 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122{
17123 char_u *p = NULL;
17124#ifdef FEAT_SYN_HL
17125 int id;
17126 char_u *what;
17127 char_u *mode;
17128 char_u modebuf[NUMBUFLEN];
17129 int modec;
17130
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017131 id = get_tv_number(&argvars[0]);
17132 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017133 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017135 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017136 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017137 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017138 modec = 0; /* replace invalid with current */
17139 }
17140 else
17141 {
17142#ifdef FEAT_GUI
17143 if (gui.in_use)
17144 modec = 'g';
17145 else
17146#endif
17147 if (t_colors > 1)
17148 modec = 'c';
17149 else
17150 modec = 't';
17151 }
17152
17153
17154 switch (TOLOWER_ASC(what[0]))
17155 {
17156 case 'b':
17157 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17158 p = highlight_color(id, what, modec);
17159 else /* bold */
17160 p = highlight_has_attr(id, HL_BOLD, modec);
17161 break;
17162
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017163 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164 p = highlight_color(id, what, modec);
17165 break;
17166
17167 case 'i':
17168 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17169 p = highlight_has_attr(id, HL_INVERSE, modec);
17170 else /* italic */
17171 p = highlight_has_attr(id, HL_ITALIC, modec);
17172 break;
17173
17174 case 'n': /* name */
17175 p = get_highlight_name(NULL, id - 1);
17176 break;
17177
17178 case 'r': /* reverse */
17179 p = highlight_has_attr(id, HL_INVERSE, modec);
17180 break;
17181
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017182 case 's':
17183 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17184 p = highlight_color(id, what, modec);
17185 else /* standout */
17186 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 break;
17188
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017189 case 'u':
17190 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17191 /* underline */
17192 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17193 else
17194 /* undercurl */
17195 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196 break;
17197 }
17198
17199 if (p != NULL)
17200 p = vim_strsave(p);
17201#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017202 rettv->v_type = VAR_STRING;
17203 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204}
17205
17206/*
17207 * "synIDtrans(id)" function
17208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017210f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017211 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213{
17214 int id;
17215
17216#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017217 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218
17219 if (id > 0)
17220 id = syn_get_final_id(id);
17221 else
17222#endif
17223 id = 0;
17224
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017225 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226}
17227
17228/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017229 * "synconcealed(lnum, col)" function
17230 */
17231 static void
17232f_synconcealed(argvars, rettv)
17233 typval_T *argvars UNUSED;
17234 typval_T *rettv;
17235{
17236#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17237 long lnum;
17238 long col;
17239 int syntax_flags = 0;
17240 int cchar;
17241 int matchid = 0;
17242 char_u str[NUMBUFLEN];
17243#endif
17244
17245 rettv->v_type = VAR_LIST;
17246 rettv->vval.v_list = NULL;
17247
17248#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17249 lnum = get_tv_lnum(argvars); /* -1 on type error */
17250 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17251
17252 vim_memset(str, NUL, sizeof(str));
17253
17254 if (rettv_list_alloc(rettv) != FAIL)
17255 {
17256 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17257 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17258 && curwin->w_p_cole > 0)
17259 {
17260 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17261 syntax_flags = get_syntax_info(&matchid);
17262
17263 /* get the conceal character */
17264 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17265 {
17266 cchar = syn_get_sub_char();
17267 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17268 cchar = lcs_conceal;
17269 if (cchar != NUL)
17270 {
17271# ifdef FEAT_MBYTE
17272 if (has_mbyte)
17273 (*mb_char2bytes)(cchar, str);
17274 else
17275# endif
17276 str[0] = cchar;
17277 }
17278 }
17279 }
17280
17281 list_append_number(rettv->vval.v_list,
17282 (syntax_flags & HL_CONCEAL) != 0);
17283 /* -1 to auto-determine strlen */
17284 list_append_string(rettv->vval.v_list, str, -1);
17285 list_append_number(rettv->vval.v_list, matchid);
17286 }
17287#endif
17288}
17289
17290/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017291 * "synstack(lnum, col)" function
17292 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017293 static void
17294f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017295 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017296 typval_T *rettv;
17297{
17298#ifdef FEAT_SYN_HL
17299 long lnum;
17300 long col;
17301 int i;
17302 int id;
17303#endif
17304
17305 rettv->v_type = VAR_LIST;
17306 rettv->vval.v_list = NULL;
17307
17308#ifdef FEAT_SYN_HL
17309 lnum = get_tv_lnum(argvars); /* -1 on type error */
17310 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17311
17312 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017313 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017314 && rettv_list_alloc(rettv) != FAIL)
17315 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017316 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017317 for (i = 0; ; ++i)
17318 {
17319 id = syn_get_stack_item(i);
17320 if (id < 0)
17321 break;
17322 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17323 break;
17324 }
17325 }
17326#endif
17327}
17328
17329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330 * "system()" function
17331 */
17332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017333f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017334 typval_T *argvars;
17335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017337 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017339 char_u *infile = NULL;
17340 char_u buf[NUMBUFLEN];
17341 int err = FALSE;
17342 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017343
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017344 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017345 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017346
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017347 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017348 {
17349 /*
17350 * Write the string to a temp file, to be used for input of the shell
17351 * command.
17352 */
17353 if ((infile = vim_tempname('i')) == NULL)
17354 {
17355 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017356 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017357 }
17358
17359 fd = mch_fopen((char *)infile, WRITEBIN);
17360 if (fd == NULL)
17361 {
17362 EMSG2(_(e_notopen), infile);
17363 goto done;
17364 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017365 p = get_tv_string_buf_chk(&argvars[1], buf);
17366 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017367 {
17368 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017369 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017370 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017371 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17372 err = TRUE;
17373 if (fclose(fd) != 0)
17374 err = TRUE;
17375 if (err)
17376 {
17377 EMSG(_("E677: Error writing temp file"));
17378 goto done;
17379 }
17380 }
17381
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017382 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17383 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017384
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385#ifdef USE_CR
17386 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017387 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388 {
17389 char_u *s;
17390
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017391 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392 {
17393 if (*s == CAR)
17394 *s = NL;
17395 }
17396 }
17397#else
17398# ifdef USE_CRNL
17399 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017400 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 {
17402 char_u *s, *d;
17403
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017404 d = res;
17405 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406 {
17407 if (s[0] == CAR && s[1] == NL)
17408 ++s;
17409 *d++ = *s;
17410 }
17411 *d = NUL;
17412 }
17413# endif
17414#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017415
17416done:
17417 if (infile != NULL)
17418 {
17419 mch_remove(infile);
17420 vim_free(infile);
17421 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017422 rettv->v_type = VAR_STRING;
17423 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424}
17425
17426/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017427 * "tabpagebuflist()" function
17428 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017429 static void
17430f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017431 typval_T *argvars UNUSED;
17432 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017433{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017434#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017435 tabpage_T *tp;
17436 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017437
17438 if (argvars[0].v_type == VAR_UNKNOWN)
17439 wp = firstwin;
17440 else
17441 {
17442 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17443 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017444 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017445 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017446 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017447 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017448 for (; wp != NULL; wp = wp->w_next)
17449 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017450 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017451 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017452 }
17453#endif
17454}
17455
17456
17457/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017458 * "tabpagenr()" function
17459 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017460 static void
17461f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017462 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017463 typval_T *rettv;
17464{
17465 int nr = 1;
17466#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017467 char_u *arg;
17468
17469 if (argvars[0].v_type != VAR_UNKNOWN)
17470 {
17471 arg = get_tv_string_chk(&argvars[0]);
17472 nr = 0;
17473 if (arg != NULL)
17474 {
17475 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017476 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017477 else
17478 EMSG2(_(e_invexpr2), arg);
17479 }
17480 }
17481 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017482 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017483#endif
17484 rettv->vval.v_number = nr;
17485}
17486
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017487
17488#ifdef FEAT_WINDOWS
17489static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17490
17491/*
17492 * Common code for tabpagewinnr() and winnr().
17493 */
17494 static int
17495get_winnr(tp, argvar)
17496 tabpage_T *tp;
17497 typval_T *argvar;
17498{
17499 win_T *twin;
17500 int nr = 1;
17501 win_T *wp;
17502 char_u *arg;
17503
17504 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17505 if (argvar->v_type != VAR_UNKNOWN)
17506 {
17507 arg = get_tv_string_chk(argvar);
17508 if (arg == NULL)
17509 nr = 0; /* type error; errmsg already given */
17510 else if (STRCMP(arg, "$") == 0)
17511 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17512 else if (STRCMP(arg, "#") == 0)
17513 {
17514 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17515 if (twin == NULL)
17516 nr = 0;
17517 }
17518 else
17519 {
17520 EMSG2(_(e_invexpr2), arg);
17521 nr = 0;
17522 }
17523 }
17524
17525 if (nr > 0)
17526 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17527 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017528 {
17529 if (wp == NULL)
17530 {
17531 /* didn't find it in this tabpage */
17532 nr = 0;
17533 break;
17534 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017535 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017536 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017537 return nr;
17538}
17539#endif
17540
17541/*
17542 * "tabpagewinnr()" function
17543 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017544 static void
17545f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017546 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017547 typval_T *rettv;
17548{
17549 int nr = 1;
17550#ifdef FEAT_WINDOWS
17551 tabpage_T *tp;
17552
17553 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17554 if (tp == NULL)
17555 nr = 0;
17556 else
17557 nr = get_winnr(tp, &argvars[1]);
17558#endif
17559 rettv->vval.v_number = nr;
17560}
17561
17562
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017563/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017564 * "tagfiles()" function
17565 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017566 static void
17567f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017568 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017569 typval_T *rettv;
17570{
17571 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017572 tagname_T tn;
17573 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017574
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017575 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017576 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017577
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017578 for (first = TRUE; ; first = FALSE)
17579 if (get_tagfname(&tn, first, fname) == FAIL
17580 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017581 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017582 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017583}
17584
17585/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017586 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017587 */
17588 static void
17589f_taglist(argvars, rettv)
17590 typval_T *argvars;
17591 typval_T *rettv;
17592{
17593 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017594
17595 tag_pattern = get_tv_string(&argvars[0]);
17596
17597 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017598 if (*tag_pattern == NUL)
17599 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017600
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017601 if (rettv_list_alloc(rettv) == OK)
17602 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017603}
17604
17605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606 * "tempname()" function
17607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017609f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017610 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612{
17613 static int x = 'A';
17614
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017615 rettv->v_type = VAR_STRING;
17616 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617
17618 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17619 * names. Skip 'I' and 'O', they are used for shell redirection. */
17620 do
17621 {
17622 if (x == 'Z')
17623 x = '0';
17624 else if (x == '9')
17625 x = 'A';
17626 else
17627 {
17628#ifdef EBCDIC
17629 if (x == 'I')
17630 x = 'J';
17631 else if (x == 'R')
17632 x = 'S';
17633 else
17634#endif
17635 ++x;
17636 }
17637 } while (x == 'I' || x == 'O');
17638}
17639
17640/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017641 * "test(list)" function: Just checking the walls...
17642 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017643 static void
17644f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017645 typval_T *argvars UNUSED;
17646 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017647{
17648 /* Used for unit testing. Change the code below to your liking. */
17649#if 0
17650 listitem_T *li;
17651 list_T *l;
17652 char_u *bad, *good;
17653
17654 if (argvars[0].v_type != VAR_LIST)
17655 return;
17656 l = argvars[0].vval.v_list;
17657 if (l == NULL)
17658 return;
17659 li = l->lv_first;
17660 if (li == NULL)
17661 return;
17662 bad = get_tv_string(&li->li_tv);
17663 li = li->li_next;
17664 if (li == NULL)
17665 return;
17666 good = get_tv_string(&li->li_tv);
17667 rettv->vval.v_number = test_edit_score(bad, good);
17668#endif
17669}
17670
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017671#ifdef FEAT_FLOAT
17672/*
17673 * "tan()" function
17674 */
17675 static void
17676f_tan(argvars, rettv)
17677 typval_T *argvars;
17678 typval_T *rettv;
17679{
17680 float_T f;
17681
17682 rettv->v_type = VAR_FLOAT;
17683 if (get_float_arg(argvars, &f) == OK)
17684 rettv->vval.v_float = tan(f);
17685 else
17686 rettv->vval.v_float = 0.0;
17687}
17688
17689/*
17690 * "tanh()" function
17691 */
17692 static void
17693f_tanh(argvars, rettv)
17694 typval_T *argvars;
17695 typval_T *rettv;
17696{
17697 float_T f;
17698
17699 rettv->v_type = VAR_FLOAT;
17700 if (get_float_arg(argvars, &f) == OK)
17701 rettv->vval.v_float = tanh(f);
17702 else
17703 rettv->vval.v_float = 0.0;
17704}
17705#endif
17706
Bram Moolenaard52d9742005-08-21 22:20:28 +000017707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708 * "tolower(string)" function
17709 */
17710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017711f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017712 typval_T *argvars;
17713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714{
17715 char_u *p;
17716
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017717 p = vim_strsave(get_tv_string(&argvars[0]));
17718 rettv->v_type = VAR_STRING;
17719 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720
17721 if (p != NULL)
17722 while (*p != NUL)
17723 {
17724#ifdef FEAT_MBYTE
17725 int l;
17726
17727 if (enc_utf8)
17728 {
17729 int c, lc;
17730
17731 c = utf_ptr2char(p);
17732 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017733 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734 /* TODO: reallocate string when byte count changes. */
17735 if (utf_char2len(lc) == l)
17736 utf_char2bytes(lc, p);
17737 p += l;
17738 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017739 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740 p += l; /* skip multi-byte character */
17741 else
17742#endif
17743 {
17744 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17745 ++p;
17746 }
17747 }
17748}
17749
17750/*
17751 * "toupper(string)" function
17752 */
17753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017754f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017755 typval_T *argvars;
17756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017758 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017759 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760}
17761
17762/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017763 * "tr(string, fromstr, tostr)" function
17764 */
17765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017766f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017767 typval_T *argvars;
17768 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017769{
17770 char_u *instr;
17771 char_u *fromstr;
17772 char_u *tostr;
17773 char_u *p;
17774#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017775 int inlen;
17776 int fromlen;
17777 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017778 int idx;
17779 char_u *cpstr;
17780 int cplen;
17781 int first = TRUE;
17782#endif
17783 char_u buf[NUMBUFLEN];
17784 char_u buf2[NUMBUFLEN];
17785 garray_T ga;
17786
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017787 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017788 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17789 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017790
17791 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017792 rettv->v_type = VAR_STRING;
17793 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017794 if (fromstr == NULL || tostr == NULL)
17795 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017796 ga_init2(&ga, (int)sizeof(char), 80);
17797
17798#ifdef FEAT_MBYTE
17799 if (!has_mbyte)
17800#endif
17801 /* not multi-byte: fromstr and tostr must be the same length */
17802 if (STRLEN(fromstr) != STRLEN(tostr))
17803 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017804#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017805error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017806#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017807 EMSG2(_(e_invarg2), fromstr);
17808 ga_clear(&ga);
17809 return;
17810 }
17811
17812 /* fromstr and tostr have to contain the same number of chars */
17813 while (*instr != NUL)
17814 {
17815#ifdef FEAT_MBYTE
17816 if (has_mbyte)
17817 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017818 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017819 cpstr = instr;
17820 cplen = inlen;
17821 idx = 0;
17822 for (p = fromstr; *p != NUL; p += fromlen)
17823 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017824 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017825 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17826 {
17827 for (p = tostr; *p != NUL; p += tolen)
17828 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017829 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017830 if (idx-- == 0)
17831 {
17832 cplen = tolen;
17833 cpstr = p;
17834 break;
17835 }
17836 }
17837 if (*p == NUL) /* tostr is shorter than fromstr */
17838 goto error;
17839 break;
17840 }
17841 ++idx;
17842 }
17843
17844 if (first && cpstr == instr)
17845 {
17846 /* Check that fromstr and tostr have the same number of
17847 * (multi-byte) characters. Done only once when a character
17848 * of instr doesn't appear in fromstr. */
17849 first = FALSE;
17850 for (p = tostr; *p != NUL; p += tolen)
17851 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017852 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017853 --idx;
17854 }
17855 if (idx != 0)
17856 goto error;
17857 }
17858
17859 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017860 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017861 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017862
17863 instr += inlen;
17864 }
17865 else
17866#endif
17867 {
17868 /* When not using multi-byte chars we can do it faster. */
17869 p = vim_strchr(fromstr, *instr);
17870 if (p != NULL)
17871 ga_append(&ga, tostr[p - fromstr]);
17872 else
17873 ga_append(&ga, *instr);
17874 ++instr;
17875 }
17876 }
17877
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017878 /* add a terminating NUL */
17879 ga_grow(&ga, 1);
17880 ga_append(&ga, NUL);
17881
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017882 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017883}
17884
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017885#ifdef FEAT_FLOAT
17886/*
17887 * "trunc({float})" function
17888 */
17889 static void
17890f_trunc(argvars, rettv)
17891 typval_T *argvars;
17892 typval_T *rettv;
17893{
17894 float_T f;
17895
17896 rettv->v_type = VAR_FLOAT;
17897 if (get_float_arg(argvars, &f) == OK)
17898 /* trunc() is not in C90, use floor() or ceil() instead. */
17899 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17900 else
17901 rettv->vval.v_float = 0.0;
17902}
17903#endif
17904
Bram Moolenaar8299df92004-07-10 09:47:34 +000017905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017906 * "type(expr)" function
17907 */
17908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017909f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017910 typval_T *argvars;
17911 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017913 int n;
17914
17915 switch (argvars[0].v_type)
17916 {
17917 case VAR_NUMBER: n = 0; break;
17918 case VAR_STRING: n = 1; break;
17919 case VAR_FUNC: n = 2; break;
17920 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017921 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017922#ifdef FEAT_FLOAT
17923 case VAR_FLOAT: n = 5; break;
17924#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017925 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17926 }
17927 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928}
17929
17930/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017931 * "undofile(name)" function
17932 */
17933 static void
17934f_undofile(argvars, rettv)
17935 typval_T *argvars;
17936 typval_T *rettv;
17937{
17938 rettv->v_type = VAR_STRING;
17939#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017940 {
17941 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17942
17943 if (ffname != NULL)
17944 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17945 vim_free(ffname);
17946 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017947#else
17948 rettv->vval.v_string = NULL;
17949#endif
17950}
17951
17952/*
Bram Moolenaara800b422010-06-27 01:15:55 +020017953 * "undotree()" function
17954 */
17955 static void
17956f_undotree(argvars, rettv)
17957 typval_T *argvars UNUSED;
17958 typval_T *rettv;
17959{
17960 if (rettv_dict_alloc(rettv) == OK)
17961 {
17962 dict_T *dict = rettv->vval.v_dict;
17963 list_T *list;
17964
Bram Moolenaar730cde92010-06-27 05:18:54 +020017965 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017966 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017967 dict_add_nr_str(dict, "save_last",
17968 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017969 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
17970 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017971 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017972
17973 list = list_alloc();
17974 if (list != NULL)
17975 {
17976 u_eval_tree(curbuf->b_u_oldhead, list);
17977 dict_add_list(dict, "entries", list);
17978 }
17979 }
17980}
17981
17982/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017983 * "values(dict)" function
17984 */
17985 static void
17986f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017987 typval_T *argvars;
17988 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017989{
17990 dict_list(argvars, rettv, 1);
17991}
17992
17993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017994 * "virtcol(string)" function
17995 */
17996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017997f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017998 typval_T *argvars;
17999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018000{
18001 colnr_T vcol = 0;
18002 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018003 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018005 fp = var2fpos(&argvars[0], FALSE, &fnum);
18006 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18007 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008 {
18009 getvvcol(curwin, fp, NULL, NULL, &vcol);
18010 ++vcol;
18011 }
18012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018013 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014}
18015
18016/*
18017 * "visualmode()" function
18018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018020f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018021 typval_T *argvars UNUSED;
18022 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023{
18024#ifdef FEAT_VISUAL
18025 char_u str[2];
18026
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018027 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028 str[0] = curbuf->b_visual_mode_eval;
18029 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018030 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031
18032 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018033 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035#endif
18036}
18037
18038/*
18039 * "winbufnr(nr)" function
18040 */
18041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018042f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018043 typval_T *argvars;
18044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045{
18046 win_T *wp;
18047
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018048 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018050 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018052 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053}
18054
18055/*
18056 * "wincol()" function
18057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018059f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018060 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062{
18063 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018064 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065}
18066
18067/*
18068 * "winheight(nr)" function
18069 */
18070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018071f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018072 typval_T *argvars;
18073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074{
18075 win_T *wp;
18076
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018077 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018079 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018081 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082}
18083
18084/*
18085 * "winline()" function
18086 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018088f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018089 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091{
18092 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018093 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094}
18095
18096/*
18097 * "winnr()" function
18098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018100f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018101 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103{
18104 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018105
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018107 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018109 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110}
18111
18112/*
18113 * "winrestcmd()" function
18114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018116f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018117 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119{
18120#ifdef FEAT_WINDOWS
18121 win_T *wp;
18122 int winnr = 1;
18123 garray_T ga;
18124 char_u buf[50];
18125
18126 ga_init2(&ga, (int)sizeof(char), 70);
18127 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18128 {
18129 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18130 ga_concat(&ga, buf);
18131# ifdef FEAT_VERTSPLIT
18132 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18133 ga_concat(&ga, buf);
18134# endif
18135 ++winnr;
18136 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018137 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018139 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018141 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018143 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018144}
18145
18146/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018147 * "winrestview()" function
18148 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018149 static void
18150f_winrestview(argvars, rettv)
18151 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018152 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018153{
18154 dict_T *dict;
18155
18156 if (argvars[0].v_type != VAR_DICT
18157 || (dict = argvars[0].vval.v_dict) == NULL)
18158 EMSG(_(e_invarg));
18159 else
18160 {
18161 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18162 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18163#ifdef FEAT_VIRTUALEDIT
18164 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18165#endif
18166 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018167 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018168
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018169 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018170#ifdef FEAT_DIFF
18171 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18172#endif
18173 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18174 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18175
18176 check_cursor();
18177 changed_cline_bef_curs();
18178 invalidate_botline();
18179 redraw_later(VALID);
18180
18181 if (curwin->w_topline == 0)
18182 curwin->w_topline = 1;
18183 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18184 curwin->w_topline = curbuf->b_ml.ml_line_count;
18185#ifdef FEAT_DIFF
18186 check_topfill(curwin, TRUE);
18187#endif
18188 }
18189}
18190
18191/*
18192 * "winsaveview()" function
18193 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018194 static void
18195f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018196 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018197 typval_T *rettv;
18198{
18199 dict_T *dict;
18200
Bram Moolenaara800b422010-06-27 01:15:55 +020018201 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018202 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018203 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018204
18205 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18206 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18207#ifdef FEAT_VIRTUALEDIT
18208 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18209#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018210 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018211 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18212
18213 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18214#ifdef FEAT_DIFF
18215 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18216#endif
18217 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18218 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18219}
18220
18221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222 * "winwidth(nr)" function
18223 */
18224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018225f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018226 typval_T *argvars;
18227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018228{
18229 win_T *wp;
18230
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018231 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018233 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018234 else
18235#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018236 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018238 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239#endif
18240}
18241
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018243 * "writefile()" function
18244 */
18245 static void
18246f_writefile(argvars, rettv)
18247 typval_T *argvars;
18248 typval_T *rettv;
18249{
18250 int binary = FALSE;
18251 char_u *fname;
18252 FILE *fd;
18253 listitem_T *li;
18254 char_u *s;
18255 int ret = 0;
18256 int c;
18257
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018258 if (check_restricted() || check_secure())
18259 return;
18260
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018261 if (argvars[0].v_type != VAR_LIST)
18262 {
18263 EMSG2(_(e_listarg), "writefile()");
18264 return;
18265 }
18266 if (argvars[0].vval.v_list == NULL)
18267 return;
18268
18269 if (argvars[2].v_type != VAR_UNKNOWN
18270 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18271 binary = TRUE;
18272
18273 /* Always open the file in binary mode, library functions have a mind of
18274 * their own about CR-LF conversion. */
18275 fname = get_tv_string(&argvars[1]);
18276 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18277 {
18278 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18279 ret = -1;
18280 }
18281 else
18282 {
18283 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18284 li = li->li_next)
18285 {
18286 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18287 {
18288 if (*s == '\n')
18289 c = putc(NUL, fd);
18290 else
18291 c = putc(*s, fd);
18292 if (c == EOF)
18293 {
18294 ret = -1;
18295 break;
18296 }
18297 }
18298 if (!binary || li->li_next != NULL)
18299 if (putc('\n', fd) == EOF)
18300 {
18301 ret = -1;
18302 break;
18303 }
18304 if (ret < 0)
18305 {
18306 EMSG(_(e_write));
18307 break;
18308 }
18309 }
18310 fclose(fd);
18311 }
18312
18313 rettv->vval.v_number = ret;
18314}
18315
18316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018318 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319 */
18320 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018321var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018322 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018323 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018324 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018326 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018328 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018329
Bram Moolenaara5525202006-03-02 22:52:09 +000018330 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018331 if (varp->v_type == VAR_LIST)
18332 {
18333 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018334 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018335 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018336 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018337
18338 l = varp->vval.v_list;
18339 if (l == NULL)
18340 return NULL;
18341
18342 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018343 pos.lnum = list_find_nr(l, 0L, &error);
18344 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018345 return NULL; /* invalid line number */
18346
18347 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018348 pos.col = list_find_nr(l, 1L, &error);
18349 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018350 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018351 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018352
18353 /* We accept "$" for the column number: last column. */
18354 li = list_find(l, 1L);
18355 if (li != NULL && li->li_tv.v_type == VAR_STRING
18356 && li->li_tv.vval.v_string != NULL
18357 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18358 pos.col = len + 1;
18359
Bram Moolenaara5525202006-03-02 22:52:09 +000018360 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018361 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018362 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018363 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018364
Bram Moolenaara5525202006-03-02 22:52:09 +000018365#ifdef FEAT_VIRTUALEDIT
18366 /* Get the virtual offset. Defaults to zero. */
18367 pos.coladd = list_find_nr(l, 2L, &error);
18368 if (error)
18369 pos.coladd = 0;
18370#endif
18371
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018372 return &pos;
18373 }
18374
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018375 name = get_tv_string_chk(varp);
18376 if (name == NULL)
18377 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018378 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018380#ifdef FEAT_VISUAL
18381 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18382 {
18383 if (VIsual_active)
18384 return &VIsual;
18385 return &curwin->w_cursor;
18386 }
18387#endif
18388 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018389 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018390 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18392 return NULL;
18393 return pp;
18394 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018395
18396#ifdef FEAT_VIRTUALEDIT
18397 pos.coladd = 0;
18398#endif
18399
Bram Moolenaar477933c2007-07-17 14:32:23 +000018400 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018401 {
18402 pos.col = 0;
18403 if (name[1] == '0') /* "w0": first visible line */
18404 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018405 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018406 pos.lnum = curwin->w_topline;
18407 return &pos;
18408 }
18409 else if (name[1] == '$') /* "w$": last visible line */
18410 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018411 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018412 pos.lnum = curwin->w_botline - 1;
18413 return &pos;
18414 }
18415 }
18416 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018417 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018418 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419 {
18420 pos.lnum = curbuf->b_ml.ml_line_count;
18421 pos.col = 0;
18422 }
18423 else
18424 {
18425 pos.lnum = curwin->w_cursor.lnum;
18426 pos.col = (colnr_T)STRLEN(ml_get_curline());
18427 }
18428 return &pos;
18429 }
18430 return NULL;
18431}
18432
18433/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018434 * Convert list in "arg" into a position and optional file number.
18435 * When "fnump" is NULL there is no file number, only 3 items.
18436 * Note that the column is passed on as-is, the caller may want to decrement
18437 * it to use 1 for the first column.
18438 * Return FAIL when conversion is not possible, doesn't check the position for
18439 * validity.
18440 */
18441 static int
18442list2fpos(arg, posp, fnump)
18443 typval_T *arg;
18444 pos_T *posp;
18445 int *fnump;
18446{
18447 list_T *l = arg->vval.v_list;
18448 long i = 0;
18449 long n;
18450
Bram Moolenaarbde35262006-07-23 20:12:24 +000018451 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18452 * when "fnump" isn't NULL and "coladd" is optional. */
18453 if (arg->v_type != VAR_LIST
18454 || l == NULL
18455 || l->lv_len < (fnump == NULL ? 2 : 3)
18456 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018457 return FAIL;
18458
18459 if (fnump != NULL)
18460 {
18461 n = list_find_nr(l, i++, NULL); /* fnum */
18462 if (n < 0)
18463 return FAIL;
18464 if (n == 0)
18465 n = curbuf->b_fnum; /* current buffer */
18466 *fnump = n;
18467 }
18468
18469 n = list_find_nr(l, i++, NULL); /* lnum */
18470 if (n < 0)
18471 return FAIL;
18472 posp->lnum = n;
18473
18474 n = list_find_nr(l, i++, NULL); /* col */
18475 if (n < 0)
18476 return FAIL;
18477 posp->col = n;
18478
18479#ifdef FEAT_VIRTUALEDIT
18480 n = list_find_nr(l, i, NULL);
18481 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018482 posp->coladd = 0;
18483 else
18484 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018485#endif
18486
18487 return OK;
18488}
18489
18490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491 * Get the length of an environment variable name.
18492 * Advance "arg" to the first character after the name.
18493 * Return 0 for error.
18494 */
18495 static int
18496get_env_len(arg)
18497 char_u **arg;
18498{
18499 char_u *p;
18500 int len;
18501
18502 for (p = *arg; vim_isIDc(*p); ++p)
18503 ;
18504 if (p == *arg) /* no name found */
18505 return 0;
18506
18507 len = (int)(p - *arg);
18508 *arg = p;
18509 return len;
18510}
18511
18512/*
18513 * Get the length of the name of a function or internal variable.
18514 * "arg" is advanced to the first non-white character after the name.
18515 * Return 0 if something is wrong.
18516 */
18517 static int
18518get_id_len(arg)
18519 char_u **arg;
18520{
18521 char_u *p;
18522 int len;
18523
18524 /* Find the end of the name. */
18525 for (p = *arg; eval_isnamec(*p); ++p)
18526 ;
18527 if (p == *arg) /* no name found */
18528 return 0;
18529
18530 len = (int)(p - *arg);
18531 *arg = skipwhite(p);
18532
18533 return len;
18534}
18535
18536/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018537 * Get the length of the name of a variable or function.
18538 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018540 * Return -1 if curly braces expansion failed.
18541 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018542 * If the name contains 'magic' {}'s, expand them and return the
18543 * expanded name in an allocated string via 'alias' - caller must free.
18544 */
18545 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018546get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547 char_u **arg;
18548 char_u **alias;
18549 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018550 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551{
18552 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553 char_u *p;
18554 char_u *expr_start;
18555 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018556
18557 *alias = NULL; /* default to no alias */
18558
18559 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18560 && (*arg)[2] == (int)KE_SNR)
18561 {
18562 /* hard coded <SNR>, already translated */
18563 *arg += 3;
18564 return get_id_len(arg) + 3;
18565 }
18566 len = eval_fname_script(*arg);
18567 if (len > 0)
18568 {
18569 /* literal "<SID>", "s:" or "<SNR>" */
18570 *arg += len;
18571 }
18572
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018574 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018576 p = find_name_end(*arg, &expr_start, &expr_end,
18577 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578 if (expr_start != NULL)
18579 {
18580 char_u *temp_string;
18581
18582 if (!evaluate)
18583 {
18584 len += (int)(p - *arg);
18585 *arg = skipwhite(p);
18586 return len;
18587 }
18588
18589 /*
18590 * Include any <SID> etc in the expanded string:
18591 * Thus the -len here.
18592 */
18593 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18594 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018595 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596 *alias = temp_string;
18597 *arg = skipwhite(p);
18598 return (int)STRLEN(temp_string);
18599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018600
18601 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018602 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603 EMSG2(_(e_invexpr2), *arg);
18604
18605 return len;
18606}
18607
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018608/*
18609 * Find the end of a variable or function name, taking care of magic braces.
18610 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18611 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018612 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018613 * Return a pointer to just after the name. Equal to "arg" if there is no
18614 * valid name.
18615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018617find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618 char_u *arg;
18619 char_u **expr_start;
18620 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018621 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018623 int mb_nest = 0;
18624 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625 char_u *p;
18626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018627 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018629 *expr_start = NULL;
18630 *expr_end = NULL;
18631 }
18632
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018633 /* Quick check for valid starting character. */
18634 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18635 return arg;
18636
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018637 for (p = arg; *p != NUL
18638 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018639 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018640 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018641 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018642 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018643 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018644 if (*p == '\'')
18645 {
18646 /* skip over 'string' to avoid counting [ and ] inside it. */
18647 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18648 ;
18649 if (*p == NUL)
18650 break;
18651 }
18652 else if (*p == '"')
18653 {
18654 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18655 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18656 if (*p == '\\' && p[1] != NUL)
18657 ++p;
18658 if (*p == NUL)
18659 break;
18660 }
18661
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018662 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018664 if (*p == '[')
18665 ++br_nest;
18666 else if (*p == ']')
18667 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018669
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018670 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018672 if (*p == '{')
18673 {
18674 mb_nest++;
18675 if (expr_start != NULL && *expr_start == NULL)
18676 *expr_start = p;
18677 }
18678 else if (*p == '}')
18679 {
18680 mb_nest--;
18681 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18682 *expr_end = p;
18683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685 }
18686
18687 return p;
18688}
18689
18690/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018691 * Expands out the 'magic' {}'s in a variable/function name.
18692 * Note that this can call itself recursively, to deal with
18693 * constructs like foo{bar}{baz}{bam}
18694 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18695 * "in_start" ^
18696 * "expr_start" ^
18697 * "expr_end" ^
18698 * "in_end" ^
18699 *
18700 * Returns a new allocated string, which the caller must free.
18701 * Returns NULL for failure.
18702 */
18703 static char_u *
18704make_expanded_name(in_start, expr_start, expr_end, in_end)
18705 char_u *in_start;
18706 char_u *expr_start;
18707 char_u *expr_end;
18708 char_u *in_end;
18709{
18710 char_u c1;
18711 char_u *retval = NULL;
18712 char_u *temp_result;
18713 char_u *nextcmd = NULL;
18714
18715 if (expr_end == NULL || in_end == NULL)
18716 return NULL;
18717 *expr_start = NUL;
18718 *expr_end = NUL;
18719 c1 = *in_end;
18720 *in_end = NUL;
18721
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018722 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018723 if (temp_result != NULL && nextcmd == NULL)
18724 {
18725 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18726 + (in_end - expr_end) + 1));
18727 if (retval != NULL)
18728 {
18729 STRCPY(retval, in_start);
18730 STRCAT(retval, temp_result);
18731 STRCAT(retval, expr_end + 1);
18732 }
18733 }
18734 vim_free(temp_result);
18735
18736 *in_end = c1; /* put char back for error messages */
18737 *expr_start = '{';
18738 *expr_end = '}';
18739
18740 if (retval != NULL)
18741 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018742 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018743 if (expr_start != NULL)
18744 {
18745 /* Further expansion! */
18746 temp_result = make_expanded_name(retval, expr_start,
18747 expr_end, temp_result);
18748 vim_free(retval);
18749 retval = temp_result;
18750 }
18751 }
18752
18753 return retval;
18754}
18755
18756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018758 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759 */
18760 static int
18761eval_isnamec(c)
18762 int c;
18763{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018764 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18765}
18766
18767/*
18768 * Return TRUE if character "c" can be used as the first character in a
18769 * variable or function name (excluding '{' and '}').
18770 */
18771 static int
18772eval_isnamec1(c)
18773 int c;
18774{
18775 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776}
18777
18778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779 * Set number v: variable to "val".
18780 */
18781 void
18782set_vim_var_nr(idx, val)
18783 int idx;
18784 long val;
18785{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018786 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787}
18788
18789/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018790 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791 */
18792 long
18793get_vim_var_nr(idx)
18794 int idx;
18795{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018796 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797}
18798
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018799/*
18800 * Get string v: variable value. Uses a static buffer, can only be used once.
18801 */
18802 char_u *
18803get_vim_var_str(idx)
18804 int idx;
18805{
18806 return get_tv_string(&vimvars[idx].vv_tv);
18807}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018808
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018810 * Get List v: variable value. Caller must take care of reference count when
18811 * needed.
18812 */
18813 list_T *
18814get_vim_var_list(idx)
18815 int idx;
18816{
18817 return vimvars[idx].vv_list;
18818}
18819
18820/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018821 * Set v:char to character "c".
18822 */
18823 void
18824set_vim_var_char(c)
18825 int c;
18826{
18827#ifdef FEAT_MBYTE
18828 char_u buf[MB_MAXBYTES];
18829#else
18830 char_u buf[2];
18831#endif
18832
18833#ifdef FEAT_MBYTE
18834 if (has_mbyte)
18835 buf[(*mb_char2bytes)(c, buf)] = NUL;
18836 else
18837#endif
18838 {
18839 buf[0] = c;
18840 buf[1] = NUL;
18841 }
18842 set_vim_var_string(VV_CHAR, buf, -1);
18843}
18844
18845/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018846 * Set v:count to "count" and v:count1 to "count1".
18847 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018848 */
18849 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018850set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018851 long count;
18852 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018853 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018855 if (set_prevcount)
18856 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018857 vimvars[VV_COUNT].vv_nr = count;
18858 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859}
18860
18861/*
18862 * Set string v: variable to a copy of "val".
18863 */
18864 void
18865set_vim_var_string(idx, val, len)
18866 int idx;
18867 char_u *val;
18868 int len; /* length of "val" to use or -1 (whole string) */
18869{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018870 /* Need to do this (at least) once, since we can't initialize a union.
18871 * Will always be invoked when "v:progname" is set. */
18872 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18873
Bram Moolenaare9a41262005-01-15 22:18:47 +000018874 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018875 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018876 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018878 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018880 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881}
18882
18883/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018884 * Set List v: variable to "val".
18885 */
18886 void
18887set_vim_var_list(idx, val)
18888 int idx;
18889 list_T *val;
18890{
18891 list_unref(vimvars[idx].vv_list);
18892 vimvars[idx].vv_list = val;
18893 if (val != NULL)
18894 ++val->lv_refcount;
18895}
18896
18897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 * Set v:register if needed.
18899 */
18900 void
18901set_reg_var(c)
18902 int c;
18903{
18904 char_u regname;
18905
18906 if (c == 0 || c == ' ')
18907 regname = '"';
18908 else
18909 regname = c;
18910 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018911 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912 set_vim_var_string(VV_REG, &regname, 1);
18913}
18914
18915/*
18916 * Get or set v:exception. If "oldval" == NULL, return the current value.
18917 * Otherwise, restore the value to "oldval" and return NULL.
18918 * Must always be called in pairs to save and restore v:exception! Does not
18919 * take care of memory allocations.
18920 */
18921 char_u *
18922v_exception(oldval)
18923 char_u *oldval;
18924{
18925 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018926 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927
Bram Moolenaare9a41262005-01-15 22:18:47 +000018928 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929 return NULL;
18930}
18931
18932/*
18933 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18934 * Otherwise, restore the value to "oldval" and return NULL.
18935 * Must always be called in pairs to save and restore v:throwpoint! Does not
18936 * take care of memory allocations.
18937 */
18938 char_u *
18939v_throwpoint(oldval)
18940 char_u *oldval;
18941{
18942 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018943 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018944
Bram Moolenaare9a41262005-01-15 22:18:47 +000018945 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946 return NULL;
18947}
18948
18949#if defined(FEAT_AUTOCMD) || defined(PROTO)
18950/*
18951 * Set v:cmdarg.
18952 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18953 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18954 * Must always be called in pairs!
18955 */
18956 char_u *
18957set_cmdarg(eap, oldarg)
18958 exarg_T *eap;
18959 char_u *oldarg;
18960{
18961 char_u *oldval;
18962 char_u *newval;
18963 unsigned len;
18964
Bram Moolenaare9a41262005-01-15 22:18:47 +000018965 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018966 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018968 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018969 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018970 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971 }
18972
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018973 if (eap->force_bin == FORCE_BIN)
18974 len = 6;
18975 else if (eap->force_bin == FORCE_NOBIN)
18976 len = 8;
18977 else
18978 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018979
18980 if (eap->read_edit)
18981 len += 7;
18982
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018983 if (eap->force_ff != 0)
18984 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18985# ifdef FEAT_MBYTE
18986 if (eap->force_enc != 0)
18987 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018988 if (eap->bad_char != 0)
18989 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018990# endif
18991
18992 newval = alloc(len + 1);
18993 if (newval == NULL)
18994 return NULL;
18995
18996 if (eap->force_bin == FORCE_BIN)
18997 sprintf((char *)newval, " ++bin");
18998 else if (eap->force_bin == FORCE_NOBIN)
18999 sprintf((char *)newval, " ++nobin");
19000 else
19001 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019002
19003 if (eap->read_edit)
19004 STRCAT(newval, " ++edit");
19005
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019006 if (eap->force_ff != 0)
19007 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19008 eap->cmd + eap->force_ff);
19009# ifdef FEAT_MBYTE
19010 if (eap->force_enc != 0)
19011 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19012 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019013 if (eap->bad_char == BAD_KEEP)
19014 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19015 else if (eap->bad_char == BAD_DROP)
19016 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19017 else if (eap->bad_char != 0)
19018 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019019# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019020 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019021 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019022}
19023#endif
19024
19025/*
19026 * Get the value of internal variable "name".
19027 * Return OK or FAIL.
19028 */
19029 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019030get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031 char_u *name;
19032 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019033 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019034 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035{
19036 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019037 typval_T *tv = NULL;
19038 typval_T atv;
19039 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019040 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041
19042 /* truncate the name, so that we can use strcmp() */
19043 cc = name[len];
19044 name[len] = NUL;
19045
19046 /*
19047 * Check for "b:changedtick".
19048 */
19049 if (STRCMP(name, "b:changedtick") == 0)
19050 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019051 atv.v_type = VAR_NUMBER;
19052 atv.vval.v_number = curbuf->b_changedtick;
19053 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054 }
19055
19056 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057 * Check for user-defined variables.
19058 */
19059 else
19060 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019061 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019062 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019063 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064 }
19065
Bram Moolenaare9a41262005-01-15 22:18:47 +000019066 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019068 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019069 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019070 ret = FAIL;
19071 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019072 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019073 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019074
19075 name[len] = cc;
19076
19077 return ret;
19078}
19079
19080/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019081 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19082 * Also handle function call with Funcref variable: func(expr)
19083 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19084 */
19085 static int
19086handle_subscript(arg, rettv, evaluate, verbose)
19087 char_u **arg;
19088 typval_T *rettv;
19089 int evaluate; /* do more than finding the end */
19090 int verbose; /* give error messages */
19091{
19092 int ret = OK;
19093 dict_T *selfdict = NULL;
19094 char_u *s;
19095 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019096 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019097
19098 while (ret == OK
19099 && (**arg == '['
19100 || (**arg == '.' && rettv->v_type == VAR_DICT)
19101 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19102 && !vim_iswhite(*(*arg - 1)))
19103 {
19104 if (**arg == '(')
19105 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019106 /* need to copy the funcref so that we can clear rettv */
19107 functv = *rettv;
19108 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019109
19110 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019111 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019112 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019113 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19114 &len, evaluate, selfdict);
19115
19116 /* Clear the funcref afterwards, so that deleting it while
19117 * evaluating the arguments is possible (see test55). */
19118 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019119
19120 /* Stop the expression evaluation when immediately aborting on
19121 * error, or when an interrupt occurred or an exception was thrown
19122 * but not caught. */
19123 if (aborting())
19124 {
19125 if (ret == OK)
19126 clear_tv(rettv);
19127 ret = FAIL;
19128 }
19129 dict_unref(selfdict);
19130 selfdict = NULL;
19131 }
19132 else /* **arg == '[' || **arg == '.' */
19133 {
19134 dict_unref(selfdict);
19135 if (rettv->v_type == VAR_DICT)
19136 {
19137 selfdict = rettv->vval.v_dict;
19138 if (selfdict != NULL)
19139 ++selfdict->dv_refcount;
19140 }
19141 else
19142 selfdict = NULL;
19143 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19144 {
19145 clear_tv(rettv);
19146 ret = FAIL;
19147 }
19148 }
19149 }
19150 dict_unref(selfdict);
19151 return ret;
19152}
19153
19154/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019155 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019156 * value).
19157 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019158 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019159alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019160{
Bram Moolenaar33570922005-01-25 22:26:29 +000019161 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019162}
19163
19164/*
19165 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019166 * The string "s" must have been allocated, it is consumed.
19167 * Return NULL for out of memory, the variable otherwise.
19168 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019169 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019170alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171 char_u *s;
19172{
Bram Moolenaar33570922005-01-25 22:26:29 +000019173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019175 rettv = alloc_tv();
19176 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019178 rettv->v_type = VAR_STRING;
19179 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019180 }
19181 else
19182 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019183 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184}
19185
19186/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019187 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019189 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019190free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019191 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019192{
19193 if (varp != NULL)
19194 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019195 switch (varp->v_type)
19196 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019197 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019198 func_unref(varp->vval.v_string);
19199 /*FALLTHROUGH*/
19200 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019201 vim_free(varp->vval.v_string);
19202 break;
19203 case VAR_LIST:
19204 list_unref(varp->vval.v_list);
19205 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019206 case VAR_DICT:
19207 dict_unref(varp->vval.v_dict);
19208 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019209 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019210#ifdef FEAT_FLOAT
19211 case VAR_FLOAT:
19212#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019213 case VAR_UNKNOWN:
19214 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019215 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019216 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019217 break;
19218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019219 vim_free(varp);
19220 }
19221}
19222
19223/*
19224 * Free the memory for a variable value and set the value to NULL or 0.
19225 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019226 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019227clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019228 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019229{
19230 if (varp != NULL)
19231 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019232 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019233 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019234 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019235 func_unref(varp->vval.v_string);
19236 /*FALLTHROUGH*/
19237 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019238 vim_free(varp->vval.v_string);
19239 varp->vval.v_string = NULL;
19240 break;
19241 case VAR_LIST:
19242 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019243 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019244 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019245 case VAR_DICT:
19246 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019247 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019248 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019249 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019250 varp->vval.v_number = 0;
19251 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019252#ifdef FEAT_FLOAT
19253 case VAR_FLOAT:
19254 varp->vval.v_float = 0.0;
19255 break;
19256#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019257 case VAR_UNKNOWN:
19258 break;
19259 default:
19260 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019261 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019262 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019263 }
19264}
19265
19266/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019267 * Set the value of a variable to NULL without freeing items.
19268 */
19269 static void
19270init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019271 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019272{
19273 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019274 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019275}
19276
19277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278 * Get the number value of a variable.
19279 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019280 * For incompatible types, return 0.
19281 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19282 * caller of incompatible types: it sets *denote to TRUE if "denote"
19283 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019284 */
19285 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019286get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019287 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019288{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019289 int error = FALSE;
19290
19291 return get_tv_number_chk(varp, &error); /* return 0L on error */
19292}
19293
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019294 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019295get_tv_number_chk(varp, denote)
19296 typval_T *varp;
19297 int *denote;
19298{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019299 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019300
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019301 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019302 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019303 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019304 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019305#ifdef FEAT_FLOAT
19306 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019307 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019308 break;
19309#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019310 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019311 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019312 break;
19313 case VAR_STRING:
19314 if (varp->vval.v_string != NULL)
19315 vim_str2nr(varp->vval.v_string, NULL, NULL,
19316 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019317 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019318 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019319 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019320 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019321 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019322 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019323 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019324 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019325 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019326 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019328 if (denote == NULL) /* useful for values that must be unsigned */
19329 n = -1;
19330 else
19331 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019332 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019333}
19334
19335/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019336 * Get the lnum from the first argument.
19337 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019338 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339 */
19340 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019341get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019342 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343{
Bram Moolenaar33570922005-01-25 22:26:29 +000019344 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345 linenr_T lnum;
19346
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019347 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019348 if (lnum == 0) /* no valid number, try using line() */
19349 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019350 rettv.v_type = VAR_NUMBER;
19351 f_line(argvars, &rettv);
19352 lnum = rettv.vval.v_number;
19353 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019354 }
19355 return lnum;
19356}
19357
19358/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019359 * Get the lnum from the first argument.
19360 * Also accepts "$", then "buf" is used.
19361 * Returns 0 on error.
19362 */
19363 static linenr_T
19364get_tv_lnum_buf(argvars, buf)
19365 typval_T *argvars;
19366 buf_T *buf;
19367{
19368 if (argvars[0].v_type == VAR_STRING
19369 && argvars[0].vval.v_string != NULL
19370 && argvars[0].vval.v_string[0] == '$'
19371 && buf != NULL)
19372 return buf->b_ml.ml_line_count;
19373 return get_tv_number_chk(&argvars[0], NULL);
19374}
19375
19376/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377 * Get the string value of a variable.
19378 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019379 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19380 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381 * If the String variable has never been set, return an empty string.
19382 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019383 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19384 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385 */
19386 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019387get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019388 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019389{
19390 static char_u mybuf[NUMBUFLEN];
19391
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019392 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019393}
19394
19395 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019396get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019397 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019398 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019400 char_u *res = get_tv_string_buf_chk(varp, buf);
19401
19402 return res != NULL ? res : (char_u *)"";
19403}
19404
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019405 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019406get_tv_string_chk(varp)
19407 typval_T *varp;
19408{
19409 static char_u mybuf[NUMBUFLEN];
19410
19411 return get_tv_string_buf_chk(varp, mybuf);
19412}
19413
19414 static char_u *
19415get_tv_string_buf_chk(varp, buf)
19416 typval_T *varp;
19417 char_u *buf;
19418{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019419 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019421 case VAR_NUMBER:
19422 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19423 return buf;
19424 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019425 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019426 break;
19427 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019428 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019429 break;
19430 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019431 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019432 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019433#ifdef FEAT_FLOAT
19434 case VAR_FLOAT:
19435 EMSG(_("E806: using Float as a String"));
19436 break;
19437#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019438 case VAR_STRING:
19439 if (varp->vval.v_string != NULL)
19440 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019441 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019442 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019443 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019444 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019446 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447}
19448
19449/*
19450 * Find variable "name" in the list of variables.
19451 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019452 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019453 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019454 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019456 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019457find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019459 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019460{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019462 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463
Bram Moolenaara7043832005-01-21 11:56:39 +000019464 ht = find_var_ht(name, &varname);
19465 if (htp != NULL)
19466 *htp = ht;
19467 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019469 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470}
19471
19472/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019473 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019474 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019475 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019476 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019477find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019478 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019479 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019480 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019481{
Bram Moolenaar33570922005-01-25 22:26:29 +000019482 hashitem_T *hi;
19483
19484 if (*varname == NUL)
19485 {
19486 /* Must be something like "s:", otherwise "ht" would be NULL. */
19487 switch (varname[-2])
19488 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019489 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019490 case 'g': return &globvars_var;
19491 case 'v': return &vimvars_var;
19492 case 'b': return &curbuf->b_bufvar;
19493 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019494#ifdef FEAT_WINDOWS
19495 case 't': return &curtab->tp_winvar;
19496#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019497 case 'l': return current_funccal == NULL
19498 ? NULL : &current_funccal->l_vars_var;
19499 case 'a': return current_funccal == NULL
19500 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019501 }
19502 return NULL;
19503 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019504
19505 hi = hash_find(ht, varname);
19506 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019507 {
19508 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019509 * worked find the variable again. Don't auto-load a script if it was
19510 * loaded already, otherwise it would be loaded every time when
19511 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019512 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019513 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019514 hi = hash_find(ht, varname);
19515 if (HASHITEM_EMPTY(hi))
19516 return NULL;
19517 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019518 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019519}
19520
19521/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019522 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019523 * Set "varname" to the start of name without ':'.
19524 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019525 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019526find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527 char_u *name;
19528 char_u **varname;
19529{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019530 hashitem_T *hi;
19531
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532 if (name[1] != ':')
19533 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019534 /* The name must not start with a colon or #. */
19535 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019536 return NULL;
19537 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019538
19539 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019540 hi = hash_find(&compat_hashtab, name);
19541 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019542 return &compat_hashtab;
19543
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019545 return &globvarht; /* global variable */
19546 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547 }
19548 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019549 if (*name == 'g') /* global variable */
19550 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019551 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19552 */
19553 if (vim_strchr(name + 2, ':') != NULL
19554 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019555 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019556 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019557 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019559 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019560#ifdef FEAT_WINDOWS
19561 if (*name == 't') /* tab page variable */
19562 return &curtab->tp_vars.dv_hashtab;
19563#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019564 if (*name == 'v') /* v: variable */
19565 return &vimvarht;
19566 if (*name == 'a' && current_funccal != NULL) /* function argument */
19567 return &current_funccal->l_avars.dv_hashtab;
19568 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19569 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019570 if (*name == 's' /* script variable */
19571 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19572 return &SCRIPT_VARS(current_SID);
19573 return NULL;
19574}
19575
19576/*
19577 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019578 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019579 * Returns NULL when it doesn't exist.
19580 */
19581 char_u *
19582get_var_value(name)
19583 char_u *name;
19584{
Bram Moolenaar33570922005-01-25 22:26:29 +000019585 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586
Bram Moolenaara7043832005-01-21 11:56:39 +000019587 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019588 if (v == NULL)
19589 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019590 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019591}
19592
19593/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019594 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019595 * sourcing this script and when executing functions defined in the script.
19596 */
19597 void
19598new_script_vars(id)
19599 scid_T id;
19600{
Bram Moolenaara7043832005-01-21 11:56:39 +000019601 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019602 hashtab_T *ht;
19603 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019604
Bram Moolenaar071d4272004-06-13 20:20:40 +000019605 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19606 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019607 /* Re-allocating ga_data means that an ht_array pointing to
19608 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019609 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019610 for (i = 1; i <= ga_scripts.ga_len; ++i)
19611 {
19612 ht = &SCRIPT_VARS(i);
19613 if (ht->ht_mask == HT_INIT_SIZE - 1)
19614 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019615 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019616 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019617 }
19618
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619 while (ga_scripts.ga_len < id)
19620 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019621 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019622 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019623 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625 }
19626 }
19627}
19628
19629/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019630 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19631 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019632 */
19633 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019634init_var_dict(dict, dict_var)
19635 dict_T *dict;
19636 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637{
Bram Moolenaar33570922005-01-25 22:26:29 +000019638 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019639 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019640 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019641 dict_var->di_tv.vval.v_dict = dict;
19642 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019643 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019644 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19645 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646}
19647
19648/*
19649 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019650 * Frees all allocated variables and the value they contain.
19651 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652 */
19653 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019654vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019655 hashtab_T *ht;
19656{
19657 vars_clear_ext(ht, TRUE);
19658}
19659
19660/*
19661 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19662 */
19663 static void
19664vars_clear_ext(ht, free_val)
19665 hashtab_T *ht;
19666 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667{
Bram Moolenaara7043832005-01-21 11:56:39 +000019668 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019669 hashitem_T *hi;
19670 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671
Bram Moolenaar33570922005-01-25 22:26:29 +000019672 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019673 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019674 for (hi = ht->ht_array; todo > 0; ++hi)
19675 {
19676 if (!HASHITEM_EMPTY(hi))
19677 {
19678 --todo;
19679
Bram Moolenaar33570922005-01-25 22:26:29 +000019680 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019681 * ht_array might change then. hash_clear() takes care of it
19682 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019683 v = HI2DI(hi);
19684 if (free_val)
19685 clear_tv(&v->di_tv);
19686 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19687 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019688 }
19689 }
19690 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019691 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019692}
19693
Bram Moolenaara7043832005-01-21 11:56:39 +000019694/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019695 * Delete a variable from hashtab "ht" at item "hi".
19696 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019697 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019699delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019700 hashtab_T *ht;
19701 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019702{
Bram Moolenaar33570922005-01-25 22:26:29 +000019703 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019704
19705 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019706 clear_tv(&di->di_tv);
19707 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708}
19709
19710/*
19711 * List the value of one internal variable.
19712 */
19713 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019714list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019715 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019717 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019719 char_u *tofree;
19720 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019721 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019722
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019723 current_copyID += COPYID_INC;
19724 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019725 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019726 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019727 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728}
19729
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019731list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732 char_u *prefix;
19733 char_u *name;
19734 int type;
19735 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019736 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737{
Bram Moolenaar31859182007-08-14 20:41:13 +000019738 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19739 msg_start();
19740 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741 if (name != NULL) /* "a:" vars don't have a name stored */
19742 msg_puts(name);
19743 msg_putchar(' ');
19744 msg_advance(22);
19745 if (type == VAR_NUMBER)
19746 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019747 else if (type == VAR_FUNC)
19748 msg_putchar('*');
19749 else if (type == VAR_LIST)
19750 {
19751 msg_putchar('[');
19752 if (*string == '[')
19753 ++string;
19754 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019755 else if (type == VAR_DICT)
19756 {
19757 msg_putchar('{');
19758 if (*string == '{')
19759 ++string;
19760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019761 else
19762 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019763
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019765
19766 if (type == VAR_FUNC)
19767 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019768 if (*first)
19769 {
19770 msg_clr_eos();
19771 *first = FALSE;
19772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773}
19774
19775/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019776 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777 * If the variable already exists, the value is updated.
19778 * Otherwise the variable is created.
19779 */
19780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019781set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019783 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019784 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785{
Bram Moolenaar33570922005-01-25 22:26:29 +000019786 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019787 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019788 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019789 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019791 ht = find_var_ht(name, &varname);
19792 if (ht == NULL || *varname == NUL)
19793 {
19794 EMSG2(_(e_illvar), name);
19795 return;
19796 }
19797 v = find_var_in_ht(ht, varname, TRUE);
19798
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019799 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019800 {
19801 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19802 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19803 ? name[2] : name[0]))
19804 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019805 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019806 return;
19807 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +010019808 /* Don't allow hiding a function. When "v" is not NULL we might be
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019809 * assigning another function to the same var, the type is checked
19810 * below. */
19811 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019812 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019813 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019814 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019815 return;
19816 }
19817 }
19818
Bram Moolenaar33570922005-01-25 22:26:29 +000019819 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019821 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019822 if (var_check_ro(v->di_flags, name)
19823 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019824 return;
19825 if (v->di_tv.v_type != tv->v_type
19826 && !((v->di_tv.v_type == VAR_STRING
19827 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019828 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019829 || tv->v_type == VAR_NUMBER))
19830#ifdef FEAT_FLOAT
19831 && !((v->di_tv.v_type == VAR_NUMBER
19832 || v->di_tv.v_type == VAR_FLOAT)
19833 && (tv->v_type == VAR_NUMBER
19834 || tv->v_type == VAR_FLOAT))
19835#endif
19836 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019837 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019838 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019839 return;
19840 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019841
19842 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019843 * Handle setting internal v: variables separately: we don't change
19844 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019845 */
19846 if (ht == &vimvarht)
19847 {
19848 if (v->di_tv.v_type == VAR_STRING)
19849 {
19850 vim_free(v->di_tv.vval.v_string);
19851 if (copy || tv->v_type != VAR_STRING)
19852 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19853 else
19854 {
19855 /* Take over the string to avoid an extra alloc/free. */
19856 v->di_tv.vval.v_string = tv->vval.v_string;
19857 tv->vval.v_string = NULL;
19858 }
19859 }
19860 else if (v->di_tv.v_type != VAR_NUMBER)
19861 EMSG2(_(e_intern2), "set_var()");
19862 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019863 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019864 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019865 if (STRCMP(varname, "searchforward") == 0)
19866 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19867 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019868 return;
19869 }
19870
19871 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872 }
19873 else /* add a new variable */
19874 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019875 /* Can't add "v:" variable. */
19876 if (ht == &vimvarht)
19877 {
19878 EMSG2(_(e_illvar), name);
19879 return;
19880 }
19881
Bram Moolenaar92124a32005-06-17 22:03:40 +000019882 /* Make sure the variable name is valid. */
19883 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019884 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19885 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019886 {
19887 EMSG2(_(e_illvar), varname);
19888 return;
19889 }
19890
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019891 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19892 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019893 if (v == NULL)
19894 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019895 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019896 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019898 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899 return;
19900 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019901 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019902 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019903
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019904 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019905 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019906 else
19907 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019908 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019909 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019910 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019912}
19913
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019914/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019915 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019916 * Also give an error message.
19917 */
19918 static int
19919var_check_ro(flags, name)
19920 int flags;
19921 char_u *name;
19922{
19923 if (flags & DI_FLAGS_RO)
19924 {
19925 EMSG2(_(e_readonlyvar), name);
19926 return TRUE;
19927 }
19928 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19929 {
19930 EMSG2(_(e_readonlysbx), name);
19931 return TRUE;
19932 }
19933 return FALSE;
19934}
19935
19936/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019937 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19938 * Also give an error message.
19939 */
19940 static int
19941var_check_fixed(flags, name)
19942 int flags;
19943 char_u *name;
19944{
19945 if (flags & DI_FLAGS_FIX)
19946 {
19947 EMSG2(_("E795: Cannot delete variable %s"), name);
19948 return TRUE;
19949 }
19950 return FALSE;
19951}
19952
19953/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019954 * Return TRUE if typeval "tv" is set to be locked (immutable).
19955 * Also give an error message, using "name".
19956 */
19957 static int
19958tv_check_lock(lock, name)
19959 int lock;
19960 char_u *name;
19961{
19962 if (lock & VAR_LOCKED)
19963 {
19964 EMSG2(_("E741: Value is locked: %s"),
19965 name == NULL ? (char_u *)_("Unknown") : name);
19966 return TRUE;
19967 }
19968 if (lock & VAR_FIXED)
19969 {
19970 EMSG2(_("E742: Cannot change value of %s"),
19971 name == NULL ? (char_u *)_("Unknown") : name);
19972 return TRUE;
19973 }
19974 return FALSE;
19975}
19976
19977/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019978 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019979 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019980 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019981 * It is OK for "from" and "to" to point to the same item. This is used to
19982 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019983 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019984 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019985copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019986 typval_T *from;
19987 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019989 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019990 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019991 switch (from->v_type)
19992 {
19993 case VAR_NUMBER:
19994 to->vval.v_number = from->vval.v_number;
19995 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019996#ifdef FEAT_FLOAT
19997 case VAR_FLOAT:
19998 to->vval.v_float = from->vval.v_float;
19999 break;
20000#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020001 case VAR_STRING:
20002 case VAR_FUNC:
20003 if (from->vval.v_string == NULL)
20004 to->vval.v_string = NULL;
20005 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020006 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020007 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020008 if (from->v_type == VAR_FUNC)
20009 func_ref(to->vval.v_string);
20010 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020011 break;
20012 case VAR_LIST:
20013 if (from->vval.v_list == NULL)
20014 to->vval.v_list = NULL;
20015 else
20016 {
20017 to->vval.v_list = from->vval.v_list;
20018 ++to->vval.v_list->lv_refcount;
20019 }
20020 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020021 case VAR_DICT:
20022 if (from->vval.v_dict == NULL)
20023 to->vval.v_dict = NULL;
20024 else
20025 {
20026 to->vval.v_dict = from->vval.v_dict;
20027 ++to->vval.v_dict->dv_refcount;
20028 }
20029 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020030 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020031 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020032 break;
20033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034}
20035
20036/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020037 * Make a copy of an item.
20038 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020039 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20040 * reference to an already copied list/dict can be used.
20041 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020042 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020043 static int
20044item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020045 typval_T *from;
20046 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020047 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020048 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020049{
20050 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020051 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020052
Bram Moolenaar33570922005-01-25 22:26:29 +000020053 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020054 {
20055 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020056 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020057 }
20058 ++recurse;
20059
20060 switch (from->v_type)
20061 {
20062 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020063#ifdef FEAT_FLOAT
20064 case VAR_FLOAT:
20065#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020066 case VAR_STRING:
20067 case VAR_FUNC:
20068 copy_tv(from, to);
20069 break;
20070 case VAR_LIST:
20071 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020072 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020073 if (from->vval.v_list == NULL)
20074 to->vval.v_list = NULL;
20075 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20076 {
20077 /* use the copy made earlier */
20078 to->vval.v_list = from->vval.v_list->lv_copylist;
20079 ++to->vval.v_list->lv_refcount;
20080 }
20081 else
20082 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20083 if (to->vval.v_list == NULL)
20084 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020085 break;
20086 case VAR_DICT:
20087 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020088 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020089 if (from->vval.v_dict == NULL)
20090 to->vval.v_dict = NULL;
20091 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20092 {
20093 /* use the copy made earlier */
20094 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20095 ++to->vval.v_dict->dv_refcount;
20096 }
20097 else
20098 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20099 if (to->vval.v_dict == NULL)
20100 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020101 break;
20102 default:
20103 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020104 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020105 }
20106 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020107 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020108}
20109
20110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111 * ":echo expr1 ..." print each argument separated with a space, add a
20112 * newline at the end.
20113 * ":echon expr1 ..." print each argument plain.
20114 */
20115 void
20116ex_echo(eap)
20117 exarg_T *eap;
20118{
20119 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020120 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020121 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122 char_u *p;
20123 int needclr = TRUE;
20124 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020125 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126
20127 if (eap->skip)
20128 ++emsg_skip;
20129 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20130 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020131 /* If eval1() causes an error message the text from the command may
20132 * still need to be cleared. E.g., "echo 22,44". */
20133 need_clr_eos = needclr;
20134
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020136 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 {
20138 /*
20139 * Report the invalid expression unless the expression evaluation
20140 * has been cancelled due to an aborting error, an interrupt, or an
20141 * exception.
20142 */
20143 if (!aborting())
20144 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020145 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146 break;
20147 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020148 need_clr_eos = FALSE;
20149
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150 if (!eap->skip)
20151 {
20152 if (atstart)
20153 {
20154 atstart = FALSE;
20155 /* Call msg_start() after eval1(), evaluating the expression
20156 * may cause a message to appear. */
20157 if (eap->cmdidx == CMD_echo)
20158 msg_start();
20159 }
20160 else if (eap->cmdidx == CMD_echo)
20161 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020162 current_copyID += COPYID_INC;
20163 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020164 if (p != NULL)
20165 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020167 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020169 if (*p != TAB && needclr)
20170 {
20171 /* remove any text still there from the command */
20172 msg_clr_eos();
20173 needclr = FALSE;
20174 }
20175 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176 }
20177 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020178 {
20179#ifdef FEAT_MBYTE
20180 if (has_mbyte)
20181 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020182 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020183
20184 (void)msg_outtrans_len_attr(p, i, echo_attr);
20185 p += i - 1;
20186 }
20187 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020189 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020192 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020193 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020194 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020195 arg = skipwhite(arg);
20196 }
20197 eap->nextcmd = check_nextcmd(arg);
20198
20199 if (eap->skip)
20200 --emsg_skip;
20201 else
20202 {
20203 /* remove text that may still be there from the command */
20204 if (needclr)
20205 msg_clr_eos();
20206 if (eap->cmdidx == CMD_echo)
20207 msg_end();
20208 }
20209}
20210
20211/*
20212 * ":echohl {name}".
20213 */
20214 void
20215ex_echohl(eap)
20216 exarg_T *eap;
20217{
20218 int id;
20219
20220 id = syn_name2id(eap->arg);
20221 if (id == 0)
20222 echo_attr = 0;
20223 else
20224 echo_attr = syn_id2attr(id);
20225}
20226
20227/*
20228 * ":execute expr1 ..." execute the result of an expression.
20229 * ":echomsg expr1 ..." Print a message
20230 * ":echoerr expr1 ..." Print an error
20231 * Each gets spaces around each argument and a newline at the end for
20232 * echo commands
20233 */
20234 void
20235ex_execute(eap)
20236 exarg_T *eap;
20237{
20238 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020239 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020240 int ret = OK;
20241 char_u *p;
20242 garray_T ga;
20243 int len;
20244 int save_did_emsg;
20245
20246 ga_init2(&ga, 1, 80);
20247
20248 if (eap->skip)
20249 ++emsg_skip;
20250 while (*arg != NUL && *arg != '|' && *arg != '\n')
20251 {
20252 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020253 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254 {
20255 /*
20256 * Report the invalid expression unless the expression evaluation
20257 * has been cancelled due to an aborting error, an interrupt, or an
20258 * exception.
20259 */
20260 if (!aborting())
20261 EMSG2(_(e_invexpr2), p);
20262 ret = FAIL;
20263 break;
20264 }
20265
20266 if (!eap->skip)
20267 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020268 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269 len = (int)STRLEN(p);
20270 if (ga_grow(&ga, len + 2) == FAIL)
20271 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020272 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020273 ret = FAIL;
20274 break;
20275 }
20276 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279 ga.ga_len += len;
20280 }
20281
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020282 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283 arg = skipwhite(arg);
20284 }
20285
20286 if (ret != FAIL && ga.ga_data != NULL)
20287 {
20288 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020291 out_flush();
20292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293 else if (eap->cmdidx == CMD_echoerr)
20294 {
20295 /* We don't want to abort following commands, restore did_emsg. */
20296 save_did_emsg = did_emsg;
20297 EMSG((char_u *)ga.ga_data);
20298 if (!force_abort)
20299 did_emsg = save_did_emsg;
20300 }
20301 else if (eap->cmdidx == CMD_execute)
20302 do_cmdline((char_u *)ga.ga_data,
20303 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20304 }
20305
20306 ga_clear(&ga);
20307
20308 if (eap->skip)
20309 --emsg_skip;
20310
20311 eap->nextcmd = check_nextcmd(arg);
20312}
20313
20314/*
20315 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20316 * "arg" points to the "&" or '+' when called, to "option" when returning.
20317 * Returns NULL when no option name found. Otherwise pointer to the char
20318 * after the option name.
20319 */
20320 static char_u *
20321find_option_end(arg, opt_flags)
20322 char_u **arg;
20323 int *opt_flags;
20324{
20325 char_u *p = *arg;
20326
20327 ++p;
20328 if (*p == 'g' && p[1] == ':')
20329 {
20330 *opt_flags = OPT_GLOBAL;
20331 p += 2;
20332 }
20333 else if (*p == 'l' && p[1] == ':')
20334 {
20335 *opt_flags = OPT_LOCAL;
20336 p += 2;
20337 }
20338 else
20339 *opt_flags = 0;
20340
20341 if (!ASCII_ISALPHA(*p))
20342 return NULL;
20343 *arg = p;
20344
20345 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20346 p += 4; /* termcap option */
20347 else
20348 while (ASCII_ISALPHA(*p))
20349 ++p;
20350 return p;
20351}
20352
20353/*
20354 * ":function"
20355 */
20356 void
20357ex_function(eap)
20358 exarg_T *eap;
20359{
20360 char_u *theline;
20361 int j;
20362 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364 char_u *name = NULL;
20365 char_u *p;
20366 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020367 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020368 garray_T newargs;
20369 garray_T newlines;
20370 int varargs = FALSE;
20371 int mustend = FALSE;
20372 int flags = 0;
20373 ufunc_T *fp;
20374 int indent;
20375 int nesting;
20376 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020377 dictitem_T *v;
20378 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020379 static int func_nr = 0; /* number for nameless function */
20380 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020381 hashtab_T *ht;
20382 int todo;
20383 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020384 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385
20386 /*
20387 * ":function" without argument: list functions.
20388 */
20389 if (ends_excmd(*eap->arg))
20390 {
20391 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020392 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020393 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020394 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020395 {
20396 if (!HASHITEM_EMPTY(hi))
20397 {
20398 --todo;
20399 fp = HI2UF(hi);
20400 if (!isdigit(*fp->uf_name))
20401 list_func_head(fp, FALSE);
20402 }
20403 }
20404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405 eap->nextcmd = check_nextcmd(eap->arg);
20406 return;
20407 }
20408
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020409 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020410 * ":function /pat": list functions matching pattern.
20411 */
20412 if (*eap->arg == '/')
20413 {
20414 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20415 if (!eap->skip)
20416 {
20417 regmatch_T regmatch;
20418
20419 c = *p;
20420 *p = NUL;
20421 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20422 *p = c;
20423 if (regmatch.regprog != NULL)
20424 {
20425 regmatch.rm_ic = p_ic;
20426
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020427 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020428 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20429 {
20430 if (!HASHITEM_EMPTY(hi))
20431 {
20432 --todo;
20433 fp = HI2UF(hi);
20434 if (!isdigit(*fp->uf_name)
20435 && vim_regexec(&regmatch, fp->uf_name, 0))
20436 list_func_head(fp, FALSE);
20437 }
20438 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020439 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020440 }
20441 }
20442 if (*p == '/')
20443 ++p;
20444 eap->nextcmd = check_nextcmd(p);
20445 return;
20446 }
20447
20448 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020449 * Get the function name. There are these situations:
20450 * func normal function name
20451 * "name" == func, "fudi.fd_dict" == NULL
20452 * dict.func new dictionary entry
20453 * "name" == NULL, "fudi.fd_dict" set,
20454 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20455 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020456 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020457 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20458 * dict.func existing dict entry that's not a Funcref
20459 * "name" == NULL, "fudi.fd_dict" set,
20460 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020463 name = trans_function_name(&p, eap->skip, 0, &fudi);
20464 paren = (vim_strchr(p, '(') != NULL);
20465 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466 {
20467 /*
20468 * Return on an invalid expression in braces, unless the expression
20469 * evaluation has been cancelled due to an aborting error, an
20470 * interrupt, or an exception.
20471 */
20472 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020473 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020474 if (!eap->skip && fudi.fd_newkey != NULL)
20475 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020476 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020477 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479 else
20480 eap->skip = TRUE;
20481 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020482
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483 /* An error in a function call during evaluation of an expression in magic
20484 * braces should not cause the function not to be defined. */
20485 saved_did_emsg = did_emsg;
20486 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020487
20488 /*
20489 * ":function func" with only function name: list function.
20490 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020491 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492 {
20493 if (!ends_excmd(*skipwhite(p)))
20494 {
20495 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020496 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497 }
20498 eap->nextcmd = check_nextcmd(p);
20499 if (eap->nextcmd != NULL)
20500 *p = NUL;
20501 if (!eap->skip && !got_int)
20502 {
20503 fp = find_func(name);
20504 if (fp != NULL)
20505 {
20506 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020507 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020509 if (FUNCLINE(fp, j) == NULL)
20510 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020511 msg_putchar('\n');
20512 msg_outnum((long)(j + 1));
20513 if (j < 9)
20514 msg_putchar(' ');
20515 if (j < 99)
20516 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020517 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518 out_flush(); /* show a line at a time */
20519 ui_breakcheck();
20520 }
20521 if (!got_int)
20522 {
20523 msg_putchar('\n');
20524 msg_puts((char_u *)" endfunction");
20525 }
20526 }
20527 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020528 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020529 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020530 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531 }
20532
20533 /*
20534 * ":function name(arg1, arg2)" Define function.
20535 */
20536 p = skipwhite(p);
20537 if (*p != '(')
20538 {
20539 if (!eap->skip)
20540 {
20541 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020542 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020543 }
20544 /* attempt to continue by skipping some text */
20545 if (vim_strchr(p, '(') != NULL)
20546 p = vim_strchr(p, '(');
20547 }
20548 p = skipwhite(p + 1);
20549
20550 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20551 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20552
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020553 if (!eap->skip)
20554 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020555 /* Check the name of the function. Unless it's a dictionary function
20556 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020557 if (name != NULL)
20558 arg = name;
20559 else
20560 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020561 if (arg != NULL && (fudi.fd_di == NULL
20562 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020563 {
20564 if (*arg == K_SPECIAL)
20565 j = 3;
20566 else
20567 j = 0;
20568 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20569 : eval_isnamec(arg[j])))
20570 ++j;
20571 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020572 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020573 }
20574 }
20575
Bram Moolenaar071d4272004-06-13 20:20:40 +000020576 /*
20577 * Isolate the arguments: "arg1, arg2, ...)"
20578 */
20579 while (*p != ')')
20580 {
20581 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20582 {
20583 varargs = TRUE;
20584 p += 3;
20585 mustend = TRUE;
20586 }
20587 else
20588 {
20589 arg = p;
20590 while (ASCII_ISALNUM(*p) || *p == '_')
20591 ++p;
20592 if (arg == p || isdigit(*arg)
20593 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20594 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20595 {
20596 if (!eap->skip)
20597 EMSG2(_("E125: Illegal argument: %s"), arg);
20598 break;
20599 }
20600 if (ga_grow(&newargs, 1) == FAIL)
20601 goto erret;
20602 c = *p;
20603 *p = NUL;
20604 arg = vim_strsave(arg);
20605 if (arg == NULL)
20606 goto erret;
20607 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20608 *p = c;
20609 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610 if (*p == ',')
20611 ++p;
20612 else
20613 mustend = TRUE;
20614 }
20615 p = skipwhite(p);
20616 if (mustend && *p != ')')
20617 {
20618 if (!eap->skip)
20619 EMSG2(_(e_invarg2), eap->arg);
20620 break;
20621 }
20622 }
20623 ++p; /* skip the ')' */
20624
Bram Moolenaare9a41262005-01-15 22:18:47 +000020625 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626 for (;;)
20627 {
20628 p = skipwhite(p);
20629 if (STRNCMP(p, "range", 5) == 0)
20630 {
20631 flags |= FC_RANGE;
20632 p += 5;
20633 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020634 else if (STRNCMP(p, "dict", 4) == 0)
20635 {
20636 flags |= FC_DICT;
20637 p += 4;
20638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020639 else if (STRNCMP(p, "abort", 5) == 0)
20640 {
20641 flags |= FC_ABORT;
20642 p += 5;
20643 }
20644 else
20645 break;
20646 }
20647
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020648 /* When there is a line break use what follows for the function body.
20649 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20650 if (*p == '\n')
20651 line_arg = p + 1;
20652 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020653 EMSG(_(e_trailing));
20654
20655 /*
20656 * Read the body of the function, until ":endfunction" is found.
20657 */
20658 if (KeyTyped)
20659 {
20660 /* Check if the function already exists, don't let the user type the
20661 * whole function before telling him it doesn't work! For a script we
20662 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020663 if (!eap->skip && !eap->forceit)
20664 {
20665 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20666 EMSG(_(e_funcdict));
20667 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020668 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020670
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020671 if (!eap->skip && did_emsg)
20672 goto erret;
20673
Bram Moolenaar071d4272004-06-13 20:20:40 +000020674 msg_putchar('\n'); /* don't overwrite the function name */
20675 cmdline_row = msg_row;
20676 }
20677
20678 indent = 2;
20679 nesting = 0;
20680 for (;;)
20681 {
20682 msg_scroll = TRUE;
20683 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020684 sourcing_lnum_off = sourcing_lnum;
20685
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020686 if (line_arg != NULL)
20687 {
20688 /* Use eap->arg, split up in parts by line breaks. */
20689 theline = line_arg;
20690 p = vim_strchr(theline, '\n');
20691 if (p == NULL)
20692 line_arg += STRLEN(line_arg);
20693 else
20694 {
20695 *p = NUL;
20696 line_arg = p + 1;
20697 }
20698 }
20699 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020700 theline = getcmdline(':', 0L, indent);
20701 else
20702 theline = eap->getline(':', eap->cookie, indent);
20703 if (KeyTyped)
20704 lines_left = Rows - 1;
20705 if (theline == NULL)
20706 {
20707 EMSG(_("E126: Missing :endfunction"));
20708 goto erret;
20709 }
20710
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020711 /* Detect line continuation: sourcing_lnum increased more than one. */
20712 if (sourcing_lnum > sourcing_lnum_off + 1)
20713 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20714 else
20715 sourcing_lnum_off = 0;
20716
Bram Moolenaar071d4272004-06-13 20:20:40 +000020717 if (skip_until != NULL)
20718 {
20719 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20720 * don't check for ":endfunc". */
20721 if (STRCMP(theline, skip_until) == 0)
20722 {
20723 vim_free(skip_until);
20724 skip_until = NULL;
20725 }
20726 }
20727 else
20728 {
20729 /* skip ':' and blanks*/
20730 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20731 ;
20732
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020733 /* Check for "endfunction". */
20734 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020736 if (line_arg == NULL)
20737 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020738 break;
20739 }
20740
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020741 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020742 * at "end". */
20743 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20744 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020745 else if (STRNCMP(p, "if", 2) == 0
20746 || STRNCMP(p, "wh", 2) == 0
20747 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020748 || STRNCMP(p, "try", 3) == 0)
20749 indent += 2;
20750
20751 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020752 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020753 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020754 if (*p == '!')
20755 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756 p += eval_fname_script(p);
20757 if (ASCII_ISALPHA(*p))
20758 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020759 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020760 if (*skipwhite(p) == '(')
20761 {
20762 ++nesting;
20763 indent += 2;
20764 }
20765 }
20766 }
20767
20768 /* Check for ":append" or ":insert". */
20769 p = skip_range(p, NULL);
20770 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20771 || (p[0] == 'i'
20772 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20773 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20774 skip_until = vim_strsave((char_u *)".");
20775
20776 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20777 arg = skipwhite(skiptowhite(p));
20778 if (arg[0] == '<' && arg[1] =='<'
20779 && ((p[0] == 'p' && p[1] == 'y'
20780 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20781 || (p[0] == 'p' && p[1] == 'e'
20782 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20783 || (p[0] == 't' && p[1] == 'c'
20784 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20785 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20786 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020787 || (p[0] == 'm' && p[1] == 'z'
20788 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020789 ))
20790 {
20791 /* ":python <<" continues until a dot, like ":append" */
20792 p = skipwhite(arg + 2);
20793 if (*p == NUL)
20794 skip_until = vim_strsave((char_u *)".");
20795 else
20796 skip_until = vim_strsave(p);
20797 }
20798 }
20799
20800 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020801 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020802 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020803 if (line_arg == NULL)
20804 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020805 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020806 }
20807
20808 /* Copy the line to newly allocated memory. get_one_sourceline()
20809 * allocates 250 bytes per line, this saves 80% on average. The cost
20810 * is an extra alloc/free. */
20811 p = vim_strsave(theline);
20812 if (p != NULL)
20813 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020814 if (line_arg == NULL)
20815 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020816 theline = p;
20817 }
20818
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020819 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20820
20821 /* Add NULL lines for continuation lines, so that the line count is
20822 * equal to the index in the growarray. */
20823 while (sourcing_lnum_off-- > 0)
20824 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020825
20826 /* Check for end of eap->arg. */
20827 if (line_arg != NULL && *line_arg == NUL)
20828 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829 }
20830
20831 /* Don't define the function when skipping commands or when an error was
20832 * detected. */
20833 if (eap->skip || did_emsg)
20834 goto erret;
20835
20836 /*
20837 * If there are no errors, add the function
20838 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020839 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020840 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020841 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020842 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020843 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020844 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020845 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020846 goto erret;
20847 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020848
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020849 fp = find_func(name);
20850 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020852 if (!eap->forceit)
20853 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020854 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020855 goto erret;
20856 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020857 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020858 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020859 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020860 name);
20861 goto erret;
20862 }
20863 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020864 ga_clear_strings(&(fp->uf_args));
20865 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020866 vim_free(name);
20867 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020869 }
20870 else
20871 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020872 char numbuf[20];
20873
20874 fp = NULL;
20875 if (fudi.fd_newkey == NULL && !eap->forceit)
20876 {
20877 EMSG(_(e_funcdict));
20878 goto erret;
20879 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020880 if (fudi.fd_di == NULL)
20881 {
20882 /* Can't add a function to a locked dictionary */
20883 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20884 goto erret;
20885 }
20886 /* Can't change an existing function if it is locked */
20887 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20888 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020889
20890 /* Give the function a sequential number. Can only be used with a
20891 * Funcref! */
20892 vim_free(name);
20893 sprintf(numbuf, "%d", ++func_nr);
20894 name = vim_strsave((char_u *)numbuf);
20895 if (name == NULL)
20896 goto erret;
20897 }
20898
20899 if (fp == NULL)
20900 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020901 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020902 {
20903 int slen, plen;
20904 char_u *scriptname;
20905
20906 /* Check that the autoload name matches the script name. */
20907 j = FAIL;
20908 if (sourcing_name != NULL)
20909 {
20910 scriptname = autoload_name(name);
20911 if (scriptname != NULL)
20912 {
20913 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020914 plen = (int)STRLEN(p);
20915 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020916 if (slen > plen && fnamecmp(p,
20917 sourcing_name + slen - plen) == 0)
20918 j = OK;
20919 vim_free(scriptname);
20920 }
20921 }
20922 if (j == FAIL)
20923 {
20924 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20925 goto erret;
20926 }
20927 }
20928
20929 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930 if (fp == NULL)
20931 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020932
20933 if (fudi.fd_dict != NULL)
20934 {
20935 if (fudi.fd_di == NULL)
20936 {
20937 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020938 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020939 if (fudi.fd_di == NULL)
20940 {
20941 vim_free(fp);
20942 goto erret;
20943 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020944 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20945 {
20946 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020947 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020948 goto erret;
20949 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020950 }
20951 else
20952 /* overwrite existing dict entry */
20953 clear_tv(&fudi.fd_di->di_tv);
20954 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020955 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020956 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020957 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020958
20959 /* behave like "dict" was used */
20960 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020961 }
20962
Bram Moolenaar071d4272004-06-13 20:20:40 +000020963 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020964 STRCPY(fp->uf_name, name);
20965 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020967 fp->uf_args = newargs;
20968 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020969#ifdef FEAT_PROFILE
20970 fp->uf_tml_count = NULL;
20971 fp->uf_tml_total = NULL;
20972 fp->uf_tml_self = NULL;
20973 fp->uf_profiling = FALSE;
20974 if (prof_def_func())
20975 func_do_profile(fp);
20976#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020977 fp->uf_varargs = varargs;
20978 fp->uf_flags = flags;
20979 fp->uf_calls = 0;
20980 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020981 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020982
20983erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020984 ga_clear_strings(&newargs);
20985 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020986ret_free:
20987 vim_free(skip_until);
20988 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020990 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991}
20992
20993/*
20994 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020995 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020997 * flags:
20998 * TFN_INT: internal function name OK
20999 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021000 * Advances "pp" to just after the function name (if no error).
21001 */
21002 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021003trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004 char_u **pp;
21005 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021006 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021007 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021009 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010 char_u *start;
21011 char_u *end;
21012 int lead;
21013 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021014 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021015 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021016
21017 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021018 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021020
21021 /* Check for hard coded <SNR>: already translated function ID (from a user
21022 * command). */
21023 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21024 && (*pp)[2] == (int)KE_SNR)
21025 {
21026 *pp += 3;
21027 len = get_id_len(pp) + 3;
21028 return vim_strnsave(start, len);
21029 }
21030
21031 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21032 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021034 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021036
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021037 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21038 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021039 if (end == start)
21040 {
21041 if (!skip)
21042 EMSG(_("E129: Function name required"));
21043 goto theend;
21044 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021045 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021046 {
21047 /*
21048 * Report an invalid expression in braces, unless the expression
21049 * evaluation has been cancelled due to an aborting error, an
21050 * interrupt, or an exception.
21051 */
21052 if (!aborting())
21053 {
21054 if (end != NULL)
21055 EMSG2(_(e_invarg2), start);
21056 }
21057 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021058 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021059 goto theend;
21060 }
21061
21062 if (lv.ll_tv != NULL)
21063 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021064 if (fdp != NULL)
21065 {
21066 fdp->fd_dict = lv.ll_dict;
21067 fdp->fd_newkey = lv.ll_newkey;
21068 lv.ll_newkey = NULL;
21069 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021070 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021071 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21072 {
21073 name = vim_strsave(lv.ll_tv->vval.v_string);
21074 *pp = end;
21075 }
21076 else
21077 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021078 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21079 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021080 EMSG(_(e_funcref));
21081 else
21082 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021083 name = NULL;
21084 }
21085 goto theend;
21086 }
21087
21088 if (lv.ll_name == NULL)
21089 {
21090 /* Error found, but continue after the function name. */
21091 *pp = end;
21092 goto theend;
21093 }
21094
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021095 /* Check if the name is a Funcref. If so, use the value. */
21096 if (lv.ll_exp_name != NULL)
21097 {
21098 len = (int)STRLEN(lv.ll_exp_name);
21099 name = deref_func_name(lv.ll_exp_name, &len);
21100 if (name == lv.ll_exp_name)
21101 name = NULL;
21102 }
21103 else
21104 {
21105 len = (int)(end - *pp);
21106 name = deref_func_name(*pp, &len);
21107 if (name == *pp)
21108 name = NULL;
21109 }
21110 if (name != NULL)
21111 {
21112 name = vim_strsave(name);
21113 *pp = end;
21114 goto theend;
21115 }
21116
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021117 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021118 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021119 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021120 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21121 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21122 {
21123 /* When there was "s:" already or the name expanded to get a
21124 * leading "s:" then remove it. */
21125 lv.ll_name += 2;
21126 len -= 2;
21127 lead = 2;
21128 }
21129 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021130 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021131 {
21132 if (lead == 2) /* skip over "s:" */
21133 lv.ll_name += 2;
21134 len = (int)(end - lv.ll_name);
21135 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021136
21137 /*
21138 * Copy the function name to allocated memory.
21139 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21140 * Accept <SNR>123_name() outside a script.
21141 */
21142 if (skip)
21143 lead = 0; /* do nothing */
21144 else if (lead > 0)
21145 {
21146 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021147 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21148 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021149 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021150 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021151 if (current_SID <= 0)
21152 {
21153 EMSG(_(e_usingsid));
21154 goto theend;
21155 }
21156 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21157 lead += (int)STRLEN(sid_buf);
21158 }
21159 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021160 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021161 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021162 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021163 goto theend;
21164 }
21165 name = alloc((unsigned)(len + lead + 1));
21166 if (name != NULL)
21167 {
21168 if (lead > 0)
21169 {
21170 name[0] = K_SPECIAL;
21171 name[1] = KS_EXTRA;
21172 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021173 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021174 STRCPY(name + 3, sid_buf);
21175 }
21176 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21177 name[len + lead] = NUL;
21178 }
21179 *pp = end;
21180
21181theend:
21182 clear_lval(&lv);
21183 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184}
21185
21186/*
21187 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21188 * Return 2 if "p" starts with "s:".
21189 * Return 0 otherwise.
21190 */
21191 static int
21192eval_fname_script(p)
21193 char_u *p;
21194{
21195 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21196 || STRNICMP(p + 1, "SNR>", 4) == 0))
21197 return 5;
21198 if (p[0] == 's' && p[1] == ':')
21199 return 2;
21200 return 0;
21201}
21202
21203/*
21204 * Return TRUE if "p" starts with "<SID>" or "s:".
21205 * Only works if eval_fname_script() returned non-zero for "p"!
21206 */
21207 static int
21208eval_fname_sid(p)
21209 char_u *p;
21210{
21211 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21212}
21213
21214/*
21215 * List the head of the function: "name(arg1, arg2)".
21216 */
21217 static void
21218list_func_head(fp, indent)
21219 ufunc_T *fp;
21220 int indent;
21221{
21222 int j;
21223
21224 msg_start();
21225 if (indent)
21226 MSG_PUTS(" ");
21227 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021228 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021229 {
21230 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021231 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232 }
21233 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021234 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021235 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021236 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 {
21238 if (j)
21239 MSG_PUTS(", ");
21240 msg_puts(FUNCARG(fp, j));
21241 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021242 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021243 {
21244 if (j)
21245 MSG_PUTS(", ");
21246 MSG_PUTS("...");
21247 }
21248 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021249 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021250 if (p_verbose > 0)
21251 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021252}
21253
21254/*
21255 * Find a function by name, return pointer to it in ufuncs.
21256 * Return NULL for unknown function.
21257 */
21258 static ufunc_T *
21259find_func(name)
21260 char_u *name;
21261{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021262 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021264 hi = hash_find(&func_hashtab, name);
21265 if (!HASHITEM_EMPTY(hi))
21266 return HI2UF(hi);
21267 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268}
21269
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021270#if defined(EXITFREE) || defined(PROTO)
21271 void
21272free_all_functions()
21273{
21274 hashitem_T *hi;
21275
21276 /* Need to start all over every time, because func_free() may change the
21277 * hash table. */
21278 while (func_hashtab.ht_used > 0)
21279 for (hi = func_hashtab.ht_array; ; ++hi)
21280 if (!HASHITEM_EMPTY(hi))
21281 {
21282 func_free(HI2UF(hi));
21283 break;
21284 }
21285}
21286#endif
21287
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021288/*
21289 * Return TRUE if a function "name" exists.
21290 */
21291 static int
21292function_exists(name)
21293 char_u *name;
21294{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021295 char_u *nm = name;
21296 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021297 int n = FALSE;
21298
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021299 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021300 nm = skipwhite(nm);
21301
21302 /* Only accept "funcname", "funcname ", "funcname (..." and
21303 * "funcname(...", not "funcname!...". */
21304 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021305 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021306 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021307 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021308 else
21309 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021310 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021311 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021312 return n;
21313}
21314
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021315/*
21316 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021317 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021318 */
21319 static int
21320builtin_function(name)
21321 char_u *name;
21322{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021323 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21324 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021325}
21326
Bram Moolenaar05159a02005-02-26 23:04:13 +000021327#if defined(FEAT_PROFILE) || defined(PROTO)
21328/*
21329 * Start profiling function "fp".
21330 */
21331 static void
21332func_do_profile(fp)
21333 ufunc_T *fp;
21334{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021335 int len = fp->uf_lines.ga_len;
21336
21337 if (len == 0)
21338 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021339 fp->uf_tm_count = 0;
21340 profile_zero(&fp->uf_tm_self);
21341 profile_zero(&fp->uf_tm_total);
21342 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021343 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021344 if (fp->uf_tml_total == NULL)
21345 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021346 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021347 if (fp->uf_tml_self == NULL)
21348 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021349 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021350 fp->uf_tml_idx = -1;
21351 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21352 || fp->uf_tml_self == NULL)
21353 return; /* out of memory */
21354
21355 fp->uf_profiling = TRUE;
21356}
21357
21358/*
21359 * Dump the profiling results for all functions in file "fd".
21360 */
21361 void
21362func_dump_profile(fd)
21363 FILE *fd;
21364{
21365 hashitem_T *hi;
21366 int todo;
21367 ufunc_T *fp;
21368 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021369 ufunc_T **sorttab;
21370 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021371
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021372 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021373 if (todo == 0)
21374 return; /* nothing to dump */
21375
Bram Moolenaar73830342005-02-28 22:48:19 +000021376 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21377
Bram Moolenaar05159a02005-02-26 23:04:13 +000021378 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21379 {
21380 if (!HASHITEM_EMPTY(hi))
21381 {
21382 --todo;
21383 fp = HI2UF(hi);
21384 if (fp->uf_profiling)
21385 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021386 if (sorttab != NULL)
21387 sorttab[st_len++] = fp;
21388
Bram Moolenaar05159a02005-02-26 23:04:13 +000021389 if (fp->uf_name[0] == K_SPECIAL)
21390 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21391 else
21392 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21393 if (fp->uf_tm_count == 1)
21394 fprintf(fd, "Called 1 time\n");
21395 else
21396 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21397 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21398 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21399 fprintf(fd, "\n");
21400 fprintf(fd, "count total (s) self (s)\n");
21401
21402 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21403 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021404 if (FUNCLINE(fp, i) == NULL)
21405 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021406 prof_func_line(fd, fp->uf_tml_count[i],
21407 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021408 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21409 }
21410 fprintf(fd, "\n");
21411 }
21412 }
21413 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021414
21415 if (sorttab != NULL && st_len > 0)
21416 {
21417 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21418 prof_total_cmp);
21419 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21420 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21421 prof_self_cmp);
21422 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21423 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021424
21425 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021426}
Bram Moolenaar73830342005-02-28 22:48:19 +000021427
21428 static void
21429prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21430 FILE *fd;
21431 ufunc_T **sorttab;
21432 int st_len;
21433 char *title;
21434 int prefer_self; /* when equal print only self time */
21435{
21436 int i;
21437 ufunc_T *fp;
21438
21439 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21440 fprintf(fd, "count total (s) self (s) function\n");
21441 for (i = 0; i < 20 && i < st_len; ++i)
21442 {
21443 fp = sorttab[i];
21444 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21445 prefer_self);
21446 if (fp->uf_name[0] == K_SPECIAL)
21447 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21448 else
21449 fprintf(fd, " %s()\n", fp->uf_name);
21450 }
21451 fprintf(fd, "\n");
21452}
21453
21454/*
21455 * Print the count and times for one function or function line.
21456 */
21457 static void
21458prof_func_line(fd, count, total, self, prefer_self)
21459 FILE *fd;
21460 int count;
21461 proftime_T *total;
21462 proftime_T *self;
21463 int prefer_self; /* when equal print only self time */
21464{
21465 if (count > 0)
21466 {
21467 fprintf(fd, "%5d ", count);
21468 if (prefer_self && profile_equal(total, self))
21469 fprintf(fd, " ");
21470 else
21471 fprintf(fd, "%s ", profile_msg(total));
21472 if (!prefer_self && profile_equal(total, self))
21473 fprintf(fd, " ");
21474 else
21475 fprintf(fd, "%s ", profile_msg(self));
21476 }
21477 else
21478 fprintf(fd, " ");
21479}
21480
21481/*
21482 * Compare function for total time sorting.
21483 */
21484 static int
21485#ifdef __BORLANDC__
21486_RTLENTRYF
21487#endif
21488prof_total_cmp(s1, s2)
21489 const void *s1;
21490 const void *s2;
21491{
21492 ufunc_T *p1, *p2;
21493
21494 p1 = *(ufunc_T **)s1;
21495 p2 = *(ufunc_T **)s2;
21496 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21497}
21498
21499/*
21500 * Compare function for self time sorting.
21501 */
21502 static int
21503#ifdef __BORLANDC__
21504_RTLENTRYF
21505#endif
21506prof_self_cmp(s1, s2)
21507 const void *s1;
21508 const void *s2;
21509{
21510 ufunc_T *p1, *p2;
21511
21512 p1 = *(ufunc_T **)s1;
21513 p2 = *(ufunc_T **)s2;
21514 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21515}
21516
Bram Moolenaar05159a02005-02-26 23:04:13 +000021517#endif
21518
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021519/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021520 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021521 * Return TRUE if a package was loaded.
21522 */
21523 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021524script_autoload(name, reload)
21525 char_u *name;
21526 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021527{
21528 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021529 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021530 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021531 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021532
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021533 /* Return quickly when autoload disabled. */
21534 if (no_autoload)
21535 return FALSE;
21536
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021537 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021538 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021539 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021540 return FALSE;
21541
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021542 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021543
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021544 /* Find the name in the list of previously loaded package names. Skip
21545 * "autoload/", it's always the same. */
21546 for (i = 0; i < ga_loaded.ga_len; ++i)
21547 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21548 break;
21549 if (!reload && i < ga_loaded.ga_len)
21550 ret = FALSE; /* was loaded already */
21551 else
21552 {
21553 /* Remember the name if it wasn't loaded already. */
21554 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21555 {
21556 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21557 tofree = NULL;
21558 }
21559
21560 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021561 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021562 ret = TRUE;
21563 }
21564
21565 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021566 return ret;
21567}
21568
21569/*
21570 * Return the autoload script name for a function or variable name.
21571 * Returns NULL when out of memory.
21572 */
21573 static char_u *
21574autoload_name(name)
21575 char_u *name;
21576{
21577 char_u *p;
21578 char_u *scriptname;
21579
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021580 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021581 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21582 if (scriptname == NULL)
21583 return FALSE;
21584 STRCPY(scriptname, "autoload/");
21585 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021586 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021587 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021588 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021589 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021590 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021591}
21592
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21594
21595/*
21596 * Function given to ExpandGeneric() to obtain the list of user defined
21597 * function names.
21598 */
21599 char_u *
21600get_user_func_name(xp, idx)
21601 expand_T *xp;
21602 int idx;
21603{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021604 static long_u done;
21605 static hashitem_T *hi;
21606 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607
21608 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021610 done = 0;
21611 hi = func_hashtab.ht_array;
21612 }
21613 if (done < func_hashtab.ht_used)
21614 {
21615 if (done++ > 0)
21616 ++hi;
21617 while (HASHITEM_EMPTY(hi))
21618 ++hi;
21619 fp = HI2UF(hi);
21620
21621 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21622 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623
21624 cat_func_name(IObuff, fp);
21625 if (xp->xp_context != EXPAND_USER_FUNC)
21626 {
21627 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021628 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629 STRCAT(IObuff, ")");
21630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631 return IObuff;
21632 }
21633 return NULL;
21634}
21635
21636#endif /* FEAT_CMDL_COMPL */
21637
21638/*
21639 * Copy the function name of "fp" to buffer "buf".
21640 * "buf" must be able to hold the function name plus three bytes.
21641 * Takes care of script-local function names.
21642 */
21643 static void
21644cat_func_name(buf, fp)
21645 char_u *buf;
21646 ufunc_T *fp;
21647{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021648 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021649 {
21650 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021651 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652 }
21653 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021654 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655}
21656
21657/*
21658 * ":delfunction {name}"
21659 */
21660 void
21661ex_delfunction(eap)
21662 exarg_T *eap;
21663{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021664 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665 char_u *p;
21666 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021667 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021668
21669 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021670 name = trans_function_name(&p, eap->skip, 0, &fudi);
21671 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021672 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021673 {
21674 if (fudi.fd_dict != NULL && !eap->skip)
21675 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021676 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021678 if (!ends_excmd(*skipwhite(p)))
21679 {
21680 vim_free(name);
21681 EMSG(_(e_trailing));
21682 return;
21683 }
21684 eap->nextcmd = check_nextcmd(p);
21685 if (eap->nextcmd != NULL)
21686 *p = NUL;
21687
21688 if (!eap->skip)
21689 fp = find_func(name);
21690 vim_free(name);
21691
21692 if (!eap->skip)
21693 {
21694 if (fp == NULL)
21695 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021696 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697 return;
21698 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021699 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700 {
21701 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21702 return;
21703 }
21704
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021705 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021707 /* Delete the dict item that refers to the function, it will
21708 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021709 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021711 else
21712 func_free(fp);
21713 }
21714}
21715
21716/*
21717 * Free a function and remove it from the list of functions.
21718 */
21719 static void
21720func_free(fp)
21721 ufunc_T *fp;
21722{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021723 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021724
21725 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021726 ga_clear_strings(&(fp->uf_args));
21727 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021728#ifdef FEAT_PROFILE
21729 vim_free(fp->uf_tml_count);
21730 vim_free(fp->uf_tml_total);
21731 vim_free(fp->uf_tml_self);
21732#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021733
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021734 /* remove the function from the function hashtable */
21735 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21736 if (HASHITEM_EMPTY(hi))
21737 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021738 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021739 hash_remove(&func_hashtab, hi);
21740
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021741 vim_free(fp);
21742}
21743
21744/*
21745 * Unreference a Function: decrement the reference count and free it when it
21746 * becomes zero. Only for numbered functions.
21747 */
21748 static void
21749func_unref(name)
21750 char_u *name;
21751{
21752 ufunc_T *fp;
21753
21754 if (name != NULL && isdigit(*name))
21755 {
21756 fp = find_func(name);
21757 if (fp == NULL)
21758 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021759 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021760 {
21761 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021762 * when "uf_calls" becomes zero. */
21763 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021764 func_free(fp);
21765 }
21766 }
21767}
21768
21769/*
21770 * Count a reference to a Function.
21771 */
21772 static void
21773func_ref(name)
21774 char_u *name;
21775{
21776 ufunc_T *fp;
21777
21778 if (name != NULL && isdigit(*name))
21779 {
21780 fp = find_func(name);
21781 if (fp == NULL)
21782 EMSG2(_(e_intern2), "func_ref()");
21783 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021784 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021785 }
21786}
21787
21788/*
21789 * Call a user function.
21790 */
21791 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021792call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021793 ufunc_T *fp; /* pointer to function */
21794 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021795 typval_T *argvars; /* arguments */
21796 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021797 linenr_T firstline; /* first line of range */
21798 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021799 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800{
Bram Moolenaar33570922005-01-25 22:26:29 +000021801 char_u *save_sourcing_name;
21802 linenr_T save_sourcing_lnum;
21803 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021804 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021805 int save_did_emsg;
21806 static int depth = 0;
21807 dictitem_T *v;
21808 int fixvar_idx = 0; /* index in fixvar[] */
21809 int i;
21810 int ai;
21811 char_u numbuf[NUMBUFLEN];
21812 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021813#ifdef FEAT_PROFILE
21814 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021815 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021817
21818 /* If depth of calling is getting too high, don't execute the function */
21819 if (depth >= p_mfd)
21820 {
21821 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021822 rettv->v_type = VAR_NUMBER;
21823 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824 return;
21825 }
21826 ++depth;
21827
21828 line_breakcheck(); /* check for CTRL-C hit */
21829
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021830 fc = (funccall_T *)alloc(sizeof(funccall_T));
21831 fc->caller = current_funccal;
21832 current_funccal = fc;
21833 fc->func = fp;
21834 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021835 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021836 fc->linenr = 0;
21837 fc->returned = FALSE;
21838 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021839 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021840 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21841 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842
Bram Moolenaar33570922005-01-25 22:26:29 +000021843 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021844 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021845 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21846 * each argument variable and saves a lot of time.
21847 */
21848 /*
21849 * Init l: variables.
21850 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021851 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021852 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021853 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021854 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21855 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021856 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021857 name = v->di_key;
21858 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021859 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021860 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021861 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021862 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021863 v->di_tv.vval.v_dict = selfdict;
21864 ++selfdict->dv_refcount;
21865 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021866
Bram Moolenaar33570922005-01-25 22:26:29 +000021867 /*
21868 * Init a: variables.
21869 * Set a:0 to "argcount".
21870 * Set a:000 to a list with room for the "..." arguments.
21871 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021872 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21873 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021874 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021875 /* Use "name" to avoid a warning from some compiler that checks the
21876 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021877 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021878 name = v->di_key;
21879 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021880 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021881 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021882 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021883 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021884 v->di_tv.vval.v_list = &fc->l_varlist;
21885 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21886 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21887 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021888
21889 /*
21890 * Set a:firstline to "firstline" and a:lastline to "lastline".
21891 * Set a:name to named arguments.
21892 * Set a:N to the "..." arguments.
21893 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021894 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021895 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021896 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021897 (varnumber_T)lastline);
21898 for (i = 0; i < argcount; ++i)
21899 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021900 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021901 if (ai < 0)
21902 /* named argument a:name */
21903 name = FUNCARG(fp, i);
21904 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021905 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021906 /* "..." argument a:1, a:2, etc. */
21907 sprintf((char *)numbuf, "%d", ai + 1);
21908 name = numbuf;
21909 }
21910 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21911 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021912 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021913 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21914 }
21915 else
21916 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021917 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21918 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021919 if (v == NULL)
21920 break;
21921 v->di_flags = DI_FLAGS_RO;
21922 }
21923 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021924 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021925
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021926 /* Note: the values are copied directly to avoid alloc/free.
21927 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021928 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021929 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021930
21931 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21932 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021933 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21934 fc->l_listitems[ai].li_tv = argvars[i];
21935 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021936 }
21937 }
21938
Bram Moolenaar071d4272004-06-13 20:20:40 +000021939 /* Don't redraw while executing the function. */
21940 ++RedrawingDisabled;
21941 save_sourcing_name = sourcing_name;
21942 save_sourcing_lnum = sourcing_lnum;
21943 sourcing_lnum = 1;
21944 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021945 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021946 if (sourcing_name != NULL)
21947 {
21948 if (save_sourcing_name != NULL
21949 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21950 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21951 else
21952 STRCPY(sourcing_name, "function ");
21953 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21954
21955 if (p_verbose >= 12)
21956 {
21957 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021958 verbose_enter_scroll();
21959
Bram Moolenaar555b2802005-05-19 21:08:39 +000021960 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021961 if (p_verbose >= 14)
21962 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021963 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021964 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021965 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021966 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021967
21968 msg_puts((char_u *)"(");
21969 for (i = 0; i < argcount; ++i)
21970 {
21971 if (i > 0)
21972 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021973 if (argvars[i].v_type == VAR_NUMBER)
21974 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021975 else
21976 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021977 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21978 if (s != NULL)
21979 {
21980 trunc_string(s, buf, MSG_BUF_CLEN);
21981 msg_puts(buf);
21982 vim_free(tofree);
21983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984 }
21985 }
21986 msg_puts((char_u *)")");
21987 }
21988 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021989
21990 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991 --no_wait_return;
21992 }
21993 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021994#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021995 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021996 {
21997 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21998 func_do_profile(fp);
21999 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022000 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022001 {
22002 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022003 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022004 profile_zero(&fp->uf_tm_children);
22005 }
22006 script_prof_save(&wait_start);
22007 }
22008#endif
22009
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022011 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012 save_did_emsg = did_emsg;
22013 did_emsg = FALSE;
22014
22015 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022016 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022017 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22018
22019 --RedrawingDisabled;
22020
22021 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022022 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022024 clear_tv(rettv);
22025 rettv->v_type = VAR_NUMBER;
22026 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022027 }
22028
Bram Moolenaar05159a02005-02-26 23:04:13 +000022029#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022030 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022031 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022032 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022033 profile_end(&call_start);
22034 profile_sub_wait(&wait_start, &call_start);
22035 profile_add(&fp->uf_tm_total, &call_start);
22036 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022037 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022038 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022039 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22040 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022041 }
22042 }
22043#endif
22044
Bram Moolenaar071d4272004-06-13 20:20:40 +000022045 /* when being verbose, mention the return value */
22046 if (p_verbose >= 12)
22047 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022049 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050
Bram Moolenaar071d4272004-06-13 20:20:40 +000022051 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022052 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022053 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022054 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022055 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022056 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022058 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022059 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022060 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022061 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022062
Bram Moolenaar555b2802005-05-19 21:08:39 +000022063 /* The value may be very long. Skip the middle part, so that we
22064 * have some idea how it starts and ends. smsg() would always
22065 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022066 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022067 if (s != NULL)
22068 {
22069 trunc_string(s, buf, MSG_BUF_CLEN);
22070 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
22071 vim_free(tofree);
22072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022073 }
22074 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022075
22076 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022077 --no_wait_return;
22078 }
22079
22080 vim_free(sourcing_name);
22081 sourcing_name = save_sourcing_name;
22082 sourcing_lnum = save_sourcing_lnum;
22083 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022084#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022085 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022086 script_prof_restore(&wait_start);
22087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088
22089 if (p_verbose >= 12 && sourcing_name != NULL)
22090 {
22091 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022092 verbose_enter_scroll();
22093
Bram Moolenaar555b2802005-05-19 21:08:39 +000022094 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022096
22097 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022098 --no_wait_return;
22099 }
22100
22101 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022102 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022103 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022104
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022105 /* If the a:000 list and the l: and a: dicts are not referenced we can
22106 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022107 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22108 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22109 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22110 {
22111 free_funccal(fc, FALSE);
22112 }
22113 else
22114 {
22115 hashitem_T *hi;
22116 listitem_T *li;
22117 int todo;
22118
22119 /* "fc" is still in use. This can happen when returning "a:000" or
22120 * assigning "l:" to a global variable.
22121 * Link "fc" in the list for garbage collection later. */
22122 fc->caller = previous_funccal;
22123 previous_funccal = fc;
22124
22125 /* Make a copy of the a: variables, since we didn't do that above. */
22126 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22127 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22128 {
22129 if (!HASHITEM_EMPTY(hi))
22130 {
22131 --todo;
22132 v = HI2DI(hi);
22133 copy_tv(&v->di_tv, &v->di_tv);
22134 }
22135 }
22136
22137 /* Make a copy of the a:000 items, since we didn't do that above. */
22138 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22139 copy_tv(&li->li_tv, &li->li_tv);
22140 }
22141}
22142
22143/*
22144 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022145 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022146 */
22147 static int
22148can_free_funccal(fc, copyID)
22149 funccall_T *fc;
22150 int copyID;
22151{
22152 return (fc->l_varlist.lv_copyID != copyID
22153 && fc->l_vars.dv_copyID != copyID
22154 && fc->l_avars.dv_copyID != copyID);
22155}
22156
22157/*
22158 * Free "fc" and what it contains.
22159 */
22160 static void
22161free_funccal(fc, free_val)
22162 funccall_T *fc;
22163 int free_val; /* a: vars were allocated */
22164{
22165 listitem_T *li;
22166
22167 /* The a: variables typevals may not have been allocated, only free the
22168 * allocated variables. */
22169 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22170
22171 /* free all l: variables */
22172 vars_clear(&fc->l_vars.dv_hashtab);
22173
22174 /* Free the a:000 variables if they were allocated. */
22175 if (free_val)
22176 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22177 clear_tv(&li->li_tv);
22178
22179 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022180}
22181
22182/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022183 * Add a number variable "name" to dict "dp" with value "nr".
22184 */
22185 static void
22186add_nr_var(dp, v, name, nr)
22187 dict_T *dp;
22188 dictitem_T *v;
22189 char *name;
22190 varnumber_T nr;
22191{
22192 STRCPY(v->di_key, name);
22193 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22194 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22195 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022196 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022197 v->di_tv.vval.v_number = nr;
22198}
22199
22200/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022201 * ":return [expr]"
22202 */
22203 void
22204ex_return(eap)
22205 exarg_T *eap;
22206{
22207 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022208 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209 int returning = FALSE;
22210
22211 if (current_funccal == NULL)
22212 {
22213 EMSG(_("E133: :return not inside a function"));
22214 return;
22215 }
22216
22217 if (eap->skip)
22218 ++emsg_skip;
22219
22220 eap->nextcmd = NULL;
22221 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022222 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022223 {
22224 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022225 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022226 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022227 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022228 }
22229 /* It's safer to return also on error. */
22230 else if (!eap->skip)
22231 {
22232 /*
22233 * Return unless the expression evaluation has been cancelled due to an
22234 * aborting error, an interrupt, or an exception.
22235 */
22236 if (!aborting())
22237 returning = do_return(eap, FALSE, TRUE, NULL);
22238 }
22239
22240 /* When skipping or the return gets pending, advance to the next command
22241 * in this line (!returning). Otherwise, ignore the rest of the line.
22242 * Following lines will be ignored by get_func_line(). */
22243 if (returning)
22244 eap->nextcmd = NULL;
22245 else if (eap->nextcmd == NULL) /* no argument */
22246 eap->nextcmd = check_nextcmd(arg);
22247
22248 if (eap->skip)
22249 --emsg_skip;
22250}
22251
22252/*
22253 * Return from a function. Possibly makes the return pending. Also called
22254 * for a pending return at the ":endtry" or after returning from an extra
22255 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022256 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022257 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022258 * FALSE when the return gets pending.
22259 */
22260 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022261do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262 exarg_T *eap;
22263 int reanimate;
22264 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022265 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022266{
22267 int idx;
22268 struct condstack *cstack = eap->cstack;
22269
22270 if (reanimate)
22271 /* Undo the return. */
22272 current_funccal->returned = FALSE;
22273
22274 /*
22275 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22276 * not in its finally clause (which then is to be executed next) is found.
22277 * In this case, make the ":return" pending for execution at the ":endtry".
22278 * Otherwise, return normally.
22279 */
22280 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22281 if (idx >= 0)
22282 {
22283 cstack->cs_pending[idx] = CSTP_RETURN;
22284
22285 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022286 /* A pending return again gets pending. "rettv" points to an
22287 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022289 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290 else
22291 {
22292 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022293 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022295 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022297 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298 {
22299 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022300 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022301 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022302 else
22303 EMSG(_(e_outofmem));
22304 }
22305 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022306 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307
22308 if (reanimate)
22309 {
22310 /* The pending return value could be overwritten by a ":return"
22311 * without argument in a finally clause; reset the default
22312 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022313 current_funccal->rettv->v_type = VAR_NUMBER;
22314 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022315 }
22316 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022317 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022318 }
22319 else
22320 {
22321 current_funccal->returned = TRUE;
22322
22323 /* If the return is carried out now, store the return value. For
22324 * a return immediately after reanimation, the value is already
22325 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022326 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022327 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022328 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022329 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022331 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022332 }
22333 }
22334
22335 return idx < 0;
22336}
22337
22338/*
22339 * Free the variable with a pending return value.
22340 */
22341 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022342discard_pending_return(rettv)
22343 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022344{
Bram Moolenaar33570922005-01-25 22:26:29 +000022345 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022346}
22347
22348/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022349 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350 * is an allocated string. Used by report_pending() for verbose messages.
22351 */
22352 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022353get_return_cmd(rettv)
22354 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022356 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022357 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022358 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022359
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022360 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022361 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022362 if (s == NULL)
22363 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022364
22365 STRCPY(IObuff, ":return ");
22366 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22367 if (STRLEN(s) + 8 >= IOSIZE)
22368 STRCPY(IObuff + IOSIZE - 4, "...");
22369 vim_free(tofree);
22370 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022371}
22372
22373/*
22374 * Get next function line.
22375 * Called by do_cmdline() to get the next line.
22376 * Returns allocated string, or NULL for end of function.
22377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022378 char_u *
22379get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022380 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022381 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022382 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022383{
Bram Moolenaar33570922005-01-25 22:26:29 +000022384 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022385 ufunc_T *fp = fcp->func;
22386 char_u *retval;
22387 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388
22389 /* If breakpoints have been added/deleted need to check for it. */
22390 if (fcp->dbg_tick != debug_tick)
22391 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022392 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393 sourcing_lnum);
22394 fcp->dbg_tick = debug_tick;
22395 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022396#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022397 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022398 func_line_end(cookie);
22399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022400
Bram Moolenaar05159a02005-02-26 23:04:13 +000022401 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022402 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22403 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022404 retval = NULL;
22405 else
22406 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022407 /* Skip NULL lines (continuation lines). */
22408 while (fcp->linenr < gap->ga_len
22409 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22410 ++fcp->linenr;
22411 if (fcp->linenr >= gap->ga_len)
22412 retval = NULL;
22413 else
22414 {
22415 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22416 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022417#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022418 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022419 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022420#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422 }
22423
22424 /* Did we encounter a breakpoint? */
22425 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22426 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022427 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022429 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022430 sourcing_lnum);
22431 fcp->dbg_tick = debug_tick;
22432 }
22433
22434 return retval;
22435}
22436
Bram Moolenaar05159a02005-02-26 23:04:13 +000022437#if defined(FEAT_PROFILE) || defined(PROTO)
22438/*
22439 * Called when starting to read a function line.
22440 * "sourcing_lnum" must be correct!
22441 * When skipping lines it may not actually be executed, but we won't find out
22442 * until later and we need to store the time now.
22443 */
22444 void
22445func_line_start(cookie)
22446 void *cookie;
22447{
22448 funccall_T *fcp = (funccall_T *)cookie;
22449 ufunc_T *fp = fcp->func;
22450
22451 if (fp->uf_profiling && sourcing_lnum >= 1
22452 && sourcing_lnum <= fp->uf_lines.ga_len)
22453 {
22454 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022455 /* Skip continuation lines. */
22456 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22457 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022458 fp->uf_tml_execed = FALSE;
22459 profile_start(&fp->uf_tml_start);
22460 profile_zero(&fp->uf_tml_children);
22461 profile_get_wait(&fp->uf_tml_wait);
22462 }
22463}
22464
22465/*
22466 * Called when actually executing a function line.
22467 */
22468 void
22469func_line_exec(cookie)
22470 void *cookie;
22471{
22472 funccall_T *fcp = (funccall_T *)cookie;
22473 ufunc_T *fp = fcp->func;
22474
22475 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22476 fp->uf_tml_execed = TRUE;
22477}
22478
22479/*
22480 * Called when done with a function line.
22481 */
22482 void
22483func_line_end(cookie)
22484 void *cookie;
22485{
22486 funccall_T *fcp = (funccall_T *)cookie;
22487 ufunc_T *fp = fcp->func;
22488
22489 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22490 {
22491 if (fp->uf_tml_execed)
22492 {
22493 ++fp->uf_tml_count[fp->uf_tml_idx];
22494 profile_end(&fp->uf_tml_start);
22495 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022496 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022497 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22498 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022499 }
22500 fp->uf_tml_idx = -1;
22501 }
22502}
22503#endif
22504
Bram Moolenaar071d4272004-06-13 20:20:40 +000022505/*
22506 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022507 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022508 */
22509 int
22510func_has_ended(cookie)
22511 void *cookie;
22512{
Bram Moolenaar33570922005-01-25 22:26:29 +000022513 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022514
22515 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22516 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022517 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022518 || fcp->returned);
22519}
22520
22521/*
22522 * return TRUE if cookie indicates a function which "abort"s on errors.
22523 */
22524 int
22525func_has_abort(cookie)
22526 void *cookie;
22527{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022528 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022529}
22530
22531#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22532typedef enum
22533{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022534 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22535 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22536 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537} var_flavour_T;
22538
22539static var_flavour_T var_flavour __ARGS((char_u *varname));
22540
22541 static var_flavour_T
22542var_flavour(varname)
22543 char_u *varname;
22544{
22545 char_u *p = varname;
22546
22547 if (ASCII_ISUPPER(*p))
22548 {
22549 while (*(++p))
22550 if (ASCII_ISLOWER(*p))
22551 return VAR_FLAVOUR_SESSION;
22552 return VAR_FLAVOUR_VIMINFO;
22553 }
22554 else
22555 return VAR_FLAVOUR_DEFAULT;
22556}
22557#endif
22558
22559#if defined(FEAT_VIMINFO) || defined(PROTO)
22560/*
22561 * Restore global vars that start with a capital from the viminfo file
22562 */
22563 int
22564read_viminfo_varlist(virp, writing)
22565 vir_T *virp;
22566 int writing;
22567{
22568 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022569 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022570 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022571
22572 if (!writing && (find_viminfo_parameter('!') != NULL))
22573 {
22574 tab = vim_strchr(virp->vir_line + 1, '\t');
22575 if (tab != NULL)
22576 {
22577 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022578 switch (*tab)
22579 {
22580 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022581#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022582 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022583#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022584 case 'D': type = VAR_DICT; break;
22585 case 'L': type = VAR_LIST; break;
22586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022587
22588 tab = vim_strchr(tab, '\t');
22589 if (tab != NULL)
22590 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022591 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022592 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022593 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022594 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022595#ifdef FEAT_FLOAT
22596 else if (type == VAR_FLOAT)
22597 (void)string2float(tab + 1, &tv.vval.v_float);
22598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022600 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022601 if (type == VAR_DICT || type == VAR_LIST)
22602 {
22603 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
22604
22605 if (etv == NULL)
22606 /* Failed to parse back the dict or list, use it as a
22607 * string. */
22608 tv.v_type = VAR_STRING;
22609 else
22610 {
22611 vim_free(tv.vval.v_string);
22612 tv = *etv;
22613 }
22614 }
22615
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022616 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022617
22618 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022619 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022620 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
22621 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622 }
22623 }
22624 }
22625
22626 return viminfo_readline(virp);
22627}
22628
22629/*
22630 * Write global vars that start with a capital to the viminfo file
22631 */
22632 void
22633write_viminfo_varlist(fp)
22634 FILE *fp;
22635{
Bram Moolenaar33570922005-01-25 22:26:29 +000022636 hashitem_T *hi;
22637 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022638 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022639 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022640 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022641 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022642 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643
22644 if (find_viminfo_parameter('!') == NULL)
22645 return;
22646
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022647 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022648
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022649 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022650 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022652 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022653 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022654 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022655 this_var = HI2DI(hi);
22656 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022657 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022658 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022659 {
22660 case VAR_STRING: s = "STR"; break;
22661 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022662#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022663 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022664#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022665 case VAR_DICT: s = "DIC"; break;
22666 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000022667 default: continue;
22668 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022669 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022670 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022671 if (p != NULL)
22672 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022673 vim_free(tofree);
22674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022675 }
22676 }
22677}
22678#endif
22679
22680#if defined(FEAT_SESSION) || defined(PROTO)
22681 int
22682store_session_globals(fd)
22683 FILE *fd;
22684{
Bram Moolenaar33570922005-01-25 22:26:29 +000022685 hashitem_T *hi;
22686 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022687 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022688 char_u *p, *t;
22689
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022690 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022691 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022692 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022693 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022695 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022696 this_var = HI2DI(hi);
22697 if ((this_var->di_tv.v_type == VAR_NUMBER
22698 || this_var->di_tv.v_type == VAR_STRING)
22699 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022700 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022701 /* Escape special characters with a backslash. Turn a LF and
22702 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022703 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022704 (char_u *)"\\\"\n\r");
22705 if (p == NULL) /* out of memory */
22706 break;
22707 for (t = p; *t != NUL; ++t)
22708 if (*t == '\n')
22709 *t = 'n';
22710 else if (*t == '\r')
22711 *t = 'r';
22712 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022713 this_var->di_key,
22714 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22715 : ' ',
22716 p,
22717 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22718 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022719 || put_eol(fd) == FAIL)
22720 {
22721 vim_free(p);
22722 return FAIL;
22723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022724 vim_free(p);
22725 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022726#ifdef FEAT_FLOAT
22727 else if (this_var->di_tv.v_type == VAR_FLOAT
22728 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22729 {
22730 float_T f = this_var->di_tv.vval.v_float;
22731 int sign = ' ';
22732
22733 if (f < 0)
22734 {
22735 f = -f;
22736 sign = '-';
22737 }
22738 if ((fprintf(fd, "let %s = %c&%f",
22739 this_var->di_key, sign, f) < 0)
22740 || put_eol(fd) == FAIL)
22741 return FAIL;
22742 }
22743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744 }
22745 }
22746 return OK;
22747}
22748#endif
22749
Bram Moolenaar661b1822005-07-28 22:36:45 +000022750/*
22751 * Display script name where an item was last set.
22752 * Should only be invoked when 'verbose' is non-zero.
22753 */
22754 void
22755last_set_msg(scriptID)
22756 scid_T scriptID;
22757{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022758 char_u *p;
22759
Bram Moolenaar661b1822005-07-28 22:36:45 +000022760 if (scriptID != 0)
22761 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022762 p = home_replace_save(NULL, get_scriptname(scriptID));
22763 if (p != NULL)
22764 {
22765 verbose_enter();
22766 MSG_PUTS(_("\n\tLast set from "));
22767 MSG_PUTS(p);
22768 vim_free(p);
22769 verbose_leave();
22770 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022771 }
22772}
22773
Bram Moolenaard812df62008-11-09 12:46:09 +000022774/*
22775 * List v:oldfiles in a nice way.
22776 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022777 void
22778ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022779 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022780{
22781 list_T *l = vimvars[VV_OLDFILES].vv_list;
22782 listitem_T *li;
22783 int nr = 0;
22784
22785 if (l == NULL)
22786 msg((char_u *)_("No old files"));
22787 else
22788 {
22789 msg_start();
22790 msg_scroll = TRUE;
22791 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22792 {
22793 msg_outnum((long)++nr);
22794 MSG_PUTS(": ");
22795 msg_outtrans(get_tv_string(&li->li_tv));
22796 msg_putchar('\n');
22797 out_flush(); /* output one line at a time */
22798 ui_breakcheck();
22799 }
22800 /* Assume "got_int" was set to truncate the listing. */
22801 got_int = FALSE;
22802
22803#ifdef FEAT_BROWSE_CMD
22804 if (cmdmod.browse)
22805 {
22806 quit_more = FALSE;
22807 nr = prompt_for_number(FALSE);
22808 msg_starthere();
22809 if (nr > 0)
22810 {
22811 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22812 (long)nr);
22813
22814 if (p != NULL)
22815 {
22816 p = expand_env_save(p);
22817 eap->arg = p;
22818 eap->cmdidx = CMD_edit;
22819 cmdmod.browse = FALSE;
22820 do_exedit(eap, NULL);
22821 vim_free(p);
22822 }
22823 }
22824 }
22825#endif
22826 }
22827}
22828
Bram Moolenaar071d4272004-06-13 20:20:40 +000022829#endif /* FEAT_EVAL */
22830
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022832#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022833
22834#ifdef WIN3264
22835/*
22836 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22837 */
22838static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22839static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22840static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22841
22842/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022843 * Get the short path (8.3) for the filename in "fnamep".
22844 * Only works for a valid file name.
22845 * When the path gets longer "fnamep" is changed and the allocated buffer
22846 * is put in "bufp".
22847 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22848 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849 */
22850 static int
22851get_short_pathname(fnamep, bufp, fnamelen)
22852 char_u **fnamep;
22853 char_u **bufp;
22854 int *fnamelen;
22855{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022856 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022857 char_u *newbuf;
22858
22859 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022860 l = GetShortPathName(*fnamep, *fnamep, len);
22861 if (l > len - 1)
22862 {
22863 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022864 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865 newbuf = vim_strnsave(*fnamep, l);
22866 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022867 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868
22869 vim_free(*bufp);
22870 *fnamep = *bufp = newbuf;
22871
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022872 /* Really should always succeed, as the buffer is big enough. */
22873 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874 }
22875
22876 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022877 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878}
22879
22880/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022881 * Get the short path (8.3) for the filename in "fname". The converted
22882 * path is returned in "bufp".
22883 *
22884 * Some of the directories specified in "fname" may not exist. This function
22885 * will shorten the existing directories at the beginning of the path and then
22886 * append the remaining non-existing path.
22887 *
22888 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022889 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022890 * bufp - Pointer to an allocated buffer for the filename.
22891 * fnamelen - Length of the filename pointed to by fname
22892 *
22893 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022894 */
22895 static int
22896shortpath_for_invalid_fname(fname, bufp, fnamelen)
22897 char_u **fname;
22898 char_u **bufp;
22899 int *fnamelen;
22900{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022901 char_u *short_fname, *save_fname, *pbuf_unused;
22902 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022904 int old_len, len;
22905 int new_len, sfx_len;
22906 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907
22908 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022909 old_len = *fnamelen;
22910 save_fname = vim_strnsave(*fname, old_len);
22911 pbuf_unused = NULL;
22912 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022913
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022914 endp = save_fname + old_len - 1; /* Find the end of the copy */
22915 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022916
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022917 /*
22918 * Try shortening the supplied path till it succeeds by removing one
22919 * directory at a time from the tail of the path.
22920 */
22921 len = 0;
22922 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022924 /* go back one path-separator */
22925 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22926 --endp;
22927 if (endp <= save_fname)
22928 break; /* processed the complete path */
22929
22930 /*
22931 * Replace the path separator with a NUL and try to shorten the
22932 * resulting path.
22933 */
22934 ch = *endp;
22935 *endp = 0;
22936 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022937 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022938 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22939 {
22940 retval = FAIL;
22941 goto theend;
22942 }
22943 *endp = ch; /* preserve the string */
22944
22945 if (len > 0)
22946 break; /* successfully shortened the path */
22947
22948 /* failed to shorten the path. Skip the path separator */
22949 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 }
22951
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022952 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022954 /*
22955 * Succeeded in shortening the path. Now concatenate the shortened
22956 * path with the remaining path at the tail.
22957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022958
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022959 /* Compute the length of the new path. */
22960 sfx_len = (int)(save_endp - endp) + 1;
22961 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022963 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022965 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022966 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022967 /* There is not enough space in the currently allocated string,
22968 * copy it to a buffer big enough. */
22969 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022970 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022971 {
22972 retval = FAIL;
22973 goto theend;
22974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975 }
22976 else
22977 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022978 /* Transfer short_fname to the main buffer (it's big enough),
22979 * unless get_short_pathname() did its work in-place. */
22980 *fname = *bufp = save_fname;
22981 if (short_fname != save_fname)
22982 vim_strncpy(save_fname, short_fname, len);
22983 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022985
22986 /* concat the not-shortened part of the path */
22987 vim_strncpy(*fname + len, endp, sfx_len);
22988 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022989 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022990
22991theend:
22992 vim_free(pbuf_unused);
22993 vim_free(save_fname);
22994
22995 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996}
22997
22998/*
22999 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023000 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023001 */
23002 static int
23003shortpath_for_partial(fnamep, bufp, fnamelen)
23004 char_u **fnamep;
23005 char_u **bufp;
23006 int *fnamelen;
23007{
23008 int sepcount, len, tflen;
23009 char_u *p;
23010 char_u *pbuf, *tfname;
23011 int hasTilde;
23012
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023013 /* Count up the path separators from the RHS.. so we know which part
23014 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023015 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023016 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023017 if (vim_ispathsep(*p))
23018 ++sepcount;
23019
23020 /* Need full path first (use expand_env() to remove a "~/") */
23021 hasTilde = (**fnamep == '~');
23022 if (hasTilde)
23023 pbuf = tfname = expand_env_save(*fnamep);
23024 else
23025 pbuf = tfname = FullName_save(*fnamep, FALSE);
23026
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023027 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023028
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023029 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23030 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023031
23032 if (len == 0)
23033 {
23034 /* Don't have a valid filename, so shorten the rest of the
23035 * path if we can. This CAN give us invalid 8.3 filenames, but
23036 * there's not a lot of point in guessing what it might be.
23037 */
23038 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023039 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23040 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023041 }
23042
23043 /* Count the paths backward to find the beginning of the desired string. */
23044 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023045 {
23046#ifdef FEAT_MBYTE
23047 if (has_mbyte)
23048 p -= mb_head_off(tfname, p);
23049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050 if (vim_ispathsep(*p))
23051 {
23052 if (sepcount == 0 || (hasTilde && sepcount == 1))
23053 break;
23054 else
23055 sepcount --;
23056 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023058 if (hasTilde)
23059 {
23060 --p;
23061 if (p >= tfname)
23062 *p = '~';
23063 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023064 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065 }
23066 else
23067 ++p;
23068
23069 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23070 vim_free(*bufp);
23071 *fnamelen = (int)STRLEN(p);
23072 *bufp = pbuf;
23073 *fnamep = p;
23074
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023075 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023076}
23077#endif /* WIN3264 */
23078
23079/*
23080 * Adjust a filename, according to a string of modifiers.
23081 * *fnamep must be NUL terminated when called. When returning, the length is
23082 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023083 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023084 * When there is an error, *fnamep is set to NULL.
23085 */
23086 int
23087modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23088 char_u *src; /* string with modifiers */
23089 int *usedlen; /* characters after src that are used */
23090 char_u **fnamep; /* file name so far */
23091 char_u **bufp; /* buffer for allocated file name or NULL */
23092 int *fnamelen; /* length of fnamep */
23093{
23094 int valid = 0;
23095 char_u *tail;
23096 char_u *s, *p, *pbuf;
23097 char_u dirname[MAXPATHL];
23098 int c;
23099 int has_fullname = 0;
23100#ifdef WIN3264
23101 int has_shortname = 0;
23102#endif
23103
23104repeat:
23105 /* ":p" - full path/file_name */
23106 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23107 {
23108 has_fullname = 1;
23109
23110 valid |= VALID_PATH;
23111 *usedlen += 2;
23112
23113 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23114 if ((*fnamep)[0] == '~'
23115#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23116 && ((*fnamep)[1] == '/'
23117# ifdef BACKSLASH_IN_FILENAME
23118 || (*fnamep)[1] == '\\'
23119# endif
23120 || (*fnamep)[1] == NUL)
23121
23122#endif
23123 )
23124 {
23125 *fnamep = expand_env_save(*fnamep);
23126 vim_free(*bufp); /* free any allocated file name */
23127 *bufp = *fnamep;
23128 if (*fnamep == NULL)
23129 return -1;
23130 }
23131
23132 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023133 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134 {
23135 if (vim_ispathsep(*p)
23136 && p[1] == '.'
23137 && (p[2] == NUL
23138 || vim_ispathsep(p[2])
23139 || (p[2] == '.'
23140 && (p[3] == NUL || vim_ispathsep(p[3])))))
23141 break;
23142 }
23143
23144 /* FullName_save() is slow, don't use it when not needed. */
23145 if (*p != NUL || !vim_isAbsName(*fnamep))
23146 {
23147 *fnamep = FullName_save(*fnamep, *p != NUL);
23148 vim_free(*bufp); /* free any allocated file name */
23149 *bufp = *fnamep;
23150 if (*fnamep == NULL)
23151 return -1;
23152 }
23153
23154 /* Append a path separator to a directory. */
23155 if (mch_isdir(*fnamep))
23156 {
23157 /* Make room for one or two extra characters. */
23158 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23159 vim_free(*bufp); /* free any allocated file name */
23160 *bufp = *fnamep;
23161 if (*fnamep == NULL)
23162 return -1;
23163 add_pathsep(*fnamep);
23164 }
23165 }
23166
23167 /* ":." - path relative to the current directory */
23168 /* ":~" - path relative to the home directory */
23169 /* ":8" - shortname path - postponed till after */
23170 while (src[*usedlen] == ':'
23171 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23172 {
23173 *usedlen += 2;
23174 if (c == '8')
23175 {
23176#ifdef WIN3264
23177 has_shortname = 1; /* Postpone this. */
23178#endif
23179 continue;
23180 }
23181 pbuf = NULL;
23182 /* Need full path first (use expand_env() to remove a "~/") */
23183 if (!has_fullname)
23184 {
23185 if (c == '.' && **fnamep == '~')
23186 p = pbuf = expand_env_save(*fnamep);
23187 else
23188 p = pbuf = FullName_save(*fnamep, FALSE);
23189 }
23190 else
23191 p = *fnamep;
23192
23193 has_fullname = 0;
23194
23195 if (p != NULL)
23196 {
23197 if (c == '.')
23198 {
23199 mch_dirname(dirname, MAXPATHL);
23200 s = shorten_fname(p, dirname);
23201 if (s != NULL)
23202 {
23203 *fnamep = s;
23204 if (pbuf != NULL)
23205 {
23206 vim_free(*bufp); /* free any allocated file name */
23207 *bufp = pbuf;
23208 pbuf = NULL;
23209 }
23210 }
23211 }
23212 else
23213 {
23214 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23215 /* Only replace it when it starts with '~' */
23216 if (*dirname == '~')
23217 {
23218 s = vim_strsave(dirname);
23219 if (s != NULL)
23220 {
23221 *fnamep = s;
23222 vim_free(*bufp);
23223 *bufp = s;
23224 }
23225 }
23226 }
23227 vim_free(pbuf);
23228 }
23229 }
23230
23231 tail = gettail(*fnamep);
23232 *fnamelen = (int)STRLEN(*fnamep);
23233
23234 /* ":h" - head, remove "/file_name", can be repeated */
23235 /* Don't remove the first "/" or "c:\" */
23236 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23237 {
23238 valid |= VALID_HEAD;
23239 *usedlen += 2;
23240 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023241 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023242 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023243 *fnamelen = (int)(tail - *fnamep);
23244#ifdef VMS
23245 if (*fnamelen > 0)
23246 *fnamelen += 1; /* the path separator is part of the path */
23247#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023248 if (*fnamelen == 0)
23249 {
23250 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23251 p = vim_strsave((char_u *)".");
23252 if (p == NULL)
23253 return -1;
23254 vim_free(*bufp);
23255 *bufp = *fnamep = tail = p;
23256 *fnamelen = 1;
23257 }
23258 else
23259 {
23260 while (tail > s && !after_pathsep(s, tail))
23261 mb_ptr_back(*fnamep, tail);
23262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023263 }
23264
23265 /* ":8" - shortname */
23266 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23267 {
23268 *usedlen += 2;
23269#ifdef WIN3264
23270 has_shortname = 1;
23271#endif
23272 }
23273
23274#ifdef WIN3264
23275 /* Check shortname after we have done 'heads' and before we do 'tails'
23276 */
23277 if (has_shortname)
23278 {
23279 pbuf = NULL;
23280 /* Copy the string if it is shortened by :h */
23281 if (*fnamelen < (int)STRLEN(*fnamep))
23282 {
23283 p = vim_strnsave(*fnamep, *fnamelen);
23284 if (p == 0)
23285 return -1;
23286 vim_free(*bufp);
23287 *bufp = *fnamep = p;
23288 }
23289
23290 /* Split into two implementations - makes it easier. First is where
23291 * there isn't a full name already, second is where there is.
23292 */
23293 if (!has_fullname && !vim_isAbsName(*fnamep))
23294 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023295 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023296 return -1;
23297 }
23298 else
23299 {
23300 int l;
23301
23302 /* Simple case, already have the full-name
23303 * Nearly always shorter, so try first time. */
23304 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023305 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023306 return -1;
23307
23308 if (l == 0)
23309 {
23310 /* Couldn't find the filename.. search the paths.
23311 */
23312 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023313 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023314 return -1;
23315 }
23316 *fnamelen = l;
23317 }
23318 }
23319#endif /* WIN3264 */
23320
23321 /* ":t" - tail, just the basename */
23322 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23323 {
23324 *usedlen += 2;
23325 *fnamelen -= (int)(tail - *fnamep);
23326 *fnamep = tail;
23327 }
23328
23329 /* ":e" - extension, can be repeated */
23330 /* ":r" - root, without extension, can be repeated */
23331 while (src[*usedlen] == ':'
23332 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23333 {
23334 /* find a '.' in the tail:
23335 * - for second :e: before the current fname
23336 * - otherwise: The last '.'
23337 */
23338 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23339 s = *fnamep - 2;
23340 else
23341 s = *fnamep + *fnamelen - 1;
23342 for ( ; s > tail; --s)
23343 if (s[0] == '.')
23344 break;
23345 if (src[*usedlen + 1] == 'e') /* :e */
23346 {
23347 if (s > tail)
23348 {
23349 *fnamelen += (int)(*fnamep - (s + 1));
23350 *fnamep = s + 1;
23351#ifdef VMS
23352 /* cut version from the extension */
23353 s = *fnamep + *fnamelen - 1;
23354 for ( ; s > *fnamep; --s)
23355 if (s[0] == ';')
23356 break;
23357 if (s > *fnamep)
23358 *fnamelen = s - *fnamep;
23359#endif
23360 }
23361 else if (*fnamep <= tail)
23362 *fnamelen = 0;
23363 }
23364 else /* :r */
23365 {
23366 if (s > tail) /* remove one extension */
23367 *fnamelen = (int)(s - *fnamep);
23368 }
23369 *usedlen += 2;
23370 }
23371
23372 /* ":s?pat?foo?" - substitute */
23373 /* ":gs?pat?foo?" - global substitute */
23374 if (src[*usedlen] == ':'
23375 && (src[*usedlen + 1] == 's'
23376 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23377 {
23378 char_u *str;
23379 char_u *pat;
23380 char_u *sub;
23381 int sep;
23382 char_u *flags;
23383 int didit = FALSE;
23384
23385 flags = (char_u *)"";
23386 s = src + *usedlen + 2;
23387 if (src[*usedlen + 1] == 'g')
23388 {
23389 flags = (char_u *)"g";
23390 ++s;
23391 }
23392
23393 sep = *s++;
23394 if (sep)
23395 {
23396 /* find end of pattern */
23397 p = vim_strchr(s, sep);
23398 if (p != NULL)
23399 {
23400 pat = vim_strnsave(s, (int)(p - s));
23401 if (pat != NULL)
23402 {
23403 s = p + 1;
23404 /* find end of substitution */
23405 p = vim_strchr(s, sep);
23406 if (p != NULL)
23407 {
23408 sub = vim_strnsave(s, (int)(p - s));
23409 str = vim_strnsave(*fnamep, *fnamelen);
23410 if (sub != NULL && str != NULL)
23411 {
23412 *usedlen = (int)(p + 1 - src);
23413 s = do_string_sub(str, pat, sub, flags);
23414 if (s != NULL)
23415 {
23416 *fnamep = s;
23417 *fnamelen = (int)STRLEN(s);
23418 vim_free(*bufp);
23419 *bufp = s;
23420 didit = TRUE;
23421 }
23422 }
23423 vim_free(sub);
23424 vim_free(str);
23425 }
23426 vim_free(pat);
23427 }
23428 }
23429 /* after using ":s", repeat all the modifiers */
23430 if (didit)
23431 goto repeat;
23432 }
23433 }
23434
23435 return valid;
23436}
23437
23438/*
23439 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23440 * "flags" can be "g" to do a global substitute.
23441 * Returns an allocated string, NULL for error.
23442 */
23443 char_u *
23444do_string_sub(str, pat, sub, flags)
23445 char_u *str;
23446 char_u *pat;
23447 char_u *sub;
23448 char_u *flags;
23449{
23450 int sublen;
23451 regmatch_T regmatch;
23452 int i;
23453 int do_all;
23454 char_u *tail;
23455 garray_T ga;
23456 char_u *ret;
23457 char_u *save_cpo;
23458
23459 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23460 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023461 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023462
23463 ga_init2(&ga, 1, 200);
23464
23465 do_all = (flags[0] == 'g');
23466
23467 regmatch.rm_ic = p_ic;
23468 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23469 if (regmatch.regprog != NULL)
23470 {
23471 tail = str;
23472 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23473 {
23474 /*
23475 * Get some space for a temporary buffer to do the substitution
23476 * into. It will contain:
23477 * - The text up to where the match is.
23478 * - The substituted text.
23479 * - The text after the match.
23480 */
23481 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23482 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23483 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23484 {
23485 ga_clear(&ga);
23486 break;
23487 }
23488
23489 /* copy the text up to where the match is */
23490 i = (int)(regmatch.startp[0] - tail);
23491 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23492 /* add the substituted text */
23493 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23494 + ga.ga_len + i, TRUE, TRUE, FALSE);
23495 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023496 /* avoid getting stuck on a match with an empty string */
23497 if (tail == regmatch.endp[0])
23498 {
23499 if (*tail == NUL)
23500 break;
23501 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23502 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023503 }
23504 else
23505 {
23506 tail = regmatch.endp[0];
23507 if (*tail == NUL)
23508 break;
23509 }
23510 if (!do_all)
23511 break;
23512 }
23513
23514 if (ga.ga_data != NULL)
23515 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23516
23517 vim_free(regmatch.regprog);
23518 }
23519
23520 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23521 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023522 if (p_cpo == empty_option)
23523 p_cpo = save_cpo;
23524 else
23525 /* Darn, evaluating {sub} expression changed the value. */
23526 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023527
23528 return ret;
23529}
23530
23531#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */