blob: b2ab0adf06699c33671319f0d8467b336f6c1822 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000115
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000116/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000117 * All user-defined global variables are stored in dictionary "globvardict".
118 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120static dict_T globvardict;
121static dictitem_T globvars_var;
122#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123
124/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000125 * Old Vim variables such as "v:version" are also available without the "v:".
126 * Also in functions. We need a special hashtable for them.
127 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000128static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000129
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200130/* When using exists() don't auto-load a script. */
131static int no_autoload = FALSE;
132
Bram Moolenaar532c7802005-01-27 14:44:31 +0000133/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000134 * When recursively copying lists and dicts we need to remember which ones we
135 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000136 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137 */
138static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000139#define COPYID_INC 2
140#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000141
142/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000143 * Array to hold the hashtab with variables local to each sourced script.
144 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000146typedef struct
147{
148 dictitem_T sv_var;
149 dict_T sv_dict;
150} scriptvar_T;
151
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200152static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
153#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
154#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
156static int echo_attr = 0; /* attributes used for ":echo" */
157
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000158/* Values for trans_function_name() argument: */
159#define TFN_INT 1 /* internal function name OK */
160#define TFN_QUIET 2 /* no error messages */
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000361 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200362 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000363};
364
365/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_type vv_di.di_tv.v_type
367#define vv_nr vv_di.di_tv.vval.v_number
368#define vv_float vv_di.di_tv.vval.v_float
369#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000370#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000371#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000372
373/*
374 * The v: variables are stored in dictionary "vimvardict".
375 * "vimvars_var" is the variable that is used for the "l:" scope.
376 */
377static dict_T vimvardict;
378static dictitem_T vimvars_var;
379#define vimvarht vimvardict.dv_hashtab
380
Bram Moolenaara40058a2005-07-11 22:42:07 +0000381static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
382static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
384static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
385static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000386static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
387static void list_glob_vars __ARGS((int *first));
388static void list_buf_vars __ARGS((int *first));
389static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000390#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000392#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000393static void list_vim_vars __ARGS((int *first));
394static void list_script_vars __ARGS((int *first));
395static void list_func_vars __ARGS((int *first));
396static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
398static int check_changedtick __ARGS((char_u *arg));
399static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
400static void clear_lval __ARGS((lval_T *lp));
401static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
402static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
403static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
404static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
405static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
406static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
407static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
408static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
409static void item_lock __ARGS((typval_T *tv, int deep, int lock));
410static int tv_islocked __ARGS((typval_T *tv));
411
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
413static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000418static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
419static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000420
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000421static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
424static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
425static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000426static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static void listitem_free __ARGS((listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100429static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
430static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
431static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000432static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000434static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
436static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000437static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100439static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000441static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200442static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000443static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
445static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000446static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000448static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000450static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
451static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#ifdef FEAT_FLOAT
454static int string2float __ARGS((char_u *text, float_T *value));
455#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
457static int find_internal_func __ARGS((char_u *name));
458static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
459static 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 +0200460static 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 +0000461static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000462static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
465static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200466static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100469static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200475static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200477static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000478#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000479static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000490#ifdef FEAT_FLOAT
491static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
492#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000493static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000494static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000496static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000498#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000499static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000500static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
502#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000505#ifdef FEAT_FLOAT
506static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200507static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
512static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200523#ifdef FEAT_FLOAT
524static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
525#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000528static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000529static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534#ifdef FEAT_FLOAT
535static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200537static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000538#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000539static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000548static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000550static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000556static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000564static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000565static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000566static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000567static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200570static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000571static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000579static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000593static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100598static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000600static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000612#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200613static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000614static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
615#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200616#ifdef FEAT_LUA
617static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
618#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000619static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000623static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000624static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000627static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000631#ifdef vim_mkdir
632static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100635#ifdef FEAT_MZSCHEME
636static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
637#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000638static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100640static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000641static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000642#ifdef FEAT_FLOAT
643static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000646static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000647static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200648#ifdef FEAT_PYTHON3
649static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
651#ifdef FEAT_PYTHON
652static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
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 Moolenaard6e256c2011-12-14 15:32:50 +0100758static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000760static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000761static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static int get_env_len __ARGS((char_u **arg));
763static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000764static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000765static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
766#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
767#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
768 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000769static 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 +0000770static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000771static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000772static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
773static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000774static typval_T *alloc_tv __ARGS((void));
775static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static void init_tv __ARGS((typval_T *varp));
777static long get_tv_number __ARGS((typval_T *varp));
778static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000779static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780static char_u *get_tv_string __ARGS((typval_T *varp));
781static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000782static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000784static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000785static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
786static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
787static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000788static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
789static 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 +0000790static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
791static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000792static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200793static int var_check_func_name __ARGS((char_u *name, int new_var));
794static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000795static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000796static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
798static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
799static int eval_fname_script __ARGS((char_u *p));
800static int eval_fname_sid __ARGS((char_u *p));
801static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000802static ufunc_T *find_func __ARGS((char_u *name));
803static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000804static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000805#ifdef FEAT_PROFILE
806static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000807static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
808static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
809static int
810# ifdef __BORLANDC__
811 _RTLENTRYF
812# endif
813 prof_total_cmp __ARGS((const void *s1, const void *s2));
814static int
815# ifdef __BORLANDC__
816 _RTLENTRYF
817# endif
818 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000820static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000821static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000822static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000823static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000824static 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 +0000825static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
826static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000827static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000828static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
829static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000830static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000831static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000832static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000833
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200834
835#ifdef EBCDIC
836static int compare_func_name __ARGS((const void *s1, const void *s2));
837static void sortFunctions __ARGS(());
838#endif
839
840
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000841/* Character used as separated in autoload function/variable names. */
842#define AUTOLOAD_CHAR '#'
843
Bram Moolenaar33570922005-01-25 22:26:29 +0000844/*
845 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000846 */
847 void
848eval_init()
849{
Bram Moolenaar33570922005-01-25 22:26:29 +0000850 int i;
851 struct vimvar *p;
852
853 init_var_dict(&globvardict, &globvars_var);
854 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200855 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000856 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000857 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000858
859 for (i = 0; i < VV_LEN; ++i)
860 {
861 p = &vimvars[i];
862 STRCPY(p->vv_di.di_key, p->vv_name);
863 if (p->vv_flags & VV_RO)
864 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
865 else if (p->vv_flags & VV_RO_SBX)
866 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
867 else
868 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000869
870 /* add to v: scope dict, unless the value is not always available */
871 if (p->vv_type != VAR_UNKNOWN)
872 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000873 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000874 /* add to compat scope dict */
875 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000876 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000877 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200878 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200879
880#ifdef EBCDIC
881 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100882 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200883 */
884 sortFunctions();
885#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000886}
887
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000888#if defined(EXITFREE) || defined(PROTO)
889 void
890eval_clear()
891{
892 int i;
893 struct vimvar *p;
894
895 for (i = 0; i < VV_LEN; ++i)
896 {
897 p = &vimvars[i];
898 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000899 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000900 vim_free(p->vv_str);
901 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000902 }
903 else if (p->vv_di.di_tv.v_type == VAR_LIST)
904 {
905 list_unref(p->vv_list);
906 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908 }
909 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000910 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911 hash_clear(&compat_hashtab);
912
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 free_scriptnames();
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200914 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000915
916 /* global variables */
917 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000918
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000919 /* autoloaded script names */
920 ga_clear_strings(&ga_loaded);
921
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200922 /* script-local variables */
923 for (i = 1; i <= ga_scripts.ga_len; ++i)
924 {
925 vars_clear(&SCRIPT_VARS(i));
926 vim_free(SCRIPT_SV(i));
927 }
928 ga_clear(&ga_scripts);
929
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000930 /* unreferenced lists and dicts */
931 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000932
933 /* functions */
934 free_all_functions();
935 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000936}
937#endif
938
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 * Return the name of the executed function.
941 */
942 char_u *
943func_name(cookie)
944 void *cookie;
945{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000946 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947}
948
949/*
950 * Return the address holding the next breakpoint line for a funccall cookie.
951 */
952 linenr_T *
953func_breakpoint(cookie)
954 void *cookie;
955{
Bram Moolenaar33570922005-01-25 22:26:29 +0000956 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957}
958
959/*
960 * Return the address holding the debug tick for a funccall cookie.
961 */
962 int *
963func_dbg_tick(cookie)
964 void *cookie;
965{
Bram Moolenaar33570922005-01-25 22:26:29 +0000966 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967}
968
969/*
970 * Return the nesting level for a funccall cookie.
971 */
972 int
973func_level(cookie)
974 void *cookie;
975{
Bram Moolenaar33570922005-01-25 22:26:29 +0000976 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977}
978
979/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000980funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000982/* pointer to list of previously used funccal, still around because some
983 * item in it is still being used. */
984funccall_T *previous_funccal = NULL;
985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986/*
987 * Return TRUE when a function was ended by a ":return" command.
988 */
989 int
990current_func_returned()
991{
992 return current_funccal->returned;
993}
994
995
996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 * Set an internal variable to a string value. Creates the variable if it does
998 * not already exist.
999 */
1000 void
1001set_internal_string_var(name, value)
1002 char_u *name;
1003 char_u *value;
1004{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001005 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001006 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007
1008 val = vim_strsave(value);
1009 if (val != NULL)
1010 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001011 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001012 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001014 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001015 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 }
1017 }
1018}
1019
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001020static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001021static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001022static char_u *redir_endp = NULL;
1023static char_u *redir_varname = NULL;
1024
1025/*
1026 * Start recording command output to a variable
1027 * Returns OK if successfully completed the setup. FAIL otherwise.
1028 */
1029 int
1030var_redir_start(name, append)
1031 char_u *name;
1032 int append; /* append to an existing variable */
1033{
1034 int save_emsg;
1035 int err;
1036 typval_T tv;
1037
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001038 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001039 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001040 {
1041 EMSG(_(e_invarg));
1042 return FAIL;
1043 }
1044
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001045 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001046 redir_varname = vim_strsave(name);
1047 if (redir_varname == NULL)
1048 return FAIL;
1049
1050 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1051 if (redir_lval == NULL)
1052 {
1053 var_redir_stop();
1054 return FAIL;
1055 }
1056
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001057 /* The output is stored in growarray "redir_ga" until redirection ends. */
1058 ga_init2(&redir_ga, (int)sizeof(char), 500);
1059
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001061 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1062 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1064 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001065 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 if (redir_endp != NULL && *redir_endp != NUL)
1067 /* Trailing characters are present after the variable name */
1068 EMSG(_(e_trailing));
1069 else
1070 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001071 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 var_redir_stop();
1073 return FAIL;
1074 }
1075
1076 /* check if we can write to the variable: set it to or append an empty
1077 * string */
1078 save_emsg = did_emsg;
1079 did_emsg = FALSE;
1080 tv.v_type = VAR_STRING;
1081 tv.vval.v_string = (char_u *)"";
1082 if (append)
1083 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1084 else
1085 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001086 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001088 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089 if (err)
1090 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001091 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 var_redir_stop();
1093 return FAIL;
1094 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095
1096 return OK;
1097}
1098
1099/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001100 * Append "value[value_len]" to the variable set by var_redir_start().
1101 * The actual appending is postponed until redirection ends, because the value
1102 * appended may in fact be the string we write to, changing it may cause freed
1103 * memory to be used:
1104 * :redir => foo
1105 * :let foo
1106 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 */
1108 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001111 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001113 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114
1115 if (redir_lval == NULL)
1116 return;
1117
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001121 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001123 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 {
1125 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001126 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 }
1128 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130}
1131
1132/*
1133 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001134 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 */
1136 void
1137var_redir_stop()
1138{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001139 typval_T tv;
1140
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 if (redir_lval != NULL)
1142 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001143 /* If there was no error: assign the text to the variable. */
1144 if (redir_endp != NULL)
1145 {
1146 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1147 tv.v_type = VAR_STRING;
1148 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001149 /* Call get_lval() again, if it's inside a Dict or List it may
1150 * have changed. */
1151 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1152 FALSE, FALSE, FALSE, FNE_CHECK_START);
1153 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1154 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1155 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001156 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001158 /* free the collected output */
1159 vim_free(redir_ga.ga_data);
1160 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001161
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162 vim_free(redir_lval);
1163 redir_lval = NULL;
1164 }
1165 vim_free(redir_varname);
1166 redir_varname = NULL;
1167}
1168
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169# if defined(FEAT_MBYTE) || defined(PROTO)
1170 int
1171eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1172 char_u *enc_from;
1173 char_u *enc_to;
1174 char_u *fname_from;
1175 char_u *fname_to;
1176{
1177 int err = FALSE;
1178
1179 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1180 set_vim_var_string(VV_CC_TO, enc_to, -1);
1181 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1182 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1183 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1184 err = TRUE;
1185 set_vim_var_string(VV_CC_FROM, NULL, -1);
1186 set_vim_var_string(VV_CC_TO, NULL, -1);
1187 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1188 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1189
1190 if (err)
1191 return FAIL;
1192 return OK;
1193}
1194# endif
1195
1196# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1197 int
1198eval_printexpr(fname, args)
1199 char_u *fname;
1200 char_u *args;
1201{
1202 int err = FALSE;
1203
1204 set_vim_var_string(VV_FNAME_IN, fname, -1);
1205 set_vim_var_string(VV_CMDARG, args, -1);
1206 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1207 err = TRUE;
1208 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1209 set_vim_var_string(VV_CMDARG, NULL, -1);
1210
1211 if (err)
1212 {
1213 mch_remove(fname);
1214 return FAIL;
1215 }
1216 return OK;
1217}
1218# endif
1219
1220# if defined(FEAT_DIFF) || defined(PROTO)
1221 void
1222eval_diff(origfile, newfile, outfile)
1223 char_u *origfile;
1224 char_u *newfile;
1225 char_u *outfile;
1226{
1227 int err = FALSE;
1228
1229 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1230 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1231 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1232 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1233 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1234 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1235 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1236}
1237
1238 void
1239eval_patch(origfile, difffile, outfile)
1240 char_u *origfile;
1241 char_u *difffile;
1242 char_u *outfile;
1243{
1244 int err;
1245
1246 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1247 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1248 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1249 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1250 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1251 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1252 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1253}
1254# endif
1255
1256/*
1257 * Top level evaluation function, returning a boolean.
1258 * Sets "error" to TRUE if there was an error.
1259 * Return TRUE or FALSE.
1260 */
1261 int
1262eval_to_bool(arg, error, nextcmd, skip)
1263 char_u *arg;
1264 int *error;
1265 char_u **nextcmd;
1266 int skip; /* only parse, don't execute */
1267{
Bram Moolenaar33570922005-01-25 22:26:29 +00001268 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 int retval = FALSE;
1270
1271 if (skip)
1272 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001273 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 else
1276 {
1277 *error = FALSE;
1278 if (!skip)
1279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001280 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001281 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
1283 }
1284 if (skip)
1285 --emsg_skip;
1286
1287 return retval;
1288}
1289
1290/*
1291 * Top level evaluation function, returning a string. If "skip" is TRUE,
1292 * only parsing to "nextcmd" is done, without reporting errors. Return
1293 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1294 */
1295 char_u *
1296eval_to_string_skip(arg, nextcmd, skip)
1297 char_u *arg;
1298 char_u **nextcmd;
1299 int skip; /* only parse, don't execute */
1300{
Bram Moolenaar33570922005-01-25 22:26:29 +00001301 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 char_u *retval;
1303
1304 if (skip)
1305 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 retval = NULL;
1308 else
1309 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001310 retval = vim_strsave(get_tv_string(&tv));
1311 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 if (skip)
1314 --emsg_skip;
1315
1316 return retval;
1317}
1318
1319/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001320 * Skip over an expression at "*pp".
1321 * Return FAIL for an error, OK otherwise.
1322 */
1323 int
1324skip_expr(pp)
1325 char_u **pp;
1326{
Bram Moolenaar33570922005-01-25 22:26:29 +00001327 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001328
1329 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001330 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001331}
1332
1333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001335 * When "convert" is TRUE convert a List into a sequence of lines and convert
1336 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 * Return pointer to allocated memory, or NULL for failure.
1338 */
1339 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001340eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 char_u *arg;
1342 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001343 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
Bram Moolenaar33570922005-01-25 22:26:29 +00001345 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001347 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001348#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001352 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 retval = NULL;
1354 else
1355 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001356 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001357 {
1358 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001359 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001360 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001361 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001362 if (tv.vval.v_list->lv_len > 0)
1363 ga_append(&ga, NL);
1364 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001365 ga_append(&ga, NUL);
1366 retval = (char_u *)ga.ga_data;
1367 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001368#ifdef FEAT_FLOAT
1369 else if (convert && tv.v_type == VAR_FLOAT)
1370 {
1371 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1372 retval = vim_strsave(numbuf);
1373 }
1374#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001375 else
1376 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001377 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 }
1379
1380 return retval;
1381}
1382
1383/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001384 * Call eval_to_string() without using current local variables and using
1385 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 */
1387 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001388eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 char_u *arg;
1390 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392{
1393 char_u *retval;
1394 void *save_funccalp;
1395
1396 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397 if (use_sandbox)
1398 ++sandbox;
1399 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001400 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001401 if (use_sandbox)
1402 --sandbox;
1403 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 restore_funccal(save_funccalp);
1405 return retval;
1406}
1407
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408/*
1409 * Top level evaluation function, returning a number.
1410 * Evaluates "expr" silently.
1411 * Returns -1 for an error.
1412 */
1413 int
1414eval_to_number(expr)
1415 char_u *expr;
1416{
Bram Moolenaar33570922005-01-25 22:26:29 +00001417 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001419 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
1421 ++emsg_off;
1422
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001423 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 retval = -1;
1425 else
1426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001427 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001428 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 }
1430 --emsg_off;
1431
1432 return retval;
1433}
1434
Bram Moolenaara40058a2005-07-11 22:42:07 +00001435/*
1436 * Prepare v: variable "idx" to be used.
1437 * Save the current typeval in "save_tv".
1438 * When not used yet add the variable to the v: hashtable.
1439 */
1440 static void
1441prepare_vimvar(idx, save_tv)
1442 int idx;
1443 typval_T *save_tv;
1444{
1445 *save_tv = vimvars[idx].vv_tv;
1446 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1447 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1448}
1449
1450/*
1451 * Restore v: variable "idx" to typeval "save_tv".
1452 * When no longer defined, remove the variable from the v: hashtable.
1453 */
1454 static void
1455restore_vimvar(idx, save_tv)
1456 int idx;
1457 typval_T *save_tv;
1458{
1459 hashitem_T *hi;
1460
Bram Moolenaara40058a2005-07-11 22:42:07 +00001461 vimvars[idx].vv_tv = *save_tv;
1462 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1463 {
1464 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1465 if (HASHITEM_EMPTY(hi))
1466 EMSG2(_(e_intern2), "restore_vimvar()");
1467 else
1468 hash_remove(&vimvarht, hi);
1469 }
1470}
1471
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001472#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001473/*
1474 * Evaluate an expression to a list with suggestions.
1475 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001476 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001477 */
1478 list_T *
1479eval_spell_expr(badword, expr)
1480 char_u *badword;
1481 char_u *expr;
1482{
1483 typval_T save_val;
1484 typval_T rettv;
1485 list_T *list = NULL;
1486 char_u *p = skipwhite(expr);
1487
1488 /* Set "v:val" to the bad word. */
1489 prepare_vimvar(VV_VAL, &save_val);
1490 vimvars[VV_VAL].vv_type = VAR_STRING;
1491 vimvars[VV_VAL].vv_str = badword;
1492 if (p_verbose == 0)
1493 ++emsg_off;
1494
1495 if (eval1(&p, &rettv, TRUE) == OK)
1496 {
1497 if (rettv.v_type != VAR_LIST)
1498 clear_tv(&rettv);
1499 else
1500 list = rettv.vval.v_list;
1501 }
1502
1503 if (p_verbose == 0)
1504 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001505 restore_vimvar(VV_VAL, &save_val);
1506
1507 return list;
1508}
1509
1510/*
1511 * "list" is supposed to contain two items: a word and a number. Return the
1512 * word in "pp" and the number as the return value.
1513 * Return -1 if anything isn't right.
1514 * Used to get the good word and score from the eval_spell_expr() result.
1515 */
1516 int
1517get_spellword(list, pp)
1518 list_T *list;
1519 char_u **pp;
1520{
1521 listitem_T *li;
1522
1523 li = list->lv_first;
1524 if (li == NULL)
1525 return -1;
1526 *pp = get_tv_string(&li->li_tv);
1527
1528 li = li->li_next;
1529 if (li == NULL)
1530 return -1;
1531 return get_tv_number(&li->li_tv);
1532}
1533#endif
1534
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001535/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001536 * Top level evaluation function.
1537 * Returns an allocated typval_T with the result.
1538 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001539 */
1540 typval_T *
1541eval_expr(arg, nextcmd)
1542 char_u *arg;
1543 char_u **nextcmd;
1544{
1545 typval_T *tv;
1546
1547 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001548 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001549 {
1550 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001551 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001552 }
1553
1554 return tv;
1555}
1556
1557
Bram Moolenaar4f688582007-07-24 12:34:30 +00001558#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1559 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001561 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001562 * Uses argv[argc] for the function arguments. Only Number and String
1563 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001564 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001566 int
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001567call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 char_u *func;
1569 int argc;
1570 char_u **argv;
1571 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573{
Bram Moolenaar33570922005-01-25 22:26:29 +00001574 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 long n;
1576 int len;
1577 int i;
1578 int doesrange;
1579 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001582 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001584 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
1586 for (i = 0; i < argc; i++)
1587 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001588 /* Pass a NULL or empty argument as an empty string */
1589 if (argv[i] == NULL || *argv[i] == NUL)
1590 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001591 argvars[i].v_type = VAR_STRING;
1592 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001593 continue;
1594 }
1595
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 /* Recognize a number argument, the others must be strings. */
1597 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1598 if (len != 0 && len == (int)STRLEN(argv[i]))
1599 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001600 argvars[i].v_type = VAR_NUMBER;
1601 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 }
1603 else
1604 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001605 argvars[i].v_type = VAR_STRING;
1606 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 }
1608 }
1609
1610 if (safe)
1611 {
1612 save_funccalp = save_funccal();
1613 ++sandbox;
1614 }
1615
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001616 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1617 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001619 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 if (safe)
1621 {
1622 --sandbox;
1623 restore_funccal(save_funccalp);
1624 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001625 vim_free(argvars);
1626
1627 if (ret == FAIL)
1628 clear_tv(rettv);
1629
1630 return ret;
1631}
1632
Bram Moolenaar4f688582007-07-24 12:34:30 +00001633# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001635 * Call vimL function "func" and return the result as a string.
1636 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 * Uses argv[argc] for the function arguments.
1638 */
1639 void *
1640call_func_retstr(func, argc, argv, safe)
1641 char_u *func;
1642 int argc;
1643 char_u **argv;
1644 int safe; /* use the sandbox */
1645{
1646 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001647 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001648
1649 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1650 return NULL;
1651
1652 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 return retval;
1655}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001656# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001657
Bram Moolenaar4f688582007-07-24 12:34:30 +00001658# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001659/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001660 * Call vimL function "func" and return the result as a number.
1661 * Returns -1 when calling the function fails.
1662 * Uses argv[argc] for the function arguments.
1663 */
1664 long
1665call_func_retnr(func, argc, argv, safe)
1666 char_u *func;
1667 int argc;
1668 char_u **argv;
1669 int safe; /* use the sandbox */
1670{
1671 typval_T rettv;
1672 long retval;
1673
1674 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1675 return -1;
1676
1677 retval = get_tv_number_chk(&rettv, NULL);
1678 clear_tv(&rettv);
1679 return retval;
1680}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001681# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001682
1683/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001684 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001685 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001686 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001687 */
1688 void *
1689call_func_retlist(func, argc, argv, safe)
1690 char_u *func;
1691 int argc;
1692 char_u **argv;
1693 int safe; /* use the sandbox */
1694{
1695 typval_T rettv;
1696
1697 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1698 return NULL;
1699
1700 if (rettv.v_type != VAR_LIST)
1701 {
1702 clear_tv(&rettv);
1703 return NULL;
1704 }
1705
1706 return rettv.vval.v_list;
1707}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708#endif
1709
Bram Moolenaar4f688582007-07-24 12:34:30 +00001710
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711/*
1712 * Save the current function call pointer, and set it to NULL.
1713 * Used when executing autocommands and for ":source".
1714 */
1715 void *
1716save_funccal()
1717{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001718 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 current_funccal = NULL;
1721 return (void *)fc;
1722}
1723
1724 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001725restore_funccal(vfc)
1726 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001728 funccall_T *fc = (funccall_T *)vfc;
1729
1730 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731}
1732
Bram Moolenaar05159a02005-02-26 23:04:13 +00001733#if defined(FEAT_PROFILE) || defined(PROTO)
1734/*
1735 * Prepare profiling for entering a child or something else that is not
1736 * counted for the script/function itself.
1737 * Should always be called in pair with prof_child_exit().
1738 */
1739 void
1740prof_child_enter(tm)
1741 proftime_T *tm; /* place to store waittime */
1742{
1743 funccall_T *fc = current_funccal;
1744
1745 if (fc != NULL && fc->func->uf_profiling)
1746 profile_start(&fc->prof_child);
1747 script_prof_save(tm);
1748}
1749
1750/*
1751 * Take care of time spent in a child.
1752 * Should always be called after prof_child_enter().
1753 */
1754 void
1755prof_child_exit(tm)
1756 proftime_T *tm; /* where waittime was stored */
1757{
1758 funccall_T *fc = current_funccal;
1759
1760 if (fc != NULL && fc->func->uf_profiling)
1761 {
1762 profile_end(&fc->prof_child);
1763 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1764 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1765 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1766 }
1767 script_prof_restore(tm);
1768}
1769#endif
1770
1771
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#ifdef FEAT_FOLDING
1773/*
1774 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1775 * it in "*cp". Doesn't give error messages.
1776 */
1777 int
1778eval_foldexpr(arg, cp)
1779 char_u *arg;
1780 int *cp;
1781{
Bram Moolenaar33570922005-01-25 22:26:29 +00001782 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 int retval;
1784 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001785 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1786 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
1788 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001789 if (use_sandbox)
1790 ++sandbox;
1791 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 retval = 0;
1795 else
1796 {
1797 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001798 if (tv.v_type == VAR_NUMBER)
1799 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001800 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 retval = 0;
1802 else
1803 {
1804 /* If the result is a string, check if there is a non-digit before
1805 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 if (!VIM_ISDIGIT(*s) && *s != '-')
1808 *cp = *s++;
1809 retval = atol((char *)s);
1810 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001811 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 }
1813 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001814 if (use_sandbox)
1815 --sandbox;
1816 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
1818 return retval;
1819}
1820#endif
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001823 * ":let" list all variable values
1824 * ":let var1 var2" list variable values
1825 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001826 * ":let var += expr" assignment command.
1827 * ":let var -= expr" assignment command.
1828 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 */
1831 void
1832ex_let(eap)
1833 exarg_T *eap;
1834{
1835 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001836 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001837 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 int var_count = 0;
1840 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001841 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001842 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001843 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844
Bram Moolenaardb552d602006-03-23 22:59:57 +00001845 argend = skip_var_list(arg, &var_count, &semicolon);
1846 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001848 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1849 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001850 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001853 /*
1854 * ":let" without "=": list variables
1855 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001856 if (*arg == '[')
1857 EMSG(_(e_invarg));
1858 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001860 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 list_glob_vars(&first);
1865 list_buf_vars(&first);
1866 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001867#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001868 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001869#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001870 list_script_vars(&first);
1871 list_func_vars(&first);
1872 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 eap->nextcmd = check_nextcmd(arg);
1875 }
1876 else
1877 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001878 op[0] = '=';
1879 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001880 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001881 {
1882 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1883 op[0] = expr[-1]; /* +=, -= or .= */
1884 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (eap->skip)
1888 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001889 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 if (eap->skip)
1891 {
1892 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 --emsg_skip;
1895 }
1896 else if (i != FAIL)
1897 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001898 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001899 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 }
1902 }
1903}
1904
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001905/*
1906 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1907 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001908 * When "nextchars" is not NULL it points to a string with characters that
1909 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1910 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 * Returns OK or FAIL;
1912 */
1913 static int
1914ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1915 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001916 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917 int copy; /* copy values from "tv", don't move */
1918 int semicolon; /* from skip_var_list() */
1919 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001920 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921{
1922 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001923 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001925 listitem_T *item;
1926 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927
1928 if (*arg != '[')
1929 {
1930 /*
1931 * ":let var = expr" or ":for var in list"
1932 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001933 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 return FAIL;
1935 return OK;
1936 }
1937
1938 /*
1939 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1940 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001941 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 {
1943 EMSG(_(e_listreq));
1944 return FAIL;
1945 }
1946
1947 i = list_len(l);
1948 if (semicolon == 0 && var_count < i)
1949 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001950 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 return FAIL;
1952 }
1953 if (var_count - semicolon > i)
1954 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001955 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 return FAIL;
1957 }
1958
1959 item = l->lv_first;
1960 while (*arg != ']')
1961 {
1962 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001963 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 item = item->li_next;
1965 if (arg == NULL)
1966 return FAIL;
1967
1968 arg = skipwhite(arg);
1969 if (*arg == ';')
1970 {
1971 /* Put the rest of the list (may be empty) in the var after ';'.
1972 * Create a new list for this. */
1973 l = list_alloc();
1974 if (l == NULL)
1975 return FAIL;
1976 while (item != NULL)
1977 {
1978 list_append_tv(l, &item->li_tv);
1979 item = item->li_next;
1980 }
1981
1982 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001983 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984 ltv.vval.v_list = l;
1985 l->lv_refcount = 1;
1986
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001987 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1988 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 clear_tv(&ltv);
1990 if (arg == NULL)
1991 return FAIL;
1992 break;
1993 }
1994 else if (*arg != ',' && *arg != ']')
1995 {
1996 EMSG2(_(e_intern2), "ex_let_vars()");
1997 return FAIL;
1998 }
1999 }
2000
2001 return OK;
2002}
2003
2004/*
2005 * Skip over assignable variable "var" or list of variables "[var, var]".
2006 * Used for ":let varvar = expr" and ":for varvar in expr".
2007 * For "[var, var]" increment "*var_count" for each variable.
2008 * for "[var, var; var]" set "semicolon".
2009 * Return NULL for an error.
2010 */
2011 static char_u *
2012skip_var_list(arg, var_count, semicolon)
2013 char_u *arg;
2014 int *var_count;
2015 int *semicolon;
2016{
2017 char_u *p, *s;
2018
2019 if (*arg == '[')
2020 {
2021 /* "[var, var]": find the matching ']'. */
2022 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002023 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 {
2025 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2026 s = skip_var_one(p);
2027 if (s == p)
2028 {
2029 EMSG2(_(e_invarg2), p);
2030 return NULL;
2031 }
2032 ++*var_count;
2033
2034 p = skipwhite(s);
2035 if (*p == ']')
2036 break;
2037 else if (*p == ';')
2038 {
2039 if (*semicolon == 1)
2040 {
2041 EMSG(_("Double ; in list of variables"));
2042 return NULL;
2043 }
2044 *semicolon = 1;
2045 }
2046 else if (*p != ',')
2047 {
2048 EMSG2(_(e_invarg2), p);
2049 return NULL;
2050 }
2051 }
2052 return p + 1;
2053 }
2054 else
2055 return skip_var_one(arg);
2056}
2057
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002058/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002059 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002060 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002061 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002062 static char_u *
2063skip_var_one(arg)
2064 char_u *arg;
2065{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002066 if (*arg == '@' && arg[1] != NUL)
2067 return arg + 2;
2068 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2069 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002070}
2071
Bram Moolenaara7043832005-01-21 11:56:39 +00002072/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002073 * List variables for hashtab "ht" with prefix "prefix".
2074 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002075 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002076 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002077list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002078 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002079 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002080 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002081 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002082{
Bram Moolenaar33570922005-01-25 22:26:29 +00002083 hashitem_T *hi;
2084 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002085 int todo;
2086
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002087 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002088 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2089 {
2090 if (!HASHITEM_EMPTY(hi))
2091 {
2092 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002093 di = HI2DI(hi);
2094 if (empty || di->di_tv.v_type != VAR_STRING
2095 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002096 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002097 }
2098 }
2099}
2100
2101/*
2102 * List global variables.
2103 */
2104 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002105list_glob_vars(first)
2106 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002107{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002108 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002109}
2110
2111/*
2112 * List buffer variables.
2113 */
2114 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002115list_buf_vars(first)
2116 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002117{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118 char_u numbuf[NUMBUFLEN];
2119
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2121 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002122
2123 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002124 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2125 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002126}
2127
2128/*
2129 * List window variables.
2130 */
2131 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132list_win_vars(first)
2133 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002134{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002135 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2136 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002137}
2138
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002139#ifdef FEAT_WINDOWS
2140/*
2141 * List tab page variables.
2142 */
2143 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144list_tab_vars(first)
2145 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002146{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002147 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2148 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002149}
2150#endif
2151
Bram Moolenaara7043832005-01-21 11:56:39 +00002152/*
2153 * List Vim variables.
2154 */
2155 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156list_vim_vars(first)
2157 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002158{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160}
2161
2162/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002163 * List script-local variables, if there is a script.
2164 */
2165 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166list_script_vars(first)
2167 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002168{
2169 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2171 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002172}
2173
2174/*
2175 * List function variables, if there is a function.
2176 */
2177 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178list_func_vars(first)
2179 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180{
2181 if (current_funccal != NULL)
2182 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002184}
2185
2186/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 * List variables in "arg".
2188 */
2189 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 exarg_T *eap;
2192 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194{
2195 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002196 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002198 char_u *name_start;
2199 char_u *arg_subsc;
2200 char_u *tofree;
2201 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202
2203 while (!ends_excmd(*arg) && !got_int)
2204 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002205 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002207 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002208 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2209 {
2210 emsg_severe = TRUE;
2211 EMSG(_(e_trailing));
2212 break;
2213 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002215 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002217 /* get_name_len() takes care of expanding curly braces */
2218 name_start = name = arg;
2219 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2220 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 /* This is mainly to keep test 49 working: when expanding
2223 * curly braces fails overrule the exception error message. */
2224 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002226 emsg_severe = TRUE;
2227 EMSG2(_(e_invarg2), arg);
2228 break;
2229 }
2230 error = TRUE;
2231 }
2232 else
2233 {
2234 if (tofree != NULL)
2235 name = tofree;
2236 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 else
2239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 /* handle d.key, l[idx], f(expr) */
2241 arg_subsc = arg;
2242 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002243 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002247 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002249 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 case 'g': list_glob_vars(first); break;
2251 case 'b': list_buf_vars(first); break;
2252 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002253#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002254 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002255#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002256 case 'v': list_vim_vars(first); break;
2257 case 's': list_script_vars(first); break;
2258 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 default:
2260 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002261 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002262 }
2263 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 {
2265 char_u numbuf[NUMBUFLEN];
2266 char_u *tf;
2267 int c;
2268 char_u *s;
2269
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002270 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002271 c = *arg;
2272 *arg = NUL;
2273 list_one_var_a((char_u *)"",
2274 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002275 tv.v_type,
2276 s == NULL ? (char_u *)"" : s,
2277 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 *arg = c;
2279 vim_free(tf);
2280 }
2281 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002282 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 }
2284 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285
2286 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288
2289 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 }
2291
2292 return arg;
2293}
2294
2295/*
2296 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2297 * Returns a pointer to the char just after the var name.
2298 * Returns NULL if there is an error.
2299 */
2300 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002301ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002303 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002305 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002306 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307{
2308 int c1;
2309 char_u *name;
2310 char_u *p;
2311 char_u *arg_end = NULL;
2312 int len;
2313 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002314 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315
2316 /*
2317 * ":let $VAR = expr": Set environment variable.
2318 */
2319 if (*arg == '$')
2320 {
2321 /* Find the end of the name. */
2322 ++arg;
2323 name = arg;
2324 len = get_env_len(&arg);
2325 if (len == 0)
2326 EMSG2(_(e_invarg2), name - 1);
2327 else
2328 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002329 if (op != NULL && (*op == '+' || *op == '-'))
2330 EMSG2(_(e_letwrong), op);
2331 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002332 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002334 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 {
2336 c1 = name[len];
2337 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002338 p = get_tv_string_chk(tv);
2339 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002340 {
2341 int mustfree = FALSE;
2342 char_u *s = vim_getenv(name, &mustfree);
2343
2344 if (s != NULL)
2345 {
2346 p = tofree = concat_str(s, p);
2347 if (mustfree)
2348 vim_free(s);
2349 }
2350 }
2351 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002354 if (STRICMP(name, "HOME") == 0)
2355 init_homedir();
2356 else if (didset_vim && STRICMP(name, "VIM") == 0)
2357 didset_vim = FALSE;
2358 else if (didset_vimruntime
2359 && STRICMP(name, "VIMRUNTIME") == 0)
2360 didset_vimruntime = FALSE;
2361 arg_end = arg;
2362 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 }
2366 }
2367 }
2368
2369 /*
2370 * ":let &option = expr": Set option value.
2371 * ":let &l:option = expr": Set local option value.
2372 * ":let &g:option = expr": Set global option value.
2373 */
2374 else if (*arg == '&')
2375 {
2376 /* Find the end of the name. */
2377 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002378 if (p == NULL || (endchars != NULL
2379 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 EMSG(_(e_letunexp));
2381 else
2382 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383 long n;
2384 int opt_type;
2385 long numval;
2386 char_u *stringval = NULL;
2387 char_u *s;
2388
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 c1 = *p;
2390 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391
2392 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002393 s = get_tv_string_chk(tv); /* != NULL if number or string */
2394 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002395 {
2396 opt_type = get_option_value(arg, &numval,
2397 &stringval, opt_flags);
2398 if ((opt_type == 1 && *op == '.')
2399 || (opt_type == 0 && *op != '.'))
2400 EMSG2(_(e_letwrong), op);
2401 else
2402 {
2403 if (opt_type == 1) /* number */
2404 {
2405 if (*op == '+')
2406 n = numval + n;
2407 else
2408 n = numval - n;
2409 }
2410 else if (opt_type == 0 && stringval != NULL) /* string */
2411 {
2412 s = concat_str(stringval, s);
2413 vim_free(stringval);
2414 stringval = s;
2415 }
2416 }
2417 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002418 if (s != NULL)
2419 {
2420 set_option_value(arg, n, s, opt_flags);
2421 arg_end = p;
2422 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002424 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002425 }
2426 }
2427
2428 /*
2429 * ":let @r = expr": Set register contents.
2430 */
2431 else if (*arg == '@')
2432 {
2433 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002434 if (op != NULL && (*op == '+' || *op == '-'))
2435 EMSG2(_(e_letwrong), op);
2436 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002437 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 EMSG(_(e_letunexp));
2439 else
2440 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002441 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 char_u *s;
2443
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002444 p = get_tv_string_chk(tv);
2445 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002447 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 if (s != NULL)
2449 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002450 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 vim_free(s);
2452 }
2453 }
2454 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002455 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002457 arg_end = arg + 1;
2458 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002459 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460 }
2461 }
2462
2463 /*
2464 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002467 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002469 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002471 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2475 EMSG(_(e_letunexp));
2476 else
2477 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 arg_end = p;
2480 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 }
2484
2485 else
2486 EMSG2(_(e_invarg2), arg);
2487
2488 return arg_end;
2489}
2490
2491/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2493 */
2494 static int
2495check_changedtick(arg)
2496 char_u *arg;
2497{
2498 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2499 {
2500 EMSG2(_(e_readonlyvar), arg);
2501 return TRUE;
2502 }
2503 return FALSE;
2504}
2505
2506/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 * Get an lval: variable, Dict item or List item that can be assigned a value
2508 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2509 * "name.key", "name.key[expr]" etc.
2510 * Indexing only works if "name" is an existing List or Dictionary.
2511 * "name" points to the start of the name.
2512 * If "rettv" is not NULL it points to the value to be assigned.
2513 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2514 * wrong; must end in space or cmd separator.
2515 *
2516 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002517 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 */
2520 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002521get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002523 typval_T *rettv;
2524 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 int unlet;
2526 int skip;
2527 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002528 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 char_u *expr_start, *expr_end;
2532 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 dictitem_T *v;
2534 typval_T var1;
2535 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002540 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002543 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544
2545 if (skip)
2546 {
2547 /* When skipping just find the end of the name. */
2548 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002549 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 }
2551
2552 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002553 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 if (expr_start != NULL)
2555 {
2556 /* Don't expand the name when we already know there is an error. */
2557 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2558 && *p != '[' && *p != '.')
2559 {
2560 EMSG(_(e_trailing));
2561 return NULL;
2562 }
2563
2564 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2565 if (lp->ll_exp_name == NULL)
2566 {
2567 /* Report an invalid expression in braces, unless the
2568 * expression evaluation has been cancelled due to an
2569 * aborting error, an interrupt, or an exception. */
2570 if (!aborting() && !quiet)
2571 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002572 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 EMSG2(_(e_invarg2), name);
2574 return NULL;
2575 }
2576 }
2577 lp->ll_name = lp->ll_exp_name;
2578 }
2579 else
2580 lp->ll_name = name;
2581
2582 /* Without [idx] or .key we are done. */
2583 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2584 return p;
2585
2586 cc = *p;
2587 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002588 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 if (v == NULL && !quiet)
2590 EMSG2(_(e_undefvar), lp->ll_name);
2591 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002592 if (v == NULL)
2593 return NULL;
2594
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 /*
2596 * Loop until no more [idx] or .key is following.
2597 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002598 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2602 && !(lp->ll_tv->v_type == VAR_DICT
2603 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (!quiet)
2606 EMSG(_("E689: Can only index a List or Dictionary"));
2607 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002608 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (!quiet)
2612 EMSG(_("E708: [:] must come last"));
2613 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002615
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 len = -1;
2617 if (*p == '.')
2618 {
2619 key = p + 1;
2620 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2621 ;
2622 if (len == 0)
2623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 if (!quiet)
2625 EMSG(_(e_emptykey));
2626 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 }
2628 p = key + len;
2629 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002630 else
2631 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002633 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 if (*p == ':')
2635 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002636 else
2637 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 empty1 = FALSE;
2639 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002641 if (get_tv_string_chk(&var1) == NULL)
2642 {
2643 /* not a number or string */
2644 clear_tv(&var1);
2645 return NULL;
2646 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 }
2648
2649 /* Optionally get the second index [ :expr]. */
2650 if (*p == ':')
2651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002655 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002656 if (!empty1)
2657 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002659 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (rettv != NULL && (rettv->v_type != VAR_LIST
2661 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 if (!quiet)
2664 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 if (!empty1)
2666 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 }
2669 p = skipwhite(p + 1);
2670 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 else
2673 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 if (!empty1)
2678 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002681 if (get_tv_string_chk(&var2) == NULL)
2682 {
2683 /* not a number or string */
2684 if (!empty1)
2685 clear_tv(&var1);
2686 clear_tv(&var2);
2687 return NULL;
2688 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002694
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (!quiet)
2698 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 if (!empty1)
2700 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 }
2705
2706 /* Skip to past ']'. */
2707 ++p;
2708 }
2709
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 {
2712 if (len == -1)
2713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002715 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 if (*key == NUL)
2717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 if (!quiet)
2719 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 }
2723 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002724 lp->ll_list = NULL;
2725 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002726 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002727
2728 /* When assigning to g: check that a function and variable name is
2729 * valid. */
2730 if (rettv != NULL && lp->ll_dict == &globvardict)
2731 {
2732 if (rettv->v_type == VAR_FUNC
2733 && var_check_func_name(key, lp->ll_di == NULL))
2734 return NULL;
2735 if (!valid_varname(key))
2736 return NULL;
2737 }
2738
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002741 /* Can't add "v:" variable. */
2742 if (lp->ll_dict == &vimvardict)
2743 {
2744 EMSG2(_(e_illvar), name);
2745 return NULL;
2746 }
2747
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002748 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002752 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 if (len == -1)
2754 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 }
2757 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 if (len == -1)
2762 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 p = NULL;
2765 break;
2766 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002767 /* existing variable, need to check if it can be changed */
2768 else if (var_check_ro(lp->ll_di->di_flags, name))
2769 return NULL;
2770
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 if (len == -1)
2772 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 }
2775 else
2776 {
2777 /*
2778 * Get the number and item for the only or first index of the List.
2779 */
2780 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 else
2783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002784 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 clear_tv(&var1);
2786 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002787 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 lp->ll_list = lp->ll_tv->vval.v_list;
2789 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2790 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002791 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002792 if (lp->ll_n1 < 0)
2793 {
2794 lp->ll_n1 = 0;
2795 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2796 }
2797 }
2798 if (lp->ll_li == NULL)
2799 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002802 if (!quiet)
2803 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 }
2806
2807 /*
2808 * May need to find the item or absolute index for the second
2809 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 * When no index given: "lp->ll_empty2" is TRUE.
2811 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002815 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002821 {
2822 if (!quiet)
2823 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002825 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 }
2828
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2830 if (lp->ll_n1 < 0)
2831 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2832 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002833 {
2834 if (!quiet)
2835 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002837 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002838 }
2839
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002841 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002842 }
2843
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 return p;
2845}
2846
2847/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002848 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 */
2850 static void
2851clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002852 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853{
2854 vim_free(lp->ll_exp_name);
2855 vim_free(lp->ll_newkey);
2856}
2857
2858/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002859 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002861 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 */
2863 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002864set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002865 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002867 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002869 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870{
2871 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002872 listitem_T *ri;
2873 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874
2875 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002876 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 cc = *endp;
2880 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002881 if (op != NULL && *op != '=')
2882 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002883 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002884
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002885 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002886 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002887 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002888 {
2889 if (tv_op(&tv, rettv, op) == OK)
2890 set_var(lp->ll_name, &tv, FALSE);
2891 clear_tv(&tv);
2892 }
2893 }
2894 else
2895 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002897 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002899 else if (tv_check_lock(lp->ll_newkey == NULL
2900 ? lp->ll_tv->v_lock
2901 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2902 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 else if (lp->ll_range)
2904 {
2905 /*
2906 * Assign the List values to the list items.
2907 */
2908 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002909 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002910 if (op != NULL && *op != '=')
2911 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2912 else
2913 {
2914 clear_tv(&lp->ll_li->li_tv);
2915 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2916 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 ri = ri->li_next;
2918 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2919 break;
2920 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002921 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002923 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002924 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 ri = NULL;
2926 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002927 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002928 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 lp->ll_li = lp->ll_li->li_next;
2930 ++lp->ll_n1;
2931 }
2932 if (ri != NULL)
2933 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002934 else if (lp->ll_empty2
2935 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 : lp->ll_n1 != lp->ll_n2)
2937 EMSG(_("E711: List value has not enough items"));
2938 }
2939 else
2940 {
2941 /*
2942 * Assign to a List or Dictionary item.
2943 */
2944 if (lp->ll_newkey != NULL)
2945 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 if (op != NULL && *op != '=')
2947 {
2948 EMSG2(_(e_letwrong), op);
2949 return;
2950 }
2951
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002953 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 if (di == NULL)
2955 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002956 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2957 {
2958 vim_free(di);
2959 return;
2960 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002962 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002963 else if (op != NULL && *op != '=')
2964 {
2965 tv_op(lp->ll_tv, rettv, op);
2966 return;
2967 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002968 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002970
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971 /*
2972 * Assign the value to the variable or list item.
2973 */
2974 if (copy)
2975 copy_tv(rettv, lp->ll_tv);
2976 else
2977 {
2978 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002979 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002981 }
2982 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002983}
2984
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002985/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002986 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2987 * Returns OK or FAIL.
2988 */
2989 static int
2990tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002991 typval_T *tv1;
2992 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002993 char_u *op;
2994{
2995 long n;
2996 char_u numbuf[NUMBUFLEN];
2997 char_u *s;
2998
2999 /* Can't do anything with a Funcref or a Dict on the right. */
3000 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3001 {
3002 switch (tv1->v_type)
3003 {
3004 case VAR_DICT:
3005 case VAR_FUNC:
3006 break;
3007
3008 case VAR_LIST:
3009 if (*op != '+' || tv2->v_type != VAR_LIST)
3010 break;
3011 /* List += List */
3012 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3013 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3014 return OK;
3015
3016 case VAR_NUMBER:
3017 case VAR_STRING:
3018 if (tv2->v_type == VAR_LIST)
3019 break;
3020 if (*op == '+' || *op == '-')
3021 {
3022 /* nr += nr or nr -= nr*/
3023 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003024#ifdef FEAT_FLOAT
3025 if (tv2->v_type == VAR_FLOAT)
3026 {
3027 float_T f = n;
3028
3029 if (*op == '+')
3030 f += tv2->vval.v_float;
3031 else
3032 f -= tv2->vval.v_float;
3033 clear_tv(tv1);
3034 tv1->v_type = VAR_FLOAT;
3035 tv1->vval.v_float = f;
3036 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003037 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003038#endif
3039 {
3040 if (*op == '+')
3041 n += get_tv_number(tv2);
3042 else
3043 n -= get_tv_number(tv2);
3044 clear_tv(tv1);
3045 tv1->v_type = VAR_NUMBER;
3046 tv1->vval.v_number = n;
3047 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003048 }
3049 else
3050 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003051 if (tv2->v_type == VAR_FLOAT)
3052 break;
3053
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003054 /* str .= str */
3055 s = get_tv_string(tv1);
3056 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3057 clear_tv(tv1);
3058 tv1->v_type = VAR_STRING;
3059 tv1->vval.v_string = s;
3060 }
3061 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003062
3063#ifdef FEAT_FLOAT
3064 case VAR_FLOAT:
3065 {
3066 float_T f;
3067
3068 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3069 && tv2->v_type != VAR_NUMBER
3070 && tv2->v_type != VAR_STRING))
3071 break;
3072 if (tv2->v_type == VAR_FLOAT)
3073 f = tv2->vval.v_float;
3074 else
3075 f = get_tv_number(tv2);
3076 if (*op == '+')
3077 tv1->vval.v_float += f;
3078 else
3079 tv1->vval.v_float -= f;
3080 }
3081 return OK;
3082#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003083 }
3084 }
3085
3086 EMSG2(_(e_letwrong), op);
3087 return FAIL;
3088}
3089
3090/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003091 * Add a watcher to a list.
3092 */
3093 static void
3094list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003095 list_T *l;
3096 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003097{
3098 lw->lw_next = l->lv_watch;
3099 l->lv_watch = lw;
3100}
3101
3102/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003103 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003104 * No warning when it isn't found...
3105 */
3106 static void
3107list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003108 list_T *l;
3109 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003110{
Bram Moolenaar33570922005-01-25 22:26:29 +00003111 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003112
3113 lwp = &l->lv_watch;
3114 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3115 {
3116 if (lw == lwrem)
3117 {
3118 *lwp = lw->lw_next;
3119 break;
3120 }
3121 lwp = &lw->lw_next;
3122 }
3123}
3124
3125/*
3126 * Just before removing an item from a list: advance watchers to the next
3127 * item.
3128 */
3129 static void
3130list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003131 list_T *l;
3132 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003133{
Bram Moolenaar33570922005-01-25 22:26:29 +00003134 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003135
3136 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3137 if (lw->lw_item == item)
3138 lw->lw_item = item->li_next;
3139}
3140
3141/*
3142 * Evaluate the expression used in a ":for var in expr" command.
3143 * "arg" points to "var".
3144 * Set "*errp" to TRUE for an error, FALSE otherwise;
3145 * Return a pointer that holds the info. Null when there is an error.
3146 */
3147 void *
3148eval_for_line(arg, errp, nextcmdp, skip)
3149 char_u *arg;
3150 int *errp;
3151 char_u **nextcmdp;
3152 int skip;
3153{
Bram Moolenaar33570922005-01-25 22:26:29 +00003154 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003155 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003156 typval_T tv;
3157 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003158
3159 *errp = TRUE; /* default: there is an error */
3160
Bram Moolenaar33570922005-01-25 22:26:29 +00003161 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003162 if (fi == NULL)
3163 return NULL;
3164
3165 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3166 if (expr == NULL)
3167 return fi;
3168
3169 expr = skipwhite(expr);
3170 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3171 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003172 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173 return fi;
3174 }
3175
3176 if (skip)
3177 ++emsg_skip;
3178 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3179 {
3180 *errp = FALSE;
3181 if (!skip)
3182 {
3183 l = tv.vval.v_list;
3184 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003185 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003186 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003187 clear_tv(&tv);
3188 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 else
3190 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003191 /* No need to increment the refcount, it's already set for the
3192 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193 fi->fi_list = l;
3194 list_add_watch(l, &fi->fi_lw);
3195 fi->fi_lw.lw_item = l->lv_first;
3196 }
3197 }
3198 }
3199 if (skip)
3200 --emsg_skip;
3201
3202 return fi;
3203}
3204
3205/*
3206 * Use the first item in a ":for" list. Advance to the next.
3207 * Assign the values to the variable (list). "arg" points to the first one.
3208 * Return TRUE when a valid item was found, FALSE when at end of list or
3209 * something wrong.
3210 */
3211 int
3212next_for_item(fi_void, arg)
3213 void *fi_void;
3214 char_u *arg;
3215{
Bram Moolenaar33570922005-01-25 22:26:29 +00003216 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003218 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219
3220 item = fi->fi_lw.lw_item;
3221 if (item == NULL)
3222 result = FALSE;
3223 else
3224 {
3225 fi->fi_lw.lw_item = item->li_next;
3226 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3227 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3228 }
3229 return result;
3230}
3231
3232/*
3233 * Free the structure used to store info used by ":for".
3234 */
3235 void
3236free_for_info(fi_void)
3237 void *fi_void;
3238{
Bram Moolenaar33570922005-01-25 22:26:29 +00003239 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003240
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003241 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003242 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003244 list_unref(fi->fi_list);
3245 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 vim_free(fi);
3247}
3248
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3250
3251 void
3252set_context_for_expression(xp, arg, cmdidx)
3253 expand_T *xp;
3254 char_u *arg;
3255 cmdidx_T cmdidx;
3256{
3257 int got_eq = FALSE;
3258 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003259 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003261 if (cmdidx == CMD_let)
3262 {
3263 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003264 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003265 {
3266 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003267 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003268 {
3269 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003270 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003271 if (vim_iswhite(*p))
3272 break;
3273 }
3274 return;
3275 }
3276 }
3277 else
3278 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3279 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 while ((xp->xp_pattern = vim_strpbrk(arg,
3281 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3282 {
3283 c = *xp->xp_pattern;
3284 if (c == '&')
3285 {
3286 c = xp->xp_pattern[1];
3287 if (c == '&')
3288 {
3289 ++xp->xp_pattern;
3290 xp->xp_context = cmdidx != CMD_let || got_eq
3291 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3292 }
3293 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003294 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003296 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3297 xp->xp_pattern += 2;
3298
3299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
3301 else if (c == '$')
3302 {
3303 /* environment variable */
3304 xp->xp_context = EXPAND_ENV_VARS;
3305 }
3306 else if (c == '=')
3307 {
3308 got_eq = TRUE;
3309 xp->xp_context = EXPAND_EXPRESSION;
3310 }
3311 else if (c == '<'
3312 && xp->xp_context == EXPAND_FUNCTIONS
3313 && vim_strchr(xp->xp_pattern, '(') == NULL)
3314 {
3315 /* Function name can start with "<SNR>" */
3316 break;
3317 }
3318 else if (cmdidx != CMD_let || got_eq)
3319 {
3320 if (c == '"') /* string */
3321 {
3322 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3323 if (c == '\\' && xp->xp_pattern[1] != NUL)
3324 ++xp->xp_pattern;
3325 xp->xp_context = EXPAND_NOTHING;
3326 }
3327 else if (c == '\'') /* literal string */
3328 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003329 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3331 /* skip */ ;
3332 xp->xp_context = EXPAND_NOTHING;
3333 }
3334 else if (c == '|')
3335 {
3336 if (xp->xp_pattern[1] == '|')
3337 {
3338 ++xp->xp_pattern;
3339 xp->xp_context = EXPAND_EXPRESSION;
3340 }
3341 else
3342 xp->xp_context = EXPAND_COMMANDS;
3343 }
3344 else
3345 xp->xp_context = EXPAND_EXPRESSION;
3346 }
3347 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 /* Doesn't look like something valid, expand as an expression
3349 * anyway. */
3350 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 arg = xp->xp_pattern;
3352 if (*arg != NUL)
3353 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3354 /* skip */ ;
3355 }
3356 xp->xp_pattern = arg;
3357}
3358
3359#endif /* FEAT_CMDL_COMPL */
3360
3361/*
3362 * ":1,25call func(arg1, arg2)" function call.
3363 */
3364 void
3365ex_call(eap)
3366 exarg_T *eap;
3367{
3368 char_u *arg = eap->arg;
3369 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003371 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003373 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 linenr_T lnum;
3375 int doesrange;
3376 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003377 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003379 if (eap->skip)
3380 {
3381 /* trans_function_name() doesn't work well when skipping, use eval0()
3382 * instead to skip to any following command, e.g. for:
3383 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003384 ++emsg_skip;
3385 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3386 clear_tv(&rettv);
3387 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003388 return;
3389 }
3390
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003391 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003392 if (fudi.fd_newkey != NULL)
3393 {
3394 /* Still need to give an error message for missing key. */
3395 EMSG2(_(e_dictkey), fudi.fd_newkey);
3396 vim_free(fudi.fd_newkey);
3397 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003398 if (tofree == NULL)
3399 return;
3400
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003401 /* Increase refcount on dictionary, it could get deleted when evaluating
3402 * the arguments. */
3403 if (fudi.fd_dict != NULL)
3404 ++fudi.fd_dict->dv_refcount;
3405
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003406 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003407 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003408 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409
Bram Moolenaar532c7802005-01-27 14:44:31 +00003410 /* Skip white space to allow ":call func ()". Not good, but required for
3411 * backward compatibility. */
3412 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003413 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414
3415 if (*startarg != '(')
3416 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003417 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 goto end;
3419 }
3420
3421 /*
3422 * When skipping, evaluate the function once, to find the end of the
3423 * arguments.
3424 * When the function takes a range, this is discovered after the first
3425 * call, and the loop is broken.
3426 */
3427 if (eap->skip)
3428 {
3429 ++emsg_skip;
3430 lnum = eap->line2; /* do it once, also with an invalid range */
3431 }
3432 else
3433 lnum = eap->line1;
3434 for ( ; lnum <= eap->line2; ++lnum)
3435 {
3436 if (!eap->skip && eap->addr_count > 0)
3437 {
3438 curwin->w_cursor.lnum = lnum;
3439 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003440#ifdef FEAT_VIRTUALEDIT
3441 curwin->w_cursor.coladd = 0;
3442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 }
3444 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003445 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003446 eap->line1, eap->line2, &doesrange,
3447 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 {
3449 failed = TRUE;
3450 break;
3451 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003452
3453 /* Handle a function returning a Funcref, Dictionary or List. */
3454 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3455 {
3456 failed = TRUE;
3457 break;
3458 }
3459
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003460 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 if (doesrange || eap->skip)
3462 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003463
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003465 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003466 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003467 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 if (aborting())
3469 break;
3470 }
3471 if (eap->skip)
3472 --emsg_skip;
3473
3474 if (!failed)
3475 {
3476 /* Check for trailing illegal characters and a following command. */
3477 if (!ends_excmd(*arg))
3478 {
3479 emsg_severe = TRUE;
3480 EMSG(_(e_trailing));
3481 }
3482 else
3483 eap->nextcmd = check_nextcmd(arg);
3484 }
3485
3486end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003487 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003488 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489}
3490
3491/*
3492 * ":unlet[!] var1 ... " command.
3493 */
3494 void
3495ex_unlet(eap)
3496 exarg_T *eap;
3497{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003498 ex_unletlock(eap, eap->arg, 0);
3499}
3500
3501/*
3502 * ":lockvar" and ":unlockvar" commands
3503 */
3504 void
3505ex_lockvar(eap)
3506 exarg_T *eap;
3507{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003509 int deep = 2;
3510
3511 if (eap->forceit)
3512 deep = -1;
3513 else if (vim_isdigit(*arg))
3514 {
3515 deep = getdigits(&arg);
3516 arg = skipwhite(arg);
3517 }
3518
3519 ex_unletlock(eap, arg, deep);
3520}
3521
3522/*
3523 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3524 */
3525 static void
3526ex_unletlock(eap, argstart, deep)
3527 exarg_T *eap;
3528 char_u *argstart;
3529 int deep;
3530{
3531 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003534 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535
3536 do
3537 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003538 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003539 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3540 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003541 if (lv.ll_name == NULL)
3542 error = TRUE; /* error but continue parsing */
3543 if (name_end == NULL || (!vim_iswhite(*name_end)
3544 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003546 if (name_end != NULL)
3547 {
3548 emsg_severe = TRUE;
3549 EMSG(_(e_trailing));
3550 }
3551 if (!(eap->skip || error))
3552 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 break;
3554 }
3555
3556 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003557 {
3558 if (eap->cmdidx == CMD_unlet)
3559 {
3560 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3561 error = TRUE;
3562 }
3563 else
3564 {
3565 if (do_lock_var(&lv, name_end, deep,
3566 eap->cmdidx == CMD_lockvar) == FAIL)
3567 error = TRUE;
3568 }
3569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003571 if (!eap->skip)
3572 clear_lval(&lv);
3573
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 arg = skipwhite(name_end);
3575 } while (!ends_excmd(*arg));
3576
3577 eap->nextcmd = check_nextcmd(arg);
3578}
3579
Bram Moolenaar8c711452005-01-14 21:53:12 +00003580 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003581do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003582 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003583 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003584 int forceit;
3585{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003586 int ret = OK;
3587 int cc;
3588
3589 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003590 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003591 cc = *name_end;
3592 *name_end = NUL;
3593
3594 /* Normal name or expanded name. */
3595 if (check_changedtick(lp->ll_name))
3596 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003597 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003598 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003599 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003600 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003601 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3602 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003603 else if (lp->ll_range)
3604 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003605 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003606
3607 /* Delete a range of List items. */
3608 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3609 {
3610 li = lp->ll_li->li_next;
3611 listitem_remove(lp->ll_list, lp->ll_li);
3612 lp->ll_li = li;
3613 ++lp->ll_n1;
3614 }
3615 }
3616 else
3617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 /* unlet a List item. */
3620 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003623 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 }
3625
3626 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003627}
3628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629/*
3630 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003631 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 */
3633 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003634do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003636 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637{
Bram Moolenaar33570922005-01-25 22:26:29 +00003638 hashtab_T *ht;
3639 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003640 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003641 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642
Bram Moolenaar33570922005-01-25 22:26:29 +00003643 ht = find_var_ht(name, &varname);
3644 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003646 hi = hash_find(ht, varname);
3647 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003648 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003649 di = HI2DI(hi);
3650 if (var_check_fixed(di->di_flags, name)
3651 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003652 return FAIL;
3653 delete_var(ht, hi);
3654 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003657 if (forceit)
3658 return OK;
3659 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 return FAIL;
3661}
3662
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003663/*
3664 * Lock or unlock variable indicated by "lp".
3665 * "deep" is the levels to go (-1 for unlimited);
3666 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3667 */
3668 static int
3669do_lock_var(lp, name_end, deep, lock)
3670 lval_T *lp;
3671 char_u *name_end;
3672 int deep;
3673 int lock;
3674{
3675 int ret = OK;
3676 int cc;
3677 dictitem_T *di;
3678
3679 if (deep == 0) /* nothing to do */
3680 return OK;
3681
3682 if (lp->ll_tv == NULL)
3683 {
3684 cc = *name_end;
3685 *name_end = NUL;
3686
3687 /* Normal name or expanded name. */
3688 if (check_changedtick(lp->ll_name))
3689 ret = FAIL;
3690 else
3691 {
3692 di = find_var(lp->ll_name, NULL);
3693 if (di == NULL)
3694 ret = FAIL;
3695 else
3696 {
3697 if (lock)
3698 di->di_flags |= DI_FLAGS_LOCK;
3699 else
3700 di->di_flags &= ~DI_FLAGS_LOCK;
3701 item_lock(&di->di_tv, deep, lock);
3702 }
3703 }
3704 *name_end = cc;
3705 }
3706 else if (lp->ll_range)
3707 {
3708 listitem_T *li = lp->ll_li;
3709
3710 /* (un)lock a range of List items. */
3711 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3712 {
3713 item_lock(&li->li_tv, deep, lock);
3714 li = li->li_next;
3715 ++lp->ll_n1;
3716 }
3717 }
3718 else if (lp->ll_list != NULL)
3719 /* (un)lock a List item. */
3720 item_lock(&lp->ll_li->li_tv, deep, lock);
3721 else
3722 /* un(lock) a Dictionary item. */
3723 item_lock(&lp->ll_di->di_tv, deep, lock);
3724
3725 return ret;
3726}
3727
3728/*
3729 * Lock or unlock an item. "deep" is nr of levels to go.
3730 */
3731 static void
3732item_lock(tv, deep, lock)
3733 typval_T *tv;
3734 int deep;
3735 int lock;
3736{
3737 static int recurse = 0;
3738 list_T *l;
3739 listitem_T *li;
3740 dict_T *d;
3741 hashitem_T *hi;
3742 int todo;
3743
3744 if (recurse >= DICT_MAXNEST)
3745 {
3746 EMSG(_("E743: variable nested too deep for (un)lock"));
3747 return;
3748 }
3749 if (deep == 0)
3750 return;
3751 ++recurse;
3752
3753 /* lock/unlock the item itself */
3754 if (lock)
3755 tv->v_lock |= VAR_LOCKED;
3756 else
3757 tv->v_lock &= ~VAR_LOCKED;
3758
3759 switch (tv->v_type)
3760 {
3761 case VAR_LIST:
3762 if ((l = tv->vval.v_list) != NULL)
3763 {
3764 if (lock)
3765 l->lv_lock |= VAR_LOCKED;
3766 else
3767 l->lv_lock &= ~VAR_LOCKED;
3768 if (deep < 0 || deep > 1)
3769 /* recursive: lock/unlock the items the List contains */
3770 for (li = l->lv_first; li != NULL; li = li->li_next)
3771 item_lock(&li->li_tv, deep - 1, lock);
3772 }
3773 break;
3774 case VAR_DICT:
3775 if ((d = tv->vval.v_dict) != NULL)
3776 {
3777 if (lock)
3778 d->dv_lock |= VAR_LOCKED;
3779 else
3780 d->dv_lock &= ~VAR_LOCKED;
3781 if (deep < 0 || deep > 1)
3782 {
3783 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003784 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003785 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3786 {
3787 if (!HASHITEM_EMPTY(hi))
3788 {
3789 --todo;
3790 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3791 }
3792 }
3793 }
3794 }
3795 }
3796 --recurse;
3797}
3798
Bram Moolenaara40058a2005-07-11 22:42:07 +00003799/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003800 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3801 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003802 */
3803 static int
3804tv_islocked(tv)
3805 typval_T *tv;
3806{
3807 return (tv->v_lock & VAR_LOCKED)
3808 || (tv->v_type == VAR_LIST
3809 && tv->vval.v_list != NULL
3810 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3811 || (tv->v_type == VAR_DICT
3812 && tv->vval.v_dict != NULL
3813 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3814}
3815
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3817/*
3818 * Delete all "menutrans_" variables.
3819 */
3820 void
3821del_menutrans_vars()
3822{
Bram Moolenaar33570922005-01-25 22:26:29 +00003823 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003824 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825
Bram Moolenaar33570922005-01-25 22:26:29 +00003826 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003827 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003828 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003829 {
3830 if (!HASHITEM_EMPTY(hi))
3831 {
3832 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003833 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3834 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003835 }
3836 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003837 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838}
3839#endif
3840
3841#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3842
3843/*
3844 * Local string buffer for the next two functions to store a variable name
3845 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3846 * get_user_var_name().
3847 */
3848
3849static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3850
3851static char_u *varnamebuf = NULL;
3852static int varnamebuflen = 0;
3853
3854/*
3855 * Function to concatenate a prefix and a variable name.
3856 */
3857 static char_u *
3858cat_prefix_varname(prefix, name)
3859 int prefix;
3860 char_u *name;
3861{
3862 int len;
3863
3864 len = (int)STRLEN(name) + 3;
3865 if (len > varnamebuflen)
3866 {
3867 vim_free(varnamebuf);
3868 len += 10; /* some additional space */
3869 varnamebuf = alloc(len);
3870 if (varnamebuf == NULL)
3871 {
3872 varnamebuflen = 0;
3873 return NULL;
3874 }
3875 varnamebuflen = len;
3876 }
3877 *varnamebuf = prefix;
3878 varnamebuf[1] = ':';
3879 STRCPY(varnamebuf + 2, name);
3880 return varnamebuf;
3881}
3882
3883/*
3884 * Function given to ExpandGeneric() to obtain the list of user defined
3885 * (global/buffer/window/built-in) variable names.
3886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 char_u *
3888get_user_var_name(xp, idx)
3889 expand_T *xp;
3890 int idx;
3891{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003892 static long_u gdone;
3893 static long_u bdone;
3894 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003895#ifdef FEAT_WINDOWS
3896 static long_u tdone;
3897#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003898 static int vidx;
3899 static hashitem_T *hi;
3900 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901
3902 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003903 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003904 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003905#ifdef FEAT_WINDOWS
3906 tdone = 0;
3907#endif
3908 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003909
3910 /* Global variables */
3911 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003913 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003914 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003915 else
3916 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003917 while (HASHITEM_EMPTY(hi))
3918 ++hi;
3919 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3920 return cat_prefix_varname('g', hi->hi_key);
3921 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003923
3924 /* b: variables */
3925 ht = &curbuf->b_vars.dv_hashtab;
3926 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003928 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003929 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003930 else
3931 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003932 while (HASHITEM_EMPTY(hi))
3933 ++hi;
3934 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003938 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 return (char_u *)"b:changedtick";
3940 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003941
3942 /* w: variables */
3943 ht = &curwin->w_vars.dv_hashtab;
3944 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003946 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003947 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003948 else
3949 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 while (HASHITEM_EMPTY(hi))
3951 ++hi;
3952 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003954
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003955#ifdef FEAT_WINDOWS
3956 /* t: variables */
3957 ht = &curtab->tp_vars.dv_hashtab;
3958 if (tdone < ht->ht_used)
3959 {
3960 if (tdone++ == 0)
3961 hi = ht->ht_array;
3962 else
3963 ++hi;
3964 while (HASHITEM_EMPTY(hi))
3965 ++hi;
3966 return cat_prefix_varname('t', hi->hi_key);
3967 }
3968#endif
3969
Bram Moolenaar33570922005-01-25 22:26:29 +00003970 /* v: variables */
3971 if (vidx < VV_LEN)
3972 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973
3974 vim_free(varnamebuf);
3975 varnamebuf = NULL;
3976 varnamebuflen = 0;
3977 return NULL;
3978}
3979
3980#endif /* FEAT_CMDL_COMPL */
3981
3982/*
3983 * types for expressions.
3984 */
3985typedef enum
3986{
3987 TYPE_UNKNOWN = 0
3988 , TYPE_EQUAL /* == */
3989 , TYPE_NEQUAL /* != */
3990 , TYPE_GREATER /* > */
3991 , TYPE_GEQUAL /* >= */
3992 , TYPE_SMALLER /* < */
3993 , TYPE_SEQUAL /* <= */
3994 , TYPE_MATCH /* =~ */
3995 , TYPE_NOMATCH /* !~ */
3996} exptype_T;
3997
3998/*
3999 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004000 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4002 */
4003
4004/*
4005 * Handle zero level expression.
4006 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004007 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004008 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 * Return OK or FAIL.
4010 */
4011 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004012eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 char_u **nextcmd;
4016 int evaluate;
4017{
4018 int ret;
4019 char_u *p;
4020
4021 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004022 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 if (ret == FAIL || !ends_excmd(*p))
4024 {
4025 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004026 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 /*
4028 * Report the invalid expression unless the expression evaluation has
4029 * been cancelled due to an aborting error, an interrupt, or an
4030 * exception.
4031 */
4032 if (!aborting())
4033 EMSG2(_(e_invexpr2), arg);
4034 ret = FAIL;
4035 }
4036 if (nextcmd != NULL)
4037 *nextcmd = check_nextcmd(p);
4038
4039 return ret;
4040}
4041
4042/*
4043 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004044 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 *
4046 * "arg" must point to the first non-white of the expression.
4047 * "arg" is advanced to the next non-white after the recognized expression.
4048 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004049 * Note: "rettv.v_lock" is not set.
4050 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 * Return OK or FAIL.
4052 */
4053 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004054eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 int evaluate;
4058{
4059 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004060 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061
4062 /*
4063 * Get the first variable.
4064 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004065 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 return FAIL;
4067
4068 if ((*arg)[0] == '?')
4069 {
4070 result = FALSE;
4071 if (evaluate)
4072 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004073 int error = FALSE;
4074
4075 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004077 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004078 if (error)
4079 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 }
4081
4082 /*
4083 * Get the second variable.
4084 */
4085 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004086 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 return FAIL;
4088
4089 /*
4090 * Check for the ":".
4091 */
4092 if ((*arg)[0] != ':')
4093 {
4094 EMSG(_("E109: Missing ':' after '?'"));
4095 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004096 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 return FAIL;
4098 }
4099
4100 /*
4101 * Get the third variable.
4102 */
4103 *arg = skipwhite(*arg + 1);
4104 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4105 {
4106 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004107 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 return FAIL;
4109 }
4110 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113
4114 return OK;
4115}
4116
4117/*
4118 * Handle first level expression:
4119 * expr2 || expr2 || expr2 logical OR
4120 *
4121 * "arg" must point to the first non-white of the expression.
4122 * "arg" is advanced to the next non-white after the recognized expression.
4123 *
4124 * Return OK or FAIL.
4125 */
4126 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 int evaluate;
4131{
Bram Moolenaar33570922005-01-25 22:26:29 +00004132 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 long result;
4134 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004135 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136
4137 /*
4138 * Get the first variable.
4139 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 return FAIL;
4142
4143 /*
4144 * Repeat until there is no following "||".
4145 */
4146 first = TRUE;
4147 result = FALSE;
4148 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4149 {
4150 if (evaluate && first)
4151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004152 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004154 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004155 if (error)
4156 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 first = FALSE;
4158 }
4159
4160 /*
4161 * Get the second variable.
4162 */
4163 *arg = skipwhite(*arg + 2);
4164 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4165 return FAIL;
4166
4167 /*
4168 * Compute the result.
4169 */
4170 if (evaluate && !result)
4171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004172 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004174 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004175 if (error)
4176 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 }
4178 if (evaluate)
4179 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004180 rettv->v_type = VAR_NUMBER;
4181 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 }
4183 }
4184
4185 return OK;
4186}
4187
4188/*
4189 * Handle second level expression:
4190 * expr3 && expr3 && expr3 logical AND
4191 *
4192 * "arg" must point to the first non-white of the expression.
4193 * "arg" is advanced to the next non-white after the recognized expression.
4194 *
4195 * Return OK or FAIL.
4196 */
4197 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 int evaluate;
4202{
Bram Moolenaar33570922005-01-25 22:26:29 +00004203 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 long result;
4205 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004206 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207
4208 /*
4209 * Get the first variable.
4210 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return FAIL;
4213
4214 /*
4215 * Repeat until there is no following "&&".
4216 */
4217 first = TRUE;
4218 result = TRUE;
4219 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4220 {
4221 if (evaluate && first)
4222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004223 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004225 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004226 if (error)
4227 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 first = FALSE;
4229 }
4230
4231 /*
4232 * Get the second variable.
4233 */
4234 *arg = skipwhite(*arg + 2);
4235 if (eval4(arg, &var2, evaluate && result) == FAIL)
4236 return FAIL;
4237
4238 /*
4239 * Compute the result.
4240 */
4241 if (evaluate && result)
4242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004246 if (error)
4247 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 }
4249 if (evaluate)
4250 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 rettv->v_type = VAR_NUMBER;
4252 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
4254 }
4255
4256 return OK;
4257}
4258
4259/*
4260 * Handle third level expression:
4261 * var1 == var2
4262 * var1 =~ var2
4263 * var1 != var2
4264 * var1 !~ var2
4265 * var1 > var2
4266 * var1 >= var2
4267 * var1 < var2
4268 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004269 * var1 is var2
4270 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 *
4272 * "arg" must point to the first non-white of the expression.
4273 * "arg" is advanced to the next non-white after the recognized expression.
4274 *
4275 * Return OK or FAIL.
4276 */
4277 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 int evaluate;
4282{
Bram Moolenaar33570922005-01-25 22:26:29 +00004283 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 char_u *p;
4285 int i;
4286 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004287 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 int len = 2;
4289 long n1, n2;
4290 char_u *s1, *s2;
4291 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4292 regmatch_T regmatch;
4293 int ic;
4294 char_u *save_cpo;
4295
4296 /*
4297 * Get the first variable.
4298 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004299 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 return FAIL;
4301
4302 p = *arg;
4303 switch (p[0])
4304 {
4305 case '=': if (p[1] == '=')
4306 type = TYPE_EQUAL;
4307 else if (p[1] == '~')
4308 type = TYPE_MATCH;
4309 break;
4310 case '!': if (p[1] == '=')
4311 type = TYPE_NEQUAL;
4312 else if (p[1] == '~')
4313 type = TYPE_NOMATCH;
4314 break;
4315 case '>': if (p[1] != '=')
4316 {
4317 type = TYPE_GREATER;
4318 len = 1;
4319 }
4320 else
4321 type = TYPE_GEQUAL;
4322 break;
4323 case '<': if (p[1] != '=')
4324 {
4325 type = TYPE_SMALLER;
4326 len = 1;
4327 }
4328 else
4329 type = TYPE_SEQUAL;
4330 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004331 case 'i': if (p[1] == 's')
4332 {
4333 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4334 len = 5;
4335 if (!vim_isIDc(p[len]))
4336 {
4337 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4338 type_is = TRUE;
4339 }
4340 }
4341 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 }
4343
4344 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004345 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 */
4347 if (type != TYPE_UNKNOWN)
4348 {
4349 /* extra question mark appended: ignore case */
4350 if (p[len] == '?')
4351 {
4352 ic = TRUE;
4353 ++len;
4354 }
4355 /* extra '#' appended: match case */
4356 else if (p[len] == '#')
4357 {
4358 ic = FALSE;
4359 ++len;
4360 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004361 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 else
4363 ic = p_ic;
4364
4365 /*
4366 * Get the second variable.
4367 */
4368 *arg = skipwhite(p + len);
4369 if (eval5(arg, &var2, evaluate) == FAIL)
4370 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004371 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 return FAIL;
4373 }
4374
4375 if (evaluate)
4376 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004377 if (type_is && rettv->v_type != var2.v_type)
4378 {
4379 /* For "is" a different type always means FALSE, for "notis"
4380 * it means TRUE. */
4381 n1 = (type == TYPE_NEQUAL);
4382 }
4383 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4384 {
4385 if (type_is)
4386 {
4387 n1 = (rettv->v_type == var2.v_type
4388 && rettv->vval.v_list == var2.vval.v_list);
4389 if (type == TYPE_NEQUAL)
4390 n1 = !n1;
4391 }
4392 else if (rettv->v_type != var2.v_type
4393 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4394 {
4395 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004396 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004397 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004398 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004399 clear_tv(rettv);
4400 clear_tv(&var2);
4401 return FAIL;
4402 }
4403 else
4404 {
4405 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004406 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4407 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004408 if (type == TYPE_NEQUAL)
4409 n1 = !n1;
4410 }
4411 }
4412
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004413 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4414 {
4415 if (type_is)
4416 {
4417 n1 = (rettv->v_type == var2.v_type
4418 && rettv->vval.v_dict == var2.vval.v_dict);
4419 if (type == TYPE_NEQUAL)
4420 n1 = !n1;
4421 }
4422 else if (rettv->v_type != var2.v_type
4423 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4424 {
4425 if (rettv->v_type != var2.v_type)
4426 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4427 else
4428 EMSG(_("E736: Invalid operation for Dictionary"));
4429 clear_tv(rettv);
4430 clear_tv(&var2);
4431 return FAIL;
4432 }
4433 else
4434 {
4435 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004436 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4437 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004438 if (type == TYPE_NEQUAL)
4439 n1 = !n1;
4440 }
4441 }
4442
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004443 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4444 {
4445 if (rettv->v_type != var2.v_type
4446 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4447 {
4448 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004449 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004450 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004451 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004452 clear_tv(rettv);
4453 clear_tv(&var2);
4454 return FAIL;
4455 }
4456 else
4457 {
4458 /* Compare two Funcrefs for being equal or unequal. */
4459 if (rettv->vval.v_string == NULL
4460 || var2.vval.v_string == NULL)
4461 n1 = FALSE;
4462 else
4463 n1 = STRCMP(rettv->vval.v_string,
4464 var2.vval.v_string) == 0;
4465 if (type == TYPE_NEQUAL)
4466 n1 = !n1;
4467 }
4468 }
4469
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004470#ifdef FEAT_FLOAT
4471 /*
4472 * If one of the two variables is a float, compare as a float.
4473 * When using "=~" or "!~", always compare as string.
4474 */
4475 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4476 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4477 {
4478 float_T f1, f2;
4479
4480 if (rettv->v_type == VAR_FLOAT)
4481 f1 = rettv->vval.v_float;
4482 else
4483 f1 = get_tv_number(rettv);
4484 if (var2.v_type == VAR_FLOAT)
4485 f2 = var2.vval.v_float;
4486 else
4487 f2 = get_tv_number(&var2);
4488 n1 = FALSE;
4489 switch (type)
4490 {
4491 case TYPE_EQUAL: n1 = (f1 == f2); break;
4492 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4493 case TYPE_GREATER: n1 = (f1 > f2); break;
4494 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4495 case TYPE_SMALLER: n1 = (f1 < f2); break;
4496 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4497 case TYPE_UNKNOWN:
4498 case TYPE_MATCH:
4499 case TYPE_NOMATCH: break; /* avoid gcc warning */
4500 }
4501 }
4502#endif
4503
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 /*
4505 * If one of the two variables is a number, compare as a number.
4506 * When using "=~" or "!~", always compare as string.
4507 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004508 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4510 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004511 n1 = get_tv_number(rettv);
4512 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 switch (type)
4514 {
4515 case TYPE_EQUAL: n1 = (n1 == n2); break;
4516 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4517 case TYPE_GREATER: n1 = (n1 > n2); break;
4518 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4519 case TYPE_SMALLER: n1 = (n1 < n2); break;
4520 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4521 case TYPE_UNKNOWN:
4522 case TYPE_MATCH:
4523 case TYPE_NOMATCH: break; /* avoid gcc warning */
4524 }
4525 }
4526 else
4527 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004528 s1 = get_tv_string_buf(rettv, buf1);
4529 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4531 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4532 else
4533 i = 0;
4534 n1 = FALSE;
4535 switch (type)
4536 {
4537 case TYPE_EQUAL: n1 = (i == 0); break;
4538 case TYPE_NEQUAL: n1 = (i != 0); break;
4539 case TYPE_GREATER: n1 = (i > 0); break;
4540 case TYPE_GEQUAL: n1 = (i >= 0); break;
4541 case TYPE_SMALLER: n1 = (i < 0); break;
4542 case TYPE_SEQUAL: n1 = (i <= 0); break;
4543
4544 case TYPE_MATCH:
4545 case TYPE_NOMATCH:
4546 /* avoid 'l' flag in 'cpoptions' */
4547 save_cpo = p_cpo;
4548 p_cpo = (char_u *)"";
4549 regmatch.regprog = vim_regcomp(s2,
4550 RE_MAGIC + RE_STRING);
4551 regmatch.rm_ic = ic;
4552 if (regmatch.regprog != NULL)
4553 {
4554 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4555 vim_free(regmatch.regprog);
4556 if (type == TYPE_NOMATCH)
4557 n1 = !n1;
4558 }
4559 p_cpo = save_cpo;
4560 break;
4561
4562 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4563 }
4564 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004565 clear_tv(rettv);
4566 clear_tv(&var2);
4567 rettv->v_type = VAR_NUMBER;
4568 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 }
4570 }
4571
4572 return OK;
4573}
4574
4575/*
4576 * Handle fourth level expression:
4577 * + number addition
4578 * - number subtraction
4579 * . string concatenation
4580 *
4581 * "arg" must point to the first non-white of the expression.
4582 * "arg" is advanced to the next non-white after the recognized expression.
4583 *
4584 * Return OK or FAIL.
4585 */
4586 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004587eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 int evaluate;
4591{
Bram Moolenaar33570922005-01-25 22:26:29 +00004592 typval_T var2;
4593 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 int op;
4595 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004596#ifdef FEAT_FLOAT
4597 float_T f1 = 0, f2 = 0;
4598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 char_u *s1, *s2;
4600 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4601 char_u *p;
4602
4603 /*
4604 * Get the first variable.
4605 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004606 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 return FAIL;
4608
4609 /*
4610 * Repeat computing, until no '+', '-' or '.' is following.
4611 */
4612 for (;;)
4613 {
4614 op = **arg;
4615 if (op != '+' && op != '-' && op != '.')
4616 break;
4617
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004618 if ((op != '+' || rettv->v_type != VAR_LIST)
4619#ifdef FEAT_FLOAT
4620 && (op == '.' || rettv->v_type != VAR_FLOAT)
4621#endif
4622 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004623 {
4624 /* For "list + ...", an illegal use of the first operand as
4625 * a number cannot be determined before evaluating the 2nd
4626 * operand: if this is also a list, all is ok.
4627 * For "something . ...", "something - ..." or "non-list + ...",
4628 * we know that the first operand needs to be a string or number
4629 * without evaluating the 2nd operand. So check before to avoid
4630 * side effects after an error. */
4631 if (evaluate && get_tv_string_chk(rettv) == NULL)
4632 {
4633 clear_tv(rettv);
4634 return FAIL;
4635 }
4636 }
4637
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 /*
4639 * Get the second variable.
4640 */
4641 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004642 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004644 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 return FAIL;
4646 }
4647
4648 if (evaluate)
4649 {
4650 /*
4651 * Compute the result.
4652 */
4653 if (op == '.')
4654 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004655 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4656 s2 = get_tv_string_buf_chk(&var2, buf2);
4657 if (s2 == NULL) /* type error ? */
4658 {
4659 clear_tv(rettv);
4660 clear_tv(&var2);
4661 return FAIL;
4662 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004663 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004664 clear_tv(rettv);
4665 rettv->v_type = VAR_STRING;
4666 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004668 else if (op == '+' && rettv->v_type == VAR_LIST
4669 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004670 {
4671 /* concatenate Lists */
4672 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4673 &var3) == FAIL)
4674 {
4675 clear_tv(rettv);
4676 clear_tv(&var2);
4677 return FAIL;
4678 }
4679 clear_tv(rettv);
4680 *rettv = var3;
4681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 else
4683 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004684 int error = FALSE;
4685
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004686#ifdef FEAT_FLOAT
4687 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004688 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004689 f1 = rettv->vval.v_float;
4690 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004691 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004692 else
4693#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004694 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004695 n1 = get_tv_number_chk(rettv, &error);
4696 if (error)
4697 {
4698 /* This can only happen for "list + non-list". For
4699 * "non-list + ..." or "something - ...", we returned
4700 * before evaluating the 2nd operand. */
4701 clear_tv(rettv);
4702 return FAIL;
4703 }
4704#ifdef FEAT_FLOAT
4705 if (var2.v_type == VAR_FLOAT)
4706 f1 = n1;
4707#endif
4708 }
4709#ifdef FEAT_FLOAT
4710 if (var2.v_type == VAR_FLOAT)
4711 {
4712 f2 = var2.vval.v_float;
4713 n2 = 0;
4714 }
4715 else
4716#endif
4717 {
4718 n2 = get_tv_number_chk(&var2, &error);
4719 if (error)
4720 {
4721 clear_tv(rettv);
4722 clear_tv(&var2);
4723 return FAIL;
4724 }
4725#ifdef FEAT_FLOAT
4726 if (rettv->v_type == VAR_FLOAT)
4727 f2 = n2;
4728#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004729 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004730 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004731
4732#ifdef FEAT_FLOAT
4733 /* If there is a float on either side the result is a float. */
4734 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4735 {
4736 if (op == '+')
4737 f1 = f1 + f2;
4738 else
4739 f1 = f1 - f2;
4740 rettv->v_type = VAR_FLOAT;
4741 rettv->vval.v_float = f1;
4742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004744#endif
4745 {
4746 if (op == '+')
4747 n1 = n1 + n2;
4748 else
4749 n1 = n1 - n2;
4750 rettv->v_type = VAR_NUMBER;
4751 rettv->vval.v_number = n1;
4752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004754 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 }
4756 }
4757 return OK;
4758}
4759
4760/*
4761 * Handle fifth level expression:
4762 * * number multiplication
4763 * / number division
4764 * % number modulo
4765 *
4766 * "arg" must point to the first non-white of the expression.
4767 * "arg" is advanced to the next non-white after the recognized expression.
4768 *
4769 * Return OK or FAIL.
4770 */
4771 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004772eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004776 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777{
Bram Moolenaar33570922005-01-25 22:26:29 +00004778 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 int op;
4780 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004781#ifdef FEAT_FLOAT
4782 int use_float = FALSE;
4783 float_T f1 = 0, f2;
4784#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004785 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786
4787 /*
4788 * Get the first variable.
4789 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004790 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 return FAIL;
4792
4793 /*
4794 * Repeat computing, until no '*', '/' or '%' is following.
4795 */
4796 for (;;)
4797 {
4798 op = **arg;
4799 if (op != '*' && op != '/' && op != '%')
4800 break;
4801
4802 if (evaluate)
4803 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004804#ifdef FEAT_FLOAT
4805 if (rettv->v_type == VAR_FLOAT)
4806 {
4807 f1 = rettv->vval.v_float;
4808 use_float = TRUE;
4809 n1 = 0;
4810 }
4811 else
4812#endif
4813 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004814 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004815 if (error)
4816 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 }
4818 else
4819 n1 = 0;
4820
4821 /*
4822 * Get the second variable.
4823 */
4824 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004825 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 return FAIL;
4827
4828 if (evaluate)
4829 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004830#ifdef FEAT_FLOAT
4831 if (var2.v_type == VAR_FLOAT)
4832 {
4833 if (!use_float)
4834 {
4835 f1 = n1;
4836 use_float = TRUE;
4837 }
4838 f2 = var2.vval.v_float;
4839 n2 = 0;
4840 }
4841 else
4842#endif
4843 {
4844 n2 = get_tv_number_chk(&var2, &error);
4845 clear_tv(&var2);
4846 if (error)
4847 return FAIL;
4848#ifdef FEAT_FLOAT
4849 if (use_float)
4850 f2 = n2;
4851#endif
4852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853
4854 /*
4855 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004856 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004858#ifdef FEAT_FLOAT
4859 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004861 if (op == '*')
4862 f1 = f1 * f2;
4863 else if (op == '/')
4864 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004865# ifdef VMS
4866 /* VMS crashes on divide by zero, work around it */
4867 if (f2 == 0.0)
4868 {
4869 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004870 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004871 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004872 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004873 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004874 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004875 }
4876 else
4877 f1 = f1 / f2;
4878# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004879 /* We rely on the floating point library to handle divide
4880 * by zero to result in "inf" and not a crash. */
4881 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004882# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004885 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004886 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004887 return FAIL;
4888 }
4889 rettv->v_type = VAR_FLOAT;
4890 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 }
4892 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895 if (op == '*')
4896 n1 = n1 * n2;
4897 else if (op == '/')
4898 {
4899 if (n2 == 0) /* give an error message? */
4900 {
4901 if (n1 == 0)
4902 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4903 else if (n1 < 0)
4904 n1 = -0x7fffffffL;
4905 else
4906 n1 = 0x7fffffffL;
4907 }
4908 else
4909 n1 = n1 / n2;
4910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004912 {
4913 if (n2 == 0) /* give an error message? */
4914 n1 = 0;
4915 else
4916 n1 = n1 % n2;
4917 }
4918 rettv->v_type = VAR_NUMBER;
4919 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 }
4922 }
4923
4924 return OK;
4925}
4926
4927/*
4928 * Handle sixth level expression:
4929 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004930 * "string" string constant
4931 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 * &option-name option value
4933 * @r register contents
4934 * identifier variable value
4935 * function() function call
4936 * $VAR environment variable
4937 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004938 * [expr, expr] List
4939 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 *
4941 * Also handle:
4942 * ! in front logical NOT
4943 * - in front unary minus
4944 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004945 * trailing [] subscript in String or List
4946 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 *
4948 * "arg" must point to the first non-white of the expression.
4949 * "arg" is advanced to the next non-white after the recognized expression.
4950 *
4951 * Return OK or FAIL.
4952 */
4953 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004954eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004958 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 long n;
4961 int len;
4962 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 char_u *start_leader, *end_leader;
4964 int ret = OK;
4965 char_u *alias;
4966
4967 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004968 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004969 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004971 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972
4973 /*
4974 * Skip '!' and '-' characters. They are handled later.
4975 */
4976 start_leader = *arg;
4977 while (**arg == '!' || **arg == '-' || **arg == '+')
4978 *arg = skipwhite(*arg + 1);
4979 end_leader = *arg;
4980
4981 switch (**arg)
4982 {
4983 /*
4984 * Number constant.
4985 */
4986 case '0':
4987 case '1':
4988 case '2':
4989 case '3':
4990 case '4':
4991 case '5':
4992 case '6':
4993 case '7':
4994 case '8':
4995 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004996 {
4997#ifdef FEAT_FLOAT
4998 char_u *p = skipdigits(*arg + 1);
4999 int get_float = FALSE;
5000
5001 /* We accept a float when the format matches
5002 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005003 * strict to avoid backwards compatibility problems.
5004 * Don't look for a float after the "." operator, so that
5005 * ":let vers = 1.2.3" doesn't fail. */
5006 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005008 get_float = TRUE;
5009 p = skipdigits(p + 2);
5010 if (*p == 'e' || *p == 'E')
5011 {
5012 ++p;
5013 if (*p == '-' || *p == '+')
5014 ++p;
5015 if (!vim_isdigit(*p))
5016 get_float = FALSE;
5017 else
5018 p = skipdigits(p + 1);
5019 }
5020 if (ASCII_ISALPHA(*p) || *p == '.')
5021 get_float = FALSE;
5022 }
5023 if (get_float)
5024 {
5025 float_T f;
5026
5027 *arg += string2float(*arg, &f);
5028 if (evaluate)
5029 {
5030 rettv->v_type = VAR_FLOAT;
5031 rettv->vval.v_float = f;
5032 }
5033 }
5034 else
5035#endif
5036 {
5037 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5038 *arg += len;
5039 if (evaluate)
5040 {
5041 rettv->v_type = VAR_NUMBER;
5042 rettv->vval.v_number = n;
5043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 }
5045 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
5048 /*
5049 * String constant: "string".
5050 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005051 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 break;
5053
5054 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005057 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005058 break;
5059
5060 /*
5061 * List: [expr, expr]
5062 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005063 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 break;
5065
5066 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 * Dictionary: {key: val, key: val}
5068 */
5069 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5070 break;
5071
5072 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005073 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005075 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 break;
5077
5078 /*
5079 * Environment variable: $VAR.
5080 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005081 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 break;
5083
5084 /*
5085 * Register contents: @r.
5086 */
5087 case '@': ++*arg;
5088 if (evaluate)
5089 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005091 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
5093 if (**arg != NUL)
5094 ++*arg;
5095 break;
5096
5097 /*
5098 * nested expression: (expression).
5099 */
5100 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005101 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 if (**arg == ')')
5103 ++*arg;
5104 else if (ret == OK)
5105 {
5106 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005107 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 ret = FAIL;
5109 }
5110 break;
5111
Bram Moolenaar8c711452005-01-14 21:53:12 +00005112 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 break;
5114 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005115
5116 if (ret == NOTDONE)
5117 {
5118 /*
5119 * Must be a variable or function name.
5120 * Can also be a curly-braces kind of name: {expr}.
5121 */
5122 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005123 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005124 if (alias != NULL)
5125 s = alias;
5126
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005127 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005128 ret = FAIL;
5129 else
5130 {
5131 if (**arg == '(') /* recursive! */
5132 {
5133 /* If "s" is the name of a variable of type VAR_FUNC
5134 * use its contents. */
5135 s = deref_func_name(s, &len);
5136
5137 /* Invoke the function. */
5138 ret = get_func_tv(s, len, rettv, arg,
5139 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005140 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005141 /* Stop the expression evaluation when immediately
5142 * aborting on error, or when an interrupt occurred or
5143 * an exception was thrown but not caught. */
5144 if (aborting())
5145 {
5146 if (ret == OK)
5147 clear_tv(rettv);
5148 ret = FAIL;
5149 }
5150 }
5151 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005152 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005153 else
5154 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005156 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005157 }
5158
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 *arg = skipwhite(*arg);
5160
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005161 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5162 * expr(expr). */
5163 if (ret == OK)
5164 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165
5166 /*
5167 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5168 */
5169 if (ret == OK && evaluate && end_leader > start_leader)
5170 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005171 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005172 int val = 0;
5173#ifdef FEAT_FLOAT
5174 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005175
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005176 if (rettv->v_type == VAR_FLOAT)
5177 f = rettv->vval.v_float;
5178 else
5179#endif
5180 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005181 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005183 clear_tv(rettv);
5184 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005186 else
5187 {
5188 while (end_leader > start_leader)
5189 {
5190 --end_leader;
5191 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005192 {
5193#ifdef FEAT_FLOAT
5194 if (rettv->v_type == VAR_FLOAT)
5195 f = !f;
5196 else
5197#endif
5198 val = !val;
5199 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005200 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005201 {
5202#ifdef FEAT_FLOAT
5203 if (rettv->v_type == VAR_FLOAT)
5204 f = -f;
5205 else
5206#endif
5207 val = -val;
5208 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005209 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005210#ifdef FEAT_FLOAT
5211 if (rettv->v_type == VAR_FLOAT)
5212 {
5213 clear_tv(rettv);
5214 rettv->vval.v_float = f;
5215 }
5216 else
5217#endif
5218 {
5219 clear_tv(rettv);
5220 rettv->v_type = VAR_NUMBER;
5221 rettv->vval.v_number = val;
5222 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
5225
5226 return ret;
5227}
5228
5229/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005230 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5231 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005232 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5233 */
5234 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005235eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005236 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005237 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005238 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005239 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005240{
5241 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005242 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005243 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005244 long len = -1;
5245 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005248
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005249 if (rettv->v_type == VAR_FUNC
5250#ifdef FEAT_FLOAT
5251 || rettv->v_type == VAR_FLOAT
5252#endif
5253 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005255 if (verbose)
5256 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 return FAIL;
5258 }
5259
Bram Moolenaar8c711452005-01-14 21:53:12 +00005260 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005262 /*
5263 * dict.name
5264 */
5265 key = *arg + 1;
5266 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5267 ;
5268 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005270 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 }
5272 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005274 /*
5275 * something[idx]
5276 *
5277 * Get the (first) variable from inside the [].
5278 */
5279 *arg = skipwhite(*arg + 1);
5280 if (**arg == ':')
5281 empty1 = TRUE;
5282 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5283 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005284 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5285 {
5286 /* not a number or string */
5287 clear_tv(&var1);
5288 return FAIL;
5289 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290
5291 /*
5292 * Get the second variable from inside the [:].
5293 */
5294 if (**arg == ':')
5295 {
5296 range = TRUE;
5297 *arg = skipwhite(*arg + 1);
5298 if (**arg == ']')
5299 empty2 = TRUE;
5300 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5301 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005302 if (!empty1)
5303 clear_tv(&var1);
5304 return FAIL;
5305 }
5306 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5307 {
5308 /* not a number or string */
5309 if (!empty1)
5310 clear_tv(&var1);
5311 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005312 return FAIL;
5313 }
5314 }
5315
5316 /* Check for the ']'. */
5317 if (**arg != ']')
5318 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005319 if (verbose)
5320 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005321 clear_tv(&var1);
5322 if (range)
5323 clear_tv(&var2);
5324 return FAIL;
5325 }
5326 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327 }
5328
5329 if (evaluate)
5330 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005331 n1 = 0;
5332 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005333 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005334 n1 = get_tv_number(&var1);
5335 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336 }
5337 if (range)
5338 {
5339 if (empty2)
5340 n2 = -1;
5341 else
5342 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005343 n2 = get_tv_number(&var2);
5344 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 }
5346 }
5347
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005348 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 {
5350 case VAR_NUMBER:
5351 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005352 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 len = (long)STRLEN(s);
5354 if (range)
5355 {
5356 /* The resulting variable is a substring. If the indexes
5357 * are out of range the result is empty. */
5358 if (n1 < 0)
5359 {
5360 n1 = len + n1;
5361 if (n1 < 0)
5362 n1 = 0;
5363 }
5364 if (n2 < 0)
5365 n2 = len + n2;
5366 else if (n2 >= len)
5367 n2 = len;
5368 if (n1 >= len || n2 < 0 || n1 > n2)
5369 s = NULL;
5370 else
5371 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5372 }
5373 else
5374 {
5375 /* The resulting variable is a string of a single
5376 * character. If the index is too big or negative the
5377 * result is empty. */
5378 if (n1 >= len || n1 < 0)
5379 s = NULL;
5380 else
5381 s = vim_strnsave(s + n1, 1);
5382 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005383 clear_tv(rettv);
5384 rettv->v_type = VAR_STRING;
5385 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 break;
5387
5388 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005389 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 if (n1 < 0)
5391 n1 = len + n1;
5392 if (!empty1 && (n1 < 0 || n1 >= len))
5393 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005394 /* For a range we allow invalid values and return an empty
5395 * list. A list index out of range is an error. */
5396 if (!range)
5397 {
5398 if (verbose)
5399 EMSGN(_(e_listidx), n1);
5400 return FAIL;
5401 }
5402 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 }
5404 if (range)
5405 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005406 list_T *l;
5407 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408
5409 if (n2 < 0)
5410 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005411 else if (n2 >= len)
5412 n2 = len - 1;
5413 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005414 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415 l = list_alloc();
5416 if (l == NULL)
5417 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005418 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419 n1 <= n2; ++n1)
5420 {
5421 if (list_append_tv(l, &item->li_tv) == FAIL)
5422 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005423 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005424 return FAIL;
5425 }
5426 item = item->li_next;
5427 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005428 clear_tv(rettv);
5429 rettv->v_type = VAR_LIST;
5430 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005431 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 }
5433 else
5434 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005435 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005436 clear_tv(rettv);
5437 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 }
5439 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005440
5441 case VAR_DICT:
5442 if (range)
5443 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005444 if (verbose)
5445 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005446 if (len == -1)
5447 clear_tv(&var1);
5448 return FAIL;
5449 }
5450 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005451 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005452
5453 if (len == -1)
5454 {
5455 key = get_tv_string(&var1);
5456 if (*key == NUL)
5457 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005458 if (verbose)
5459 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005460 clear_tv(&var1);
5461 return FAIL;
5462 }
5463 }
5464
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005465 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005466
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005467 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005468 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005469 if (len == -1)
5470 clear_tv(&var1);
5471 if (item == NULL)
5472 return FAIL;
5473
5474 copy_tv(&item->di_tv, &var1);
5475 clear_tv(rettv);
5476 *rettv = var1;
5477 }
5478 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 }
5480 }
5481
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482 return OK;
5483}
5484
5485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 * Get an option value.
5487 * "arg" points to the '&' or '+' before the option name.
5488 * "arg" is advanced to character after the option name.
5489 * Return OK or FAIL.
5490 */
5491 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005492get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005494 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 int evaluate;
5496{
5497 char_u *option_end;
5498 long numval;
5499 char_u *stringval;
5500 int opt_type;
5501 int c;
5502 int working = (**arg == '+'); /* has("+option") */
5503 int ret = OK;
5504 int opt_flags;
5505
5506 /*
5507 * Isolate the option name and find its value.
5508 */
5509 option_end = find_option_end(arg, &opt_flags);
5510 if (option_end == NULL)
5511 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005512 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 EMSG2(_("E112: Option name missing: %s"), *arg);
5514 return FAIL;
5515 }
5516
5517 if (!evaluate)
5518 {
5519 *arg = option_end;
5520 return OK;
5521 }
5522
5523 c = *option_end;
5524 *option_end = NUL;
5525 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005526 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527
5528 if (opt_type == -3) /* invalid name */
5529 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005530 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 EMSG2(_("E113: Unknown option: %s"), *arg);
5532 ret = FAIL;
5533 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005534 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 {
5536 if (opt_type == -2) /* hidden string option */
5537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005538 rettv->v_type = VAR_STRING;
5539 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 }
5541 else if (opt_type == -1) /* hidden number option */
5542 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005543 rettv->v_type = VAR_NUMBER;
5544 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 }
5546 else if (opt_type == 1) /* number option */
5547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005548 rettv->v_type = VAR_NUMBER;
5549 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 }
5551 else /* string option */
5552 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 rettv->v_type = VAR_STRING;
5554 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 }
5556 }
5557 else if (working && (opt_type == -2 || opt_type == -1))
5558 ret = FAIL;
5559
5560 *option_end = c; /* put back for error messages */
5561 *arg = option_end;
5562
5563 return ret;
5564}
5565
5566/*
5567 * Allocate a variable for a string constant.
5568 * Return OK or FAIL.
5569 */
5570 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 int evaluate;
5575{
5576 char_u *p;
5577 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 int extra = 0;
5579
5580 /*
5581 * Find the end of the string, skipping backslashed characters.
5582 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005583 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 {
5585 if (*p == '\\' && p[1] != NUL)
5586 {
5587 ++p;
5588 /* A "\<x>" form occupies at least 4 characters, and produces up
5589 * to 6 characters: reserve space for 2 extra */
5590 if (*p == '<')
5591 extra += 2;
5592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 }
5594
5595 if (*p != '"')
5596 {
5597 EMSG2(_("E114: Missing quote: %s"), *arg);
5598 return FAIL;
5599 }
5600
5601 /* If only parsing, set *arg and return here */
5602 if (!evaluate)
5603 {
5604 *arg = p + 1;
5605 return OK;
5606 }
5607
5608 /*
5609 * Copy the string into allocated memory, handling backslashed
5610 * characters.
5611 */
5612 name = alloc((unsigned)(p - *arg + extra));
5613 if (name == NULL)
5614 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005615 rettv->v_type = VAR_STRING;
5616 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617
Bram Moolenaar8c711452005-01-14 21:53:12 +00005618 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
5620 if (*p == '\\')
5621 {
5622 switch (*++p)
5623 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005624 case 'b': *name++ = BS; ++p; break;
5625 case 'e': *name++ = ESC; ++p; break;
5626 case 'f': *name++ = FF; ++p; break;
5627 case 'n': *name++ = NL; ++p; break;
5628 case 'r': *name++ = CAR; ++p; break;
5629 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630
5631 case 'X': /* hex: "\x1", "\x12" */
5632 case 'x':
5633 case 'u': /* Unicode: "\u0023" */
5634 case 'U':
5635 if (vim_isxdigit(p[1]))
5636 {
5637 int n, nr;
5638 int c = toupper(*p);
5639
5640 if (c == 'X')
5641 n = 2;
5642 else
5643 n = 4;
5644 nr = 0;
5645 while (--n >= 0 && vim_isxdigit(p[1]))
5646 {
5647 ++p;
5648 nr = (nr << 4) + hex2nr(*p);
5649 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005650 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651#ifdef FEAT_MBYTE
5652 /* For "\u" store the number according to
5653 * 'encoding'. */
5654 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005655 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 else
5657#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005658 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 break;
5661
5662 /* octal: "\1", "\12", "\123" */
5663 case '0':
5664 case '1':
5665 case '2':
5666 case '3':
5667 case '4':
5668 case '5':
5669 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670 case '7': *name = *p++ - '0';
5671 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005673 *name = (*name << 3) + *p++ - '0';
5674 if (*p >= '0' && *p <= '7')
5675 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 break;
5679
5680 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005681 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 if (extra != 0)
5683 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005684 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 break;
5686 }
5687 /* FALLTHROUGH */
5688
Bram Moolenaar8c711452005-01-14 21:53:12 +00005689 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 break;
5691 }
5692 }
5693 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005694 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005697 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 *arg = p + 1;
5699
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 return OK;
5701}
5702
5703/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005704 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 * Return OK or FAIL.
5706 */
5707 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005708get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 int evaluate;
5712{
5713 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005714 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005715 int reduce = 0;
5716
5717 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005718 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005719 */
5720 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5721 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005722 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005723 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005725 break;
5726 ++reduce;
5727 ++p;
5728 }
5729 }
5730
Bram Moolenaar8c711452005-01-14 21:53:12 +00005731 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005732 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005733 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005734 return FAIL;
5735 }
5736
Bram Moolenaar8c711452005-01-14 21:53:12 +00005737 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005738 if (!evaluate)
5739 {
5740 *arg = p + 1;
5741 return OK;
5742 }
5743
5744 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005746 */
5747 str = alloc((unsigned)((p - *arg) - reduce));
5748 if (str == NULL)
5749 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 rettv->v_type = VAR_STRING;
5751 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005752
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005754 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005756 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005758 break;
5759 ++p;
5760 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005762 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005763 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005764 *arg = p + 1;
5765
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 return OK;
5767}
5768
5769/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005770 * Allocate a variable for a List and fill it from "*arg".
5771 * Return OK or FAIL.
5772 */
5773 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005774get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005776 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005777 int evaluate;
5778{
Bram Moolenaar33570922005-01-25 22:26:29 +00005779 list_T *l = NULL;
5780 typval_T tv;
5781 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005782
5783 if (evaluate)
5784 {
5785 l = list_alloc();
5786 if (l == NULL)
5787 return FAIL;
5788 }
5789
5790 *arg = skipwhite(*arg + 1);
5791 while (**arg != ']' && **arg != NUL)
5792 {
5793 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5794 goto failret;
5795 if (evaluate)
5796 {
5797 item = listitem_alloc();
5798 if (item != NULL)
5799 {
5800 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005801 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802 list_append(l, item);
5803 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005804 else
5805 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 }
5807
5808 if (**arg == ']')
5809 break;
5810 if (**arg != ',')
5811 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813 goto failret;
5814 }
5815 *arg = skipwhite(*arg + 1);
5816 }
5817
5818 if (**arg != ']')
5819 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821failret:
5822 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005823 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005824 return FAIL;
5825 }
5826
5827 *arg = skipwhite(*arg + 1);
5828 if (evaluate)
5829 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005830 rettv->v_type = VAR_LIST;
5831 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832 ++l->lv_refcount;
5833 }
5834
5835 return OK;
5836}
5837
5838/*
5839 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005840 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005841 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005842 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005843list_alloc()
5844{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005845 list_T *l;
5846
5847 l = (list_T *)alloc_clear(sizeof(list_T));
5848 if (l != NULL)
5849 {
5850 /* Prepend the list to the list of lists for garbage collection. */
5851 if (first_list != NULL)
5852 first_list->lv_used_prev = l;
5853 l->lv_used_prev = NULL;
5854 l->lv_used_next = first_list;
5855 first_list = l;
5856 }
5857 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005858}
5859
5860/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005861 * Allocate an empty list for a return value.
5862 * Returns OK or FAIL.
5863 */
5864 static int
5865rettv_list_alloc(rettv)
5866 typval_T *rettv;
5867{
5868 list_T *l = list_alloc();
5869
5870 if (l == NULL)
5871 return FAIL;
5872
5873 rettv->vval.v_list = l;
5874 rettv->v_type = VAR_LIST;
5875 ++l->lv_refcount;
5876 return OK;
5877}
5878
5879/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880 * Unreference a list: decrement the reference count and free it when it
5881 * becomes zero.
5882 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005883 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005885 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005887 if (l != NULL && --l->lv_refcount <= 0)
5888 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005889}
5890
5891/*
5892 * Free a list, including all items it points to.
5893 * Ignores the reference count.
5894 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005895 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005896list_free(l, recurse)
5897 list_T *l;
5898 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899{
Bram Moolenaar33570922005-01-25 22:26:29 +00005900 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005902 /* Remove the list from the list of lists for garbage collection. */
5903 if (l->lv_used_prev == NULL)
5904 first_list = l->lv_used_next;
5905 else
5906 l->lv_used_prev->lv_used_next = l->lv_used_next;
5907 if (l->lv_used_next != NULL)
5908 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5909
Bram Moolenaard9fba312005-06-26 22:34:35 +00005910 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005912 /* Remove the item before deleting it. */
5913 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005914 if (recurse || (item->li_tv.v_type != VAR_LIST
5915 && item->li_tv.v_type != VAR_DICT))
5916 clear_tv(&item->li_tv);
5917 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918 }
5919 vim_free(l);
5920}
5921
5922/*
5923 * Allocate a list item.
5924 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005925 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926listitem_alloc()
5927{
Bram Moolenaar33570922005-01-25 22:26:29 +00005928 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929}
5930
5931/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005932 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933 */
5934 static void
5935listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005936 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005938 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 vim_free(item);
5940}
5941
5942/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005943 * Remove a list item from a List and free it. Also clears the value.
5944 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005945 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005946listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005947 list_T *l;
5948 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005949{
5950 list_remove(l, item, item);
5951 listitem_free(item);
5952}
5953
5954/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955 * Get the number of items in a list.
5956 */
5957 static long
5958list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005959 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961 if (l == NULL)
5962 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005963 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964}
5965
5966/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005967 * Return TRUE when two lists have exactly the same values.
5968 */
5969 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005970list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005971 list_T *l1;
5972 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005973 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005974 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005975{
Bram Moolenaar33570922005-01-25 22:26:29 +00005976 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005977
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005978 if (l1 == NULL || l2 == NULL)
5979 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005980 if (l1 == l2)
5981 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005982 if (list_len(l1) != list_len(l2))
5983 return FALSE;
5984
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005985 for (item1 = l1->lv_first, item2 = l2->lv_first;
5986 item1 != NULL && item2 != NULL;
5987 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005988 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005989 return FALSE;
5990 return item1 == NULL && item2 == NULL;
5991}
5992
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005993#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5994 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005995/*
5996 * Return the dictitem that an entry in a hashtable points to.
5997 */
5998 dictitem_T *
5999dict_lookup(hi)
6000 hashitem_T *hi;
6001{
6002 return HI2DI(hi);
6003}
6004#endif
6005
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006006/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006007 * Return TRUE when two dictionaries have exactly the same key/values.
6008 */
6009 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006010dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006011 dict_T *d1;
6012 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006013 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006014 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006015{
Bram Moolenaar33570922005-01-25 22:26:29 +00006016 hashitem_T *hi;
6017 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006018 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006019
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006020 if (d1 == NULL || d2 == NULL)
6021 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006022 if (d1 == d2)
6023 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006024 if (dict_len(d1) != dict_len(d2))
6025 return FALSE;
6026
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006027 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006028 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006029 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006030 if (!HASHITEM_EMPTY(hi))
6031 {
6032 item2 = dict_find(d2, hi->hi_key, -1);
6033 if (item2 == NULL)
6034 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006035 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006036 return FALSE;
6037 --todo;
6038 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006039 }
6040 return TRUE;
6041}
6042
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006043static int tv_equal_recurse_limit;
6044
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006045/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006046 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006047 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006048 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006049 */
6050 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006051tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 typval_T *tv1;
6053 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006054 int ic; /* ignore case */
6055 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006056{
6057 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006058 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006059 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006060 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006061
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006062 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006063 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006064
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006065 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006066 * recursiveness to a limit. We guess they are equal then.
6067 * A fixed limit has the problem of still taking an awful long time.
6068 * Reduce the limit every time running into it. That should work fine for
6069 * deeply linked structures that are not recursively linked and catch
6070 * recursiveness quickly. */
6071 if (!recursive)
6072 tv_equal_recurse_limit = 1000;
6073 if (recursive_cnt >= tv_equal_recurse_limit)
6074 {
6075 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006076 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006077 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006078
6079 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006080 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006081 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006082 ++recursive_cnt;
6083 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6084 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006085 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006086
6087 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006088 ++recursive_cnt;
6089 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6090 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006091 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006092
6093 case VAR_FUNC:
6094 return (tv1->vval.v_string != NULL
6095 && tv2->vval.v_string != NULL
6096 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6097
6098 case VAR_NUMBER:
6099 return tv1->vval.v_number == tv2->vval.v_number;
6100
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006101#ifdef FEAT_FLOAT
6102 case VAR_FLOAT:
6103 return tv1->vval.v_float == tv2->vval.v_float;
6104#endif
6105
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006106 case VAR_STRING:
6107 s1 = get_tv_string_buf(tv1, buf1);
6108 s2 = get_tv_string_buf(tv2, buf2);
6109 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006110 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006111
6112 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006113 return TRUE;
6114}
6115
6116/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006117 * Locate item with index "n" in list "l" and return it.
6118 * A negative index is counted from the end; -1 is the last item.
6119 * Returns NULL when "n" is out of range.
6120 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006121 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006122list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006123 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006124 long n;
6125{
Bram Moolenaar33570922005-01-25 22:26:29 +00006126 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006127 long idx;
6128
6129 if (l == NULL)
6130 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006131
6132 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006133 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006134 n = l->lv_len + n;
6135
6136 /* Check for index out of range. */
6137 if (n < 0 || n >= l->lv_len)
6138 return NULL;
6139
6140 /* When there is a cached index may start search from there. */
6141 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006142 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006143 if (n < l->lv_idx / 2)
6144 {
6145 /* closest to the start of the list */
6146 item = l->lv_first;
6147 idx = 0;
6148 }
6149 else if (n > (l->lv_idx + l->lv_len) / 2)
6150 {
6151 /* closest to the end of the list */
6152 item = l->lv_last;
6153 idx = l->lv_len - 1;
6154 }
6155 else
6156 {
6157 /* closest to the cached index */
6158 item = l->lv_idx_item;
6159 idx = l->lv_idx;
6160 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006161 }
6162 else
6163 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006164 if (n < l->lv_len / 2)
6165 {
6166 /* closest to the start of the list */
6167 item = l->lv_first;
6168 idx = 0;
6169 }
6170 else
6171 {
6172 /* closest to the end of the list */
6173 item = l->lv_last;
6174 idx = l->lv_len - 1;
6175 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006176 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006177
6178 while (n > idx)
6179 {
6180 /* search forward */
6181 item = item->li_next;
6182 ++idx;
6183 }
6184 while (n < idx)
6185 {
6186 /* search backward */
6187 item = item->li_prev;
6188 --idx;
6189 }
6190
6191 /* cache the used index */
6192 l->lv_idx = idx;
6193 l->lv_idx_item = item;
6194
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006195 return item;
6196}
6197
6198/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006199 * Get list item "l[idx]" as a number.
6200 */
6201 static long
6202list_find_nr(l, idx, errorp)
6203 list_T *l;
6204 long idx;
6205 int *errorp; /* set to TRUE when something wrong */
6206{
6207 listitem_T *li;
6208
6209 li = list_find(l, idx);
6210 if (li == NULL)
6211 {
6212 if (errorp != NULL)
6213 *errorp = TRUE;
6214 return -1L;
6215 }
6216 return get_tv_number_chk(&li->li_tv, errorp);
6217}
6218
6219/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006220 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6221 */
6222 char_u *
6223list_find_str(l, idx)
6224 list_T *l;
6225 long idx;
6226{
6227 listitem_T *li;
6228
6229 li = list_find(l, idx - 1);
6230 if (li == NULL)
6231 {
6232 EMSGN(_(e_listidx), idx);
6233 return NULL;
6234 }
6235 return get_tv_string(&li->li_tv);
6236}
6237
6238/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006239 * Locate "item" list "l" and return its index.
6240 * Returns -1 when "item" is not in the list.
6241 */
6242 static long
6243list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006244 list_T *l;
6245 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006246{
6247 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006248 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006249
6250 if (l == NULL)
6251 return -1;
6252 idx = 0;
6253 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6254 ++idx;
6255 if (li == NULL)
6256 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006257 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006258}
6259
6260/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261 * Append item "item" to the end of list "l".
6262 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006263 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006264list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006265 list_T *l;
6266 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006267{
6268 if (l->lv_last == NULL)
6269 {
6270 /* empty list */
6271 l->lv_first = item;
6272 l->lv_last = item;
6273 item->li_prev = NULL;
6274 }
6275 else
6276 {
6277 l->lv_last->li_next = item;
6278 item->li_prev = l->lv_last;
6279 l->lv_last = item;
6280 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006281 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006282 item->li_next = NULL;
6283}
6284
6285/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006286 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006287 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006288 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006289 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006290list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006291 list_T *l;
6292 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006293{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006294 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006295
Bram Moolenaar05159a02005-02-26 23:04:13 +00006296 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006298 copy_tv(tv, &li->li_tv);
6299 list_append(l, li);
6300 return OK;
6301}
6302
6303/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006304 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006305 * Return FAIL when out of memory.
6306 */
6307 int
6308list_append_dict(list, dict)
6309 list_T *list;
6310 dict_T *dict;
6311{
6312 listitem_T *li = listitem_alloc();
6313
6314 if (li == NULL)
6315 return FAIL;
6316 li->li_tv.v_type = VAR_DICT;
6317 li->li_tv.v_lock = 0;
6318 li->li_tv.vval.v_dict = dict;
6319 list_append(list, li);
6320 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006321 return OK;
6322}
6323
6324/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006325 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006326 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006327 * Returns FAIL when out of memory.
6328 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006329 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006330list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006331 list_T *l;
6332 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006333 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006334{
6335 listitem_T *li = listitem_alloc();
6336
6337 if (li == NULL)
6338 return FAIL;
6339 list_append(l, li);
6340 li->li_tv.v_type = VAR_STRING;
6341 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006342 if (str == NULL)
6343 li->li_tv.vval.v_string = NULL;
6344 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006345 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006346 return FAIL;
6347 return OK;
6348}
6349
6350/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006351 * Append "n" to list "l".
6352 * Returns FAIL when out of memory.
6353 */
6354 static int
6355list_append_number(l, n)
6356 list_T *l;
6357 varnumber_T n;
6358{
6359 listitem_T *li;
6360
6361 li = listitem_alloc();
6362 if (li == NULL)
6363 return FAIL;
6364 li->li_tv.v_type = VAR_NUMBER;
6365 li->li_tv.v_lock = 0;
6366 li->li_tv.vval.v_number = n;
6367 list_append(l, li);
6368 return OK;
6369}
6370
6371/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006372 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006373 * If "item" is NULL append at the end.
6374 * Return FAIL when out of memory.
6375 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006376 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006377list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006378 list_T *l;
6379 typval_T *tv;
6380 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006381{
Bram Moolenaar33570922005-01-25 22:26:29 +00006382 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006383
6384 if (ni == NULL)
6385 return FAIL;
6386 copy_tv(tv, &ni->li_tv);
6387 if (item == NULL)
6388 /* Append new item at end of list. */
6389 list_append(l, ni);
6390 else
6391 {
6392 /* Insert new item before existing item. */
6393 ni->li_prev = item->li_prev;
6394 ni->li_next = item;
6395 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006396 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006397 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006398 ++l->lv_idx;
6399 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006400 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006401 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006402 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006403 l->lv_idx_item = NULL;
6404 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006405 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006406 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006407 }
6408 return OK;
6409}
6410
6411/*
6412 * Extend "l1" with "l2".
6413 * If "bef" is NULL append at the end, otherwise insert before this item.
6414 * Returns FAIL when out of memory.
6415 */
6416 static int
6417list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006418 list_T *l1;
6419 list_T *l2;
6420 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006421{
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006423 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006425 /* We also quit the loop when we have inserted the original item count of
6426 * the list, avoid a hang when we extend a list with itself. */
6427 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6429 return FAIL;
6430 return OK;
6431}
6432
6433/*
6434 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6435 * Return FAIL when out of memory.
6436 */
6437 static int
6438list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006439 list_T *l1;
6440 list_T *l2;
6441 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006442{
Bram Moolenaar33570922005-01-25 22:26:29 +00006443 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006444
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006445 if (l1 == NULL || l2 == NULL)
6446 return FAIL;
6447
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006449 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006450 if (l == NULL)
6451 return FAIL;
6452 tv->v_type = VAR_LIST;
6453 tv->vval.v_list = l;
6454
6455 /* append all items from the second list */
6456 return list_extend(l, l2, NULL);
6457}
6458
6459/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006460 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006461 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006462 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006463 * Returns NULL when out of memory.
6464 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006465 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006466list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006467 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006468 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006469 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006470{
Bram Moolenaar33570922005-01-25 22:26:29 +00006471 list_T *copy;
6472 listitem_T *item;
6473 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006474
6475 if (orig == NULL)
6476 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006477
6478 copy = list_alloc();
6479 if (copy != NULL)
6480 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006481 if (copyID != 0)
6482 {
6483 /* Do this before adding the items, because one of the items may
6484 * refer back to this list. */
6485 orig->lv_copyID = copyID;
6486 orig->lv_copylist = copy;
6487 }
6488 for (item = orig->lv_first; item != NULL && !got_int;
6489 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006490 {
6491 ni = listitem_alloc();
6492 if (ni == NULL)
6493 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006494 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006495 {
6496 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6497 {
6498 vim_free(ni);
6499 break;
6500 }
6501 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006502 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006503 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006504 list_append(copy, ni);
6505 }
6506 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006507 if (item != NULL)
6508 {
6509 list_unref(copy);
6510 copy = NULL;
6511 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006512 }
6513
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006514 return copy;
6515}
6516
6517/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006518 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006519 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006520 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006521 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006522list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006523 list_T *l;
6524 listitem_T *item;
6525 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526{
Bram Moolenaar33570922005-01-25 22:26:29 +00006527 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006528
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006529 /* notify watchers */
6530 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006531 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006532 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533 list_fix_watch(l, ip);
6534 if (ip == item2)
6535 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006536 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006537
6538 if (item2->li_next == NULL)
6539 l->lv_last = item->li_prev;
6540 else
6541 item2->li_next->li_prev = item->li_prev;
6542 if (item->li_prev == NULL)
6543 l->lv_first = item2->li_next;
6544 else
6545 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006546 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006547}
6548
6549/*
6550 * Return an allocated string with the string representation of a list.
6551 * May return NULL.
6552 */
6553 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006554list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006555 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006556 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557{
6558 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006559
6560 if (tv->vval.v_list == NULL)
6561 return NULL;
6562 ga_init2(&ga, (int)sizeof(char), 80);
6563 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006564 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006565 {
6566 vim_free(ga.ga_data);
6567 return NULL;
6568 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006569 ga_append(&ga, ']');
6570 ga_append(&ga, NUL);
6571 return (char_u *)ga.ga_data;
6572}
6573
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006574typedef struct join_S {
6575 char_u *s;
6576 char_u *tofree;
6577} join_T;
6578
6579 static int
6580list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6581 garray_T *gap; /* to store the result in */
6582 list_T *l;
6583 char_u *sep;
6584 int echo_style;
6585 int copyID;
6586 garray_T *join_gap; /* to keep each list item string */
6587{
6588 int i;
6589 join_T *p;
6590 int len;
6591 int sumlen = 0;
6592 int first = TRUE;
6593 char_u *tofree;
6594 char_u numbuf[NUMBUFLEN];
6595 listitem_T *item;
6596 char_u *s;
6597
6598 /* Stringify each item in the list. */
6599 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6600 {
6601 if (echo_style)
6602 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6603 else
6604 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6605 if (s == NULL)
6606 return FAIL;
6607
6608 len = (int)STRLEN(s);
6609 sumlen += len;
6610
6611 ga_grow(join_gap, 1);
6612 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6613 if (tofree != NULL || s != numbuf)
6614 {
6615 p->s = s;
6616 p->tofree = tofree;
6617 }
6618 else
6619 {
6620 p->s = vim_strnsave(s, len);
6621 p->tofree = p->s;
6622 }
6623
6624 line_breakcheck();
6625 }
6626
6627 /* Allocate result buffer with its total size, avoid re-allocation and
6628 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6629 if (join_gap->ga_len >= 2)
6630 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6631 if (ga_grow(gap, sumlen + 2) == FAIL)
6632 return FAIL;
6633
6634 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6635 {
6636 if (first)
6637 first = FALSE;
6638 else
6639 ga_concat(gap, sep);
6640 p = ((join_T *)join_gap->ga_data) + i;
6641
6642 if (p->s != NULL)
6643 ga_concat(gap, p->s);
6644 line_breakcheck();
6645 }
6646
6647 return OK;
6648}
6649
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006650/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006651 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006652 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006653 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006654 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006655 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006656list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006657 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006658 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006659 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006660 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006661 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006662{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006663 garray_T join_ga;
6664 int retval;
6665 join_T *p;
6666 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006667
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006668 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6669 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6670
6671 /* Dispose each item in join_ga. */
6672 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006673 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006674 p = (join_T *)join_ga.ga_data;
6675 for (i = 0; i < join_ga.ga_len; ++i)
6676 {
6677 vim_free(p->tofree);
6678 ++p;
6679 }
6680 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006681 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006682
6683 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006684}
6685
6686/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006687 * Garbage collection for lists and dictionaries.
6688 *
6689 * We use reference counts to be able to free most items right away when they
6690 * are no longer used. But for composite items it's possible that it becomes
6691 * unused while the reference count is > 0: When there is a recursive
6692 * reference. Example:
6693 * :let l = [1, 2, 3]
6694 * :let d = {9: l}
6695 * :let l[1] = d
6696 *
6697 * Since this is quite unusual we handle this with garbage collection: every
6698 * once in a while find out which lists and dicts are not referenced from any
6699 * variable.
6700 *
6701 * Here is a good reference text about garbage collection (refers to Python
6702 * but it applies to all reference-counting mechanisms):
6703 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006704 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006705
6706/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006707 * Do garbage collection for lists and dicts.
6708 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006709 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006710 int
6711garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006712{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006713 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006714 buf_T *buf;
6715 win_T *wp;
6716 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006717 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006718 int did_free;
6719 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006720#ifdef FEAT_WINDOWS
6721 tabpage_T *tp;
6722#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006723
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006724 /* Only do this once. */
6725 want_garbage_collect = FALSE;
6726 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006727 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006728
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006729 /* We advance by two because we add one for items referenced through
6730 * previous_funccal. */
6731 current_copyID += COPYID_INC;
6732 copyID = current_copyID;
6733
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006734 /*
6735 * 1. Go through all accessible variables and mark all lists and dicts
6736 * with copyID.
6737 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006738
6739 /* Don't free variables in the previous_funccal list unless they are only
6740 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006741 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006742 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6743 {
6744 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6745 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6746 }
6747
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006748 /* script-local variables */
6749 for (i = 1; i <= ga_scripts.ga_len; ++i)
6750 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6751
6752 /* buffer-local variables */
6753 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6754 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6755
6756 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006757 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006758 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6759
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006760#ifdef FEAT_WINDOWS
6761 /* tabpage-local variables */
6762 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6763 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6764#endif
6765
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006766 /* global variables */
6767 set_ref_in_ht(&globvarht, copyID);
6768
6769 /* function-local variables */
6770 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6771 {
6772 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6773 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6774 }
6775
Bram Moolenaard812df62008-11-09 12:46:09 +00006776 /* v: vars */
6777 set_ref_in_ht(&vimvarht, copyID);
6778
Bram Moolenaar1dced572012-04-05 16:54:08 +02006779#ifdef FEAT_LUA
6780 set_ref_in_lua(copyID);
6781#endif
6782
Bram Moolenaardb913952012-06-29 12:54:53 +02006783#ifdef FEAT_PYTHON
6784 set_ref_in_python(copyID);
6785#endif
6786
6787#ifdef FEAT_PYTHON3
6788 set_ref_in_python3(copyID);
6789#endif
6790
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006791 /*
6792 * 2. Free lists and dictionaries that are not referenced.
6793 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006794 did_free = free_unref_items(copyID);
6795
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006796 /*
6797 * 3. Check if any funccal can be freed now.
6798 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006799 for (pfc = &previous_funccal; *pfc != NULL; )
6800 {
6801 if (can_free_funccal(*pfc, copyID))
6802 {
6803 fc = *pfc;
6804 *pfc = fc->caller;
6805 free_funccal(fc, TRUE);
6806 did_free = TRUE;
6807 did_free_funccal = TRUE;
6808 }
6809 else
6810 pfc = &(*pfc)->caller;
6811 }
6812 if (did_free_funccal)
6813 /* When a funccal was freed some more items might be garbage
6814 * collected, so run again. */
6815 (void)garbage_collect();
6816
6817 return did_free;
6818}
6819
6820/*
6821 * Free lists and dictionaries that are no longer referenced.
6822 */
6823 static int
6824free_unref_items(copyID)
6825 int copyID;
6826{
6827 dict_T *dd;
6828 list_T *ll;
6829 int did_free = FALSE;
6830
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006831 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006832 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006833 */
6834 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006835 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006836 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006837 /* Free the Dictionary and ordinary items it contains, but don't
6838 * recurse into Lists and Dictionaries, they will be in the list
6839 * of dicts or list of lists. */
6840 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006841 did_free = TRUE;
6842
6843 /* restart, next dict may also have been freed */
6844 dd = first_dict;
6845 }
6846 else
6847 dd = dd->dv_used_next;
6848
6849 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006850 * Go through the list of lists and free items without the copyID.
6851 * But don't free a list that has a watcher (used in a for loop), these
6852 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006853 */
6854 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006855 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6856 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006857 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006858 /* Free the List and ordinary items it contains, but don't recurse
6859 * into Lists and Dictionaries, they will be in the list of dicts
6860 * or list of lists. */
6861 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006862 did_free = TRUE;
6863
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006864 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006865 ll = first_list;
6866 }
6867 else
6868 ll = ll->lv_used_next;
6869
6870 return did_free;
6871}
6872
6873/*
6874 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6875 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006876 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006877set_ref_in_ht(ht, copyID)
6878 hashtab_T *ht;
6879 int copyID;
6880{
6881 int todo;
6882 hashitem_T *hi;
6883
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006884 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885 for (hi = ht->ht_array; todo > 0; ++hi)
6886 if (!HASHITEM_EMPTY(hi))
6887 {
6888 --todo;
6889 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6890 }
6891}
6892
6893/*
6894 * Mark all lists and dicts referenced through list "l" with "copyID".
6895 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006896 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006897set_ref_in_list(l, copyID)
6898 list_T *l;
6899 int copyID;
6900{
6901 listitem_T *li;
6902
6903 for (li = l->lv_first; li != NULL; li = li->li_next)
6904 set_ref_in_item(&li->li_tv, copyID);
6905}
6906
6907/*
6908 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6909 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006910 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911set_ref_in_item(tv, copyID)
6912 typval_T *tv;
6913 int copyID;
6914{
6915 dict_T *dd;
6916 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006917
6918 switch (tv->v_type)
6919 {
6920 case VAR_DICT:
6921 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006922 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006923 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006924 /* Didn't see this dict yet. */
6925 dd->dv_copyID = copyID;
6926 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006927 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006928 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006929
6930 case VAR_LIST:
6931 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006932 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006933 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006934 /* Didn't see this list yet. */
6935 ll->lv_copyID = copyID;
6936 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006937 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006938 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006939 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006940 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006941}
6942
6943/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006944 * Allocate an empty header for a dictionary.
6945 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006946 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006947dict_alloc()
6948{
Bram Moolenaar33570922005-01-25 22:26:29 +00006949 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006950
Bram Moolenaar33570922005-01-25 22:26:29 +00006951 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006952 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006953 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006954 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006955 if (first_dict != NULL)
6956 first_dict->dv_used_prev = d;
6957 d->dv_used_next = first_dict;
6958 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006959 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006960
Bram Moolenaar33570922005-01-25 22:26:29 +00006961 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006962 d->dv_lock = 0;
6963 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006964 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006965 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006966 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006967}
6968
6969/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006970 * Allocate an empty dict for a return value.
6971 * Returns OK or FAIL.
6972 */
6973 static int
6974rettv_dict_alloc(rettv)
6975 typval_T *rettv;
6976{
6977 dict_T *d = dict_alloc();
6978
6979 if (d == NULL)
6980 return FAIL;
6981
6982 rettv->vval.v_dict = d;
6983 rettv->v_type = VAR_DICT;
6984 ++d->dv_refcount;
6985 return OK;
6986}
6987
6988
6989/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006990 * Unreference a Dictionary: decrement the reference count and free it when it
6991 * becomes zero.
6992 */
Bram Moolenaar82139082011-09-14 16:52:09 +02006993 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006994dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006995 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006996{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006997 if (d != NULL && --d->dv_refcount <= 0)
6998 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006999}
7000
7001/*
7002 * Free a Dictionary, including all items it contains.
7003 * Ignores the reference count.
7004 */
7005 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007006dict_free(d, recurse)
7007 dict_T *d;
7008 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007009{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007010 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007011 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007012 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007013
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014 /* Remove the dict from the list of dicts for garbage collection. */
7015 if (d->dv_used_prev == NULL)
7016 first_dict = d->dv_used_next;
7017 else
7018 d->dv_used_prev->dv_used_next = d->dv_used_next;
7019 if (d->dv_used_next != NULL)
7020 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7021
7022 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007023 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007024 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007025 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007026 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007027 if (!HASHITEM_EMPTY(hi))
7028 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007029 /* Remove the item before deleting it, just in case there is
7030 * something recursive causing trouble. */
7031 di = HI2DI(hi);
7032 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007033 if (recurse || (di->di_tv.v_type != VAR_LIST
7034 && di->di_tv.v_type != VAR_DICT))
7035 clear_tv(&di->di_tv);
7036 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007037 --todo;
7038 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007039 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007040 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007041 vim_free(d);
7042}
7043
7044/*
7045 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007046 * The "key" is copied to the new item.
7047 * Note that the value of the item "di_tv" still needs to be initialized!
7048 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007049 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007050 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007051dictitem_alloc(key)
7052 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007053{
Bram Moolenaar33570922005-01-25 22:26:29 +00007054 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007055
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007056 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007057 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007058 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007059 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007060 di->di_flags = 0;
7061 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007062 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007063}
7064
7065/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007066 * Make a copy of a Dictionary item.
7067 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007068 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007069dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007070 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007071{
Bram Moolenaar33570922005-01-25 22:26:29 +00007072 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007073
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007074 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7075 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007076 if (di != NULL)
7077 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007078 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007079 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007080 copy_tv(&org->di_tv, &di->di_tv);
7081 }
7082 return di;
7083}
7084
7085/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007086 * Remove item "item" from Dictionary "dict" and free it.
7087 */
7088 static void
7089dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007090 dict_T *dict;
7091 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007092{
Bram Moolenaar33570922005-01-25 22:26:29 +00007093 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007094
Bram Moolenaar33570922005-01-25 22:26:29 +00007095 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007096 if (HASHITEM_EMPTY(hi))
7097 EMSG2(_(e_intern2), "dictitem_remove()");
7098 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007099 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007100 dictitem_free(item);
7101}
7102
7103/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007104 * Free a dict item. Also clears the value.
7105 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007106 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007107dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007108 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007109{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007110 clear_tv(&item->di_tv);
7111 vim_free(item);
7112}
7113
7114/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007115 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7116 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007117 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007118 * Returns NULL when out of memory.
7119 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007120 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007121dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007122 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007123 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007124 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007125{
Bram Moolenaar33570922005-01-25 22:26:29 +00007126 dict_T *copy;
7127 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007128 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007129 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007130
7131 if (orig == NULL)
7132 return NULL;
7133
7134 copy = dict_alloc();
7135 if (copy != NULL)
7136 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007137 if (copyID != 0)
7138 {
7139 orig->dv_copyID = copyID;
7140 orig->dv_copydict = copy;
7141 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007142 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007143 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007144 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007145 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007146 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007147 --todo;
7148
7149 di = dictitem_alloc(hi->hi_key);
7150 if (di == NULL)
7151 break;
7152 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007153 {
7154 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7155 copyID) == FAIL)
7156 {
7157 vim_free(di);
7158 break;
7159 }
7160 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007161 else
7162 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7163 if (dict_add(copy, di) == FAIL)
7164 {
7165 dictitem_free(di);
7166 break;
7167 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007168 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007169 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007170
Bram Moolenaare9a41262005-01-15 22:18:47 +00007171 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007172 if (todo > 0)
7173 {
7174 dict_unref(copy);
7175 copy = NULL;
7176 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007177 }
7178
7179 return copy;
7180}
7181
7182/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007183 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007184 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007185 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007186 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007187dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007188 dict_T *d;
7189 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007190{
Bram Moolenaar33570922005-01-25 22:26:29 +00007191 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007192}
7193
Bram Moolenaar8c711452005-01-14 21:53:12 +00007194/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007195 * Add a number or string entry to dictionary "d".
7196 * When "str" is NULL use number "nr", otherwise use "str".
7197 * Returns FAIL when out of memory and when key already exists.
7198 */
7199 int
7200dict_add_nr_str(d, key, nr, str)
7201 dict_T *d;
7202 char *key;
7203 long nr;
7204 char_u *str;
7205{
7206 dictitem_T *item;
7207
7208 item = dictitem_alloc((char_u *)key);
7209 if (item == NULL)
7210 return FAIL;
7211 item->di_tv.v_lock = 0;
7212 if (str == NULL)
7213 {
7214 item->di_tv.v_type = VAR_NUMBER;
7215 item->di_tv.vval.v_number = nr;
7216 }
7217 else
7218 {
7219 item->di_tv.v_type = VAR_STRING;
7220 item->di_tv.vval.v_string = vim_strsave(str);
7221 }
7222 if (dict_add(d, item) == FAIL)
7223 {
7224 dictitem_free(item);
7225 return FAIL;
7226 }
7227 return OK;
7228}
7229
7230/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007231 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007232 * Returns FAIL when out of memory and when key already exists.
7233 */
7234 int
7235dict_add_list(d, key, list)
7236 dict_T *d;
7237 char *key;
7238 list_T *list;
7239{
7240 dictitem_T *item;
7241
7242 item = dictitem_alloc((char_u *)key);
7243 if (item == NULL)
7244 return FAIL;
7245 item->di_tv.v_lock = 0;
7246 item->di_tv.v_type = VAR_LIST;
7247 item->di_tv.vval.v_list = list;
7248 if (dict_add(d, item) == FAIL)
7249 {
7250 dictitem_free(item);
7251 return FAIL;
7252 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007253 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007254 return OK;
7255}
7256
7257/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007258 * Get the number of items in a Dictionary.
7259 */
7260 static long
7261dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007262 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007263{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007264 if (d == NULL)
7265 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007266 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007267}
7268
7269/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270 * Find item "key[len]" in Dictionary "d".
7271 * If "len" is negative use strlen(key).
7272 * Returns NULL when not found.
7273 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007274 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007275dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007276 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007277 char_u *key;
7278 int len;
7279{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280#define AKEYLEN 200
7281 char_u buf[AKEYLEN];
7282 char_u *akey;
7283 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007284 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007285
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286 if (len < 0)
7287 akey = key;
7288 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007289 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290 tofree = akey = vim_strnsave(key, len);
7291 if (akey == NULL)
7292 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007293 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007294 else
7295 {
7296 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007297 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007298 akey = buf;
7299 }
7300
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007302 vim_free(tofree);
7303 if (HASHITEM_EMPTY(hi))
7304 return NULL;
7305 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007306}
7307
7308/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007309 * Get a string item from a dictionary.
7310 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007311 * Returns NULL if the entry doesn't exist or out of memory.
7312 */
7313 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007314get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007315 dict_T *d;
7316 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007317 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007318{
7319 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007320 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007321
7322 di = dict_find(d, key, -1);
7323 if (di == NULL)
7324 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007325 s = get_tv_string(&di->di_tv);
7326 if (save && s != NULL)
7327 s = vim_strsave(s);
7328 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007329}
7330
7331/*
7332 * Get a number item from a dictionary.
7333 * Returns 0 if the entry doesn't exist or out of memory.
7334 */
7335 long
7336get_dict_number(d, key)
7337 dict_T *d;
7338 char_u *key;
7339{
7340 dictitem_T *di;
7341
7342 di = dict_find(d, key, -1);
7343 if (di == NULL)
7344 return 0;
7345 return get_tv_number(&di->di_tv);
7346}
7347
7348/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007349 * Return an allocated string with the string representation of a Dictionary.
7350 * May return NULL.
7351 */
7352 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007353dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007354 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007355 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007356{
7357 garray_T ga;
7358 int first = TRUE;
7359 char_u *tofree;
7360 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007361 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007362 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007363 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007364 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007365
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007366 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007367 return NULL;
7368 ga_init2(&ga, (int)sizeof(char), 80);
7369 ga_append(&ga, '{');
7370
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007371 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007372 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007373 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007374 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007375 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007376 --todo;
7377
7378 if (first)
7379 first = FALSE;
7380 else
7381 ga_concat(&ga, (char_u *)", ");
7382
7383 tofree = string_quote(hi->hi_key, FALSE);
7384 if (tofree != NULL)
7385 {
7386 ga_concat(&ga, tofree);
7387 vim_free(tofree);
7388 }
7389 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007390 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007391 if (s != NULL)
7392 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007393 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007394 if (s == NULL)
7395 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007396 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007397 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007398 if (todo > 0)
7399 {
7400 vim_free(ga.ga_data);
7401 return NULL;
7402 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007403
7404 ga_append(&ga, '}');
7405 ga_append(&ga, NUL);
7406 return (char_u *)ga.ga_data;
7407}
7408
7409/*
7410 * Allocate a variable for a Dictionary and fill it from "*arg".
7411 * Return OK or FAIL. Returns NOTDONE for {expr}.
7412 */
7413 static int
7414get_dict_tv(arg, rettv, evaluate)
7415 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007416 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007417 int evaluate;
7418{
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 dict_T *d = NULL;
7420 typval_T tvkey;
7421 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007422 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007423 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007425 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007426
7427 /*
7428 * First check if it's not a curly-braces thing: {expr}.
7429 * Must do this without evaluating, otherwise a function may be called
7430 * twice. Unfortunately this means we need to call eval1() twice for the
7431 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007432 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007433 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007434 if (*start != '}')
7435 {
7436 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7437 return FAIL;
7438 if (*start == '}')
7439 return NOTDONE;
7440 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007441
7442 if (evaluate)
7443 {
7444 d = dict_alloc();
7445 if (d == NULL)
7446 return FAIL;
7447 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007448 tvkey.v_type = VAR_UNKNOWN;
7449 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007450
7451 *arg = skipwhite(*arg + 1);
7452 while (**arg != '}' && **arg != NUL)
7453 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007454 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007455 goto failret;
7456 if (**arg != ':')
7457 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007458 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007459 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007460 goto failret;
7461 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007462 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007464 key = get_tv_string_buf_chk(&tvkey, buf);
7465 if (key == NULL || *key == NUL)
7466 {
7467 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7468 if (key != NULL)
7469 EMSG(_(e_emptykey));
7470 clear_tv(&tvkey);
7471 goto failret;
7472 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007473 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007474
7475 *arg = skipwhite(*arg + 1);
7476 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7477 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007478 if (evaluate)
7479 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007480 goto failret;
7481 }
7482 if (evaluate)
7483 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007484 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485 if (item != NULL)
7486 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007487 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007488 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 clear_tv(&tv);
7490 goto failret;
7491 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007492 item = dictitem_alloc(key);
7493 clear_tv(&tvkey);
7494 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007496 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007497 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007498 if (dict_add(d, item) == FAIL)
7499 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500 }
7501 }
7502
7503 if (**arg == '}')
7504 break;
7505 if (**arg != ',')
7506 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007507 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508 goto failret;
7509 }
7510 *arg = skipwhite(*arg + 1);
7511 }
7512
7513 if (**arg != '}')
7514 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007515 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007516failret:
7517 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007518 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007519 return FAIL;
7520 }
7521
7522 *arg = skipwhite(*arg + 1);
7523 if (evaluate)
7524 {
7525 rettv->v_type = VAR_DICT;
7526 rettv->vval.v_dict = d;
7527 ++d->dv_refcount;
7528 }
7529
7530 return OK;
7531}
7532
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007534 * Return a string with the string representation of a variable.
7535 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007536 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007537 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007538 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007539 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007540 */
7541 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007542echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007543 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007544 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007545 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007546 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007547{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007548 static int recurse = 0;
7549 char_u *r = NULL;
7550
Bram Moolenaar33570922005-01-25 22:26:29 +00007551 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007552 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007553 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007554 *tofree = NULL;
7555 return NULL;
7556 }
7557 ++recurse;
7558
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007559 switch (tv->v_type)
7560 {
7561 case VAR_FUNC:
7562 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007563 r = tv->vval.v_string;
7564 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007565
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007566 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007567 if (tv->vval.v_list == NULL)
7568 {
7569 *tofree = NULL;
7570 r = NULL;
7571 }
7572 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7573 {
7574 *tofree = NULL;
7575 r = (char_u *)"[...]";
7576 }
7577 else
7578 {
7579 tv->vval.v_list->lv_copyID = copyID;
7580 *tofree = list2string(tv, copyID);
7581 r = *tofree;
7582 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007583 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007584
Bram Moolenaar8c711452005-01-14 21:53:12 +00007585 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007586 if (tv->vval.v_dict == NULL)
7587 {
7588 *tofree = NULL;
7589 r = NULL;
7590 }
7591 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7592 {
7593 *tofree = NULL;
7594 r = (char_u *)"{...}";
7595 }
7596 else
7597 {
7598 tv->vval.v_dict->dv_copyID = copyID;
7599 *tofree = dict2string(tv, copyID);
7600 r = *tofree;
7601 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007602 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007603
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007604 case VAR_STRING:
7605 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007606 *tofree = NULL;
7607 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007608 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007609
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007610#ifdef FEAT_FLOAT
7611 case VAR_FLOAT:
7612 *tofree = NULL;
7613 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7614 r = numbuf;
7615 break;
7616#endif
7617
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007618 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007619 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007620 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007621 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007622
7623 --recurse;
7624 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007625}
7626
7627/*
7628 * Return a string with the string representation of a variable.
7629 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7630 * "numbuf" is used for a number.
7631 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007632 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007633 */
7634 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007635tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007636 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007637 char_u **tofree;
7638 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007639 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007640{
7641 switch (tv->v_type)
7642 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007643 case VAR_FUNC:
7644 *tofree = string_quote(tv->vval.v_string, TRUE);
7645 return *tofree;
7646 case VAR_STRING:
7647 *tofree = string_quote(tv->vval.v_string, FALSE);
7648 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007649#ifdef FEAT_FLOAT
7650 case VAR_FLOAT:
7651 *tofree = NULL;
7652 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7653 return numbuf;
7654#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007655 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007656 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007658 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007659 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007660 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007661 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007662 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007663}
7664
7665/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007666 * Return string "str" in ' quotes, doubling ' characters.
7667 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007668 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007669 */
7670 static char_u *
7671string_quote(str, function)
7672 char_u *str;
7673 int function;
7674{
Bram Moolenaar33570922005-01-25 22:26:29 +00007675 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007676 char_u *p, *r, *s;
7677
Bram Moolenaar33570922005-01-25 22:26:29 +00007678 len = (function ? 13 : 3);
7679 if (str != NULL)
7680 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007681 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 for (p = str; *p != NUL; mb_ptr_adv(p))
7683 if (*p == '\'')
7684 ++len;
7685 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007686 s = r = alloc(len);
7687 if (r != NULL)
7688 {
7689 if (function)
7690 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007692 r += 10;
7693 }
7694 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007695 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007696 if (str != NULL)
7697 for (p = str; *p != NUL; )
7698 {
7699 if (*p == '\'')
7700 *r++ = '\'';
7701 MB_COPY_CHAR(p, r);
7702 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007704 if (function)
7705 *r++ = ')';
7706 *r++ = NUL;
7707 }
7708 return s;
7709}
7710
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007711#ifdef FEAT_FLOAT
7712/*
7713 * Convert the string "text" to a floating point number.
7714 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7715 * this always uses a decimal point.
7716 * Returns the length of the text that was consumed.
7717 */
7718 static int
7719string2float(text, value)
7720 char_u *text;
7721 float_T *value; /* result stored here */
7722{
7723 char *s = (char *)text;
7724 float_T f;
7725
7726 f = strtod(s, &s);
7727 *value = f;
7728 return (int)((char_u *)s - text);
7729}
7730#endif
7731
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 * Get the value of an environment variable.
7734 * "arg" is pointing to the '$'. It is advanced to after the name.
7735 * If the environment variable was not set, silently assume it is empty.
7736 * Always return OK.
7737 */
7738 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007739get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 int evaluate;
7743{
7744 char_u *string = NULL;
7745 int len;
7746 int cc;
7747 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007748 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749
7750 ++*arg;
7751 name = *arg;
7752 len = get_env_len(arg);
7753 if (evaluate)
7754 {
7755 if (len != 0)
7756 {
7757 cc = name[len];
7758 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007759 /* first try vim_getenv(), fast for normal environment vars */
7760 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007762 {
7763 if (!mustfree)
7764 string = vim_strsave(string);
7765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 else
7767 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007768 if (mustfree)
7769 vim_free(string);
7770
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 /* next try expanding things like $VIM and ${HOME} */
7772 string = expand_env_save(name - 1);
7773 if (string != NULL && *string == '$')
7774 {
7775 vim_free(string);
7776 string = NULL;
7777 }
7778 }
7779 name[len] = cc;
7780 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007781 rettv->v_type = VAR_STRING;
7782 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 }
7784
7785 return OK;
7786}
7787
7788/*
7789 * Array with names and number of arguments of all internal functions
7790 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7791 */
7792static struct fst
7793{
7794 char *f_name; /* function name */
7795 char f_min_argc; /* minimal number of arguments */
7796 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007797 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007798 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799} functions[] =
7800{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007801#ifdef FEAT_FLOAT
7802 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007803 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007804#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007805 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007806 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 {"append", 2, 2, f_append},
7808 {"argc", 0, 0, f_argc},
7809 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007810 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007811#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007812 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007813 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007814 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007817 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 {"bufexists", 1, 1, f_bufexists},
7819 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7820 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7821 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7822 {"buflisted", 1, 1, f_buflisted},
7823 {"bufloaded", 1, 1, f_bufloaded},
7824 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007825 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 {"bufwinnr", 1, 1, f_bufwinnr},
7827 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007828 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007829 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007830#ifdef FEAT_FLOAT
7831 {"ceil", 1, 1, f_ceil},
7832#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007833 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 {"char2nr", 1, 1, f_char2nr},
7835 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007836 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007838#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007839 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007840 {"complete_add", 1, 1, f_complete_add},
7841 {"complete_check", 0, 0, f_complete_check},
7842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007844 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007845#ifdef FEAT_FLOAT
7846 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007847 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007848#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007849 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007851 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007852 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {"delete", 1, 1, f_delete},
7854 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007855 {"diff_filler", 1, 1, f_diff_filler},
7856 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007857 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007859 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 {"eventhandler", 0, 0, f_eventhandler},
7861 {"executable", 1, 1, f_executable},
7862 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007863#ifdef FEAT_FLOAT
7864 {"exp", 1, 1, f_exp},
7865#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007866 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007867 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007868 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7870 {"filereadable", 1, 1, f_filereadable},
7871 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007872 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007873 {"finddir", 1, 3, f_finddir},
7874 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007875#ifdef FEAT_FLOAT
7876 {"float2nr", 1, 1, f_float2nr},
7877 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007878 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007879#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007880 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {"fnamemodify", 2, 2, f_fnamemodify},
7882 {"foldclosed", 1, 1, f_foldclosed},
7883 {"foldclosedend", 1, 1, f_foldclosedend},
7884 {"foldlevel", 1, 1, f_foldlevel},
7885 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007886 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007888 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007889 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007890 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007891 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {"getbufvar", 2, 2, f_getbufvar},
7893 {"getchar", 0, 1, f_getchar},
7894 {"getcharmod", 0, 0, f_getcharmod},
7895 {"getcmdline", 0, 0, f_getcmdline},
7896 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007897 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007899 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007900 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 {"getfsize", 1, 1, f_getfsize},
7902 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007903 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007904 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007905 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007906 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007907 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007908 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007909 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007910 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007912 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007913 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"getwinposx", 0, 0, f_getwinposx},
7915 {"getwinposy", 0, 0, f_getwinposy},
7916 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007917 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007918 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007920 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007921 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007922 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7924 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7925 {"histadd", 2, 2, f_histadd},
7926 {"histdel", 1, 2, f_histdel},
7927 {"histget", 1, 2, f_histget},
7928 {"histnr", 1, 1, f_histnr},
7929 {"hlID", 1, 1, f_hlID},
7930 {"hlexists", 1, 1, f_hlexists},
7931 {"hostname", 0, 0, f_hostname},
7932 {"iconv", 3, 3, f_iconv},
7933 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007934 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007935 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007937 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 {"inputrestore", 0, 0, f_inputrestore},
7939 {"inputsave", 0, 0, f_inputsave},
7940 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007941 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007942 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007944 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007945 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007946 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007947 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007949 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 {"libcall", 3, 3, f_libcall},
7951 {"libcallnr", 3, 3, f_libcallnr},
7952 {"line", 1, 1, f_line},
7953 {"line2byte", 1, 1, f_line2byte},
7954 {"lispindent", 1, 1, f_lispindent},
7955 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007956#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007957 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007958 {"log10", 1, 1, f_log10},
7959#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007960#ifdef FEAT_LUA
7961 {"luaeval", 1, 2, f_luaeval},
7962#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007963 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007964 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007965 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007966 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007967 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007968 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007969 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007970 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007971 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007972 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007973 {"max", 1, 1, f_max},
7974 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007975#ifdef vim_mkdir
7976 {"mkdir", 1, 3, f_mkdir},
7977#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007978 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007979#ifdef FEAT_MZSCHEME
7980 {"mzeval", 1, 1, f_mzeval},
7981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 {"nextnonblank", 1, 1, f_nextnonblank},
7983 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007984 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007985 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007986#ifdef FEAT_FLOAT
7987 {"pow", 2, 2, f_pow},
7988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007990 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007991 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02007992#ifdef FEAT_PYTHON3
7993 {"py3eval", 1, 1, f_py3eval},
7994#endif
7995#ifdef FEAT_PYTHON
7996 {"pyeval", 1, 1, f_pyeval},
7997#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00007998 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007999 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008000 {"reltime", 0, 2, f_reltime},
8001 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 {"remote_expr", 2, 3, f_remote_expr},
8003 {"remote_foreground", 1, 1, f_remote_foreground},
8004 {"remote_peek", 1, 2, f_remote_peek},
8005 {"remote_read", 1, 1, f_remote_read},
8006 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008007 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008009 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008011 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008012#ifdef FEAT_FLOAT
8013 {"round", 1, 1, f_round},
8014#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00008015 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008016 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008017 {"searchpair", 3, 7, f_searchpair},
8018 {"searchpairpos", 3, 7, f_searchpairpos},
8019 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 {"server2client", 2, 2, f_server2client},
8021 {"serverlist", 0, 0, f_serverlist},
8022 {"setbufvar", 3, 3, f_setbufvar},
8023 {"setcmdpos", 1, 1, f_setcmdpos},
8024 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008025 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008026 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008027 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008028 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008030 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008031 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008033 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008035#ifdef FEAT_FLOAT
8036 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008037 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008038#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008039 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008040 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008041 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008042 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008043 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008044#ifdef FEAT_FLOAT
8045 {"sqrt", 1, 1, f_sqrt},
8046 {"str2float", 1, 1, f_str2float},
8047#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008048 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008049 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008050 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051#ifdef HAVE_STRFTIME
8052 {"strftime", 1, 2, f_strftime},
8053#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008054 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008055 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 {"strlen", 1, 1, f_strlen},
8057 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008058 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008060 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 {"submatch", 1, 1, f_submatch},
8062 {"substitute", 4, 4, f_substitute},
8063 {"synID", 3, 3, f_synID},
8064 {"synIDattr", 2, 3, f_synIDattr},
8065 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008066 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008067 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008068 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008069 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008070 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008071 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008072 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008073 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008074#ifdef FEAT_FLOAT
8075 {"tan", 1, 1, f_tan},
8076 {"tanh", 1, 1, f_tanh},
8077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008079 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {"tolower", 1, 1, f_tolower},
8081 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008082 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008083#ifdef FEAT_FLOAT
8084 {"trunc", 1, 1, f_trunc},
8085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008087 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008088 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008089 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {"virtcol", 1, 1, f_virtcol},
8091 {"visualmode", 0, 1, f_visualmode},
8092 {"winbufnr", 1, 1, f_winbufnr},
8093 {"wincol", 0, 0, f_wincol},
8094 {"winheight", 1, 1, f_winheight},
8095 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008096 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008098 {"winrestview", 1, 1, f_winrestview},
8099 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008101 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008102 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103};
8104
8105#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8106
8107/*
8108 * Function given to ExpandGeneric() to obtain the list of internal
8109 * or user defined function names.
8110 */
8111 char_u *
8112get_function_name(xp, idx)
8113 expand_T *xp;
8114 int idx;
8115{
8116 static int intidx = -1;
8117 char_u *name;
8118
8119 if (idx == 0)
8120 intidx = -1;
8121 if (intidx < 0)
8122 {
8123 name = get_user_func_name(xp, idx);
8124 if (name != NULL)
8125 return name;
8126 }
8127 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8128 {
8129 STRCPY(IObuff, functions[intidx].f_name);
8130 STRCAT(IObuff, "(");
8131 if (functions[intidx].f_max_argc == 0)
8132 STRCAT(IObuff, ")");
8133 return IObuff;
8134 }
8135
8136 return NULL;
8137}
8138
8139/*
8140 * Function given to ExpandGeneric() to obtain the list of internal or
8141 * user defined variable or function names.
8142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 char_u *
8144get_expr_name(xp, idx)
8145 expand_T *xp;
8146 int idx;
8147{
8148 static int intidx = -1;
8149 char_u *name;
8150
8151 if (idx == 0)
8152 intidx = -1;
8153 if (intidx < 0)
8154 {
8155 name = get_function_name(xp, idx);
8156 if (name != NULL)
8157 return name;
8158 }
8159 return get_user_var_name(xp, ++intidx);
8160}
8161
8162#endif /* FEAT_CMDL_COMPL */
8163
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008164#if defined(EBCDIC) || defined(PROTO)
8165/*
8166 * Compare struct fst by function name.
8167 */
8168 static int
8169compare_func_name(s1, s2)
8170 const void *s1;
8171 const void *s2;
8172{
8173 struct fst *p1 = (struct fst *)s1;
8174 struct fst *p2 = (struct fst *)s2;
8175
8176 return STRCMP(p1->f_name, p2->f_name);
8177}
8178
8179/*
8180 * Sort the function table by function name.
8181 * The sorting of the table above is ASCII dependant.
8182 * On machines using EBCDIC we have to sort it.
8183 */
8184 static void
8185sortFunctions()
8186{
8187 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8188
8189 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8190}
8191#endif
8192
8193
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194/*
8195 * Find internal function in table above.
8196 * Return index, or -1 if not found
8197 */
8198 static int
8199find_internal_func(name)
8200 char_u *name; /* name of the function */
8201{
8202 int first = 0;
8203 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8204 int cmp;
8205 int x;
8206
8207 /*
8208 * Find the function name in the table. Binary search.
8209 */
8210 while (first <= last)
8211 {
8212 x = first + ((unsigned)(last - first) >> 1);
8213 cmp = STRCMP(name, functions[x].f_name);
8214 if (cmp < 0)
8215 last = x - 1;
8216 else if (cmp > 0)
8217 first = x + 1;
8218 else
8219 return x;
8220 }
8221 return -1;
8222}
8223
8224/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008225 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8226 * name it contains, otherwise return "name".
8227 */
8228 static char_u *
8229deref_func_name(name, lenp)
8230 char_u *name;
8231 int *lenp;
8232{
Bram Moolenaar33570922005-01-25 22:26:29 +00008233 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008234 int cc;
8235
8236 cc = name[*lenp];
8237 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008238 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008239 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008240 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008241 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008242 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008243 {
8244 *lenp = 0;
8245 return (char_u *)""; /* just in case */
8246 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008247 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008248 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008249 }
8250
8251 return name;
8252}
8253
8254/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 * Allocate a variable for the result of a function.
8256 * Return OK or FAIL.
8257 */
8258 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008259get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8260 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 char_u *name; /* name of the function */
8262 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 char_u **arg; /* argument, pointing to the '(' */
8265 linenr_T firstline; /* first line of range */
8266 linenr_T lastline; /* last line of range */
8267 int *doesrange; /* return: function handled range */
8268 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008269 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270{
8271 char_u *argp;
8272 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008273 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 int argcount = 0; /* number of arguments found */
8275
8276 /*
8277 * Get the arguments.
8278 */
8279 argp = *arg;
8280 while (argcount < MAX_FUNC_ARGS)
8281 {
8282 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8283 if (*argp == ')' || *argp == ',' || *argp == NUL)
8284 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8286 {
8287 ret = FAIL;
8288 break;
8289 }
8290 ++argcount;
8291 if (*argp != ',')
8292 break;
8293 }
8294 if (*argp == ')')
8295 ++argp;
8296 else
8297 ret = FAIL;
8298
8299 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008300 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008301 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008303 {
8304 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008305 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008306 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008307 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309
8310 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008311 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312
8313 *arg = skipwhite(argp);
8314 return ret;
8315}
8316
8317
8318/*
8319 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008320 * Return OK when the function can't be called, FAIL otherwise.
8321 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 */
8323 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008324call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008325 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008326 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008328 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008330 typval_T *argvars; /* vars for arguments, must have "argcount"
8331 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 linenr_T firstline; /* first line of range */
8333 linenr_T lastline; /* last line of range */
8334 int *doesrange; /* return: function handled range */
8335 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008336 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337{
8338 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339#define ERROR_UNKNOWN 0
8340#define ERROR_TOOMANY 1
8341#define ERROR_TOOFEW 2
8342#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008343#define ERROR_DICT 4
8344#define ERROR_NONE 5
8345#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 int error = ERROR_NONE;
8347 int i;
8348 int llen;
8349 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350#define FLEN_FIXED 40
8351 char_u fname_buf[FLEN_FIXED + 1];
8352 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008353 char_u *name;
8354
8355 /* Make a copy of the name, if it comes from a funcref variable it could
8356 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008357 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008358 if (name == NULL)
8359 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360
8361 /*
8362 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8363 * Change <SNR>123_name() to K_SNR 123_name().
8364 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 llen = eval_fname_script(name);
8367 if (llen > 0)
8368 {
8369 fname_buf[0] = K_SPECIAL;
8370 fname_buf[1] = KS_EXTRA;
8371 fname_buf[2] = (int)KE_SNR;
8372 i = 3;
8373 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8374 {
8375 if (current_SID <= 0)
8376 error = ERROR_SCRIPT;
8377 else
8378 {
8379 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8380 i = (int)STRLEN(fname_buf);
8381 }
8382 }
8383 if (i + STRLEN(name + llen) < FLEN_FIXED)
8384 {
8385 STRCPY(fname_buf + i, name + llen);
8386 fname = fname_buf;
8387 }
8388 else
8389 {
8390 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8391 if (fname == NULL)
8392 error = ERROR_OTHER;
8393 else
8394 {
8395 mch_memmove(fname, fname_buf, (size_t)i);
8396 STRCPY(fname + i, name + llen);
8397 }
8398 }
8399 }
8400 else
8401 fname = name;
8402
8403 *doesrange = FALSE;
8404
8405
8406 /* execute the function if no errors detected and executing */
8407 if (evaluate && error == ERROR_NONE)
8408 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008409 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8410 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 error = ERROR_UNKNOWN;
8412
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008413 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {
8415 /*
8416 * User defined function.
8417 */
8418 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008419
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008421 /* Trigger FuncUndefined event, may load the function. */
8422 if (fp == NULL
8423 && apply_autocmds(EVENT_FUNCUNDEFINED,
8424 fname, fname, TRUE, NULL)
8425 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008427 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 fp = find_func(fname);
8429 }
8430#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008431 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008432 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008433 {
8434 /* loaded a package, search for the function again */
8435 fp = find_func(fname);
8436 }
8437
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 if (fp != NULL)
8439 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008440 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008442 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008444 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008446 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008447 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 else
8449 {
8450 /*
8451 * Call the user function.
8452 * Save and restore search patterns, script variables and
8453 * redo buffer.
8454 */
8455 save_search_patterns();
8456 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008457 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008458 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008459 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008460 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8461 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8462 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008463 /* Function was unreferenced while being used, free it
8464 * now. */
8465 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 restoreRedobuff();
8467 restore_search_patterns();
8468 error = ERROR_NONE;
8469 }
8470 }
8471 }
8472 else
8473 {
8474 /*
8475 * Find the function name in the table, call its implementation.
8476 */
8477 i = find_internal_func(fname);
8478 if (i >= 0)
8479 {
8480 if (argcount < functions[i].f_min_argc)
8481 error = ERROR_TOOFEW;
8482 else if (argcount > functions[i].f_max_argc)
8483 error = ERROR_TOOMANY;
8484 else
8485 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008486 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008487 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 error = ERROR_NONE;
8489 }
8490 }
8491 }
8492 /*
8493 * The function call (or "FuncUndefined" autocommand sequence) might
8494 * have been aborted by an error, an interrupt, or an explicitly thrown
8495 * exception that has not been caught so far. This situation can be
8496 * tested for by calling aborting(). For an error in an internal
8497 * function or for the "E132" error in call_user_func(), however, the
8498 * throw point at which the "force_abort" flag (temporarily reset by
8499 * emsg()) is normally updated has not been reached yet. We need to
8500 * update that flag first to make aborting() reliable.
8501 */
8502 update_force_abort();
8503 }
8504 if (error == ERROR_NONE)
8505 ret = OK;
8506
8507 /*
8508 * Report an error unless the argument evaluation or function call has been
8509 * cancelled due to an aborting error, an interrupt, or an exception.
8510 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008511 if (!aborting())
8512 {
8513 switch (error)
8514 {
8515 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008516 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008517 break;
8518 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008519 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008520 break;
8521 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008522 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008523 name);
8524 break;
8525 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008526 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008527 name);
8528 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008529 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008530 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008531 name);
8532 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008533 }
8534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 if (fname != name && fname != fname_buf)
8537 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008538 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539
8540 return ret;
8541}
8542
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008543/*
8544 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008545 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008546 */
8547 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008548emsg_funcname(ermsg, name)
8549 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008550 char_u *name;
8551{
8552 char_u *p;
8553
8554 if (*name == K_SPECIAL)
8555 p = concat_str((char_u *)"<SNR>", name + 3);
8556 else
8557 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008558 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008559 if (p != name)
8560 vim_free(p);
8561}
8562
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008563/*
8564 * Return TRUE for a non-zero Number and a non-empty String.
8565 */
8566 static int
8567non_zero_arg(argvars)
8568 typval_T *argvars;
8569{
8570 return ((argvars[0].v_type == VAR_NUMBER
8571 && argvars[0].vval.v_number != 0)
8572 || (argvars[0].v_type == VAR_STRING
8573 && argvars[0].vval.v_string != NULL
8574 && *argvars[0].vval.v_string != NUL));
8575}
8576
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577/*********************************************
8578 * Implementation of the built-in functions
8579 */
8580
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008581#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008582static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8583
8584/*
8585 * Get the float value of "argvars[0]" into "f".
8586 * Returns FAIL when the argument is not a Number or Float.
8587 */
8588 static int
8589get_float_arg(argvars, f)
8590 typval_T *argvars;
8591 float_T *f;
8592{
8593 if (argvars[0].v_type == VAR_FLOAT)
8594 {
8595 *f = argvars[0].vval.v_float;
8596 return OK;
8597 }
8598 if (argvars[0].v_type == VAR_NUMBER)
8599 {
8600 *f = (float_T)argvars[0].vval.v_number;
8601 return OK;
8602 }
8603 EMSG(_("E808: Number or Float required"));
8604 return FAIL;
8605}
8606
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008607/*
8608 * "abs(expr)" function
8609 */
8610 static void
8611f_abs(argvars, rettv)
8612 typval_T *argvars;
8613 typval_T *rettv;
8614{
8615 if (argvars[0].v_type == VAR_FLOAT)
8616 {
8617 rettv->v_type = VAR_FLOAT;
8618 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8619 }
8620 else
8621 {
8622 varnumber_T n;
8623 int error = FALSE;
8624
8625 n = get_tv_number_chk(&argvars[0], &error);
8626 if (error)
8627 rettv->vval.v_number = -1;
8628 else if (n > 0)
8629 rettv->vval.v_number = n;
8630 else
8631 rettv->vval.v_number = -n;
8632 }
8633}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008634
8635/*
8636 * "acos()" function
8637 */
8638 static void
8639f_acos(argvars, rettv)
8640 typval_T *argvars;
8641 typval_T *rettv;
8642{
8643 float_T f;
8644
8645 rettv->v_type = VAR_FLOAT;
8646 if (get_float_arg(argvars, &f) == OK)
8647 rettv->vval.v_float = acos(f);
8648 else
8649 rettv->vval.v_float = 0.0;
8650}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008651#endif
8652
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008654 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 */
8656 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008657f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008658 typval_T *argvars;
8659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660{
Bram Moolenaar33570922005-01-25 22:26:29 +00008661 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008663 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008664 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008666 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008667 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008668 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008669 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008670 }
8671 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008672 EMSG(_(e_listreq));
8673}
8674
8675/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008676 * "and(expr, expr)" function
8677 */
8678 static void
8679f_and(argvars, rettv)
8680 typval_T *argvars;
8681 typval_T *rettv;
8682{
8683 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8684 & get_tv_number_chk(&argvars[1], NULL);
8685}
8686
8687/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008688 * "append(lnum, string/list)" function
8689 */
8690 static void
8691f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008692 typval_T *argvars;
8693 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008694{
8695 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008696 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008697 list_T *l = NULL;
8698 listitem_T *li = NULL;
8699 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008700 long added = 0;
8701
Bram Moolenaar0d660222005-01-07 21:51:51 +00008702 lnum = get_tv_lnum(argvars);
8703 if (lnum >= 0
8704 && lnum <= curbuf->b_ml.ml_line_count
8705 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008706 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008707 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008708 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008709 l = argvars[1].vval.v_list;
8710 if (l == NULL)
8711 return;
8712 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008713 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008714 for (;;)
8715 {
8716 if (l == NULL)
8717 tv = &argvars[1]; /* append a string */
8718 else if (li == NULL)
8719 break; /* end of list */
8720 else
8721 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008722 line = get_tv_string_chk(tv);
8723 if (line == NULL) /* type error */
8724 {
8725 rettv->vval.v_number = 1; /* Failed */
8726 break;
8727 }
8728 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008729 ++added;
8730 if (l == NULL)
8731 break;
8732 li = li->li_next;
8733 }
8734
8735 appended_lines_mark(lnum, added);
8736 if (curwin->w_cursor.lnum > lnum)
8737 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008739 else
8740 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741}
8742
8743/*
8744 * "argc()" function
8745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008747f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008748 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008751 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752}
8753
8754/*
8755 * "argidx()" function
8756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008758f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008759 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008762 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763}
8764
8765/*
8766 * "argv(nr)" function
8767 */
8768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008769f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008770 typval_T *argvars;
8771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772{
8773 int idx;
8774
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008775 if (argvars[0].v_type != VAR_UNKNOWN)
8776 {
8777 idx = get_tv_number_chk(&argvars[0], NULL);
8778 if (idx >= 0 && idx < ARGCOUNT)
8779 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8780 else
8781 rettv->vval.v_string = NULL;
8782 rettv->v_type = VAR_STRING;
8783 }
8784 else if (rettv_list_alloc(rettv) == OK)
8785 for (idx = 0; idx < ARGCOUNT; ++idx)
8786 list_append_string(rettv->vval.v_list,
8787 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788}
8789
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008790#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008791/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008792 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008793 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008794 static void
8795f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008796 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008797 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008798{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008799 float_T f;
8800
8801 rettv->v_type = VAR_FLOAT;
8802 if (get_float_arg(argvars, &f) == OK)
8803 rettv->vval.v_float = asin(f);
8804 else
8805 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008806}
8807
8808/*
8809 * "atan()" function
8810 */
8811 static void
8812f_atan(argvars, rettv)
8813 typval_T *argvars;
8814 typval_T *rettv;
8815{
8816 float_T f;
8817
8818 rettv->v_type = VAR_FLOAT;
8819 if (get_float_arg(argvars, &f) == OK)
8820 rettv->vval.v_float = atan(f);
8821 else
8822 rettv->vval.v_float = 0.0;
8823}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008824
8825/*
8826 * "atan2()" function
8827 */
8828 static void
8829f_atan2(argvars, rettv)
8830 typval_T *argvars;
8831 typval_T *rettv;
8832{
8833 float_T fx, fy;
8834
8835 rettv->v_type = VAR_FLOAT;
8836 if (get_float_arg(argvars, &fx) == OK
8837 && get_float_arg(&argvars[1], &fy) == OK)
8838 rettv->vval.v_float = atan2(fx, fy);
8839 else
8840 rettv->vval.v_float = 0.0;
8841}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008842#endif
8843
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844/*
8845 * "browse(save, title, initdir, default)" function
8846 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008848f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008849 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851{
8852#ifdef FEAT_BROWSE
8853 int save;
8854 char_u *title;
8855 char_u *initdir;
8856 char_u *defname;
8857 char_u buf[NUMBUFLEN];
8858 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008859 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008861 save = get_tv_number_chk(&argvars[0], &error);
8862 title = get_tv_string_chk(&argvars[1]);
8863 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8864 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008866 if (error || title == NULL || initdir == NULL || defname == NULL)
8867 rettv->vval.v_string = NULL;
8868 else
8869 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008870 do_browse(save ? BROWSE_SAVE : 0,
8871 title, defname, NULL, initdir, NULL, curbuf);
8872#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008873 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008874#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008875 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008876}
8877
8878/*
8879 * "browsedir(title, initdir)" function
8880 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008882f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008883 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008884 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008885{
8886#ifdef FEAT_BROWSE
8887 char_u *title;
8888 char_u *initdir;
8889 char_u buf[NUMBUFLEN];
8890
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008891 title = get_tv_string_chk(&argvars[0]);
8892 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008893
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008894 if (title == NULL || initdir == NULL)
8895 rettv->vval.v_string = NULL;
8896 else
8897 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008898 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008900 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008902 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903}
8904
Bram Moolenaar33570922005-01-25 22:26:29 +00008905static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008906
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907/*
8908 * Find a buffer by number or exact name.
8909 */
8910 static buf_T *
8911find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008912 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913{
8914 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008916 if (avar->v_type == VAR_NUMBER)
8917 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008918 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008920 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008921 if (buf == NULL)
8922 {
8923 /* No full path name match, try a match with a URL or a "nofile"
8924 * buffer, these don't use the full path. */
8925 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8926 if (buf->b_fname != NULL
8927 && (path_with_url(buf->b_fname)
8928#ifdef FEAT_QUICKFIX
8929 || bt_nofile(buf)
8930#endif
8931 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008932 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008933 break;
8934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 }
8936 return buf;
8937}
8938
8939/*
8940 * "bufexists(expr)" function
8941 */
8942 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008943f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008944 typval_T *argvars;
8945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008947 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948}
8949
8950/*
8951 * "buflisted(expr)" function
8952 */
8953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008954f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008955 typval_T *argvars;
8956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957{
8958 buf_T *buf;
8959
8960 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008961 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962}
8963
8964/*
8965 * "bufloaded(expr)" function
8966 */
8967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008968f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008969 typval_T *argvars;
8970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971{
8972 buf_T *buf;
8973
8974 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008975 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976}
8977
Bram Moolenaar33570922005-01-25 22:26:29 +00008978static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008979
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980/*
8981 * Get buffer by number or pattern.
8982 */
8983 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008984get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008985 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008987 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 int save_magic;
8989 char_u *save_cpo;
8990 buf_T *buf;
8991
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008992 if (tv->v_type == VAR_NUMBER)
8993 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008994 if (tv->v_type != VAR_STRING)
8995 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 if (name == NULL || *name == NUL)
8997 return curbuf;
8998 if (name[0] == '$' && name[1] == NUL)
8999 return lastbuf;
9000
9001 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9002 save_magic = p_magic;
9003 p_magic = TRUE;
9004 save_cpo = p_cpo;
9005 p_cpo = (char_u *)"";
9006
9007 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
9008 TRUE, FALSE));
9009
9010 p_magic = save_magic;
9011 p_cpo = save_cpo;
9012
9013 /* If not found, try expanding the name, like done for bufexists(). */
9014 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009015 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016
9017 return buf;
9018}
9019
9020/*
9021 * "bufname(expr)" function
9022 */
9023 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009024f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009025 typval_T *argvars;
9026 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027{
9028 buf_T *buf;
9029
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009030 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009032 buf = get_buf_tv(&argvars[0]);
9033 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009035 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009037 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 --emsg_off;
9039}
9040
9041/*
9042 * "bufnr(expr)" function
9043 */
9044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009045f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009046 typval_T *argvars;
9047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048{
9049 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009050 int error = FALSE;
9051 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009053 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009055 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009056 --emsg_off;
9057
9058 /* If the buffer isn't found and the second argument is not zero create a
9059 * new buffer. */
9060 if (buf == NULL
9061 && argvars[1].v_type != VAR_UNKNOWN
9062 && get_tv_number_chk(&argvars[1], &error) != 0
9063 && !error
9064 && (name = get_tv_string_chk(&argvars[0])) != NULL
9065 && !error)
9066 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9067
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009069 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072}
9073
9074/*
9075 * "bufwinnr(nr)" function
9076 */
9077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009078f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009079 typval_T *argvars;
9080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081{
9082#ifdef FEAT_WINDOWS
9083 win_T *wp;
9084 int winnr = 0;
9085#endif
9086 buf_T *buf;
9087
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009088 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091#ifdef FEAT_WINDOWS
9092 for (wp = firstwin; wp; wp = wp->w_next)
9093 {
9094 ++winnr;
9095 if (wp->w_buffer == buf)
9096 break;
9097 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009100 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101#endif
9102 --emsg_off;
9103}
9104
9105/*
9106 * "byte2line(byte)" function
9107 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009109f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009110 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112{
9113#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009114 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115#else
9116 long boff = 0;
9117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009118 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 (linenr_T)0, &boff);
9124#endif
9125}
9126
9127/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009128 * "byteidx()" function
9129 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009131f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009132 typval_T *argvars;
9133 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009134{
9135#ifdef FEAT_MBYTE
9136 char_u *t;
9137#endif
9138 char_u *str;
9139 long idx;
9140
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009141 str = get_tv_string_chk(&argvars[0]);
9142 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009143 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009144 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009145 return;
9146
9147#ifdef FEAT_MBYTE
9148 t = str;
9149 for ( ; idx > 0; idx--)
9150 {
9151 if (*t == NUL) /* EOL reached */
9152 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009153 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009154 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009155 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009156#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009157 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009158 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009159#endif
9160}
9161
Bram Moolenaardb913952012-06-29 12:54:53 +02009162 int
9163func_call(name, args, selfdict, rettv)
9164 char_u *name;
9165 typval_T *args;
9166 dict_T *selfdict;
9167 typval_T *rettv;
9168{
9169 listitem_T *item;
9170 typval_T argv[MAX_FUNC_ARGS + 1];
9171 int argc = 0;
9172 int dummy;
9173 int r = 0;
9174
9175 for (item = args->vval.v_list->lv_first; item != NULL;
9176 item = item->li_next)
9177 {
9178 if (argc == MAX_FUNC_ARGS)
9179 {
9180 EMSG(_("E699: Too many arguments"));
9181 break;
9182 }
9183 /* Make a copy of each argument. This is needed to be able to set
9184 * v_lock to VAR_FIXED in the copy without changing the original list.
9185 */
9186 copy_tv(&item->li_tv, &argv[argc++]);
9187 }
9188
9189 if (item == NULL)
9190 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9191 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9192 &dummy, TRUE, selfdict);
9193
9194 /* Free the arguments. */
9195 while (argc > 0)
9196 clear_tv(&argv[--argc]);
9197
9198 return r;
9199}
9200
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009201/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009202 * "call(func, arglist)" function
9203 */
9204 static void
9205f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009206 typval_T *argvars;
9207 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009208{
9209 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009210 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009211
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009212 if (argvars[1].v_type != VAR_LIST)
9213 {
9214 EMSG(_(e_listreq));
9215 return;
9216 }
9217 if (argvars[1].vval.v_list == NULL)
9218 return;
9219
9220 if (argvars[0].v_type == VAR_FUNC)
9221 func = argvars[0].vval.v_string;
9222 else
9223 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009224 if (*func == NUL)
9225 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009226
Bram Moolenaare9a41262005-01-15 22:18:47 +00009227 if (argvars[2].v_type != VAR_UNKNOWN)
9228 {
9229 if (argvars[2].v_type != VAR_DICT)
9230 {
9231 EMSG(_(e_dictreq));
9232 return;
9233 }
9234 selfdict = argvars[2].vval.v_dict;
9235 }
9236
Bram Moolenaardb913952012-06-29 12:54:53 +02009237 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009238}
9239
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009240#ifdef FEAT_FLOAT
9241/*
9242 * "ceil({float})" function
9243 */
9244 static void
9245f_ceil(argvars, rettv)
9246 typval_T *argvars;
9247 typval_T *rettv;
9248{
9249 float_T f;
9250
9251 rettv->v_type = VAR_FLOAT;
9252 if (get_float_arg(argvars, &f) == OK)
9253 rettv->vval.v_float = ceil(f);
9254 else
9255 rettv->vval.v_float = 0.0;
9256}
9257#endif
9258
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009259/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009260 * "changenr()" function
9261 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009262 static void
9263f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009264 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009265 typval_T *rettv;
9266{
9267 rettv->vval.v_number = curbuf->b_u_seq_cur;
9268}
9269
9270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 * "char2nr(string)" function
9272 */
9273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009274f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009275 typval_T *argvars;
9276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277{
9278#ifdef FEAT_MBYTE
9279 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009280 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 else
9282#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009283 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284}
9285
9286/*
9287 * "cindent(lnum)" function
9288 */
9289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009290f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009291 typval_T *argvars;
9292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293{
9294#ifdef FEAT_CINDENT
9295 pos_T pos;
9296 linenr_T lnum;
9297
9298 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009299 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9301 {
9302 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 curwin->w_cursor = pos;
9305 }
9306 else
9307#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009308 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309}
9310
9311/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009312 * "clearmatches()" function
9313 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009314 static void
9315f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009316 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009317 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009318{
9319#ifdef FEAT_SEARCH_EXTRA
9320 clear_matches(curwin);
9321#endif
9322}
9323
9324/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 * "col(string)" function
9326 */
9327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009328f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009329 typval_T *argvars;
9330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331{
9332 colnr_T col = 0;
9333 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009334 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009336 fp = var2fpos(&argvars[0], FALSE, &fnum);
9337 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 {
9339 if (fp->col == MAXCOL)
9340 {
9341 /* '> can be MAXCOL, get the length of the line then */
9342 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009343 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 else
9345 col = MAXCOL;
9346 }
9347 else
9348 {
9349 col = fp->col + 1;
9350#ifdef FEAT_VIRTUALEDIT
9351 /* col(".") when the cursor is on the NUL at the end of the line
9352 * because of "coladd" can be seen as an extra column. */
9353 if (virtual_active() && fp == &curwin->w_cursor)
9354 {
9355 char_u *p = ml_get_cursor();
9356
9357 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9358 curwin->w_virtcol - curwin->w_cursor.coladd))
9359 {
9360# ifdef FEAT_MBYTE
9361 int l;
9362
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009363 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 col += l;
9365# else
9366 if (*p != NUL && p[1] == NUL)
9367 ++col;
9368# endif
9369 }
9370 }
9371#endif
9372 }
9373 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009374 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375}
9376
Bram Moolenaar572cb562005-08-05 21:35:02 +00009377#if defined(FEAT_INS_EXPAND)
9378/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009379 * "complete()" function
9380 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009381 static void
9382f_complete(argvars, rettv)
9383 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009384 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009385{
9386 int startcol;
9387
9388 if ((State & INSERT) == 0)
9389 {
9390 EMSG(_("E785: complete() can only be used in Insert mode"));
9391 return;
9392 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009393
9394 /* Check for undo allowed here, because if something was already inserted
9395 * the line was already saved for undo and this check isn't done. */
9396 if (!undo_allowed())
9397 return;
9398
Bram Moolenaarade00832006-03-10 21:46:58 +00009399 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9400 {
9401 EMSG(_(e_invarg));
9402 return;
9403 }
9404
9405 startcol = get_tv_number_chk(&argvars[0], NULL);
9406 if (startcol <= 0)
9407 return;
9408
9409 set_completion(startcol - 1, argvars[1].vval.v_list);
9410}
9411
9412/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009413 * "complete_add()" function
9414 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009415 static void
9416f_complete_add(argvars, rettv)
9417 typval_T *argvars;
9418 typval_T *rettv;
9419{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009420 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009421}
9422
9423/*
9424 * "complete_check()" function
9425 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009426 static void
9427f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009428 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009429 typval_T *rettv;
9430{
9431 int saved = RedrawingDisabled;
9432
9433 RedrawingDisabled = 0;
9434 ins_compl_check_keys(0);
9435 rettv->vval.v_number = compl_interrupted;
9436 RedrawingDisabled = saved;
9437}
9438#endif
9439
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440/*
9441 * "confirm(message, buttons[, default [, type]])" function
9442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009444f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009445 typval_T *argvars UNUSED;
9446 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447{
9448#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9449 char_u *message;
9450 char_u *buttons = NULL;
9451 char_u buf[NUMBUFLEN];
9452 char_u buf2[NUMBUFLEN];
9453 int def = 1;
9454 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009455 char_u *typestr;
9456 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009458 message = get_tv_string_chk(&argvars[0]);
9459 if (message == NULL)
9460 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009461 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009463 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9464 if (buttons == NULL)
9465 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009466 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009468 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009469 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009471 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9472 if (typestr == NULL)
9473 error = TRUE;
9474 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009476 switch (TOUPPER_ASC(*typestr))
9477 {
9478 case 'E': type = VIM_ERROR; break;
9479 case 'Q': type = VIM_QUESTION; break;
9480 case 'I': type = VIM_INFO; break;
9481 case 'W': type = VIM_WARNING; break;
9482 case 'G': type = VIM_GENERIC; break;
9483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 }
9485 }
9486 }
9487 }
9488
9489 if (buttons == NULL || *buttons == NUL)
9490 buttons = (char_u *)_("&Ok");
9491
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009492 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009493 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009494 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495#endif
9496}
9497
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009498/*
9499 * "copy()" function
9500 */
9501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009502f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009503 typval_T *argvars;
9504 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009505{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009506 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009507}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009509#ifdef FEAT_FLOAT
9510/*
9511 * "cos()" function
9512 */
9513 static void
9514f_cos(argvars, rettv)
9515 typval_T *argvars;
9516 typval_T *rettv;
9517{
9518 float_T f;
9519
9520 rettv->v_type = VAR_FLOAT;
9521 if (get_float_arg(argvars, &f) == OK)
9522 rettv->vval.v_float = cos(f);
9523 else
9524 rettv->vval.v_float = 0.0;
9525}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009526
9527/*
9528 * "cosh()" function
9529 */
9530 static void
9531f_cosh(argvars, rettv)
9532 typval_T *argvars;
9533 typval_T *rettv;
9534{
9535 float_T f;
9536
9537 rettv->v_type = VAR_FLOAT;
9538 if (get_float_arg(argvars, &f) == OK)
9539 rettv->vval.v_float = cosh(f);
9540 else
9541 rettv->vval.v_float = 0.0;
9542}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009543#endif
9544
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009546 * "count()" function
9547 */
9548 static void
9549f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009550 typval_T *argvars;
9551 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009552{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009553 long n = 0;
9554 int ic = FALSE;
9555
Bram Moolenaare9a41262005-01-15 22:18:47 +00009556 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009557 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009558 listitem_T *li;
9559 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009560 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009561
Bram Moolenaare9a41262005-01-15 22:18:47 +00009562 if ((l = argvars[0].vval.v_list) != NULL)
9563 {
9564 li = l->lv_first;
9565 if (argvars[2].v_type != VAR_UNKNOWN)
9566 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009567 int error = FALSE;
9568
9569 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009570 if (argvars[3].v_type != VAR_UNKNOWN)
9571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009572 idx = get_tv_number_chk(&argvars[3], &error);
9573 if (!error)
9574 {
9575 li = list_find(l, idx);
9576 if (li == NULL)
9577 EMSGN(_(e_listidx), idx);
9578 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009579 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009580 if (error)
9581 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009582 }
9583
9584 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009585 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009586 ++n;
9587 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009588 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009589 else if (argvars[0].v_type == VAR_DICT)
9590 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009591 int todo;
9592 dict_T *d;
9593 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009594
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009595 if ((d = argvars[0].vval.v_dict) != NULL)
9596 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009597 int error = FALSE;
9598
Bram Moolenaare9a41262005-01-15 22:18:47 +00009599 if (argvars[2].v_type != VAR_UNKNOWN)
9600 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009601 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009602 if (argvars[3].v_type != VAR_UNKNOWN)
9603 EMSG(_(e_invarg));
9604 }
9605
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009606 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009607 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009608 {
9609 if (!HASHITEM_EMPTY(hi))
9610 {
9611 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009612 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009613 ++n;
9614 }
9615 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009616 }
9617 }
9618 else
9619 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009620 rettv->vval.v_number = n;
9621}
9622
9623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9625 *
9626 * Checks the existence of a cscope connection.
9627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009630 typval_T *argvars UNUSED;
9631 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632{
9633#ifdef FEAT_CSCOPE
9634 int num = 0;
9635 char_u *dbpath = NULL;
9636 char_u *prepend = NULL;
9637 char_u buf[NUMBUFLEN];
9638
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009639 if (argvars[0].v_type != VAR_UNKNOWN
9640 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009642 num = (int)get_tv_number(&argvars[0]);
9643 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009644 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009645 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 }
9647
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009648 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649#endif
9650}
9651
9652/*
9653 * "cursor(lnum, col)" function
9654 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009655 * Moves the cursor to the specified line and column.
9656 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009659f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009660 typval_T *argvars;
9661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662{
9663 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009664#ifdef FEAT_VIRTUALEDIT
9665 long coladd = 0;
9666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009668 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009669 if (argvars[1].v_type == VAR_UNKNOWN)
9670 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009671 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009672
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009673 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009674 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009675 line = pos.lnum;
9676 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009677#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009678 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009679#endif
9680 }
9681 else
9682 {
9683 line = get_tv_lnum(argvars);
9684 col = get_tv_number_chk(&argvars[1], NULL);
9685#ifdef FEAT_VIRTUALEDIT
9686 if (argvars[2].v_type != VAR_UNKNOWN)
9687 coladd = get_tv_number_chk(&argvars[2], NULL);
9688#endif
9689 }
9690 if (line < 0 || col < 0
9691#ifdef FEAT_VIRTUALEDIT
9692 || coladd < 0
9693#endif
9694 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009695 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 if (line > 0)
9697 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 if (col > 0)
9699 curwin->w_cursor.col = col - 1;
9700#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009701 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702#endif
9703
9704 /* Make sure the cursor is in a valid position. */
9705 check_cursor();
9706#ifdef FEAT_MBYTE
9707 /* Correct cursor for multi-byte character. */
9708 if (has_mbyte)
9709 mb_adjust_cursor();
9710#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009711
9712 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009713 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714}
9715
9716/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009717 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 */
9719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009720f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009721 typval_T *argvars;
9722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009724 int noref = 0;
9725
9726 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009727 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009728 if (noref < 0 || noref > 1)
9729 EMSG(_(e_invarg));
9730 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009731 {
9732 current_copyID += COPYID_INC;
9733 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735}
9736
9737/*
9738 * "delete()" function
9739 */
9740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009741f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009742 typval_T *argvars;
9743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744{
9745 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009746 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009748 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749}
9750
9751/*
9752 * "did_filetype()" function
9753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009755f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009756 typval_T *argvars UNUSED;
9757 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758{
9759#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009760 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761#endif
9762}
9763
9764/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009765 * "diff_filler()" function
9766 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009768f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009769 typval_T *argvars UNUSED;
9770 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009771{
9772#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009773 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009774#endif
9775}
9776
9777/*
9778 * "diff_hlID()" function
9779 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009781f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009782 typval_T *argvars UNUSED;
9783 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009784{
9785#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009786 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009787 static linenr_T prev_lnum = 0;
9788 static int changedtick = 0;
9789 static int fnum = 0;
9790 static int change_start = 0;
9791 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009792 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009793 int filler_lines;
9794 int col;
9795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009796 if (lnum < 0) /* ignore type error in {lnum} arg */
9797 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009798 if (lnum != prev_lnum
9799 || changedtick != curbuf->b_changedtick
9800 || fnum != curbuf->b_fnum)
9801 {
9802 /* New line, buffer, change: need to get the values. */
9803 filler_lines = diff_check(curwin, lnum);
9804 if (filler_lines < 0)
9805 {
9806 if (filler_lines == -1)
9807 {
9808 change_start = MAXCOL;
9809 change_end = -1;
9810 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9811 hlID = HLF_ADD; /* added line */
9812 else
9813 hlID = HLF_CHD; /* changed line */
9814 }
9815 else
9816 hlID = HLF_ADD; /* added line */
9817 }
9818 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009819 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009820 prev_lnum = lnum;
9821 changedtick = curbuf->b_changedtick;
9822 fnum = curbuf->b_fnum;
9823 }
9824
9825 if (hlID == HLF_CHD || hlID == HLF_TXD)
9826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009827 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009828 if (col >= change_start && col <= change_end)
9829 hlID = HLF_TXD; /* changed text */
9830 else
9831 hlID = HLF_CHD; /* changed line */
9832 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009833 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009834#endif
9835}
9836
9837/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009838 * "empty({expr})" function
9839 */
9840 static void
9841f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009842 typval_T *argvars;
9843 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009844{
9845 int n;
9846
9847 switch (argvars[0].v_type)
9848 {
9849 case VAR_STRING:
9850 case VAR_FUNC:
9851 n = argvars[0].vval.v_string == NULL
9852 || *argvars[0].vval.v_string == NUL;
9853 break;
9854 case VAR_NUMBER:
9855 n = argvars[0].vval.v_number == 0;
9856 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009857#ifdef FEAT_FLOAT
9858 case VAR_FLOAT:
9859 n = argvars[0].vval.v_float == 0.0;
9860 break;
9861#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009862 case VAR_LIST:
9863 n = argvars[0].vval.v_list == NULL
9864 || argvars[0].vval.v_list->lv_first == NULL;
9865 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009866 case VAR_DICT:
9867 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009868 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009869 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009870 default:
9871 EMSG2(_(e_intern2), "f_empty()");
9872 n = 0;
9873 }
9874
9875 rettv->vval.v_number = n;
9876}
9877
9878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 * "escape({string}, {chars})" function
9880 */
9881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009882f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009883 typval_T *argvars;
9884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885{
9886 char_u buf[NUMBUFLEN];
9887
Bram Moolenaar758711c2005-02-02 23:11:38 +00009888 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9889 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009890 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891}
9892
9893/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009894 * "eval()" function
9895 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009896 static void
9897f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009898 typval_T *argvars;
9899 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009900{
9901 char_u *s;
9902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009903 s = get_tv_string_chk(&argvars[0]);
9904 if (s != NULL)
9905 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009906
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009907 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9908 {
9909 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009910 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009911 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009912 else if (*s != NUL)
9913 EMSG(_(e_trailing));
9914}
9915
9916/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 * "eventhandler()" function
9918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009920f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009921 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009922 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009924 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925}
9926
9927/*
9928 * "executable()" function
9929 */
9930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009931f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009932 typval_T *argvars;
9933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009935 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936}
9937
9938/*
9939 * "exists()" function
9940 */
9941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009942f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009943 typval_T *argvars;
9944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945{
9946 char_u *p;
9947 char_u *name;
9948 int n = FALSE;
9949 int len = 0;
9950
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009951 no_autoload = TRUE;
9952
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009953 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 if (*p == '$') /* environment variable */
9955 {
9956 /* first try "normal" environment variables (fast) */
9957 if (mch_getenv(p + 1) != NULL)
9958 n = TRUE;
9959 else
9960 {
9961 /* try expanding things like $VIM and ${HOME} */
9962 p = expand_env_save(p);
9963 if (p != NULL && *p != '$')
9964 n = TRUE;
9965 vim_free(p);
9966 }
9967 }
9968 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009969 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009970 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009971 if (*skipwhite(p) != NUL)
9972 n = FALSE; /* trailing garbage */
9973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 else if (*p == '*') /* internal or user defined function */
9975 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009976 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 }
9978 else if (*p == ':')
9979 {
9980 n = cmd_exists(p + 1);
9981 }
9982 else if (*p == '#')
9983 {
9984#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009985 if (p[1] == '#')
9986 n = autocmd_supported(p + 2);
9987 else
9988 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989#endif
9990 }
9991 else /* internal variable */
9992 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009993 char_u *tofree;
9994 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009996 /* get_name_len() takes care of expanding curly braces */
9997 name = p;
9998 len = get_name_len(&p, &tofree, TRUE, FALSE);
9999 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010001 if (tofree != NULL)
10002 name = tofree;
10003 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10004 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010006 /* handle d.key, l[idx], f(expr) */
10007 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10008 if (n)
10009 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 }
10011 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010012 if (*p != NUL)
10013 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010015 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 }
10017
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010018 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010019
10020 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021}
10022
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010023#ifdef FEAT_FLOAT
10024/*
10025 * "exp()" function
10026 */
10027 static void
10028f_exp(argvars, rettv)
10029 typval_T *argvars;
10030 typval_T *rettv;
10031{
10032 float_T f;
10033
10034 rettv->v_type = VAR_FLOAT;
10035 if (get_float_arg(argvars, &f) == OK)
10036 rettv->vval.v_float = exp(f);
10037 else
10038 rettv->vval.v_float = 0.0;
10039}
10040#endif
10041
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042/*
10043 * "expand()" function
10044 */
10045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010046f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010047 typval_T *argvars;
10048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049{
10050 char_u *s;
10051 int len;
10052 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010053 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010055 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010056 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010058 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010059 if (argvars[1].v_type != VAR_UNKNOWN
10060 && argvars[2].v_type != VAR_UNKNOWN
10061 && get_tv_number_chk(&argvars[2], &error)
10062 && !error)
10063 {
10064 rettv->v_type = VAR_LIST;
10065 rettv->vval.v_list = NULL;
10066 }
10067
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010068 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 if (*s == '%' || *s == '#' || *s == '<')
10070 {
10071 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010072 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010074 if (rettv->v_type == VAR_LIST)
10075 {
10076 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10077 list_append_string(rettv->vval.v_list, result, -1);
10078 else
10079 vim_free(result);
10080 }
10081 else
10082 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 }
10084 else
10085 {
10086 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010087 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010088 if (argvars[1].v_type != VAR_UNKNOWN
10089 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010090 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010091 if (!error)
10092 {
10093 ExpandInit(&xpc);
10094 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010095 if (p_wic)
10096 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010097 if (rettv->v_type == VAR_STRING)
10098 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10099 options, WILD_ALL);
10100 else if (rettv_list_alloc(rettv) != FAIL)
10101 {
10102 int i;
10103
10104 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10105 for (i = 0; i < xpc.xp_numfiles; i++)
10106 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10107 ExpandCleanup(&xpc);
10108 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010109 }
10110 else
10111 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 }
10113}
10114
10115/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010116 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010117 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010118 */
10119 static void
10120f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010121 typval_T *argvars;
10122 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010123{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010124 char *arg_errmsg = N_("extend() argument");
10125
Bram Moolenaare9a41262005-01-15 22:18:47 +000010126 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010127 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010128 list_T *l1, *l2;
10129 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010130 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010131 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010132
Bram Moolenaare9a41262005-01-15 22:18:47 +000010133 l1 = argvars[0].vval.v_list;
10134 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010135 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010136 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010137 {
10138 if (argvars[2].v_type != VAR_UNKNOWN)
10139 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010140 before = get_tv_number_chk(&argvars[2], &error);
10141 if (error)
10142 return; /* type error; errmsg already given */
10143
Bram Moolenaar758711c2005-02-02 23:11:38 +000010144 if (before == l1->lv_len)
10145 item = NULL;
10146 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010147 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010148 item = list_find(l1, before);
10149 if (item == NULL)
10150 {
10151 EMSGN(_(e_listidx), before);
10152 return;
10153 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010154 }
10155 }
10156 else
10157 item = NULL;
10158 list_extend(l1, l2, item);
10159
Bram Moolenaare9a41262005-01-15 22:18:47 +000010160 copy_tv(&argvars[0], rettv);
10161 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010162 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010163 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10164 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010165 dict_T *d1, *d2;
10166 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010167 char_u *action;
10168 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010169 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010170 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010171
10172 d1 = argvars[0].vval.v_dict;
10173 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010174 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010175 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010176 {
10177 /* Check the third argument. */
10178 if (argvars[2].v_type != VAR_UNKNOWN)
10179 {
10180 static char *(av[]) = {"keep", "force", "error"};
10181
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010182 action = get_tv_string_chk(&argvars[2]);
10183 if (action == NULL)
10184 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010185 for (i = 0; i < 3; ++i)
10186 if (STRCMP(action, av[i]) == 0)
10187 break;
10188 if (i == 3)
10189 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010190 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010191 return;
10192 }
10193 }
10194 else
10195 action = (char_u *)"force";
10196
10197 /* Go over all entries in the second dict and add them to the
10198 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010199 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010200 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010201 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010202 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010203 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010204 --todo;
10205 di1 = dict_find(d1, hi2->hi_key, -1);
10206 if (di1 == NULL)
10207 {
10208 di1 = dictitem_copy(HI2DI(hi2));
10209 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10210 dictitem_free(di1);
10211 }
10212 else if (*action == 'e')
10213 {
10214 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10215 break;
10216 }
Bram Moolenaar2fc88022012-05-18 12:07:05 +020010217 else if (*action == 'f' && HI2DI(hi2) != di1)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010218 {
10219 clear_tv(&di1->di_tv);
10220 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10221 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010222 }
10223 }
10224
Bram Moolenaare9a41262005-01-15 22:18:47 +000010225 copy_tv(&argvars[0], rettv);
10226 }
10227 }
10228 else
10229 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010230}
10231
10232/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010233 * "feedkeys()" function
10234 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010235 static void
10236f_feedkeys(argvars, rettv)
10237 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010238 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010239{
10240 int remap = TRUE;
10241 char_u *keys, *flags;
10242 char_u nbuf[NUMBUFLEN];
10243 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010244 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010245
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010246 /* This is not allowed in the sandbox. If the commands would still be
10247 * executed in the sandbox it would be OK, but it probably happens later,
10248 * when "sandbox" is no longer set. */
10249 if (check_secure())
10250 return;
10251
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010252 keys = get_tv_string(&argvars[0]);
10253 if (*keys != NUL)
10254 {
10255 if (argvars[1].v_type != VAR_UNKNOWN)
10256 {
10257 flags = get_tv_string_buf(&argvars[1], nbuf);
10258 for ( ; *flags != NUL; ++flags)
10259 {
10260 switch (*flags)
10261 {
10262 case 'n': remap = FALSE; break;
10263 case 'm': remap = TRUE; break;
10264 case 't': typed = TRUE; break;
10265 }
10266 }
10267 }
10268
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010269 /* Need to escape K_SPECIAL and CSI before putting the string in the
10270 * typeahead buffer. */
10271 keys_esc = vim_strsave_escape_csi(keys);
10272 if (keys_esc != NULL)
10273 {
10274 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010275 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010276 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010277 if (vgetc_busy)
10278 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010279 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010280 }
10281}
10282
10283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 * "filereadable()" function
10285 */
10286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010287f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010288 typval_T *argvars;
10289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010291 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 char_u *p;
10293 int n;
10294
Bram Moolenaarc236c162008-07-13 17:41:49 +000010295#ifndef O_NONBLOCK
10296# define O_NONBLOCK 0
10297#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010298 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010299 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10300 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 {
10302 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010303 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 }
10305 else
10306 n = FALSE;
10307
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010308 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309}
10310
10311/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010312 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 * rights to write into.
10314 */
10315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010316f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010317 typval_T *argvars;
10318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010320 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321}
10322
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010323static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010324
10325 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010326findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010327 typval_T *argvars;
10328 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010329 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010330{
10331#ifdef FEAT_SEARCHPATH
10332 char_u *fname;
10333 char_u *fresult = NULL;
10334 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10335 char_u *p;
10336 char_u pathbuf[NUMBUFLEN];
10337 int count = 1;
10338 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010339 int error = FALSE;
10340#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010341
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010342 rettv->vval.v_string = NULL;
10343 rettv->v_type = VAR_STRING;
10344
10345#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010346 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010347
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010348 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010350 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10351 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010352 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010353 else
10354 {
10355 if (*p != NUL)
10356 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010357
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010358 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010359 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010360 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010361 }
10362
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010363 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10364 error = TRUE;
10365
10366 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010367 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010368 do
10369 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010370 if (rettv->v_type == VAR_STRING)
10371 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010372 fresult = find_file_in_path_option(first ? fname : NULL,
10373 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010374 0, first, path,
10375 find_what,
10376 curbuf->b_ffname,
10377 find_what == FINDFILE_DIR
10378 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010379 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010380
10381 if (fresult != NULL && rettv->v_type == VAR_LIST)
10382 list_append_string(rettv->vval.v_list, fresult, -1);
10383
10384 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010385 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010386
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010387 if (rettv->v_type == VAR_STRING)
10388 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010389#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010390}
10391
Bram Moolenaar33570922005-01-25 22:26:29 +000010392static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10393static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010394
10395/*
10396 * Implementation of map() and filter().
10397 */
10398 static void
10399filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010400 typval_T *argvars;
10401 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010402 int map;
10403{
10404 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010405 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010406 listitem_T *li, *nli;
10407 list_T *l = NULL;
10408 dictitem_T *di;
10409 hashtab_T *ht;
10410 hashitem_T *hi;
10411 dict_T *d = NULL;
10412 typval_T save_val;
10413 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010414 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010415 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010416 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10417 char *arg_errmsg = (map ? N_("map() argument")
10418 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010419 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010420 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010421
Bram Moolenaare9a41262005-01-15 22:18:47 +000010422 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010423 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010424 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010425 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010426 return;
10427 }
10428 else if (argvars[0].v_type == VAR_DICT)
10429 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010430 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010431 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010432 return;
10433 }
10434 else
10435 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010436 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010437 return;
10438 }
10439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 expr = get_tv_string_buf_chk(&argvars[1], buf);
10441 /* On type errors, the preceding call has already displayed an error
10442 * message. Avoid a misleading error message for an empty string that
10443 * was not passed as argument. */
10444 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010446 prepare_vimvar(VV_VAL, &save_val);
10447 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010448
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010449 /* We reset "did_emsg" to be able to detect whether an error
10450 * occurred during evaluation of the expression. */
10451 save_did_emsg = did_emsg;
10452 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010453
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010454 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010455 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010457 vimvars[VV_KEY].vv_type = VAR_STRING;
10458
10459 ht = &d->dv_hashtab;
10460 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010461 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010462 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010463 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010464 if (!HASHITEM_EMPTY(hi))
10465 {
10466 --todo;
10467 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010468 if (tv_check_lock(di->di_tv.v_lock,
10469 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010470 break;
10471 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010472 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010473 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010474 break;
10475 if (!map && rem)
10476 dictitem_remove(d, di);
10477 clear_tv(&vimvars[VV_KEY].vv_tv);
10478 }
10479 }
10480 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 }
10482 else
10483 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010484 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10485
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010486 for (li = l->lv_first; li != NULL; li = nli)
10487 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010488 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010489 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010490 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010491 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010492 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010493 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010494 break;
10495 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010496 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010497 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010498 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010499 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010500
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010501 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010502 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010503
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010504 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010505 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010506
10507 copy_tv(&argvars[0], rettv);
10508}
10509
10510 static int
10511filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010512 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010513 char_u *expr;
10514 int map;
10515 int *remp;
10516{
Bram Moolenaar33570922005-01-25 22:26:29 +000010517 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010518 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010519 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010520
Bram Moolenaar33570922005-01-25 22:26:29 +000010521 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010522 s = expr;
10523 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010524 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010525 if (*s != NUL) /* check for trailing chars after expr */
10526 {
10527 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010528 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010529 }
10530 if (map)
10531 {
10532 /* map(): replace the list item value */
10533 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010534 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010535 *tv = rettv;
10536 }
10537 else
10538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010539 int error = FALSE;
10540
Bram Moolenaare9a41262005-01-15 22:18:47 +000010541 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010542 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010543 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010544 /* On type error, nothing has been removed; return FAIL to stop the
10545 * loop. The error message was given by get_tv_number_chk(). */
10546 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010547 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010548 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010549 retval = OK;
10550theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010551 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010552 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010553}
10554
10555/*
10556 * "filter()" function
10557 */
10558 static void
10559f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010560 typval_T *argvars;
10561 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010562{
10563 filter_map(argvars, rettv, FALSE);
10564}
10565
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010566/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010567 * "finddir({fname}[, {path}[, {count}]])" function
10568 */
10569 static void
10570f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010571 typval_T *argvars;
10572 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010573{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010574 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010575}
10576
10577/*
10578 * "findfile({fname}[, {path}[, {count}]])" function
10579 */
10580 static void
10581f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010582 typval_T *argvars;
10583 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010584{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010585 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010586}
10587
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010588#ifdef FEAT_FLOAT
10589/*
10590 * "float2nr({float})" function
10591 */
10592 static void
10593f_float2nr(argvars, rettv)
10594 typval_T *argvars;
10595 typval_T *rettv;
10596{
10597 float_T f;
10598
10599 if (get_float_arg(argvars, &f) == OK)
10600 {
10601 if (f < -0x7fffffff)
10602 rettv->vval.v_number = -0x7fffffff;
10603 else if (f > 0x7fffffff)
10604 rettv->vval.v_number = 0x7fffffff;
10605 else
10606 rettv->vval.v_number = (varnumber_T)f;
10607 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010608}
10609
10610/*
10611 * "floor({float})" function
10612 */
10613 static void
10614f_floor(argvars, rettv)
10615 typval_T *argvars;
10616 typval_T *rettv;
10617{
10618 float_T f;
10619
10620 rettv->v_type = VAR_FLOAT;
10621 if (get_float_arg(argvars, &f) == OK)
10622 rettv->vval.v_float = floor(f);
10623 else
10624 rettv->vval.v_float = 0.0;
10625}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010626
10627/*
10628 * "fmod()" function
10629 */
10630 static void
10631f_fmod(argvars, rettv)
10632 typval_T *argvars;
10633 typval_T *rettv;
10634{
10635 float_T fx, fy;
10636
10637 rettv->v_type = VAR_FLOAT;
10638 if (get_float_arg(argvars, &fx) == OK
10639 && get_float_arg(&argvars[1], &fy) == OK)
10640 rettv->vval.v_float = fmod(fx, fy);
10641 else
10642 rettv->vval.v_float = 0.0;
10643}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010644#endif
10645
Bram Moolenaar0d660222005-01-07 21:51:51 +000010646/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010647 * "fnameescape({string})" function
10648 */
10649 static void
10650f_fnameescape(argvars, rettv)
10651 typval_T *argvars;
10652 typval_T *rettv;
10653{
10654 rettv->vval.v_string = vim_strsave_fnameescape(
10655 get_tv_string(&argvars[0]), FALSE);
10656 rettv->v_type = VAR_STRING;
10657}
10658
10659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 * "fnamemodify({fname}, {mods})" function
10661 */
10662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010663f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010664 typval_T *argvars;
10665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666{
10667 char_u *fname;
10668 char_u *mods;
10669 int usedlen = 0;
10670 int len;
10671 char_u *fbuf = NULL;
10672 char_u buf[NUMBUFLEN];
10673
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010674 fname = get_tv_string_chk(&argvars[0]);
10675 mods = get_tv_string_buf_chk(&argvars[1], buf);
10676 if (fname == NULL || mods == NULL)
10677 fname = NULL;
10678 else
10679 {
10680 len = (int)STRLEN(fname);
10681 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010684 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010686 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010688 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689 vim_free(fbuf);
10690}
10691
Bram Moolenaar33570922005-01-25 22:26:29 +000010692static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693
10694/*
10695 * "foldclosed()" function
10696 */
10697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010698foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010699 typval_T *argvars;
10700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 int end;
10702{
10703#ifdef FEAT_FOLDING
10704 linenr_T lnum;
10705 linenr_T first, last;
10706
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010707 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10709 {
10710 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10711 {
10712 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010713 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010715 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716 return;
10717 }
10718 }
10719#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010720 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721}
10722
10723/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010724 * "foldclosed()" function
10725 */
10726 static void
10727f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010728 typval_T *argvars;
10729 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010730{
10731 foldclosed_both(argvars, rettv, FALSE);
10732}
10733
10734/*
10735 * "foldclosedend()" function
10736 */
10737 static void
10738f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010739 typval_T *argvars;
10740 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010741{
10742 foldclosed_both(argvars, rettv, TRUE);
10743}
10744
10745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 * "foldlevel()" function
10747 */
10748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010749f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010750 typval_T *argvars;
10751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752{
10753#ifdef FEAT_FOLDING
10754 linenr_T lnum;
10755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010756 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010758 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760}
10761
10762/*
10763 * "foldtext()" function
10764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010766f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010767 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769{
10770#ifdef FEAT_FOLDING
10771 linenr_T lnum;
10772 char_u *s;
10773 char_u *r;
10774 int len;
10775 char *txt;
10776#endif
10777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010778 rettv->v_type = VAR_STRING;
10779 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010781 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10782 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10783 <= curbuf->b_ml.ml_line_count
10784 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 {
10786 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010787 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10788 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789 {
10790 if (!linewhite(lnum))
10791 break;
10792 ++lnum;
10793 }
10794
10795 /* Find interesting text in this line. */
10796 s = skipwhite(ml_get(lnum));
10797 /* skip C comment-start */
10798 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010799 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010801 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010802 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010803 {
10804 s = skipwhite(ml_get(lnum + 1));
10805 if (*s == '*')
10806 s = skipwhite(s + 1);
10807 }
10808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 txt = _("+-%s%3ld lines: ");
10810 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010811 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 + 20 /* for %3ld */
10813 + STRLEN(s))); /* concatenated */
10814 if (r != NULL)
10815 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010816 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10817 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10818 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 len = (int)STRLEN(r);
10820 STRCAT(r, s);
10821 /* remove 'foldmarker' and 'commentstring' */
10822 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010823 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 }
10825 }
10826#endif
10827}
10828
10829/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010830 * "foldtextresult(lnum)" function
10831 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010833f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010834 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010835 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010836{
10837#ifdef FEAT_FOLDING
10838 linenr_T lnum;
10839 char_u *text;
10840 char_u buf[51];
10841 foldinfo_T foldinfo;
10842 int fold_count;
10843#endif
10844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010845 rettv->v_type = VAR_STRING;
10846 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010847#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010848 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010849 /* treat illegal types and illegal string values for {lnum} the same */
10850 if (lnum < 0)
10851 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010852 fold_count = foldedCount(curwin, lnum, &foldinfo);
10853 if (fold_count > 0)
10854 {
10855 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10856 &foldinfo, buf);
10857 if (text == buf)
10858 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010859 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010860 }
10861#endif
10862}
10863
10864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 * "foreground()" function
10866 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010868f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010869 typval_T *argvars UNUSED;
10870 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872#ifdef FEAT_GUI
10873 if (gui.in_use)
10874 gui_mch_set_foreground();
10875#else
10876# ifdef WIN32
10877 win32_set_foreground();
10878# endif
10879#endif
10880}
10881
10882/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010883 * "function()" function
10884 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010886f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010887 typval_T *argvars;
10888 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010889{
10890 char_u *s;
10891
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010892 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010893 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010894 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010895 /* Don't check an autoload name for existence here. */
10896 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010897 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010898 else
10899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010900 rettv->vval.v_string = vim_strsave(s);
10901 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010902 }
10903}
10904
10905/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010906 * "garbagecollect()" function
10907 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010908 static void
10909f_garbagecollect(argvars, rettv)
10910 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010911 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010912{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010913 /* This is postponed until we are back at the toplevel, because we may be
10914 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10915 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010916
10917 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10918 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010919}
10920
10921/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010922 * "get()" function
10923 */
10924 static void
10925f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010926 typval_T *argvars;
10927 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010928{
Bram Moolenaar33570922005-01-25 22:26:29 +000010929 listitem_T *li;
10930 list_T *l;
10931 dictitem_T *di;
10932 dict_T *d;
10933 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010934
Bram Moolenaare9a41262005-01-15 22:18:47 +000010935 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010936 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010937 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010938 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010939 int error = FALSE;
10940
10941 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10942 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010943 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010944 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010945 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010946 else if (argvars[0].v_type == VAR_DICT)
10947 {
10948 if ((d = argvars[0].vval.v_dict) != NULL)
10949 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010950 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010951 if (di != NULL)
10952 tv = &di->di_tv;
10953 }
10954 }
10955 else
10956 EMSG2(_(e_listdictarg), "get()");
10957
10958 if (tv == NULL)
10959 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010960 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010961 copy_tv(&argvars[2], rettv);
10962 }
10963 else
10964 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010965}
10966
Bram Moolenaar342337a2005-07-21 21:11:17 +000010967static 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 +000010968
10969/*
10970 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010971 * Return a range (from start to end) of lines in rettv from the specified
10972 * buffer.
10973 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010974 */
10975 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010976get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010977 buf_T *buf;
10978 linenr_T start;
10979 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010980 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010981 typval_T *rettv;
10982{
10983 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010984
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010985 if (retlist && rettv_list_alloc(rettv) == FAIL)
10986 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010987
10988 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10989 return;
10990
10991 if (!retlist)
10992 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010993 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10994 p = ml_get_buf(buf, start, FALSE);
10995 else
10996 p = (char_u *)"";
10997
10998 rettv->v_type = VAR_STRING;
10999 rettv->vval.v_string = vim_strsave(p);
11000 }
11001 else
11002 {
11003 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011004 return;
11005
11006 if (start < 1)
11007 start = 1;
11008 if (end > buf->b_ml.ml_line_count)
11009 end = buf->b_ml.ml_line_count;
11010 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011011 if (list_append_string(rettv->vval.v_list,
11012 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011013 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011014 }
11015}
11016
11017/*
11018 * "getbufline()" function
11019 */
11020 static void
11021f_getbufline(argvars, rettv)
11022 typval_T *argvars;
11023 typval_T *rettv;
11024{
11025 linenr_T lnum;
11026 linenr_T end;
11027 buf_T *buf;
11028
11029 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11030 ++emsg_off;
11031 buf = get_buf_tv(&argvars[0]);
11032 --emsg_off;
11033
Bram Moolenaar661b1822005-07-28 22:36:45 +000011034 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011035 if (argvars[2].v_type == VAR_UNKNOWN)
11036 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011037 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011038 end = get_tv_lnum_buf(&argvars[2], buf);
11039
Bram Moolenaar342337a2005-07-21 21:11:17 +000011040 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011041}
11042
Bram Moolenaar0d660222005-01-07 21:51:51 +000011043/*
11044 * "getbufvar()" function
11045 */
11046 static void
11047f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011048 typval_T *argvars;
11049 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011050{
11051 buf_T *buf;
11052 buf_T *save_curbuf;
11053 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011054 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011056 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11057 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011058 ++emsg_off;
11059 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011060
11061 rettv->v_type = VAR_STRING;
11062 rettv->vval.v_string = NULL;
11063
11064 if (buf != NULL && varname != NULL)
11065 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011066 /* set curbuf to be our buf, temporarily */
11067 save_curbuf = curbuf;
11068 curbuf = buf;
11069
Bram Moolenaar0d660222005-01-07 21:51:51 +000011070 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011071 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010011072 else if (STRCMP(varname, "changedtick") == 0)
11073 {
11074 rettv->v_type = VAR_NUMBER;
11075 rettv->vval.v_number = curbuf->b_changedtick;
11076 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011077 else
11078 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011079 if (*varname == NUL)
11080 /* let getbufvar({nr}, "") return the "b:" dictionary. The
11081 * scope prefix before the NUL byte is required by
11082 * find_var_in_ht(). */
11083 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011084 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000011085 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011086 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011087 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011088 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011089
11090 /* restore previous notion of curbuf */
11091 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011092 }
11093
11094 --emsg_off;
11095}
11096
11097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098 * "getchar()" function
11099 */
11100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011101f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011102 typval_T *argvars;
11103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104{
11105 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011106 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011108 /* Position the cursor. Needed after a message that ends in a space. */
11109 windgoto(msg_row, msg_col);
11110
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 ++no_mapping;
11112 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011113 for (;;)
11114 {
11115 if (argvars[0].v_type == VAR_UNKNOWN)
11116 /* getchar(): blocking wait. */
11117 n = safe_vgetc();
11118 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11119 /* getchar(1): only check if char avail */
11120 n = vpeekc();
11121 else if (error || vpeekc() == NUL)
11122 /* illegal argument or getchar(0) and no char avail: return zero */
11123 n = 0;
11124 else
11125 /* getchar(0) and char avail: return char */
11126 n = safe_vgetc();
11127 if (n == K_IGNORE)
11128 continue;
11129 break;
11130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 --no_mapping;
11132 --allow_keys;
11133
Bram Moolenaar219b8702006-11-01 14:32:36 +000011134 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11135 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11136 vimvars[VV_MOUSE_COL].vv_nr = 0;
11137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011138 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139 if (IS_SPECIAL(n) || mod_mask != 0)
11140 {
11141 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11142 int i = 0;
11143
11144 /* Turn a special key into three bytes, plus modifier. */
11145 if (mod_mask != 0)
11146 {
11147 temp[i++] = K_SPECIAL;
11148 temp[i++] = KS_MODIFIER;
11149 temp[i++] = mod_mask;
11150 }
11151 if (IS_SPECIAL(n))
11152 {
11153 temp[i++] = K_SPECIAL;
11154 temp[i++] = K_SECOND(n);
11155 temp[i++] = K_THIRD(n);
11156 }
11157#ifdef FEAT_MBYTE
11158 else if (has_mbyte)
11159 i += (*mb_char2bytes)(n, temp + i);
11160#endif
11161 else
11162 temp[i++] = n;
11163 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164 rettv->v_type = VAR_STRING;
11165 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011166
11167#ifdef FEAT_MOUSE
11168 if (n == K_LEFTMOUSE
11169 || n == K_LEFTMOUSE_NM
11170 || n == K_LEFTDRAG
11171 || n == K_LEFTRELEASE
11172 || n == K_LEFTRELEASE_NM
11173 || n == K_MIDDLEMOUSE
11174 || n == K_MIDDLEDRAG
11175 || n == K_MIDDLERELEASE
11176 || n == K_RIGHTMOUSE
11177 || n == K_RIGHTDRAG
11178 || n == K_RIGHTRELEASE
11179 || n == K_X1MOUSE
11180 || n == K_X1DRAG
11181 || n == K_X1RELEASE
11182 || n == K_X2MOUSE
11183 || n == K_X2DRAG
11184 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011185 || n == K_MOUSELEFT
11186 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011187 || n == K_MOUSEDOWN
11188 || n == K_MOUSEUP)
11189 {
11190 int row = mouse_row;
11191 int col = mouse_col;
11192 win_T *win;
11193 linenr_T lnum;
11194# ifdef FEAT_WINDOWS
11195 win_T *wp;
11196# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011197 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011198
11199 if (row >= 0 && col >= 0)
11200 {
11201 /* Find the window at the mouse coordinates and compute the
11202 * text position. */
11203 win = mouse_find_win(&row, &col);
11204 (void)mouse_comp_pos(win, &row, &col, &lnum);
11205# ifdef FEAT_WINDOWS
11206 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011207 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011208# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011209 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011210 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11211 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11212 }
11213 }
11214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 }
11216}
11217
11218/*
11219 * "getcharmod()" function
11220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011222f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011223 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227}
11228
11229/*
11230 * "getcmdline()" function
11231 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011233f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011234 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011237 rettv->v_type = VAR_STRING;
11238 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239}
11240
11241/*
11242 * "getcmdpos()" function
11243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011245f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011246 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011249 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250}
11251
11252/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011253 * "getcmdtype()" function
11254 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011255 static void
11256f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011257 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011258 typval_T *rettv;
11259{
11260 rettv->v_type = VAR_STRING;
11261 rettv->vval.v_string = alloc(2);
11262 if (rettv->vval.v_string != NULL)
11263 {
11264 rettv->vval.v_string[0] = get_cmdline_type();
11265 rettv->vval.v_string[1] = NUL;
11266 }
11267}
11268
11269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 * "getcwd()" function
11271 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011274 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011277 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011279 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011280 rettv->vval.v_string = NULL;
11281 cwd = alloc(MAXPATHL);
11282 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011284 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11285 {
11286 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011288 if (rettv->vval.v_string != NULL)
11289 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011291 }
11292 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 }
11294}
11295
11296/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011297 * "getfontname()" function
11298 */
11299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011300f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011301 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011302 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011303{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011304 rettv->v_type = VAR_STRING;
11305 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011306#ifdef FEAT_GUI
11307 if (gui.in_use)
11308 {
11309 GuiFont font;
11310 char_u *name = NULL;
11311
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011312 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011313 {
11314 /* Get the "Normal" font. Either the name saved by
11315 * hl_set_font_name() or from the font ID. */
11316 font = gui.norm_font;
11317 name = hl_get_font_name();
11318 }
11319 else
11320 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011321 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011322 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11323 return;
11324 font = gui_mch_get_font(name, FALSE);
11325 if (font == NOFONT)
11326 return; /* Invalid font name, return empty string. */
11327 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011328 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011329 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011330 gui_mch_free_font(font);
11331 }
11332#endif
11333}
11334
11335/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011336 * "getfperm({fname})" function
11337 */
11338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011339f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011340 typval_T *argvars;
11341 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011342{
11343 char_u *fname;
11344 struct stat st;
11345 char_u *perm = NULL;
11346 char_u flags[] = "rwx";
11347 int i;
11348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011349 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011350
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011351 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011352 if (mch_stat((char *)fname, &st) >= 0)
11353 {
11354 perm = vim_strsave((char_u *)"---------");
11355 if (perm != NULL)
11356 {
11357 for (i = 0; i < 9; i++)
11358 {
11359 if (st.st_mode & (1 << (8 - i)))
11360 perm[i] = flags[i % 3];
11361 }
11362 }
11363 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011364 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011365}
11366
11367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 * "getfsize({fname})" function
11369 */
11370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011371f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011372 typval_T *argvars;
11373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374{
11375 char_u *fname;
11376 struct stat st;
11377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011378 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011380 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381
11382 if (mch_stat((char *)fname, &st) >= 0)
11383 {
11384 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011385 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011389
11390 /* non-perfect check for overflow */
11391 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11392 rettv->vval.v_number = -2;
11393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394 }
11395 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011396 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397}
11398
11399/*
11400 * "getftime({fname})" function
11401 */
11402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011403f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011404 typval_T *argvars;
11405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406{
11407 char_u *fname;
11408 struct stat st;
11409
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011410 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411
11412 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011413 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011415 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416}
11417
11418/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011419 * "getftype({fname})" function
11420 */
11421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011422f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011423 typval_T *argvars;
11424 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011425{
11426 char_u *fname;
11427 struct stat st;
11428 char_u *type = NULL;
11429 char *t;
11430
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011431 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011433 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011434 if (mch_lstat((char *)fname, &st) >= 0)
11435 {
11436#ifdef S_ISREG
11437 if (S_ISREG(st.st_mode))
11438 t = "file";
11439 else if (S_ISDIR(st.st_mode))
11440 t = "dir";
11441# ifdef S_ISLNK
11442 else if (S_ISLNK(st.st_mode))
11443 t = "link";
11444# endif
11445# ifdef S_ISBLK
11446 else if (S_ISBLK(st.st_mode))
11447 t = "bdev";
11448# endif
11449# ifdef S_ISCHR
11450 else if (S_ISCHR(st.st_mode))
11451 t = "cdev";
11452# endif
11453# ifdef S_ISFIFO
11454 else if (S_ISFIFO(st.st_mode))
11455 t = "fifo";
11456# endif
11457# ifdef S_ISSOCK
11458 else if (S_ISSOCK(st.st_mode))
11459 t = "fifo";
11460# endif
11461 else
11462 t = "other";
11463#else
11464# ifdef S_IFMT
11465 switch (st.st_mode & S_IFMT)
11466 {
11467 case S_IFREG: t = "file"; break;
11468 case S_IFDIR: t = "dir"; break;
11469# ifdef S_IFLNK
11470 case S_IFLNK: t = "link"; break;
11471# endif
11472# ifdef S_IFBLK
11473 case S_IFBLK: t = "bdev"; break;
11474# endif
11475# ifdef S_IFCHR
11476 case S_IFCHR: t = "cdev"; break;
11477# endif
11478# ifdef S_IFIFO
11479 case S_IFIFO: t = "fifo"; break;
11480# endif
11481# ifdef S_IFSOCK
11482 case S_IFSOCK: t = "socket"; break;
11483# endif
11484 default: t = "other";
11485 }
11486# else
11487 if (mch_isdir(fname))
11488 t = "dir";
11489 else
11490 t = "file";
11491# endif
11492#endif
11493 type = vim_strsave((char_u *)t);
11494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011495 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011496}
11497
11498/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011499 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011500 */
11501 static void
11502f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011503 typval_T *argvars;
11504 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011505{
11506 linenr_T lnum;
11507 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011508 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011509
11510 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011511 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011512 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011513 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011514 retlist = FALSE;
11515 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011516 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011517 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011518 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011519 retlist = TRUE;
11520 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011521
Bram Moolenaar342337a2005-07-21 21:11:17 +000011522 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011523}
11524
11525/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011526 * "getmatches()" function
11527 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011528 static void
11529f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011530 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011531 typval_T *rettv;
11532{
11533#ifdef FEAT_SEARCH_EXTRA
11534 dict_T *dict;
11535 matchitem_T *cur = curwin->w_match_head;
11536
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011537 if (rettv_list_alloc(rettv) == OK)
11538 {
11539 while (cur != NULL)
11540 {
11541 dict = dict_alloc();
11542 if (dict == NULL)
11543 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011544 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11545 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11546 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11547 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11548 list_append_dict(rettv->vval.v_list, dict);
11549 cur = cur->next;
11550 }
11551 }
11552#endif
11553}
11554
11555/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011556 * "getpid()" function
11557 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011558 static void
11559f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011560 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011561 typval_T *rettv;
11562{
11563 rettv->vval.v_number = mch_get_pid();
11564}
11565
11566/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011567 * "getpos(string)" function
11568 */
11569 static void
11570f_getpos(argvars, rettv)
11571 typval_T *argvars;
11572 typval_T *rettv;
11573{
11574 pos_T *fp;
11575 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011576 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011577
11578 if (rettv_list_alloc(rettv) == OK)
11579 {
11580 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011581 fp = var2fpos(&argvars[0], TRUE, &fnum);
11582 if (fnum != -1)
11583 list_append_number(l, (varnumber_T)fnum);
11584 else
11585 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011586 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11587 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011588 list_append_number(l, (fp != NULL)
11589 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011590 : (varnumber_T)0);
11591 list_append_number(l,
11592#ifdef FEAT_VIRTUALEDIT
11593 (fp != NULL) ? (varnumber_T)fp->coladd :
11594#endif
11595 (varnumber_T)0);
11596 }
11597 else
11598 rettv->vval.v_number = FALSE;
11599}
11600
11601/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011602 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011603 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011604 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011605f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011606 typval_T *argvars UNUSED;
11607 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011608{
11609#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011610 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011611#endif
11612
Bram Moolenaar2641f772005-03-25 21:58:17 +000011613#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011614 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011615 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011616 wp = NULL;
11617 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11618 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011619 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011620 if (wp == NULL)
11621 return;
11622 }
11623
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011624 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011625 }
11626#endif
11627}
11628
11629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630 * "getreg()" function
11631 */
11632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011633f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011634 typval_T *argvars;
11635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636{
11637 char_u *strregname;
11638 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011639 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011640 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011642 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011643 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011644 strregname = get_tv_string_chk(&argvars[0]);
11645 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011646 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011647 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011649 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011650 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011651 regname = (strregname == NULL ? '"' : *strregname);
11652 if (regname == 0)
11653 regname = '"';
11654
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011655 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011656 rettv->vval.v_string = error ? NULL :
11657 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658}
11659
11660/*
11661 * "getregtype()" function
11662 */
11663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011664f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011665 typval_T *argvars;
11666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667{
11668 char_u *strregname;
11669 int regname;
11670 char_u buf[NUMBUFLEN + 2];
11671 long reglen = 0;
11672
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011673 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011674 {
11675 strregname = get_tv_string_chk(&argvars[0]);
11676 if (strregname == NULL) /* type error; errmsg already given */
11677 {
11678 rettv->v_type = VAR_STRING;
11679 rettv->vval.v_string = NULL;
11680 return;
11681 }
11682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683 else
11684 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011685 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686
11687 regname = (strregname == NULL ? '"' : *strregname);
11688 if (regname == 0)
11689 regname = '"';
11690
11691 buf[0] = NUL;
11692 buf[1] = NUL;
11693 switch (get_reg_type(regname, &reglen))
11694 {
11695 case MLINE: buf[0] = 'V'; break;
11696 case MCHAR: buf[0] = 'v'; break;
11697#ifdef FEAT_VISUAL
11698 case MBLOCK:
11699 buf[0] = Ctrl_V;
11700 sprintf((char *)buf + 1, "%ld", reglen + 1);
11701 break;
11702#endif
11703 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011704 rettv->v_type = VAR_STRING;
11705 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706}
11707
11708/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011709 * "gettabvar()" function
11710 */
11711 static void
11712f_gettabvar(argvars, rettv)
11713 typval_T *argvars;
11714 typval_T *rettv;
11715{
11716 tabpage_T *tp;
11717 dictitem_T *v;
11718 char_u *varname;
11719
11720 rettv->v_type = VAR_STRING;
11721 rettv->vval.v_string = NULL;
11722
11723 varname = get_tv_string_chk(&argvars[1]);
11724 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11725 if (tp != NULL && varname != NULL)
11726 {
11727 /* look up the variable */
11728 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11729 if (v != NULL)
11730 copy_tv(&v->di_tv, rettv);
11731 }
11732}
11733
11734/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011735 * "gettabwinvar()" function
11736 */
11737 static void
11738f_gettabwinvar(argvars, rettv)
11739 typval_T *argvars;
11740 typval_T *rettv;
11741{
11742 getwinvar(argvars, rettv, 1);
11743}
11744
11745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746 * "getwinposx()" function
11747 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011749f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011750 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754#ifdef FEAT_GUI
11755 if (gui.in_use)
11756 {
11757 int x, y;
11758
11759 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011760 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761 }
11762#endif
11763}
11764
11765/*
11766 * "getwinposy()" function
11767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011769f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011770 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011773 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774#ifdef FEAT_GUI
11775 if (gui.in_use)
11776 {
11777 int x, y;
11778
11779 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011780 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781 }
11782#endif
11783}
11784
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011785/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011786 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011787 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011788 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011789find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011790 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011791 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011792{
11793#ifdef FEAT_WINDOWS
11794 win_T *wp;
11795#endif
11796 int nr;
11797
11798 nr = get_tv_number_chk(vp, NULL);
11799
11800#ifdef FEAT_WINDOWS
11801 if (nr < 0)
11802 return NULL;
11803 if (nr == 0)
11804 return curwin;
11805
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011806 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11807 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011808 if (--nr <= 0)
11809 break;
11810 return wp;
11811#else
11812 if (nr == 0 || nr == 1)
11813 return curwin;
11814 return NULL;
11815#endif
11816}
11817
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818/*
11819 * "getwinvar()" function
11820 */
11821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011822f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011823 typval_T *argvars;
11824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011826 getwinvar(argvars, rettv, 0);
11827}
11828
11829/*
11830 * getwinvar() and gettabwinvar()
11831 */
11832 static void
11833getwinvar(argvars, rettv, off)
11834 typval_T *argvars;
11835 typval_T *rettv;
11836 int off; /* 1 for gettabwinvar() */
11837{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838 win_T *win, *oldcurwin;
11839 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011840 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011841 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011843#ifdef FEAT_WINDOWS
11844 if (off == 1)
11845 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11846 else
11847 tp = curtab;
11848#endif
11849 win = find_win_by_nr(&argvars[off], tp);
11850 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011851 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011853 rettv->v_type = VAR_STRING;
11854 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855
11856 if (win != NULL && varname != NULL)
11857 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011858 /* Set curwin to be our win, temporarily. Also set curbuf, so
11859 * that we can get buffer-local options. */
11860 oldcurwin = curwin;
11861 curwin = win;
11862 curbuf = win->w_buffer;
11863
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866 else
11867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011868 if (*varname == NUL)
11869 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11870 * scope prefix before the NUL byte is required by
11871 * find_var_in_ht(). */
11872 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011874 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011876 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011878
11879 /* restore previous notion of curwin */
11880 curwin = oldcurwin;
11881 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882 }
11883
11884 --emsg_off;
11885}
11886
11887/*
11888 * "glob()" function
11889 */
11890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011891f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011892 typval_T *argvars;
11893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011895 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011897 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011899 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011900 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011901 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011902 if (argvars[1].v_type != VAR_UNKNOWN)
11903 {
11904 if (get_tv_number_chk(&argvars[1], &error))
11905 options |= WILD_KEEP_ALL;
11906 if (argvars[2].v_type != VAR_UNKNOWN
11907 && get_tv_number_chk(&argvars[2], &error))
11908 {
11909 rettv->v_type = VAR_LIST;
11910 rettv->vval.v_list = NULL;
11911 }
11912 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011913 if (!error)
11914 {
11915 ExpandInit(&xpc);
11916 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011917 if (p_wic)
11918 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011919 if (rettv->v_type == VAR_STRING)
11920 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011921 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011922 else if (rettv_list_alloc(rettv) != FAIL)
11923 {
11924 int i;
11925
11926 ExpandOne(&xpc, get_tv_string(&argvars[0]),
11927 NULL, options, WILD_ALL_KEEP);
11928 for (i = 0; i < xpc.xp_numfiles; i++)
11929 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11930
11931 ExpandCleanup(&xpc);
11932 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011933 }
11934 else
11935 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936}
11937
11938/*
11939 * "globpath()" function
11940 */
11941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011942f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011943 typval_T *argvars;
11944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011946 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011948 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011949 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011951 /* When the optional second argument is non-zero, don't remove matches
11952 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11953 if (argvars[2].v_type != VAR_UNKNOWN
11954 && get_tv_number_chk(&argvars[2], &error))
11955 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011956 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011957 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011958 rettv->vval.v_string = NULL;
11959 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011960 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11961 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962}
11963
11964/*
11965 * "has()" function
11966 */
11967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011968f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011969 typval_T *argvars;
11970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971{
11972 int i;
11973 char_u *name;
11974 int n = FALSE;
11975 static char *(has_list[]) =
11976 {
11977#ifdef AMIGA
11978 "amiga",
11979# ifdef FEAT_ARP
11980 "arp",
11981# endif
11982#endif
11983#ifdef __BEOS__
11984 "beos",
11985#endif
11986#ifdef MSDOS
11987# ifdef DJGPP
11988 "dos32",
11989# else
11990 "dos16",
11991# endif
11992#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011993#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994 "mac",
11995#endif
11996#if defined(MACOS_X_UNIX)
11997 "macunix",
11998#endif
11999#ifdef OS2
12000 "os2",
12001#endif
12002#ifdef __QNX__
12003 "qnx",
12004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005#ifdef UNIX
12006 "unix",
12007#endif
12008#ifdef VMS
12009 "vms",
12010#endif
12011#ifdef WIN16
12012 "win16",
12013#endif
12014#ifdef WIN32
12015 "win32",
12016#endif
12017#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12018 "win32unix",
12019#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012020#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021 "win64",
12022#endif
12023#ifdef EBCDIC
12024 "ebcdic",
12025#endif
12026#ifndef CASE_INSENSITIVE_FILENAME
12027 "fname_case",
12028#endif
12029#ifdef FEAT_ARABIC
12030 "arabic",
12031#endif
12032#ifdef FEAT_AUTOCMD
12033 "autocmd",
12034#endif
12035#ifdef FEAT_BEVAL
12036 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012037# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12038 "balloon_multiline",
12039# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040#endif
12041#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12042 "builtin_terms",
12043# ifdef ALL_BUILTIN_TCAPS
12044 "all_builtin_terms",
12045# endif
12046#endif
12047#ifdef FEAT_BYTEOFF
12048 "byte_offset",
12049#endif
12050#ifdef FEAT_CINDENT
12051 "cindent",
12052#endif
12053#ifdef FEAT_CLIENTSERVER
12054 "clientserver",
12055#endif
12056#ifdef FEAT_CLIPBOARD
12057 "clipboard",
12058#endif
12059#ifdef FEAT_CMDL_COMPL
12060 "cmdline_compl",
12061#endif
12062#ifdef FEAT_CMDHIST
12063 "cmdline_hist",
12064#endif
12065#ifdef FEAT_COMMENTS
12066 "comments",
12067#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012068#ifdef FEAT_CONCEAL
12069 "conceal",
12070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071#ifdef FEAT_CRYPT
12072 "cryptv",
12073#endif
12074#ifdef FEAT_CSCOPE
12075 "cscope",
12076#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012077#ifdef FEAT_CURSORBIND
12078 "cursorbind",
12079#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012080#ifdef CURSOR_SHAPE
12081 "cursorshape",
12082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083#ifdef DEBUG
12084 "debug",
12085#endif
12086#ifdef FEAT_CON_DIALOG
12087 "dialog_con",
12088#endif
12089#ifdef FEAT_GUI_DIALOG
12090 "dialog_gui",
12091#endif
12092#ifdef FEAT_DIFF
12093 "diff",
12094#endif
12095#ifdef FEAT_DIGRAPHS
12096 "digraphs",
12097#endif
12098#ifdef FEAT_DND
12099 "dnd",
12100#endif
12101#ifdef FEAT_EMACS_TAGS
12102 "emacs_tags",
12103#endif
12104 "eval", /* always present, of course! */
12105#ifdef FEAT_EX_EXTRA
12106 "ex_extra",
12107#endif
12108#ifdef FEAT_SEARCH_EXTRA
12109 "extra_search",
12110#endif
12111#ifdef FEAT_FKMAP
12112 "farsi",
12113#endif
12114#ifdef FEAT_SEARCHPATH
12115 "file_in_path",
12116#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012117#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012118 "filterpipe",
12119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012120#ifdef FEAT_FIND_ID
12121 "find_in_path",
12122#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012123#ifdef FEAT_FLOAT
12124 "float",
12125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126#ifdef FEAT_FOLDING
12127 "folding",
12128#endif
12129#ifdef FEAT_FOOTER
12130 "footer",
12131#endif
12132#if !defined(USE_SYSTEM) && defined(UNIX)
12133 "fork",
12134#endif
12135#ifdef FEAT_GETTEXT
12136 "gettext",
12137#endif
12138#ifdef FEAT_GUI
12139 "gui",
12140#endif
12141#ifdef FEAT_GUI_ATHENA
12142# ifdef FEAT_GUI_NEXTAW
12143 "gui_neXtaw",
12144# else
12145 "gui_athena",
12146# endif
12147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148#ifdef FEAT_GUI_GTK
12149 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012152#ifdef FEAT_GUI_GNOME
12153 "gui_gnome",
12154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155#ifdef FEAT_GUI_MAC
12156 "gui_mac",
12157#endif
12158#ifdef FEAT_GUI_MOTIF
12159 "gui_motif",
12160#endif
12161#ifdef FEAT_GUI_PHOTON
12162 "gui_photon",
12163#endif
12164#ifdef FEAT_GUI_W16
12165 "gui_win16",
12166#endif
12167#ifdef FEAT_GUI_W32
12168 "gui_win32",
12169#endif
12170#ifdef FEAT_HANGULIN
12171 "hangul_input",
12172#endif
12173#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12174 "iconv",
12175#endif
12176#ifdef FEAT_INS_EXPAND
12177 "insert_expand",
12178#endif
12179#ifdef FEAT_JUMPLIST
12180 "jumplist",
12181#endif
12182#ifdef FEAT_KEYMAP
12183 "keymap",
12184#endif
12185#ifdef FEAT_LANGMAP
12186 "langmap",
12187#endif
12188#ifdef FEAT_LIBCALL
12189 "libcall",
12190#endif
12191#ifdef FEAT_LINEBREAK
12192 "linebreak",
12193#endif
12194#ifdef FEAT_LISP
12195 "lispindent",
12196#endif
12197#ifdef FEAT_LISTCMDS
12198 "listcmds",
12199#endif
12200#ifdef FEAT_LOCALMAP
12201 "localmap",
12202#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012203#ifdef FEAT_LUA
12204# ifndef DYNAMIC_LUA
12205 "lua",
12206# endif
12207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208#ifdef FEAT_MENU
12209 "menu",
12210#endif
12211#ifdef FEAT_SESSION
12212 "mksession",
12213#endif
12214#ifdef FEAT_MODIFY_FNAME
12215 "modify_fname",
12216#endif
12217#ifdef FEAT_MOUSE
12218 "mouse",
12219#endif
12220#ifdef FEAT_MOUSESHAPE
12221 "mouseshape",
12222#endif
12223#if defined(UNIX) || defined(VMS)
12224# ifdef FEAT_MOUSE_DEC
12225 "mouse_dec",
12226# endif
12227# ifdef FEAT_MOUSE_GPM
12228 "mouse_gpm",
12229# endif
12230# ifdef FEAT_MOUSE_JSB
12231 "mouse_jsbterm",
12232# endif
12233# ifdef FEAT_MOUSE_NET
12234 "mouse_netterm",
12235# endif
12236# ifdef FEAT_MOUSE_PTERM
12237 "mouse_pterm",
12238# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012239# ifdef FEAT_SYSMOUSE
12240 "mouse_sysmouse",
12241# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242# ifdef FEAT_MOUSE_XTERM
12243 "mouse_xterm",
12244# endif
12245#endif
12246#ifdef FEAT_MBYTE
12247 "multi_byte",
12248#endif
12249#ifdef FEAT_MBYTE_IME
12250 "multi_byte_ime",
12251#endif
12252#ifdef FEAT_MULTI_LANG
12253 "multi_lang",
12254#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012255#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012256#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012257 "mzscheme",
12258#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260#ifdef FEAT_OLE
12261 "ole",
12262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263#ifdef FEAT_PATH_EXTRA
12264 "path_extra",
12265#endif
12266#ifdef FEAT_PERL
12267#ifndef DYNAMIC_PERL
12268 "perl",
12269#endif
12270#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012271#ifdef FEAT_PERSISTENT_UNDO
12272 "persistent_undo",
12273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274#ifdef FEAT_PYTHON
12275#ifndef DYNAMIC_PYTHON
12276 "python",
12277#endif
12278#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012279#ifdef FEAT_PYTHON3
12280#ifndef DYNAMIC_PYTHON3
12281 "python3",
12282#endif
12283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284#ifdef FEAT_POSTSCRIPT
12285 "postscript",
12286#endif
12287#ifdef FEAT_PRINTER
12288 "printer",
12289#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012290#ifdef FEAT_PROFILE
12291 "profile",
12292#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012293#ifdef FEAT_RELTIME
12294 "reltime",
12295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296#ifdef FEAT_QUICKFIX
12297 "quickfix",
12298#endif
12299#ifdef FEAT_RIGHTLEFT
12300 "rightleft",
12301#endif
12302#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12303 "ruby",
12304#endif
12305#ifdef FEAT_SCROLLBIND
12306 "scrollbind",
12307#endif
12308#ifdef FEAT_CMDL_INFO
12309 "showcmd",
12310 "cmdline_info",
12311#endif
12312#ifdef FEAT_SIGNS
12313 "signs",
12314#endif
12315#ifdef FEAT_SMARTINDENT
12316 "smartindent",
12317#endif
12318#ifdef FEAT_SNIFF
12319 "sniff",
12320#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012321#ifdef STARTUPTIME
12322 "startuptime",
12323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324#ifdef FEAT_STL_OPT
12325 "statusline",
12326#endif
12327#ifdef FEAT_SUN_WORKSHOP
12328 "sun_workshop",
12329#endif
12330#ifdef FEAT_NETBEANS_INTG
12331 "netbeans_intg",
12332#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012333#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012334 "spell",
12335#endif
12336#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337 "syntax",
12338#endif
12339#if defined(USE_SYSTEM) || !defined(UNIX)
12340 "system",
12341#endif
12342#ifdef FEAT_TAG_BINS
12343 "tag_binary",
12344#endif
12345#ifdef FEAT_TAG_OLDSTATIC
12346 "tag_old_static",
12347#endif
12348#ifdef FEAT_TAG_ANYWHITE
12349 "tag_any_white",
12350#endif
12351#ifdef FEAT_TCL
12352# ifndef DYNAMIC_TCL
12353 "tcl",
12354# endif
12355#endif
12356#ifdef TERMINFO
12357 "terminfo",
12358#endif
12359#ifdef FEAT_TERMRESPONSE
12360 "termresponse",
12361#endif
12362#ifdef FEAT_TEXTOBJ
12363 "textobjects",
12364#endif
12365#ifdef HAVE_TGETENT
12366 "tgetent",
12367#endif
12368#ifdef FEAT_TITLE
12369 "title",
12370#endif
12371#ifdef FEAT_TOOLBAR
12372 "toolbar",
12373#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012374#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12375 "unnamedplus",
12376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377#ifdef FEAT_USR_CMDS
12378 "user-commands", /* was accidentally included in 5.4 */
12379 "user_commands",
12380#endif
12381#ifdef FEAT_VIMINFO
12382 "viminfo",
12383#endif
12384#ifdef FEAT_VERTSPLIT
12385 "vertsplit",
12386#endif
12387#ifdef FEAT_VIRTUALEDIT
12388 "virtualedit",
12389#endif
12390#ifdef FEAT_VISUAL
12391 "visual",
12392#endif
12393#ifdef FEAT_VISUALEXTRA
12394 "visualextra",
12395#endif
12396#ifdef FEAT_VREPLACE
12397 "vreplace",
12398#endif
12399#ifdef FEAT_WILDIGN
12400 "wildignore",
12401#endif
12402#ifdef FEAT_WILDMENU
12403 "wildmenu",
12404#endif
12405#ifdef FEAT_WINDOWS
12406 "windows",
12407#endif
12408#ifdef FEAT_WAK
12409 "winaltkeys",
12410#endif
12411#ifdef FEAT_WRITEBACKUP
12412 "writebackup",
12413#endif
12414#ifdef FEAT_XIM
12415 "xim",
12416#endif
12417#ifdef FEAT_XFONTSET
12418 "xfontset",
12419#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012420#ifdef FEAT_XPM_W32
12421 "xpm_w32",
12422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012423#ifdef USE_XSMP
12424 "xsmp",
12425#endif
12426#ifdef USE_XSMP_INTERACT
12427 "xsmp_interact",
12428#endif
12429#ifdef FEAT_XCLIPBOARD
12430 "xterm_clipboard",
12431#endif
12432#ifdef FEAT_XTERM_SAVE
12433 "xterm_save",
12434#endif
12435#if defined(UNIX) && defined(FEAT_X11)
12436 "X11",
12437#endif
12438 NULL
12439 };
12440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012441 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442 for (i = 0; has_list[i] != NULL; ++i)
12443 if (STRICMP(name, has_list[i]) == 0)
12444 {
12445 n = TRUE;
12446 break;
12447 }
12448
12449 if (n == FALSE)
12450 {
12451 if (STRNICMP(name, "patch", 5) == 0)
12452 n = has_patch(atoi((char *)name + 5));
12453 else if (STRICMP(name, "vim_starting") == 0)
12454 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012455#ifdef FEAT_MBYTE
12456 else if (STRICMP(name, "multi_byte_encoding") == 0)
12457 n = has_mbyte;
12458#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012459#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12460 else if (STRICMP(name, "balloon_multiline") == 0)
12461 n = multiline_balloon_available();
12462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012463#ifdef DYNAMIC_TCL
12464 else if (STRICMP(name, "tcl") == 0)
12465 n = tcl_enabled(FALSE);
12466#endif
12467#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12468 else if (STRICMP(name, "iconv") == 0)
12469 n = iconv_enabled(FALSE);
12470#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012471#ifdef DYNAMIC_LUA
12472 else if (STRICMP(name, "lua") == 0)
12473 n = lua_enabled(FALSE);
12474#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012475#ifdef DYNAMIC_MZSCHEME
12476 else if (STRICMP(name, "mzscheme") == 0)
12477 n = mzscheme_enabled(FALSE);
12478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479#ifdef DYNAMIC_RUBY
12480 else if (STRICMP(name, "ruby") == 0)
12481 n = ruby_enabled(FALSE);
12482#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012483#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484#ifdef DYNAMIC_PYTHON
12485 else if (STRICMP(name, "python") == 0)
12486 n = python_enabled(FALSE);
12487#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012488#endif
12489#ifdef FEAT_PYTHON3
12490#ifdef DYNAMIC_PYTHON3
12491 else if (STRICMP(name, "python3") == 0)
12492 n = python3_enabled(FALSE);
12493#endif
12494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012495#ifdef DYNAMIC_PERL
12496 else if (STRICMP(name, "perl") == 0)
12497 n = perl_enabled(FALSE);
12498#endif
12499#ifdef FEAT_GUI
12500 else if (STRICMP(name, "gui_running") == 0)
12501 n = (gui.in_use || gui.starting);
12502# ifdef FEAT_GUI_W32
12503 else if (STRICMP(name, "gui_win32s") == 0)
12504 n = gui_is_win32s();
12505# endif
12506# ifdef FEAT_BROWSE
12507 else if (STRICMP(name, "browse") == 0)
12508 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12509# endif
12510#endif
12511#ifdef FEAT_SYN_HL
12512 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012513 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514#endif
12515#if defined(WIN3264)
12516 else if (STRICMP(name, "win95") == 0)
12517 n = mch_windows95();
12518#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012519#ifdef FEAT_NETBEANS_INTG
12520 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012521 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523 }
12524
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012525 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526}
12527
12528/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012529 * "has_key()" function
12530 */
12531 static void
12532f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012533 typval_T *argvars;
12534 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012535{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012536 if (argvars[0].v_type != VAR_DICT)
12537 {
12538 EMSG(_(e_dictreq));
12539 return;
12540 }
12541 if (argvars[0].vval.v_dict == NULL)
12542 return;
12543
12544 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012545 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012546}
12547
12548/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012549 * "haslocaldir()" function
12550 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012551 static void
12552f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012553 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012554 typval_T *rettv;
12555{
12556 rettv->vval.v_number = (curwin->w_localdir != NULL);
12557}
12558
12559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560 * "hasmapto()" function
12561 */
12562 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012563f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012564 typval_T *argvars;
12565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012566{
12567 char_u *name;
12568 char_u *mode;
12569 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012570 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012572 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012573 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574 mode = (char_u *)"nvo";
12575 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012577 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012578 if (argvars[2].v_type != VAR_UNKNOWN)
12579 abbr = get_tv_number(&argvars[2]);
12580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012581
Bram Moolenaar2c932302006-03-18 21:42:09 +000012582 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012583 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012585 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012586}
12587
12588/*
12589 * "histadd()" function
12590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012592f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012593 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595{
12596#ifdef FEAT_CMDHIST
12597 int histype;
12598 char_u *str;
12599 char_u buf[NUMBUFLEN];
12600#endif
12601
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012602 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012603 if (check_restricted() || check_secure())
12604 return;
12605#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012606 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12607 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608 if (histype >= 0)
12609 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012610 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012611 if (*str != NUL)
12612 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012613 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012615 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012616 return;
12617 }
12618 }
12619#endif
12620}
12621
12622/*
12623 * "histdel()" function
12624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012626f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012627 typval_T *argvars UNUSED;
12628 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012629{
12630#ifdef FEAT_CMDHIST
12631 int n;
12632 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012633 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012635 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12636 if (str == NULL)
12637 n = 0;
12638 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012640 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012641 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012642 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012643 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012644 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645 else
12646 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012647 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012648 get_tv_string_buf(&argvars[1], buf));
12649 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650#endif
12651}
12652
12653/*
12654 * "histget()" function
12655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012657f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012658 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660{
12661#ifdef FEAT_CMDHIST
12662 int type;
12663 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012664 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012666 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12667 if (str == NULL)
12668 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012670 {
12671 type = get_histtype(str);
12672 if (argvars[1].v_type == VAR_UNKNOWN)
12673 idx = get_history_idx(type);
12674 else
12675 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12676 /* -1 on type error */
12677 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012680 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012682 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683}
12684
12685/*
12686 * "histnr()" function
12687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012689f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012690 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692{
12693 int i;
12694
12695#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012696 char_u *history = get_tv_string_chk(&argvars[0]);
12697
12698 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699 if (i >= HIST_CMD && i < HIST_COUNT)
12700 i = get_history_idx(i);
12701 else
12702#endif
12703 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012704 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705}
12706
12707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708 * "highlightID(name)" function
12709 */
12710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012711f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012712 typval_T *argvars;
12713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012714{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012715 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716}
12717
12718/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012719 * "highlight_exists()" function
12720 */
12721 static void
12722f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012723 typval_T *argvars;
12724 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012725{
12726 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12727}
12728
12729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730 * "hostname()" function
12731 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012733f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012734 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736{
12737 char_u hostname[256];
12738
12739 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012740 rettv->v_type = VAR_STRING;
12741 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742}
12743
12744/*
12745 * iconv() function
12746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012748f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012749 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751{
12752#ifdef FEAT_MBYTE
12753 char_u buf1[NUMBUFLEN];
12754 char_u buf2[NUMBUFLEN];
12755 char_u *from, *to, *str;
12756 vimconv_T vimconv;
12757#endif
12758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012759 rettv->v_type = VAR_STRING;
12760 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761
12762#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012763 str = get_tv_string(&argvars[0]);
12764 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12765 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766 vimconv.vc_type = CONV_NONE;
12767 convert_setup(&vimconv, from, to);
12768
12769 /* If the encodings are equal, no conversion needed. */
12770 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012771 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012773 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774
12775 convert_setup(&vimconv, NULL, NULL);
12776 vim_free(from);
12777 vim_free(to);
12778#endif
12779}
12780
12781/*
12782 * "indent()" function
12783 */
12784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012785f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012786 typval_T *argvars;
12787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788{
12789 linenr_T lnum;
12790
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012791 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012793 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012795 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796}
12797
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012798/*
12799 * "index()" function
12800 */
12801 static void
12802f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012803 typval_T *argvars;
12804 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012805{
Bram Moolenaar33570922005-01-25 22:26:29 +000012806 list_T *l;
12807 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012808 long idx = 0;
12809 int ic = FALSE;
12810
12811 rettv->vval.v_number = -1;
12812 if (argvars[0].v_type != VAR_LIST)
12813 {
12814 EMSG(_(e_listreq));
12815 return;
12816 }
12817 l = argvars[0].vval.v_list;
12818 if (l != NULL)
12819 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012820 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012821 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012823 int error = FALSE;
12824
Bram Moolenaar758711c2005-02-02 23:11:38 +000012825 /* Start at specified item. Use the cached index that list_find()
12826 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012827 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012828 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012829 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012830 ic = get_tv_number_chk(&argvars[3], &error);
12831 if (error)
12832 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012833 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012834
Bram Moolenaar758711c2005-02-02 23:11:38 +000012835 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012836 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012837 {
12838 rettv->vval.v_number = idx;
12839 break;
12840 }
12841 }
12842}
12843
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844static int inputsecret_flag = 0;
12845
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012846static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12847
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012849 * This function is used by f_input() and f_inputdialog() functions. The third
12850 * argument to f_input() specifies the type of completion to use at the
12851 * prompt. The third argument to f_inputdialog() specifies the value to return
12852 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853 */
12854 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012855get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012856 typval_T *argvars;
12857 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012858 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012860 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861 char_u *p = NULL;
12862 int c;
12863 char_u buf[NUMBUFLEN];
12864 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012865 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012866 int xp_type = EXPAND_NOTHING;
12867 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012869 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012870 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871
12872#ifdef NO_CONSOLE_INPUT
12873 /* While starting up, there is no place to enter text. */
12874 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876#endif
12877
12878 cmd_silent = FALSE; /* Want to see the prompt. */
12879 if (prompt != NULL)
12880 {
12881 /* Only the part of the message after the last NL is considered as
12882 * prompt for the command line */
12883 p = vim_strrchr(prompt, '\n');
12884 if (p == NULL)
12885 p = prompt;
12886 else
12887 {
12888 ++p;
12889 c = *p;
12890 *p = NUL;
12891 msg_start();
12892 msg_clr_eos();
12893 msg_puts_attr(prompt, echo_attr);
12894 msg_didout = FALSE;
12895 msg_starthere();
12896 *p = c;
12897 }
12898 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012900 if (argvars[1].v_type != VAR_UNKNOWN)
12901 {
12902 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12903 if (defstr != NULL)
12904 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012906 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012907 {
12908 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012909 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012910 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012911
Bram Moolenaar4463f292005-09-25 22:20:24 +000012912 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012913
Bram Moolenaar4463f292005-09-25 22:20:24 +000012914 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12915 if (xp_name == NULL)
12916 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012917
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012918 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012919
Bram Moolenaar4463f292005-09-25 22:20:24 +000012920 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12921 &xp_arg) == FAIL)
12922 return;
12923 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012924 }
12925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012926 if (defstr != NULL)
12927 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012928 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12929 xp_type, xp_arg);
12930
12931 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012933 /* since the user typed this, no need to wait for return */
12934 need_wait_return = FALSE;
12935 msg_didout = FALSE;
12936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937 cmd_silent = cmd_silent_save;
12938}
12939
12940/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012941 * "input()" function
12942 * Also handles inputsecret() when inputsecret is set.
12943 */
12944 static void
12945f_input(argvars, rettv)
12946 typval_T *argvars;
12947 typval_T *rettv;
12948{
12949 get_user_input(argvars, rettv, FALSE);
12950}
12951
12952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012953 * "inputdialog()" function
12954 */
12955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012956f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012957 typval_T *argvars;
12958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959{
12960#if defined(FEAT_GUI_TEXTDIALOG)
12961 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12962 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12963 {
12964 char_u *message;
12965 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012966 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012968 message = get_tv_string_chk(&argvars[0]);
12969 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012970 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012971 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012972 else
12973 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012974 if (message != NULL && defstr != NULL
12975 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010012976 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012977 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012978 else
12979 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012980 if (message != NULL && defstr != NULL
12981 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012982 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012983 rettv->vval.v_string = vim_strsave(
12984 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012986 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012988 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989 }
12990 else
12991#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012992 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993}
12994
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012995/*
12996 * "inputlist()" function
12997 */
12998 static void
12999f_inputlist(argvars, rettv)
13000 typval_T *argvars;
13001 typval_T *rettv;
13002{
13003 listitem_T *li;
13004 int selected;
13005 int mouse_used;
13006
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013007#ifdef NO_CONSOLE_INPUT
13008 /* While starting up, there is no place to enter text. */
13009 if (no_console_input())
13010 return;
13011#endif
13012 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13013 {
13014 EMSG2(_(e_listarg), "inputlist()");
13015 return;
13016 }
13017
13018 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013019 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013020 lines_left = Rows; /* avoid more prompt */
13021 msg_scroll = TRUE;
13022 msg_clr_eos();
13023
13024 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13025 {
13026 msg_puts(get_tv_string(&li->li_tv));
13027 msg_putchar('\n');
13028 }
13029
13030 /* Ask for choice. */
13031 selected = prompt_for_number(&mouse_used);
13032 if (mouse_used)
13033 selected -= lines_left;
13034
13035 rettv->vval.v_number = selected;
13036}
13037
13038
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13040
13041/*
13042 * "inputrestore()" function
13043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013045f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013046 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048{
13049 if (ga_userinput.ga_len > 0)
13050 {
13051 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013052 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13053 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013054 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055 }
13056 else if (p_verbose > 1)
13057 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013058 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013059 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060 }
13061}
13062
13063/*
13064 * "inputsave()" function
13065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013067f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013068 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013071 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013072 if (ga_grow(&ga_userinput, 1) == OK)
13073 {
13074 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13075 + ga_userinput.ga_len);
13076 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013077 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078 }
13079 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013080 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081}
13082
13083/*
13084 * "inputsecret()" function
13085 */
13086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013087f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013088 typval_T *argvars;
13089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090{
13091 ++cmdline_star;
13092 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013093 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094 --cmdline_star;
13095 --inputsecret_flag;
13096}
13097
13098/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013099 * "insert()" function
13100 */
13101 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013102f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013103 typval_T *argvars;
13104 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013105{
13106 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013107 listitem_T *item;
13108 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013109 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013110
13111 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013112 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013113 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013114 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013115 {
13116 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013117 before = get_tv_number_chk(&argvars[2], &error);
13118 if (error)
13119 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013120
Bram Moolenaar758711c2005-02-02 23:11:38 +000013121 if (before == l->lv_len)
13122 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013123 else
13124 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013125 item = list_find(l, before);
13126 if (item == NULL)
13127 {
13128 EMSGN(_(e_listidx), before);
13129 l = NULL;
13130 }
13131 }
13132 if (l != NULL)
13133 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013134 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013135 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013136 }
13137 }
13138}
13139
13140/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013141 * "invert(expr)" function
13142 */
13143 static void
13144f_invert(argvars, rettv)
13145 typval_T *argvars;
13146 typval_T *rettv;
13147{
13148 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13149}
13150
13151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152 * "isdirectory()" function
13153 */
13154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013155f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013156 typval_T *argvars;
13157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013159 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160}
13161
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013162/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013163 * "islocked()" function
13164 */
13165 static void
13166f_islocked(argvars, rettv)
13167 typval_T *argvars;
13168 typval_T *rettv;
13169{
13170 lval_T lv;
13171 char_u *end;
13172 dictitem_T *di;
13173
13174 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013175 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13176 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013177 if (end != NULL && lv.ll_name != NULL)
13178 {
13179 if (*end != NUL)
13180 EMSG(_(e_trailing));
13181 else
13182 {
13183 if (lv.ll_tv == NULL)
13184 {
13185 if (check_changedtick(lv.ll_name))
13186 rettv->vval.v_number = 1; /* always locked */
13187 else
13188 {
13189 di = find_var(lv.ll_name, NULL);
13190 if (di != NULL)
13191 {
13192 /* Consider a variable locked when:
13193 * 1. the variable itself is locked
13194 * 2. the value of the variable is locked.
13195 * 3. the List or Dict value is locked.
13196 */
13197 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13198 || tv_islocked(&di->di_tv));
13199 }
13200 }
13201 }
13202 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013203 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013204 else if (lv.ll_newkey != NULL)
13205 EMSG2(_(e_dictkey), lv.ll_newkey);
13206 else if (lv.ll_list != NULL)
13207 /* List item. */
13208 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13209 else
13210 /* Dictionary item. */
13211 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13212 }
13213 }
13214
13215 clear_lval(&lv);
13216}
13217
Bram Moolenaar33570922005-01-25 22:26:29 +000013218static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013219
13220/*
13221 * Turn a dict into a list:
13222 * "what" == 0: list of keys
13223 * "what" == 1: list of values
13224 * "what" == 2: list of items
13225 */
13226 static void
13227dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013228 typval_T *argvars;
13229 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013230 int what;
13231{
Bram Moolenaar33570922005-01-25 22:26:29 +000013232 list_T *l2;
13233 dictitem_T *di;
13234 hashitem_T *hi;
13235 listitem_T *li;
13236 listitem_T *li2;
13237 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013238 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013239
Bram Moolenaar8c711452005-01-14 21:53:12 +000013240 if (argvars[0].v_type != VAR_DICT)
13241 {
13242 EMSG(_(e_dictreq));
13243 return;
13244 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013245 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013246 return;
13247
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013248 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013249 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013250
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013251 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013252 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013253 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013254 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013255 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013256 --todo;
13257 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013258
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013259 li = listitem_alloc();
13260 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013261 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013262 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013263
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013264 if (what == 0)
13265 {
13266 /* keys() */
13267 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013268 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013269 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13270 }
13271 else if (what == 1)
13272 {
13273 /* values() */
13274 copy_tv(&di->di_tv, &li->li_tv);
13275 }
13276 else
13277 {
13278 /* items() */
13279 l2 = list_alloc();
13280 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013281 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013282 li->li_tv.vval.v_list = l2;
13283 if (l2 == NULL)
13284 break;
13285 ++l2->lv_refcount;
13286
13287 li2 = listitem_alloc();
13288 if (li2 == NULL)
13289 break;
13290 list_append(l2, li2);
13291 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013292 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013293 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13294
13295 li2 = listitem_alloc();
13296 if (li2 == NULL)
13297 break;
13298 list_append(l2, li2);
13299 copy_tv(&di->di_tv, &li2->li_tv);
13300 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013301 }
13302 }
13303}
13304
13305/*
13306 * "items(dict)" function
13307 */
13308 static void
13309f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013310 typval_T *argvars;
13311 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013312{
13313 dict_list(argvars, rettv, 2);
13314}
13315
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013317 * "join()" function
13318 */
13319 static void
13320f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013321 typval_T *argvars;
13322 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013323{
13324 garray_T ga;
13325 char_u *sep;
13326
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013327 if (argvars[0].v_type != VAR_LIST)
13328 {
13329 EMSG(_(e_listreq));
13330 return;
13331 }
13332 if (argvars[0].vval.v_list == NULL)
13333 return;
13334 if (argvars[1].v_type == VAR_UNKNOWN)
13335 sep = (char_u *)" ";
13336 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013337 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013338
13339 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013340
13341 if (sep != NULL)
13342 {
13343 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013344 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013345 ga_append(&ga, NUL);
13346 rettv->vval.v_string = (char_u *)ga.ga_data;
13347 }
13348 else
13349 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013350}
13351
13352/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013353 * "keys()" function
13354 */
13355 static void
13356f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013357 typval_T *argvars;
13358 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013359{
13360 dict_list(argvars, rettv, 0);
13361}
13362
13363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364 * "last_buffer_nr()" function.
13365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013367f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013368 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013369 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370{
13371 int n = 0;
13372 buf_T *buf;
13373
13374 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13375 if (n < buf->b_fnum)
13376 n = buf->b_fnum;
13377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013378 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013379}
13380
13381/*
13382 * "len()" function
13383 */
13384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013385f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013386 typval_T *argvars;
13387 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013388{
13389 switch (argvars[0].v_type)
13390 {
13391 case VAR_STRING:
13392 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013393 rettv->vval.v_number = (varnumber_T)STRLEN(
13394 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013395 break;
13396 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013397 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013398 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013399 case VAR_DICT:
13400 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13401 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013402 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013403 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013404 break;
13405 }
13406}
13407
Bram Moolenaar33570922005-01-25 22:26:29 +000013408static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013409
13410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013411libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013412 typval_T *argvars;
13413 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013414 int type;
13415{
13416#ifdef FEAT_LIBCALL
13417 char_u *string_in;
13418 char_u **string_result;
13419 int nr_result;
13420#endif
13421
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013422 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013423 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013424 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013425
13426 if (check_restricted() || check_secure())
13427 return;
13428
13429#ifdef FEAT_LIBCALL
13430 /* The first two args must be strings, otherwise its meaningless */
13431 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13432 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013433 string_in = NULL;
13434 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013435 string_in = argvars[2].vval.v_string;
13436 if (type == VAR_NUMBER)
13437 string_result = NULL;
13438 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013439 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013440 if (mch_libcall(argvars[0].vval.v_string,
13441 argvars[1].vval.v_string,
13442 string_in,
13443 argvars[2].vval.v_number,
13444 string_result,
13445 &nr_result) == OK
13446 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013447 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013448 }
13449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450}
13451
13452/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013453 * "libcall()" function
13454 */
13455 static void
13456f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013457 typval_T *argvars;
13458 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013459{
13460 libcall_common(argvars, rettv, VAR_STRING);
13461}
13462
13463/*
13464 * "libcallnr()" function
13465 */
13466 static void
13467f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013468 typval_T *argvars;
13469 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013470{
13471 libcall_common(argvars, rettv, VAR_NUMBER);
13472}
13473
13474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475 * "line(string)" function
13476 */
13477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013478f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013479 typval_T *argvars;
13480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481{
13482 linenr_T lnum = 0;
13483 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013484 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013486 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487 if (fp != NULL)
13488 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013489 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490}
13491
13492/*
13493 * "line2byte(lnum)" function
13494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013496f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013497 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499{
13500#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013501 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502#else
13503 linenr_T lnum;
13504
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013505 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013507 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013508 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013509 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13510 if (rettv->vval.v_number >= 0)
13511 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512#endif
13513}
13514
13515/*
13516 * "lispindent(lnum)" function
13517 */
13518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013519f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013520 typval_T *argvars;
13521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522{
13523#ifdef FEAT_LISP
13524 pos_T pos;
13525 linenr_T lnum;
13526
13527 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013528 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13530 {
13531 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013532 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533 curwin->w_cursor = pos;
13534 }
13535 else
13536#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013537 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538}
13539
13540/*
13541 * "localtime()" function
13542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013544f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013545 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013548 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549}
13550
Bram Moolenaar33570922005-01-25 22:26:29 +000013551static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552
13553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013554get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013555 typval_T *argvars;
13556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557 int exact;
13558{
13559 char_u *keys;
13560 char_u *which;
13561 char_u buf[NUMBUFLEN];
13562 char_u *keys_buf = NULL;
13563 char_u *rhs;
13564 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013565 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013566 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013567 mapblock_T *mp;
13568 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569
13570 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013571 rettv->v_type = VAR_STRING;
13572 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013574 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575 if (*keys == NUL)
13576 return;
13577
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013578 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013580 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013581 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013582 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013583 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013584 if (argvars[3].v_type != VAR_UNKNOWN)
13585 get_dict = get_tv_number(&argvars[3]);
13586 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588 else
13589 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013590 if (which == NULL)
13591 return;
13592
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593 mode = get_map_mode(&which, 0);
13594
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013595 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013596 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013598
13599 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013601 /* Return a string. */
13602 if (rhs != NULL)
13603 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604
Bram Moolenaarbd743252010-10-20 21:23:33 +020013605 }
13606 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13607 {
13608 /* Return a dictionary. */
13609 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13610 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13611 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612
Bram Moolenaarbd743252010-10-20 21:23:33 +020013613 dict_add_nr_str(dict, "lhs", 0L, lhs);
13614 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13615 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13616 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13617 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13618 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13619 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13620 dict_add_nr_str(dict, "mode", 0L, mapmode);
13621
13622 vim_free(lhs);
13623 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624 }
13625}
13626
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013627#ifdef FEAT_FLOAT
13628/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013629 * "log()" function
13630 */
13631 static void
13632f_log(argvars, rettv)
13633 typval_T *argvars;
13634 typval_T *rettv;
13635{
13636 float_T f;
13637
13638 rettv->v_type = VAR_FLOAT;
13639 if (get_float_arg(argvars, &f) == OK)
13640 rettv->vval.v_float = log(f);
13641 else
13642 rettv->vval.v_float = 0.0;
13643}
13644
13645/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013646 * "log10()" function
13647 */
13648 static void
13649f_log10(argvars, rettv)
13650 typval_T *argvars;
13651 typval_T *rettv;
13652{
13653 float_T f;
13654
13655 rettv->v_type = VAR_FLOAT;
13656 if (get_float_arg(argvars, &f) == OK)
13657 rettv->vval.v_float = log10(f);
13658 else
13659 rettv->vval.v_float = 0.0;
13660}
13661#endif
13662
Bram Moolenaar1dced572012-04-05 16:54:08 +020013663#ifdef FEAT_LUA
13664/*
13665 * "luaeval()" function
13666 */
13667 static void
13668f_luaeval(argvars, rettv)
13669 typval_T *argvars;
13670 typval_T *rettv;
13671{
13672 char_u *str;
13673 char_u buf[NUMBUFLEN];
13674
13675 str = get_tv_string_buf(&argvars[0], buf);
13676 do_luaeval(str, argvars + 1, rettv);
13677}
13678#endif
13679
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013681 * "map()" function
13682 */
13683 static void
13684f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013685 typval_T *argvars;
13686 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013687{
13688 filter_map(argvars, rettv, TRUE);
13689}
13690
13691/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013692 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 */
13694 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013695f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013696 typval_T *argvars;
13697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013698{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013699 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700}
13701
13702/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013703 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 */
13705 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013706f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013707 typval_T *argvars;
13708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013710 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711}
13712
Bram Moolenaar33570922005-01-25 22:26:29 +000013713static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714
13715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013716find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013717 typval_T *argvars;
13718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719 int type;
13720{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013721 char_u *str = NULL;
13722 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723 char_u *pat;
13724 regmatch_T regmatch;
13725 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013726 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727 char_u *save_cpo;
13728 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013729 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013730 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013731 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013732 list_T *l = NULL;
13733 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013734 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013735 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736
13737 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13738 save_cpo = p_cpo;
13739 p_cpo = (char_u *)"";
13740
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013741 rettv->vval.v_number = -1;
13742 if (type == 3)
13743 {
13744 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013745 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013746 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013747 }
13748 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013750 rettv->v_type = VAR_STRING;
13751 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013754 if (argvars[0].v_type == VAR_LIST)
13755 {
13756 if ((l = argvars[0].vval.v_list) == NULL)
13757 goto theend;
13758 li = l->lv_first;
13759 }
13760 else
13761 expr = str = get_tv_string(&argvars[0]);
13762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013763 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13764 if (pat == NULL)
13765 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013766
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013767 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013769 int error = FALSE;
13770
13771 start = get_tv_number_chk(&argvars[2], &error);
13772 if (error)
13773 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013774 if (l != NULL)
13775 {
13776 li = list_find(l, start);
13777 if (li == NULL)
13778 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013779 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013780 }
13781 else
13782 {
13783 if (start < 0)
13784 start = 0;
13785 if (start > (long)STRLEN(str))
13786 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013787 /* When "count" argument is there ignore matches before "start",
13788 * otherwise skip part of the string. Differs when pattern is "^"
13789 * or "\<". */
13790 if (argvars[3].v_type != VAR_UNKNOWN)
13791 startcol = start;
13792 else
13793 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013794 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013795
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013796 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013797 nth = get_tv_number_chk(&argvars[3], &error);
13798 if (error)
13799 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800 }
13801
13802 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13803 if (regmatch.regprog != NULL)
13804 {
13805 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013806
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013807 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013808 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013809 if (l != NULL)
13810 {
13811 if (li == NULL)
13812 {
13813 match = FALSE;
13814 break;
13815 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013816 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013817 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013818 if (str == NULL)
13819 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013820 }
13821
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013822 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013823
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013824 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013825 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013826 if (l == NULL && !match)
13827 break;
13828
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013829 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013830 if (l != NULL)
13831 {
13832 li = li->li_next;
13833 ++idx;
13834 }
13835 else
13836 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013837#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013838 startcol = (colnr_T)(regmatch.startp[0]
13839 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013840#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013841 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013842#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013843 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013844 }
13845
13846 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013848 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013849 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013850 int i;
13851
13852 /* return list with matched string and submatches */
13853 for (i = 0; i < NSUBEXP; ++i)
13854 {
13855 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013856 {
13857 if (list_append_string(rettv->vval.v_list,
13858 (char_u *)"", 0) == FAIL)
13859 break;
13860 }
13861 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013862 regmatch.startp[i],
13863 (int)(regmatch.endp[i] - regmatch.startp[i]))
13864 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013865 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013866 }
13867 }
13868 else if (type == 2)
13869 {
13870 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013871 if (l != NULL)
13872 copy_tv(&li->li_tv, rettv);
13873 else
13874 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013876 }
13877 else if (l != NULL)
13878 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879 else
13880 {
13881 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013882 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883 (varnumber_T)(regmatch.startp[0] - str);
13884 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013885 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013887 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888 }
13889 }
13890 vim_free(regmatch.regprog);
13891 }
13892
13893theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013894 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895 p_cpo = save_cpo;
13896}
13897
13898/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013899 * "match()" function
13900 */
13901 static void
13902f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013903 typval_T *argvars;
13904 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013905{
13906 find_some_match(argvars, rettv, 1);
13907}
13908
13909/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013910 * "matchadd()" function
13911 */
13912 static void
13913f_matchadd(argvars, rettv)
13914 typval_T *argvars;
13915 typval_T *rettv;
13916{
13917#ifdef FEAT_SEARCH_EXTRA
13918 char_u buf[NUMBUFLEN];
13919 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13920 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13921 int prio = 10; /* default priority */
13922 int id = -1;
13923 int error = FALSE;
13924
13925 rettv->vval.v_number = -1;
13926
13927 if (grp == NULL || pat == NULL)
13928 return;
13929 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013930 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013931 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013932 if (argvars[3].v_type != VAR_UNKNOWN)
13933 id = get_tv_number_chk(&argvars[3], &error);
13934 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013935 if (error == TRUE)
13936 return;
13937 if (id >= 1 && id <= 3)
13938 {
13939 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13940 return;
13941 }
13942
13943 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13944#endif
13945}
13946
13947/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013948 * "matcharg()" function
13949 */
13950 static void
13951f_matcharg(argvars, rettv)
13952 typval_T *argvars;
13953 typval_T *rettv;
13954{
13955 if (rettv_list_alloc(rettv) == OK)
13956 {
13957#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013958 int id = get_tv_number(&argvars[0]);
13959 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013960
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013961 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013962 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013963 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13964 {
13965 list_append_string(rettv->vval.v_list,
13966 syn_id2name(m->hlg_id), -1);
13967 list_append_string(rettv->vval.v_list, m->pattern, -1);
13968 }
13969 else
13970 {
13971 list_append_string(rettv->vval.v_list, NUL, -1);
13972 list_append_string(rettv->vval.v_list, NUL, -1);
13973 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013974 }
13975#endif
13976 }
13977}
13978
13979/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013980 * "matchdelete()" function
13981 */
13982 static void
13983f_matchdelete(argvars, rettv)
13984 typval_T *argvars;
13985 typval_T *rettv;
13986{
13987#ifdef FEAT_SEARCH_EXTRA
13988 rettv->vval.v_number = match_delete(curwin,
13989 (int)get_tv_number(&argvars[0]), TRUE);
13990#endif
13991}
13992
13993/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013994 * "matchend()" function
13995 */
13996 static void
13997f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013998 typval_T *argvars;
13999 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014000{
14001 find_some_match(argvars, rettv, 0);
14002}
14003
14004/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014005 * "matchlist()" function
14006 */
14007 static void
14008f_matchlist(argvars, rettv)
14009 typval_T *argvars;
14010 typval_T *rettv;
14011{
14012 find_some_match(argvars, rettv, 3);
14013}
14014
14015/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014016 * "matchstr()" function
14017 */
14018 static void
14019f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014020 typval_T *argvars;
14021 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014022{
14023 find_some_match(argvars, rettv, 2);
14024}
14025
Bram Moolenaar33570922005-01-25 22:26:29 +000014026static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014027
14028 static void
14029max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014030 typval_T *argvars;
14031 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014032 int domax;
14033{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014034 long n = 0;
14035 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014036 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014037
14038 if (argvars[0].v_type == VAR_LIST)
14039 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014040 list_T *l;
14041 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014042
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014043 l = argvars[0].vval.v_list;
14044 if (l != NULL)
14045 {
14046 li = l->lv_first;
14047 if (li != NULL)
14048 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014049 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014050 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014051 {
14052 li = li->li_next;
14053 if (li == NULL)
14054 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014055 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014056 if (domax ? i > n : i < n)
14057 n = i;
14058 }
14059 }
14060 }
14061 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014062 else if (argvars[0].v_type == VAR_DICT)
14063 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014064 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014065 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014066 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014067 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014068
14069 d = argvars[0].vval.v_dict;
14070 if (d != NULL)
14071 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014072 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014073 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014074 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014075 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014076 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014077 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014078 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014079 if (first)
14080 {
14081 n = i;
14082 first = FALSE;
14083 }
14084 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014085 n = i;
14086 }
14087 }
14088 }
14089 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014090 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014091 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014092 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014093}
14094
14095/*
14096 * "max()" function
14097 */
14098 static void
14099f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014100 typval_T *argvars;
14101 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014102{
14103 max_min(argvars, rettv, TRUE);
14104}
14105
14106/*
14107 * "min()" function
14108 */
14109 static void
14110f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014111 typval_T *argvars;
14112 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014113{
14114 max_min(argvars, rettv, FALSE);
14115}
14116
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014117static int mkdir_recurse __ARGS((char_u *dir, int prot));
14118
14119/*
14120 * Create the directory in which "dir" is located, and higher levels when
14121 * needed.
14122 */
14123 static int
14124mkdir_recurse(dir, prot)
14125 char_u *dir;
14126 int prot;
14127{
14128 char_u *p;
14129 char_u *updir;
14130 int r = FAIL;
14131
14132 /* Get end of directory name in "dir".
14133 * We're done when it's "/" or "c:/". */
14134 p = gettail_sep(dir);
14135 if (p <= get_past_head(dir))
14136 return OK;
14137
14138 /* If the directory exists we're done. Otherwise: create it.*/
14139 updir = vim_strnsave(dir, (int)(p - dir));
14140 if (updir == NULL)
14141 return FAIL;
14142 if (mch_isdir(updir))
14143 r = OK;
14144 else if (mkdir_recurse(updir, prot) == OK)
14145 r = vim_mkdir_emsg(updir, prot);
14146 vim_free(updir);
14147 return r;
14148}
14149
14150#ifdef vim_mkdir
14151/*
14152 * "mkdir()" function
14153 */
14154 static void
14155f_mkdir(argvars, rettv)
14156 typval_T *argvars;
14157 typval_T *rettv;
14158{
14159 char_u *dir;
14160 char_u buf[NUMBUFLEN];
14161 int prot = 0755;
14162
14163 rettv->vval.v_number = FAIL;
14164 if (check_restricted() || check_secure())
14165 return;
14166
14167 dir = get_tv_string_buf(&argvars[0], buf);
14168 if (argvars[1].v_type != VAR_UNKNOWN)
14169 {
14170 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014171 prot = get_tv_number_chk(&argvars[2], NULL);
14172 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014173 mkdir_recurse(dir, prot);
14174 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014175 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014176}
14177#endif
14178
Bram Moolenaar0d660222005-01-07 21:51:51 +000014179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180 * "mode()" function
14181 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014183f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014184 typval_T *argvars;
14185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014187 char_u buf[3];
14188
14189 buf[1] = NUL;
14190 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191
14192#ifdef FEAT_VISUAL
14193 if (VIsual_active)
14194 {
14195 if (VIsual_select)
14196 buf[0] = VIsual_mode + 's' - 'v';
14197 else
14198 buf[0] = VIsual_mode;
14199 }
14200 else
14201#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014202 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14203 || State == CONFIRM)
14204 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014206 if (State == ASKMORE)
14207 buf[1] = 'm';
14208 else if (State == CONFIRM)
14209 buf[1] = '?';
14210 }
14211 else if (State == EXTERNCMD)
14212 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014213 else if (State & INSERT)
14214 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014215#ifdef FEAT_VREPLACE
14216 if (State & VREPLACE_FLAG)
14217 {
14218 buf[0] = 'R';
14219 buf[1] = 'v';
14220 }
14221 else
14222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223 if (State & REPLACE_FLAG)
14224 buf[0] = 'R';
14225 else
14226 buf[0] = 'i';
14227 }
14228 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014229 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014231 if (exmode_active)
14232 buf[1] = 'v';
14233 }
14234 else if (exmode_active)
14235 {
14236 buf[0] = 'c';
14237 buf[1] = 'e';
14238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014242 if (finish_op)
14243 buf[1] = 'o';
14244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014246 /* Clear out the minor mode when the argument is not a non-zero number or
14247 * non-empty string. */
14248 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014249 buf[1] = NUL;
14250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014251 rettv->vval.v_string = vim_strsave(buf);
14252 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014253}
14254
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014255#ifdef FEAT_MZSCHEME
14256/*
14257 * "mzeval()" function
14258 */
14259 static void
14260f_mzeval(argvars, rettv)
14261 typval_T *argvars;
14262 typval_T *rettv;
14263{
14264 char_u *str;
14265 char_u buf[NUMBUFLEN];
14266
14267 str = get_tv_string_buf(&argvars[0], buf);
14268 do_mzeval(str, rettv);
14269}
14270#endif
14271
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014273 * "nextnonblank()" function
14274 */
14275 static void
14276f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014277 typval_T *argvars;
14278 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014279{
14280 linenr_T lnum;
14281
14282 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14283 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014284 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014285 {
14286 lnum = 0;
14287 break;
14288 }
14289 if (*skipwhite(ml_get(lnum)) != NUL)
14290 break;
14291 }
14292 rettv->vval.v_number = lnum;
14293}
14294
14295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014296 * "nr2char()" function
14297 */
14298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014299f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014300 typval_T *argvars;
14301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014302{
14303 char_u buf[NUMBUFLEN];
14304
14305#ifdef FEAT_MBYTE
14306 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014307 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308 else
14309#endif
14310 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014311 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312 buf[1] = NUL;
14313 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014314 rettv->v_type = VAR_STRING;
14315 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014316}
14317
14318/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014319 * "or(expr, expr)" function
14320 */
14321 static void
14322f_or(argvars, rettv)
14323 typval_T *argvars;
14324 typval_T *rettv;
14325{
14326 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14327 | get_tv_number_chk(&argvars[1], NULL);
14328}
14329
14330/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014331 * "pathshorten()" function
14332 */
14333 static void
14334f_pathshorten(argvars, rettv)
14335 typval_T *argvars;
14336 typval_T *rettv;
14337{
14338 char_u *p;
14339
14340 rettv->v_type = VAR_STRING;
14341 p = get_tv_string_chk(&argvars[0]);
14342 if (p == NULL)
14343 rettv->vval.v_string = NULL;
14344 else
14345 {
14346 p = vim_strsave(p);
14347 rettv->vval.v_string = p;
14348 if (p != NULL)
14349 shorten_dir(p);
14350 }
14351}
14352
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014353#ifdef FEAT_FLOAT
14354/*
14355 * "pow()" function
14356 */
14357 static void
14358f_pow(argvars, rettv)
14359 typval_T *argvars;
14360 typval_T *rettv;
14361{
14362 float_T fx, fy;
14363
14364 rettv->v_type = VAR_FLOAT;
14365 if (get_float_arg(argvars, &fx) == OK
14366 && get_float_arg(&argvars[1], &fy) == OK)
14367 rettv->vval.v_float = pow(fx, fy);
14368 else
14369 rettv->vval.v_float = 0.0;
14370}
14371#endif
14372
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014373/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014374 * "prevnonblank()" function
14375 */
14376 static void
14377f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014378 typval_T *argvars;
14379 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014380{
14381 linenr_T lnum;
14382
14383 lnum = get_tv_lnum(argvars);
14384 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14385 lnum = 0;
14386 else
14387 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14388 --lnum;
14389 rettv->vval.v_number = lnum;
14390}
14391
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014392#ifdef HAVE_STDARG_H
14393/* This dummy va_list is here because:
14394 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14395 * - locally in the function results in a "used before set" warning
14396 * - using va_start() to initialize it gives "function with fixed args" error */
14397static va_list ap;
14398#endif
14399
Bram Moolenaar8c711452005-01-14 21:53:12 +000014400/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014401 * "printf()" function
14402 */
14403 static void
14404f_printf(argvars, rettv)
14405 typval_T *argvars;
14406 typval_T *rettv;
14407{
14408 rettv->v_type = VAR_STRING;
14409 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014410#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014411 {
14412 char_u buf[NUMBUFLEN];
14413 int len;
14414 char_u *s;
14415 int saved_did_emsg = did_emsg;
14416 char *fmt;
14417
14418 /* Get the required length, allocate the buffer and do it for real. */
14419 did_emsg = FALSE;
14420 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014421 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014422 if (!did_emsg)
14423 {
14424 s = alloc(len + 1);
14425 if (s != NULL)
14426 {
14427 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014428 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014429 }
14430 }
14431 did_emsg |= saved_did_emsg;
14432 }
14433#endif
14434}
14435
14436/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014437 * "pumvisible()" function
14438 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014439 static void
14440f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014441 typval_T *argvars UNUSED;
14442 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014443{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014444#ifdef FEAT_INS_EXPAND
14445 if (pum_visible())
14446 rettv->vval.v_number = 1;
14447#endif
14448}
14449
Bram Moolenaardb913952012-06-29 12:54:53 +020014450#ifdef FEAT_PYTHON3
14451/*
14452 * "py3eval()" function
14453 */
14454 static void
14455f_py3eval(argvars, rettv)
14456 typval_T *argvars;
14457 typval_T *rettv;
14458{
14459 char_u *str;
14460 char_u buf[NUMBUFLEN];
14461
14462 str = get_tv_string_buf(&argvars[0], buf);
14463 do_py3eval(str, rettv);
14464}
14465#endif
14466
14467#ifdef FEAT_PYTHON
14468/*
14469 * "pyeval()" function
14470 */
14471 static void
14472f_pyeval(argvars, rettv)
14473 typval_T *argvars;
14474 typval_T *rettv;
14475{
14476 char_u *str;
14477 char_u buf[NUMBUFLEN];
14478
14479 str = get_tv_string_buf(&argvars[0], buf);
14480 do_pyeval(str, rettv);
14481}
14482#endif
14483
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014484/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014485 * "range()" function
14486 */
14487 static void
14488f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014489 typval_T *argvars;
14490 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014491{
14492 long start;
14493 long end;
14494 long stride = 1;
14495 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014496 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014497
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014498 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014499 if (argvars[1].v_type == VAR_UNKNOWN)
14500 {
14501 end = start - 1;
14502 start = 0;
14503 }
14504 else
14505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014506 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014507 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014508 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014509 }
14510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014511 if (error)
14512 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014513 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014514 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014515 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014516 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014517 else
14518 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014519 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014520 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014521 if (list_append_number(rettv->vval.v_list,
14522 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014523 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014524 }
14525}
14526
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014527/*
14528 * "readfile()" function
14529 */
14530 static void
14531f_readfile(argvars, rettv)
14532 typval_T *argvars;
14533 typval_T *rettv;
14534{
14535 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014536 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014537 char_u *fname;
14538 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014539 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14540 int io_size = sizeof(buf);
14541 int readlen; /* size of last fread() */
14542 char_u *prev = NULL; /* previously read bytes, if any */
14543 long prevlen = 0; /* length of data in prev */
14544 long prevsize = 0; /* size of prev buffer */
14545 long maxline = MAXLNUM;
14546 long cnt = 0;
14547 char_u *p; /* position in buf */
14548 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014549
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014550 if (argvars[1].v_type != VAR_UNKNOWN)
14551 {
14552 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14553 binary = TRUE;
14554 if (argvars[2].v_type != VAR_UNKNOWN)
14555 maxline = get_tv_number(&argvars[2]);
14556 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014557
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014558 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014559 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014560
14561 /* Always open the file in binary mode, library functions have a mind of
14562 * their own about CR-LF conversion. */
14563 fname = get_tv_string(&argvars[0]);
14564 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14565 {
14566 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14567 return;
14568 }
14569
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014570 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014571 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014572 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014573
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014574 /* This for loop processes what was read, but is also entered at end
14575 * of file so that either:
14576 * - an incomplete line gets written
14577 * - a "binary" file gets an empty line at the end if it ends in a
14578 * newline. */
14579 for (p = buf, start = buf;
14580 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14581 ++p)
14582 {
14583 if (*p == '\n' || readlen <= 0)
14584 {
14585 listitem_T *li;
14586 char_u *s = NULL;
14587 long_u len = p - start;
14588
14589 /* Finished a line. Remove CRs before NL. */
14590 if (readlen > 0 && !binary)
14591 {
14592 while (len > 0 && start[len - 1] == '\r')
14593 --len;
14594 /* removal may cross back to the "prev" string */
14595 if (len == 0)
14596 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14597 --prevlen;
14598 }
14599 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014600 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014601 else
14602 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014603 /* Change "prev" buffer to be the right size. This way
14604 * the bytes are only copied once, and very long lines are
14605 * allocated only once. */
14606 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014607 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014608 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014609 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014610 prev = NULL; /* the list will own the string */
14611 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014612 }
14613 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014614 if (s == NULL)
14615 {
14616 do_outofmem_msg((long_u) prevlen + len + 1);
14617 failed = TRUE;
14618 break;
14619 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014620
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014621 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014622 {
14623 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014624 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014625 break;
14626 }
14627 li->li_tv.v_type = VAR_STRING;
14628 li->li_tv.v_lock = 0;
14629 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014630 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014631
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014632 start = p + 1; /* step over newline */
14633 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014634 break;
14635 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014636 else if (*p == NUL)
14637 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014638#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014639 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14640 * when finding the BF and check the previous two bytes. */
14641 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014642 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014643 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14644 * + 1, these may be in the "prev" string. */
14645 char_u back1 = p >= buf + 1 ? p[-1]
14646 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14647 char_u back2 = p >= buf + 2 ? p[-2]
14648 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14649 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014650
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014651 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014652 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014653 char_u *dest = p - 2;
14654
14655 /* Usually a BOM is at the beginning of a file, and so at
14656 * the beginning of a line; then we can just step over it.
14657 */
14658 if (start == dest)
14659 start = p + 1;
14660 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014661 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014662 /* have to shuffle buf to close gap */
14663 int adjust_prevlen = 0;
14664
14665 if (dest < buf)
14666 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014667 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014668 dest = buf;
14669 }
14670 if (readlen > p - buf + 1)
14671 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14672 readlen -= 3 - adjust_prevlen;
14673 prevlen -= adjust_prevlen;
14674 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014675 }
14676 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014677 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014678#endif
14679 } /* for */
14680
14681 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14682 break;
14683 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014684 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014685 /* There's part of a line in buf, store it in "prev". */
14686 if (p - start + prevlen >= prevsize)
14687 {
14688 /* need bigger "prev" buffer */
14689 char_u *newprev;
14690
14691 /* A common use case is ordinary text files and "prev" gets a
14692 * fragment of a line, so the first allocation is made
14693 * small, to avoid repeatedly 'allocing' large and
14694 * 'reallocing' small. */
14695 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014696 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014697 else
14698 {
14699 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014700 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014701 prevsize = grow50pc > growmin ? grow50pc : growmin;
14702 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014703 newprev = prev == NULL ? alloc(prevsize)
14704 : vim_realloc(prev, prevsize);
14705 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014706 {
14707 do_outofmem_msg((long_u)prevsize);
14708 failed = TRUE;
14709 break;
14710 }
14711 prev = newprev;
14712 }
14713 /* Add the line part to end of "prev". */
14714 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014715 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014716 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014717 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014718
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014719 /*
14720 * For a negative line count use only the lines at the end of the file,
14721 * free the rest.
14722 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014723 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014724 while (cnt > -maxline)
14725 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014726 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014727 --cnt;
14728 }
14729
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014730 if (failed)
14731 {
14732 list_free(rettv->vval.v_list, TRUE);
14733 /* readfile doc says an empty list is returned on error */
14734 rettv->vval.v_list = list_alloc();
14735 }
14736
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014737 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014738 fclose(fd);
14739}
14740
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014741#if defined(FEAT_RELTIME)
14742static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14743
14744/*
14745 * Convert a List to proftime_T.
14746 * Return FAIL when there is something wrong.
14747 */
14748 static int
14749list2proftime(arg, tm)
14750 typval_T *arg;
14751 proftime_T *tm;
14752{
14753 long n1, n2;
14754 int error = FALSE;
14755
14756 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14757 || arg->vval.v_list->lv_len != 2)
14758 return FAIL;
14759 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14760 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14761# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014762 tm->HighPart = n1;
14763 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014764# else
14765 tm->tv_sec = n1;
14766 tm->tv_usec = n2;
14767# endif
14768 return error ? FAIL : OK;
14769}
14770#endif /* FEAT_RELTIME */
14771
14772/*
14773 * "reltime()" function
14774 */
14775 static void
14776f_reltime(argvars, rettv)
14777 typval_T *argvars;
14778 typval_T *rettv;
14779{
14780#ifdef FEAT_RELTIME
14781 proftime_T res;
14782 proftime_T start;
14783
14784 if (argvars[0].v_type == VAR_UNKNOWN)
14785 {
14786 /* No arguments: get current time. */
14787 profile_start(&res);
14788 }
14789 else if (argvars[1].v_type == VAR_UNKNOWN)
14790 {
14791 if (list2proftime(&argvars[0], &res) == FAIL)
14792 return;
14793 profile_end(&res);
14794 }
14795 else
14796 {
14797 /* Two arguments: compute the difference. */
14798 if (list2proftime(&argvars[0], &start) == FAIL
14799 || list2proftime(&argvars[1], &res) == FAIL)
14800 return;
14801 profile_sub(&res, &start);
14802 }
14803
14804 if (rettv_list_alloc(rettv) == OK)
14805 {
14806 long n1, n2;
14807
14808# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014809 n1 = res.HighPart;
14810 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014811# else
14812 n1 = res.tv_sec;
14813 n2 = res.tv_usec;
14814# endif
14815 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14816 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14817 }
14818#endif
14819}
14820
14821/*
14822 * "reltimestr()" function
14823 */
14824 static void
14825f_reltimestr(argvars, rettv)
14826 typval_T *argvars;
14827 typval_T *rettv;
14828{
14829#ifdef FEAT_RELTIME
14830 proftime_T tm;
14831#endif
14832
14833 rettv->v_type = VAR_STRING;
14834 rettv->vval.v_string = NULL;
14835#ifdef FEAT_RELTIME
14836 if (list2proftime(&argvars[0], &tm) == OK)
14837 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14838#endif
14839}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014840
Bram Moolenaar0d660222005-01-07 21:51:51 +000014841#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14842static void make_connection __ARGS((void));
14843static int check_connection __ARGS((void));
14844
14845 static void
14846make_connection()
14847{
14848 if (X_DISPLAY == NULL
14849# ifdef FEAT_GUI
14850 && !gui.in_use
14851# endif
14852 )
14853 {
14854 x_force_connect = TRUE;
14855 setup_term_clip();
14856 x_force_connect = FALSE;
14857 }
14858}
14859
14860 static int
14861check_connection()
14862{
14863 make_connection();
14864 if (X_DISPLAY == NULL)
14865 {
14866 EMSG(_("E240: No connection to Vim server"));
14867 return FAIL;
14868 }
14869 return OK;
14870}
14871#endif
14872
14873#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014874static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014875
14876 static void
14877remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014878 typval_T *argvars;
14879 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014880 int expr;
14881{
14882 char_u *server_name;
14883 char_u *keys;
14884 char_u *r = NULL;
14885 char_u buf[NUMBUFLEN];
14886# ifdef WIN32
14887 HWND w;
14888# else
14889 Window w;
14890# endif
14891
14892 if (check_restricted() || check_secure())
14893 return;
14894
14895# ifdef FEAT_X11
14896 if (check_connection() == FAIL)
14897 return;
14898# endif
14899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014900 server_name = get_tv_string_chk(&argvars[0]);
14901 if (server_name == NULL)
14902 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014903 keys = get_tv_string_buf(&argvars[1], buf);
14904# ifdef WIN32
14905 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14906# else
14907 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14908 < 0)
14909# endif
14910 {
14911 if (r != NULL)
14912 EMSG(r); /* sending worked but evaluation failed */
14913 else
14914 EMSG2(_("E241: Unable to send to %s"), server_name);
14915 return;
14916 }
14917
14918 rettv->vval.v_string = r;
14919
14920 if (argvars[2].v_type != VAR_UNKNOWN)
14921 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014922 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014923 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014924 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014925
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014926 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014927 v.di_tv.v_type = VAR_STRING;
14928 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014929 idvar = get_tv_string_chk(&argvars[2]);
14930 if (idvar != NULL)
14931 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014932 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014933 }
14934}
14935#endif
14936
14937/*
14938 * "remote_expr()" function
14939 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014940 static void
14941f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014942 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014943 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014944{
14945 rettv->v_type = VAR_STRING;
14946 rettv->vval.v_string = NULL;
14947#ifdef FEAT_CLIENTSERVER
14948 remote_common(argvars, rettv, TRUE);
14949#endif
14950}
14951
14952/*
14953 * "remote_foreground()" function
14954 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014955 static void
14956f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014957 typval_T *argvars UNUSED;
14958 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014959{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014960#ifdef FEAT_CLIENTSERVER
14961# ifdef WIN32
14962 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014963 {
14964 char_u *server_name = get_tv_string_chk(&argvars[0]);
14965
14966 if (server_name != NULL)
14967 serverForeground(server_name);
14968 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014969# else
14970 /* Send a foreground() expression to the server. */
14971 argvars[1].v_type = VAR_STRING;
14972 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14973 argvars[2].v_type = VAR_UNKNOWN;
14974 remote_common(argvars, rettv, TRUE);
14975 vim_free(argvars[1].vval.v_string);
14976# endif
14977#endif
14978}
14979
Bram Moolenaar0d660222005-01-07 21:51:51 +000014980 static void
14981f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014982 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014983 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014984{
14985#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014986 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014987 char_u *s = NULL;
14988# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014989 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014990# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014991 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014992
14993 if (check_restricted() || check_secure())
14994 {
14995 rettv->vval.v_number = -1;
14996 return;
14997 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014998 serverid = get_tv_string_chk(&argvars[0]);
14999 if (serverid == NULL)
15000 {
15001 rettv->vval.v_number = -1;
15002 return; /* type error; errmsg already given */
15003 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015004# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015005 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015006 if (n == 0)
15007 rettv->vval.v_number = -1;
15008 else
15009 {
15010 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15011 rettv->vval.v_number = (s != NULL);
15012 }
15013# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015014 if (check_connection() == FAIL)
15015 return;
15016
15017 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015018 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015019# endif
15020
15021 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015023 char_u *retvar;
15024
Bram Moolenaar33570922005-01-25 22:26:29 +000015025 v.di_tv.v_type = VAR_STRING;
15026 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015027 retvar = get_tv_string_chk(&argvars[1]);
15028 if (retvar != NULL)
15029 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015030 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015031 }
15032#else
15033 rettv->vval.v_number = -1;
15034#endif
15035}
15036
Bram Moolenaar0d660222005-01-07 21:51:51 +000015037 static void
15038f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015039 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015040 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015041{
15042 char_u *r = NULL;
15043
15044#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015045 char_u *serverid = get_tv_string_chk(&argvars[0]);
15046
15047 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015048 {
15049# ifdef WIN32
15050 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015051 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015052
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015053 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015054 if (n != 0)
15055 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15056 if (r == NULL)
15057# else
15058 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015059 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015060# endif
15061 EMSG(_("E277: Unable to read a server reply"));
15062 }
15063#endif
15064 rettv->v_type = VAR_STRING;
15065 rettv->vval.v_string = r;
15066}
15067
15068/*
15069 * "remote_send()" function
15070 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015071 static void
15072f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015073 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015074 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015075{
15076 rettv->v_type = VAR_STRING;
15077 rettv->vval.v_string = NULL;
15078#ifdef FEAT_CLIENTSERVER
15079 remote_common(argvars, rettv, FALSE);
15080#endif
15081}
15082
15083/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015084 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015085 */
15086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015087f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015088 typval_T *argvars;
15089 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015090{
Bram Moolenaar33570922005-01-25 22:26:29 +000015091 list_T *l;
15092 listitem_T *item, *item2;
15093 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015094 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015095 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015096 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015097 dict_T *d;
15098 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015099 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015100
Bram Moolenaar8c711452005-01-14 21:53:12 +000015101 if (argvars[0].v_type == VAR_DICT)
15102 {
15103 if (argvars[2].v_type != VAR_UNKNOWN)
15104 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015105 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015106 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015107 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015108 key = get_tv_string_chk(&argvars[1]);
15109 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015110 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015111 di = dict_find(d, key, -1);
15112 if (di == NULL)
15113 EMSG2(_(e_dictkey), key);
15114 else
15115 {
15116 *rettv = di->di_tv;
15117 init_tv(&di->di_tv);
15118 dictitem_remove(d, di);
15119 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015120 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015121 }
15122 }
15123 else if (argvars[0].v_type != VAR_LIST)
15124 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015125 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015126 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015127 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015128 int error = FALSE;
15129
15130 idx = get_tv_number_chk(&argvars[1], &error);
15131 if (error)
15132 ; /* type error: do nothing, errmsg already given */
15133 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015134 EMSGN(_(e_listidx), idx);
15135 else
15136 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015137 if (argvars[2].v_type == VAR_UNKNOWN)
15138 {
15139 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015140 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015141 *rettv = item->li_tv;
15142 vim_free(item);
15143 }
15144 else
15145 {
15146 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015147 end = get_tv_number_chk(&argvars[2], &error);
15148 if (error)
15149 ; /* type error: do nothing */
15150 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015151 EMSGN(_(e_listidx), end);
15152 else
15153 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015154 int cnt = 0;
15155
15156 for (li = item; li != NULL; li = li->li_next)
15157 {
15158 ++cnt;
15159 if (li == item2)
15160 break;
15161 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015162 if (li == NULL) /* didn't find "item2" after "item" */
15163 EMSG(_(e_invrange));
15164 else
15165 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015166 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015167 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015168 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015169 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015170 l->lv_first = item;
15171 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015172 item->li_prev = NULL;
15173 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015174 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015175 }
15176 }
15177 }
15178 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015179 }
15180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015181}
15182
15183/*
15184 * "rename({from}, {to})" function
15185 */
15186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015187f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015188 typval_T *argvars;
15189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190{
15191 char_u buf[NUMBUFLEN];
15192
15193 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015194 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015195 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015196 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15197 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015198}
15199
15200/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015201 * "repeat()" function
15202 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015203 static void
15204f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015205 typval_T *argvars;
15206 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015207{
15208 char_u *p;
15209 int n;
15210 int slen;
15211 int len;
15212 char_u *r;
15213 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015214
15215 n = get_tv_number(&argvars[1]);
15216 if (argvars[0].v_type == VAR_LIST)
15217 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015218 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015219 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015220 if (list_extend(rettv->vval.v_list,
15221 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015222 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015223 }
15224 else
15225 {
15226 p = get_tv_string(&argvars[0]);
15227 rettv->v_type = VAR_STRING;
15228 rettv->vval.v_string = NULL;
15229
15230 slen = (int)STRLEN(p);
15231 len = slen * n;
15232 if (len <= 0)
15233 return;
15234
15235 r = alloc(len + 1);
15236 if (r != NULL)
15237 {
15238 for (i = 0; i < n; i++)
15239 mch_memmove(r + i * slen, p, (size_t)slen);
15240 r[len] = NUL;
15241 }
15242
15243 rettv->vval.v_string = r;
15244 }
15245}
15246
15247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015248 * "resolve()" function
15249 */
15250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015251f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015252 typval_T *argvars;
15253 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015254{
15255 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015256#ifdef HAVE_READLINK
15257 char_u *buf = NULL;
15258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015260 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015261#ifdef FEAT_SHORTCUT
15262 {
15263 char_u *v = NULL;
15264
15265 v = mch_resolve_shortcut(p);
15266 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015267 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015268 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015269 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270 }
15271#else
15272# ifdef HAVE_READLINK
15273 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274 char_u *cpy;
15275 int len;
15276 char_u *remain = NULL;
15277 char_u *q;
15278 int is_relative_to_current = FALSE;
15279 int has_trailing_pathsep = FALSE;
15280 int limit = 100;
15281
15282 p = vim_strsave(p);
15283
15284 if (p[0] == '.' && (vim_ispathsep(p[1])
15285 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15286 is_relative_to_current = TRUE;
15287
15288 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015289 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015290 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015291 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015292 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015294
15295 q = getnextcomp(p);
15296 if (*q != NUL)
15297 {
15298 /* Separate the first path component in "p", and keep the
15299 * remainder (beginning with the path separator). */
15300 remain = vim_strsave(q - 1);
15301 q[-1] = NUL;
15302 }
15303
Bram Moolenaard9462e32011-04-11 21:35:11 +020015304 buf = alloc(MAXPATHL + 1);
15305 if (buf == NULL)
15306 goto fail;
15307
Bram Moolenaar071d4272004-06-13 20:20:40 +000015308 for (;;)
15309 {
15310 for (;;)
15311 {
15312 len = readlink((char *)p, (char *)buf, MAXPATHL);
15313 if (len <= 0)
15314 break;
15315 buf[len] = NUL;
15316
15317 if (limit-- == 0)
15318 {
15319 vim_free(p);
15320 vim_free(remain);
15321 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015322 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323 goto fail;
15324 }
15325
15326 /* Ensure that the result will have a trailing path separator
15327 * if the argument has one. */
15328 if (remain == NULL && has_trailing_pathsep)
15329 add_pathsep(buf);
15330
15331 /* Separate the first path component in the link value and
15332 * concatenate the remainders. */
15333 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15334 if (*q != NUL)
15335 {
15336 if (remain == NULL)
15337 remain = vim_strsave(q - 1);
15338 else
15339 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015340 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015341 if (cpy != NULL)
15342 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015343 vim_free(remain);
15344 remain = cpy;
15345 }
15346 }
15347 q[-1] = NUL;
15348 }
15349
15350 q = gettail(p);
15351 if (q > p && *q == NUL)
15352 {
15353 /* Ignore trailing path separator. */
15354 q[-1] = NUL;
15355 q = gettail(p);
15356 }
15357 if (q > p && !mch_isFullName(buf))
15358 {
15359 /* symlink is relative to directory of argument */
15360 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15361 if (cpy != NULL)
15362 {
15363 STRCPY(cpy, p);
15364 STRCPY(gettail(cpy), buf);
15365 vim_free(p);
15366 p = cpy;
15367 }
15368 }
15369 else
15370 {
15371 vim_free(p);
15372 p = vim_strsave(buf);
15373 }
15374 }
15375
15376 if (remain == NULL)
15377 break;
15378
15379 /* Append the first path component of "remain" to "p". */
15380 q = getnextcomp(remain + 1);
15381 len = q - remain - (*q != NUL);
15382 cpy = vim_strnsave(p, STRLEN(p) + len);
15383 if (cpy != NULL)
15384 {
15385 STRNCAT(cpy, remain, len);
15386 vim_free(p);
15387 p = cpy;
15388 }
15389 /* Shorten "remain". */
15390 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015391 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392 else
15393 {
15394 vim_free(remain);
15395 remain = NULL;
15396 }
15397 }
15398
15399 /* If the result is a relative path name, make it explicitly relative to
15400 * the current directory if and only if the argument had this form. */
15401 if (!vim_ispathsep(*p))
15402 {
15403 if (is_relative_to_current
15404 && *p != NUL
15405 && !(p[0] == '.'
15406 && (p[1] == NUL
15407 || vim_ispathsep(p[1])
15408 || (p[1] == '.'
15409 && (p[2] == NUL
15410 || vim_ispathsep(p[2]))))))
15411 {
15412 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015413 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414 if (cpy != NULL)
15415 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416 vim_free(p);
15417 p = cpy;
15418 }
15419 }
15420 else if (!is_relative_to_current)
15421 {
15422 /* Strip leading "./". */
15423 q = p;
15424 while (q[0] == '.' && vim_ispathsep(q[1]))
15425 q += 2;
15426 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015427 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015428 }
15429 }
15430
15431 /* Ensure that the result will have no trailing path separator
15432 * if the argument had none. But keep "/" or "//". */
15433 if (!has_trailing_pathsep)
15434 {
15435 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015436 if (after_pathsep(p, q))
15437 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438 }
15439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015440 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015441 }
15442# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015443 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444# endif
15445#endif
15446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015447 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448
15449#ifdef HAVE_READLINK
15450fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015451 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015452#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015453 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015454}
15455
15456/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015457 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458 */
15459 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015460f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015461 typval_T *argvars;
15462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015463{
Bram Moolenaar33570922005-01-25 22:26:29 +000015464 list_T *l;
15465 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466
Bram Moolenaar0d660222005-01-07 21:51:51 +000015467 if (argvars[0].v_type != VAR_LIST)
15468 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015469 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015470 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015471 {
15472 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015473 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015474 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015475 while (li != NULL)
15476 {
15477 ni = li->li_prev;
15478 list_append(l, li);
15479 li = ni;
15480 }
15481 rettv->vval.v_list = l;
15482 rettv->v_type = VAR_LIST;
15483 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015484 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015486}
15487
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015488#define SP_NOMOVE 0x01 /* don't move cursor */
15489#define SP_REPEAT 0x02 /* repeat to find outer pair */
15490#define SP_RETCOUNT 0x04 /* return matchcount */
15491#define SP_SETPCMARK 0x08 /* set previous context mark */
15492#define SP_START 0x10 /* accept match at start position */
15493#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15494#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015495
Bram Moolenaar33570922005-01-25 22:26:29 +000015496static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015497
15498/*
15499 * Get flags for a search function.
15500 * Possibly sets "p_ws".
15501 * Returns BACKWARD, FORWARD or zero (for an error).
15502 */
15503 static int
15504get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015505 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015506 int *flagsp;
15507{
15508 int dir = FORWARD;
15509 char_u *flags;
15510 char_u nbuf[NUMBUFLEN];
15511 int mask;
15512
15513 if (varp->v_type != VAR_UNKNOWN)
15514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015515 flags = get_tv_string_buf_chk(varp, nbuf);
15516 if (flags == NULL)
15517 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015518 while (*flags != NUL)
15519 {
15520 switch (*flags)
15521 {
15522 case 'b': dir = BACKWARD; break;
15523 case 'w': p_ws = TRUE; break;
15524 case 'W': p_ws = FALSE; break;
15525 default: mask = 0;
15526 if (flagsp != NULL)
15527 switch (*flags)
15528 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015529 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015530 case 'e': mask = SP_END; break;
15531 case 'm': mask = SP_RETCOUNT; break;
15532 case 'n': mask = SP_NOMOVE; break;
15533 case 'p': mask = SP_SUBPAT; break;
15534 case 'r': mask = SP_REPEAT; break;
15535 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015536 }
15537 if (mask == 0)
15538 {
15539 EMSG2(_(e_invarg2), flags);
15540 dir = 0;
15541 }
15542 else
15543 *flagsp |= mask;
15544 }
15545 if (dir == 0)
15546 break;
15547 ++flags;
15548 }
15549 }
15550 return dir;
15551}
15552
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015554 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015556 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015557search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015558 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015559 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015560 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015562 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 char_u *pat;
15564 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015565 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566 int save_p_ws = p_ws;
15567 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015568 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015569 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015570 proftime_T tm;
15571#ifdef FEAT_RELTIME
15572 long time_limit = 0;
15573#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015574 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015575 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015577 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015578 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015579 if (dir == 0)
15580 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015581 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015582 if (flags & SP_START)
15583 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015584 if (flags & SP_END)
15585 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015586
Bram Moolenaar76929292008-01-06 19:07:36 +000015587 /* Optional arguments: line number to stop searching and timeout. */
15588 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015589 {
15590 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15591 if (lnum_stop < 0)
15592 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015593#ifdef FEAT_RELTIME
15594 if (argvars[3].v_type != VAR_UNKNOWN)
15595 {
15596 time_limit = get_tv_number_chk(&argvars[3], NULL);
15597 if (time_limit < 0)
15598 goto theend;
15599 }
15600#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015601 }
15602
Bram Moolenaar76929292008-01-06 19:07:36 +000015603#ifdef FEAT_RELTIME
15604 /* Set the time limit, if there is one. */
15605 profile_setlimit(time_limit, &tm);
15606#endif
15607
Bram Moolenaar231334e2005-07-25 20:46:57 +000015608 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015609 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015610 * Check to make sure only those flags are set.
15611 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15612 * flags cannot be set. Check for that condition also.
15613 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015614 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015615 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015616 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015617 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015618 goto theend;
15619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015621 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015622 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015623 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015624 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015626 if (flags & SP_SUBPAT)
15627 retval = subpatnum;
15628 else
15629 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015630 if (flags & SP_SETPCMARK)
15631 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015633 if (match_pos != NULL)
15634 {
15635 /* Store the match cursor position */
15636 match_pos->lnum = pos.lnum;
15637 match_pos->col = pos.col + 1;
15638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639 /* "/$" will put the cursor after the end of the line, may need to
15640 * correct that here */
15641 check_cursor();
15642 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015643
15644 /* If 'n' flag is used: restore cursor position. */
15645 if (flags & SP_NOMOVE)
15646 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015647 else
15648 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015649theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015651
15652 return retval;
15653}
15654
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015655#ifdef FEAT_FLOAT
15656/*
15657 * "round({float})" function
15658 */
15659 static void
15660f_round(argvars, rettv)
15661 typval_T *argvars;
15662 typval_T *rettv;
15663{
15664 float_T f;
15665
15666 rettv->v_type = VAR_FLOAT;
15667 if (get_float_arg(argvars, &f) == OK)
15668 /* round() is not in C90, use ceil() or floor() instead. */
15669 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15670 else
15671 rettv->vval.v_float = 0.0;
15672}
15673#endif
15674
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015675/*
15676 * "search()" function
15677 */
15678 static void
15679f_search(argvars, rettv)
15680 typval_T *argvars;
15681 typval_T *rettv;
15682{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015683 int flags = 0;
15684
15685 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015686}
15687
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015689 * "searchdecl()" function
15690 */
15691 static void
15692f_searchdecl(argvars, rettv)
15693 typval_T *argvars;
15694 typval_T *rettv;
15695{
15696 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015697 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015698 int error = FALSE;
15699 char_u *name;
15700
15701 rettv->vval.v_number = 1; /* default: FAIL */
15702
15703 name = get_tv_string_chk(&argvars[0]);
15704 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015705 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015706 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015707 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15708 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15709 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015710 if (!error && name != NULL)
15711 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015712 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015713}
15714
15715/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015716 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015717 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015718 static int
15719searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015720 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015721 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722{
15723 char_u *spat, *mpat, *epat;
15724 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015726 int dir;
15727 int flags = 0;
15728 char_u nbuf1[NUMBUFLEN];
15729 char_u nbuf2[NUMBUFLEN];
15730 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015731 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015732 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015733 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015736 spat = get_tv_string_chk(&argvars[0]);
15737 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15738 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15739 if (spat == NULL || mpat == NULL || epat == NULL)
15740 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742 /* Handle the optional fourth argument: flags */
15743 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015744 if (dir == 0)
15745 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015746
15747 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015748 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15749 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015750 if ((flags & (SP_END | SP_SUBPAT)) != 0
15751 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015752 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015753 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015754 goto theend;
15755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015757 /* Using 'r' implies 'W', otherwise it doesn't work. */
15758 if (flags & SP_REPEAT)
15759 p_ws = FALSE;
15760
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015761 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015762 if (argvars[3].v_type == VAR_UNKNOWN
15763 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764 skip = (char_u *)"";
15765 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015766 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015767 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015768 if (argvars[5].v_type != VAR_UNKNOWN)
15769 {
15770 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15771 if (lnum_stop < 0)
15772 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015773#ifdef FEAT_RELTIME
15774 if (argvars[6].v_type != VAR_UNKNOWN)
15775 {
15776 time_limit = get_tv_number_chk(&argvars[6], NULL);
15777 if (time_limit < 0)
15778 goto theend;
15779 }
15780#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015781 }
15782 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015783 if (skip == NULL)
15784 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015786 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015787 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015788
15789theend:
15790 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015791
15792 return retval;
15793}
15794
15795/*
15796 * "searchpair()" function
15797 */
15798 static void
15799f_searchpair(argvars, rettv)
15800 typval_T *argvars;
15801 typval_T *rettv;
15802{
15803 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15804}
15805
15806/*
15807 * "searchpairpos()" function
15808 */
15809 static void
15810f_searchpairpos(argvars, rettv)
15811 typval_T *argvars;
15812 typval_T *rettv;
15813{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015814 pos_T match_pos;
15815 int lnum = 0;
15816 int col = 0;
15817
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015818 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015819 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015820
15821 if (searchpair_cmn(argvars, &match_pos) > 0)
15822 {
15823 lnum = match_pos.lnum;
15824 col = match_pos.col;
15825 }
15826
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015827 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15828 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015829}
15830
15831/*
15832 * Search for a start/middle/end thing.
15833 * Used by searchpair(), see its documentation for the details.
15834 * Returns 0 or -1 for no match,
15835 */
15836 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015837do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15838 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015839 char_u *spat; /* start pattern */
15840 char_u *mpat; /* middle pattern */
15841 char_u *epat; /* end pattern */
15842 int dir; /* BACKWARD or FORWARD */
15843 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015844 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015845 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015846 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015847 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015848{
15849 char_u *save_cpo;
15850 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15851 long retval = 0;
15852 pos_T pos;
15853 pos_T firstpos;
15854 pos_T foundpos;
15855 pos_T save_cursor;
15856 pos_T save_pos;
15857 int n;
15858 int r;
15859 int nest = 1;
15860 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015861 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015862 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015863
15864 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15865 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015866 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015867
Bram Moolenaar76929292008-01-06 19:07:36 +000015868#ifdef FEAT_RELTIME
15869 /* Set the time limit, if there is one. */
15870 profile_setlimit(time_limit, &tm);
15871#endif
15872
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015873 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15874 * start/middle/end (pat3, for the top pair). */
15875 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15876 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15877 if (pat2 == NULL || pat3 == NULL)
15878 goto theend;
15879 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15880 if (*mpat == NUL)
15881 STRCPY(pat3, pat2);
15882 else
15883 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15884 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015885 if (flags & SP_START)
15886 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015887
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888 save_cursor = curwin->w_cursor;
15889 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015890 clearpos(&firstpos);
15891 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015892 pat = pat3;
15893 for (;;)
15894 {
15895 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015896 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15898 /* didn't find it or found the first match again: FAIL */
15899 break;
15900
15901 if (firstpos.lnum == 0)
15902 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015903 if (equalpos(pos, foundpos))
15904 {
15905 /* Found the same position again. Can happen with a pattern that
15906 * has "\zs" at the end and searching backwards. Advance one
15907 * character and try again. */
15908 if (dir == BACKWARD)
15909 decl(&pos);
15910 else
15911 incl(&pos);
15912 }
15913 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015914
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015915 /* clear the start flag to avoid getting stuck here */
15916 options &= ~SEARCH_START;
15917
Bram Moolenaar071d4272004-06-13 20:20:40 +000015918 /* If the skip pattern matches, ignore this match. */
15919 if (*skip != NUL)
15920 {
15921 save_pos = curwin->w_cursor;
15922 curwin->w_cursor = pos;
15923 r = eval_to_bool(skip, &err, NULL, FALSE);
15924 curwin->w_cursor = save_pos;
15925 if (err)
15926 {
15927 /* Evaluating {skip} caused an error, break here. */
15928 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015929 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930 break;
15931 }
15932 if (r)
15933 continue;
15934 }
15935
15936 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15937 {
15938 /* Found end when searching backwards or start when searching
15939 * forward: nested pair. */
15940 ++nest;
15941 pat = pat2; /* nested, don't search for middle */
15942 }
15943 else
15944 {
15945 /* Found end when searching forward or start when searching
15946 * backward: end of (nested) pair; or found middle in outer pair. */
15947 if (--nest == 1)
15948 pat = pat3; /* outer level, search for middle */
15949 }
15950
15951 if (nest == 0)
15952 {
15953 /* Found the match: return matchcount or line number. */
15954 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015955 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015956 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015957 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015958 if (flags & SP_SETPCMARK)
15959 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015960 curwin->w_cursor = pos;
15961 if (!(flags & SP_REPEAT))
15962 break;
15963 nest = 1; /* search for next unmatched */
15964 }
15965 }
15966
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015967 if (match_pos != NULL)
15968 {
15969 /* Store the match cursor position */
15970 match_pos->lnum = curwin->w_cursor.lnum;
15971 match_pos->col = curwin->w_cursor.col + 1;
15972 }
15973
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015975 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015976 curwin->w_cursor = save_cursor;
15977
15978theend:
15979 vim_free(pat2);
15980 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015981 if (p_cpo == empty_option)
15982 p_cpo = save_cpo;
15983 else
15984 /* Darn, evaluating the {skip} expression changed the value. */
15985 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015986
15987 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015988}
15989
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015990/*
15991 * "searchpos()" function
15992 */
15993 static void
15994f_searchpos(argvars, rettv)
15995 typval_T *argvars;
15996 typval_T *rettv;
15997{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015998 pos_T match_pos;
15999 int lnum = 0;
16000 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016001 int n;
16002 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016003
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016004 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016005 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016006
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016007 n = search_cmn(argvars, &match_pos, &flags);
16008 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016009 {
16010 lnum = match_pos.lnum;
16011 col = match_pos.col;
16012 }
16013
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016014 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16015 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016016 if (flags & SP_SUBPAT)
16017 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016018}
16019
16020
Bram Moolenaar0d660222005-01-07 21:51:51 +000016021 static void
16022f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016023 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016025{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016026#ifdef FEAT_CLIENTSERVER
16027 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016028 char_u *server = get_tv_string_chk(&argvars[0]);
16029 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016030
Bram Moolenaar0d660222005-01-07 21:51:51 +000016031 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016032 if (server == NULL || reply == NULL)
16033 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016034 if (check_restricted() || check_secure())
16035 return;
16036# ifdef FEAT_X11
16037 if (check_connection() == FAIL)
16038 return;
16039# endif
16040
16041 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016043 EMSG(_("E258: Unable to send to client"));
16044 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016046 rettv->vval.v_number = 0;
16047#else
16048 rettv->vval.v_number = -1;
16049#endif
16050}
16051
Bram Moolenaar0d660222005-01-07 21:51:51 +000016052 static void
16053f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016054 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016055 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016056{
16057 char_u *r = NULL;
16058
16059#ifdef FEAT_CLIENTSERVER
16060# ifdef WIN32
16061 r = serverGetVimNames();
16062# else
16063 make_connection();
16064 if (X_DISPLAY != NULL)
16065 r = serverGetVimNames(X_DISPLAY);
16066# endif
16067#endif
16068 rettv->v_type = VAR_STRING;
16069 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016070}
16071
16072/*
16073 * "setbufvar()" function
16074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016076f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016077 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016078 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016079{
16080 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016083 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084 char_u nbuf[NUMBUFLEN];
16085
16086 if (check_restricted() || check_secure())
16087 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016088 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16089 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016090 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091 varp = &argvars[2];
16092
16093 if (buf != NULL && varname != NULL && varp != NULL)
16094 {
16095 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016096 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016097
16098 if (*varname == '&')
16099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016100 long numval;
16101 char_u *strval;
16102 int error = FALSE;
16103
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016105 numval = get_tv_number_chk(varp, &error);
16106 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016107 if (!error && strval != NULL)
16108 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109 }
16110 else
16111 {
16112 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16113 if (bufvarname != NULL)
16114 {
16115 STRCPY(bufvarname, "b:");
16116 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016117 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118 vim_free(bufvarname);
16119 }
16120 }
16121
16122 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125}
16126
16127/*
16128 * "setcmdpos()" function
16129 */
16130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016131f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016132 typval_T *argvars;
16133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016134{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016135 int pos = (int)get_tv_number(&argvars[0]) - 1;
16136
16137 if (pos >= 0)
16138 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016139}
16140
16141/*
16142 * "setline()" function
16143 */
16144 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016145f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016146 typval_T *argvars;
16147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148{
16149 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016150 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016151 list_T *l = NULL;
16152 listitem_T *li = NULL;
16153 long added = 0;
16154 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016155
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016156 lnum = get_tv_lnum(&argvars[0]);
16157 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016159 l = argvars[1].vval.v_list;
16160 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016162 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016163 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016164
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016165 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016166 for (;;)
16167 {
16168 if (l != NULL)
16169 {
16170 /* list argument, get next string */
16171 if (li == NULL)
16172 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016173 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016174 li = li->li_next;
16175 }
16176
16177 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016178 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016179 break;
16180 if (lnum <= curbuf->b_ml.ml_line_count)
16181 {
16182 /* existing line, replace it */
16183 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16184 {
16185 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016186 if (lnum == curwin->w_cursor.lnum)
16187 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016188 rettv->vval.v_number = 0; /* OK */
16189 }
16190 }
16191 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16192 {
16193 /* lnum is one past the last line, append the line */
16194 ++added;
16195 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16196 rettv->vval.v_number = 0; /* OK */
16197 }
16198
16199 if (l == NULL) /* only one string argument */
16200 break;
16201 ++lnum;
16202 }
16203
16204 if (added > 0)
16205 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206}
16207
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016208static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16209
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016211 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016212 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016213 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016214set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016215 win_T *wp UNUSED;
16216 typval_T *list_arg UNUSED;
16217 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016218 typval_T *rettv;
16219{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016220#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016221 char_u *act;
16222 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016223#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016224
Bram Moolenaar2641f772005-03-25 21:58:17 +000016225 rettv->vval.v_number = -1;
16226
16227#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016228 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016229 EMSG(_(e_listreq));
16230 else
16231 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016232 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016233
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016234 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016235 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016236 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016237 if (act == NULL)
16238 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016239 if (*act == 'a' || *act == 'r')
16240 action = *act;
16241 }
16242
Bram Moolenaarbc226b62010-08-09 22:14:48 +020016243 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016244 rettv->vval.v_number = 0;
16245 }
16246#endif
16247}
16248
16249/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016250 * "setloclist()" function
16251 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016252 static void
16253f_setloclist(argvars, rettv)
16254 typval_T *argvars;
16255 typval_T *rettv;
16256{
16257 win_T *win;
16258
16259 rettv->vval.v_number = -1;
16260
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016261 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016262 if (win != NULL)
16263 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16264}
16265
16266/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016267 * "setmatches()" function
16268 */
16269 static void
16270f_setmatches(argvars, rettv)
16271 typval_T *argvars;
16272 typval_T *rettv;
16273{
16274#ifdef FEAT_SEARCH_EXTRA
16275 list_T *l;
16276 listitem_T *li;
16277 dict_T *d;
16278
16279 rettv->vval.v_number = -1;
16280 if (argvars[0].v_type != VAR_LIST)
16281 {
16282 EMSG(_(e_listreq));
16283 return;
16284 }
16285 if ((l = argvars[0].vval.v_list) != NULL)
16286 {
16287
16288 /* To some extent make sure that we are dealing with a list from
16289 * "getmatches()". */
16290 li = l->lv_first;
16291 while (li != NULL)
16292 {
16293 if (li->li_tv.v_type != VAR_DICT
16294 || (d = li->li_tv.vval.v_dict) == NULL)
16295 {
16296 EMSG(_(e_invarg));
16297 return;
16298 }
16299 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16300 && dict_find(d, (char_u *)"pattern", -1) != NULL
16301 && dict_find(d, (char_u *)"priority", -1) != NULL
16302 && dict_find(d, (char_u *)"id", -1) != NULL))
16303 {
16304 EMSG(_(e_invarg));
16305 return;
16306 }
16307 li = li->li_next;
16308 }
16309
16310 clear_matches(curwin);
16311 li = l->lv_first;
16312 while (li != NULL)
16313 {
16314 d = li->li_tv.vval.v_dict;
16315 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16316 get_dict_string(d, (char_u *)"pattern", FALSE),
16317 (int)get_dict_number(d, (char_u *)"priority"),
16318 (int)get_dict_number(d, (char_u *)"id"));
16319 li = li->li_next;
16320 }
16321 rettv->vval.v_number = 0;
16322 }
16323#endif
16324}
16325
16326/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016327 * "setpos()" function
16328 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016329 static void
16330f_setpos(argvars, rettv)
16331 typval_T *argvars;
16332 typval_T *rettv;
16333{
16334 pos_T pos;
16335 int fnum;
16336 char_u *name;
16337
Bram Moolenaar08250432008-02-13 11:42:46 +000016338 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016339 name = get_tv_string_chk(argvars);
16340 if (name != NULL)
16341 {
16342 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16343 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016344 if (--pos.col < 0)
16345 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016346 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016347 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016348 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016349 if (fnum == curbuf->b_fnum)
16350 {
16351 curwin->w_cursor = pos;
16352 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016353 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016354 }
16355 else
16356 EMSG(_(e_invarg));
16357 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016358 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16359 {
16360 /* set mark */
16361 if (setmark_pos(name[1], &pos, fnum) == OK)
16362 rettv->vval.v_number = 0;
16363 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016364 else
16365 EMSG(_(e_invarg));
16366 }
16367 }
16368}
16369
16370/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016371 * "setqflist()" function
16372 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016373 static void
16374f_setqflist(argvars, rettv)
16375 typval_T *argvars;
16376 typval_T *rettv;
16377{
16378 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16379}
16380
16381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382 * "setreg()" function
16383 */
16384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016385f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016386 typval_T *argvars;
16387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388{
16389 int regname;
16390 char_u *strregname;
16391 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016392 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393 int append;
16394 char_u yank_type;
16395 long block_len;
16396
16397 block_len = -1;
16398 yank_type = MAUTO;
16399 append = FALSE;
16400
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016401 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016402 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016403
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016404 if (strregname == NULL)
16405 return; /* type error; errmsg already given */
16406 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016407 if (regname == 0 || regname == '@')
16408 regname = '"';
16409 else if (regname == '=')
16410 return;
16411
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016412 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016413 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016414 stropt = get_tv_string_chk(&argvars[2]);
16415 if (stropt == NULL)
16416 return; /* type error */
16417 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418 switch (*stropt)
16419 {
16420 case 'a': case 'A': /* append */
16421 append = TRUE;
16422 break;
16423 case 'v': case 'c': /* character-wise selection */
16424 yank_type = MCHAR;
16425 break;
16426 case 'V': case 'l': /* line-wise selection */
16427 yank_type = MLINE;
16428 break;
16429#ifdef FEAT_VISUAL
16430 case 'b': case Ctrl_V: /* block-wise selection */
16431 yank_type = MBLOCK;
16432 if (VIM_ISDIGIT(stropt[1]))
16433 {
16434 ++stropt;
16435 block_len = getdigits(&stropt) - 1;
16436 --stropt;
16437 }
16438 break;
16439#endif
16440 }
16441 }
16442
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016443 strval = get_tv_string_chk(&argvars[1]);
16444 if (strval != NULL)
16445 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016446 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016447 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448}
16449
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016450/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016451 * "settabvar()" function
16452 */
16453 static void
16454f_settabvar(argvars, rettv)
16455 typval_T *argvars;
16456 typval_T *rettv;
16457{
16458 tabpage_T *save_curtab;
16459 char_u *varname, *tabvarname;
16460 typval_T *varp;
16461 tabpage_T *tp;
16462
16463 rettv->vval.v_number = 0;
16464
16465 if (check_restricted() || check_secure())
16466 return;
16467
16468 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16469 varname = get_tv_string_chk(&argvars[1]);
16470 varp = &argvars[2];
16471
16472 if (tp != NULL && varname != NULL && varp != NULL)
16473 {
16474 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016475 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016476
16477 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16478 if (tabvarname != NULL)
16479 {
16480 STRCPY(tabvarname, "t:");
16481 STRCPY(tabvarname + 2, varname);
16482 set_var(tabvarname, varp, TRUE);
16483 vim_free(tabvarname);
16484 }
16485
16486 /* Restore current tabpage */
16487 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016488 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016489 }
16490}
16491
16492/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016493 * "settabwinvar()" function
16494 */
16495 static void
16496f_settabwinvar(argvars, rettv)
16497 typval_T *argvars;
16498 typval_T *rettv;
16499{
16500 setwinvar(argvars, rettv, 1);
16501}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016502
16503/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016504 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016507f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016508 typval_T *argvars;
16509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016510{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016511 setwinvar(argvars, rettv, 0);
16512}
16513
16514/*
16515 * "setwinvar()" and "settabwinvar()" functions
16516 */
16517 static void
16518setwinvar(argvars, rettv, off)
16519 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016520 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016521 int off;
16522{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523 win_T *win;
16524#ifdef FEAT_WINDOWS
16525 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016526 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016527#endif
16528 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016529 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016530 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016531 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532
16533 if (check_restricted() || check_secure())
16534 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016535
16536#ifdef FEAT_WINDOWS
16537 if (off == 1)
16538 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16539 else
16540 tp = curtab;
16541#endif
16542 win = find_win_by_nr(&argvars[off], tp);
16543 varname = get_tv_string_chk(&argvars[off + 1]);
16544 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545
16546 if (win != NULL && varname != NULL && varp != NULL)
16547 {
16548#ifdef FEAT_WINDOWS
16549 /* set curwin to be our win, temporarily */
16550 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016551 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016552 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016553 if (!win_valid(win))
16554 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555 curwin = win;
16556 curbuf = curwin->w_buffer;
16557#endif
16558
16559 if (*varname == '&')
16560 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016561 long numval;
16562 char_u *strval;
16563 int error = FALSE;
16564
Bram Moolenaar071d4272004-06-13 20:20:40 +000016565 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016566 numval = get_tv_number_chk(varp, &error);
16567 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016568 if (!error && strval != NULL)
16569 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570 }
16571 else
16572 {
16573 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16574 if (winvarname != NULL)
16575 {
16576 STRCPY(winvarname, "w:");
16577 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016578 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016579 vim_free(winvarname);
16580 }
16581 }
16582
16583#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016584 /* Restore current tabpage and window, if still valid (autocomands can
16585 * make them invalid). */
16586 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016587 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588 if (win_valid(save_curwin))
16589 {
16590 curwin = save_curwin;
16591 curbuf = curwin->w_buffer;
16592 }
16593#endif
16594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595}
16596
16597/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016598 * "shellescape({string})" function
16599 */
16600 static void
16601f_shellescape(argvars, rettv)
16602 typval_T *argvars;
16603 typval_T *rettv;
16604{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016605 rettv->vval.v_string = vim_strsave_shellescape(
16606 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016607 rettv->v_type = VAR_STRING;
16608}
16609
16610/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016611 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016612 */
16613 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016614f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016615 typval_T *argvars;
16616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016617{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016618 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619
Bram Moolenaar0d660222005-01-07 21:51:51 +000016620 p = get_tv_string(&argvars[0]);
16621 rettv->vval.v_string = vim_strsave(p);
16622 simplify_filename(rettv->vval.v_string); /* simplify in place */
16623 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624}
16625
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016626#ifdef FEAT_FLOAT
16627/*
16628 * "sin()" function
16629 */
16630 static void
16631f_sin(argvars, rettv)
16632 typval_T *argvars;
16633 typval_T *rettv;
16634{
16635 float_T f;
16636
16637 rettv->v_type = VAR_FLOAT;
16638 if (get_float_arg(argvars, &f) == OK)
16639 rettv->vval.v_float = sin(f);
16640 else
16641 rettv->vval.v_float = 0.0;
16642}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016643
16644/*
16645 * "sinh()" function
16646 */
16647 static void
16648f_sinh(argvars, rettv)
16649 typval_T *argvars;
16650 typval_T *rettv;
16651{
16652 float_T f;
16653
16654 rettv->v_type = VAR_FLOAT;
16655 if (get_float_arg(argvars, &f) == OK)
16656 rettv->vval.v_float = sinh(f);
16657 else
16658 rettv->vval.v_float = 0.0;
16659}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016660#endif
16661
Bram Moolenaar0d660222005-01-07 21:51:51 +000016662static int
16663#ifdef __BORLANDC__
16664 _RTLENTRYF
16665#endif
16666 item_compare __ARGS((const void *s1, const void *s2));
16667static int
16668#ifdef __BORLANDC__
16669 _RTLENTRYF
16670#endif
16671 item_compare2 __ARGS((const void *s1, const void *s2));
16672
16673static int item_compare_ic;
16674static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016675static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016676static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016677#define ITEM_COMPARE_FAIL 999
16678
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016680 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016681 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016682 static int
16683#ifdef __BORLANDC__
16684_RTLENTRYF
16685#endif
16686item_compare(s1, s2)
16687 const void *s1;
16688 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016689{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016690 char_u *p1, *p2;
16691 char_u *tofree1, *tofree2;
16692 int res;
16693 char_u numbuf1[NUMBUFLEN];
16694 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016695
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016696 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16697 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016698 if (p1 == NULL)
16699 p1 = (char_u *)"";
16700 if (p2 == NULL)
16701 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016702 if (item_compare_ic)
16703 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016705 res = STRCMP(p1, p2);
16706 vim_free(tofree1);
16707 vim_free(tofree2);
16708 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709}
16710
16711 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016712#ifdef __BORLANDC__
16713_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016714#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016715item_compare2(s1, s2)
16716 const void *s1;
16717 const void *s2;
16718{
16719 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016720 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016721 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016722 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016724 /* shortcut after failure in previous call; compare all items equal */
16725 if (item_compare_func_err)
16726 return 0;
16727
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016728 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16729 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016730 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16731 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016732
16733 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016734 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016735 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16736 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016737 clear_tv(&argv[0]);
16738 clear_tv(&argv[1]);
16739
16740 if (res == FAIL)
16741 res = ITEM_COMPARE_FAIL;
16742 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016743 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16744 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016745 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016746 clear_tv(&rettv);
16747 return res;
16748}
16749
16750/*
16751 * "sort({list})" function
16752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016754f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016755 typval_T *argvars;
16756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016757{
Bram Moolenaar33570922005-01-25 22:26:29 +000016758 list_T *l;
16759 listitem_T *li;
16760 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016761 long len;
16762 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016763
Bram Moolenaar0d660222005-01-07 21:51:51 +000016764 if (argvars[0].v_type != VAR_LIST)
16765 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766 else
16767 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016768 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016769 if (l == NULL || tv_check_lock(l->lv_lock,
16770 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016771 return;
16772 rettv->vval.v_list = l;
16773 rettv->v_type = VAR_LIST;
16774 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016775
Bram Moolenaar0d660222005-01-07 21:51:51 +000016776 len = list_len(l);
16777 if (len <= 1)
16778 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016779
Bram Moolenaar0d660222005-01-07 21:51:51 +000016780 item_compare_ic = FALSE;
16781 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016782 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016783 if (argvars[1].v_type != VAR_UNKNOWN)
16784 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016785 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016786 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016787 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016788 else
16789 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016790 int error = FALSE;
16791
16792 i = get_tv_number_chk(&argvars[1], &error);
16793 if (error)
16794 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016795 if (i == 1)
16796 item_compare_ic = TRUE;
16797 else
16798 item_compare_func = get_tv_string(&argvars[1]);
16799 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016800
16801 if (argvars[2].v_type != VAR_UNKNOWN)
16802 {
16803 /* optional third argument: {dict} */
16804 if (argvars[2].v_type != VAR_DICT)
16805 {
16806 EMSG(_(e_dictreq));
16807 return;
16808 }
16809 item_compare_selfdict = argvars[2].vval.v_dict;
16810 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812
Bram Moolenaar0d660222005-01-07 21:51:51 +000016813 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016814 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016815 if (ptrs == NULL)
16816 return;
16817 i = 0;
16818 for (li = l->lv_first; li != NULL; li = li->li_next)
16819 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016821 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016822 /* test the compare function */
16823 if (item_compare_func != NULL
16824 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16825 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016826 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016828 {
16829 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016830 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016831 item_compare_func == NULL ? item_compare : item_compare2);
16832
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016833 if (!item_compare_func_err)
16834 {
16835 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016836 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016837 l->lv_len = 0;
16838 for (i = 0; i < len; ++i)
16839 list_append(l, ptrs[i]);
16840 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016841 }
16842
16843 vim_free(ptrs);
16844 }
16845}
16846
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016847/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016848 * "soundfold({word})" function
16849 */
16850 static void
16851f_soundfold(argvars, rettv)
16852 typval_T *argvars;
16853 typval_T *rettv;
16854{
16855 char_u *s;
16856
16857 rettv->v_type = VAR_STRING;
16858 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016859#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016860 rettv->vval.v_string = eval_soundfold(s);
16861#else
16862 rettv->vval.v_string = vim_strsave(s);
16863#endif
16864}
16865
16866/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016867 * "spellbadword()" function
16868 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016869 static void
16870f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016871 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016872 typval_T *rettv;
16873{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016874 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016875 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016876 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016877
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016878 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016879 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016880
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016881#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016882 if (argvars[0].v_type == VAR_UNKNOWN)
16883 {
16884 /* Find the start and length of the badly spelled word. */
16885 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16886 if (len != 0)
16887 word = ml_get_cursor();
16888 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016889 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016890 {
16891 char_u *str = get_tv_string_chk(&argvars[0]);
16892 int capcol = -1;
16893
16894 if (str != NULL)
16895 {
16896 /* Check the argument for spelling. */
16897 while (*str != NUL)
16898 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016899 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016900 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016901 {
16902 word = str;
16903 break;
16904 }
16905 str += len;
16906 }
16907 }
16908 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016909#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016910
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016911 list_append_string(rettv->vval.v_list, word, len);
16912 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016913 attr == HLF_SPB ? "bad" :
16914 attr == HLF_SPR ? "rare" :
16915 attr == HLF_SPL ? "local" :
16916 attr == HLF_SPC ? "caps" :
16917 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016918}
16919
16920/*
16921 * "spellsuggest()" function
16922 */
16923 static void
16924f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016925 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016926 typval_T *rettv;
16927{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016928#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016929 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016930 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016931 int maxcount;
16932 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016933 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016934 listitem_T *li;
16935 int need_capital = FALSE;
16936#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016937
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016938 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016939 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016940
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016941#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016942 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016943 {
16944 str = get_tv_string(&argvars[0]);
16945 if (argvars[1].v_type != VAR_UNKNOWN)
16946 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016947 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016948 if (maxcount <= 0)
16949 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016950 if (argvars[2].v_type != VAR_UNKNOWN)
16951 {
16952 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16953 if (typeerr)
16954 return;
16955 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016956 }
16957 else
16958 maxcount = 25;
16959
Bram Moolenaar4770d092006-01-12 23:22:24 +000016960 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016961
16962 for (i = 0; i < ga.ga_len; ++i)
16963 {
16964 str = ((char_u **)ga.ga_data)[i];
16965
16966 li = listitem_alloc();
16967 if (li == NULL)
16968 vim_free(str);
16969 else
16970 {
16971 li->li_tv.v_type = VAR_STRING;
16972 li->li_tv.v_lock = 0;
16973 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016974 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016975 }
16976 }
16977 ga_clear(&ga);
16978 }
16979#endif
16980}
16981
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016983f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016984 typval_T *argvars;
16985 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016986{
16987 char_u *str;
16988 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016989 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016990 regmatch_T regmatch;
16991 char_u patbuf[NUMBUFLEN];
16992 char_u *save_cpo;
16993 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016994 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016995 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016996 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997
16998 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16999 save_cpo = p_cpo;
17000 p_cpo = (char_u *)"";
17001
17002 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017003 if (argvars[1].v_type != VAR_UNKNOWN)
17004 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017005 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17006 if (pat == NULL)
17007 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017008 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017009 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017010 }
17011 if (pat == NULL || *pat == NUL)
17012 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017013
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017014 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017016 if (typeerr)
17017 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018
Bram Moolenaar0d660222005-01-07 21:51:51 +000017019 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17020 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017022 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017023 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017024 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017025 if (*str == NUL)
17026 match = FALSE; /* empty item at the end */
17027 else
17028 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017029 if (match)
17030 end = regmatch.startp[0];
17031 else
17032 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017033 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17034 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017035 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017036 if (list_append_string(rettv->vval.v_list, str,
17037 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017038 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017039 }
17040 if (!match)
17041 break;
17042 /* Advance to just after the match. */
17043 if (regmatch.endp[0] > str)
17044 col = 0;
17045 else
17046 {
17047 /* Don't get stuck at the same match. */
17048#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017049 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017050#else
17051 col = 1;
17052#endif
17053 }
17054 str = regmatch.endp[0];
17055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056
Bram Moolenaar0d660222005-01-07 21:51:51 +000017057 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059
Bram Moolenaar0d660222005-01-07 21:51:51 +000017060 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017061}
17062
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017063#ifdef FEAT_FLOAT
17064/*
17065 * "sqrt()" function
17066 */
17067 static void
17068f_sqrt(argvars, rettv)
17069 typval_T *argvars;
17070 typval_T *rettv;
17071{
17072 float_T f;
17073
17074 rettv->v_type = VAR_FLOAT;
17075 if (get_float_arg(argvars, &f) == OK)
17076 rettv->vval.v_float = sqrt(f);
17077 else
17078 rettv->vval.v_float = 0.0;
17079}
17080
17081/*
17082 * "str2float()" function
17083 */
17084 static void
17085f_str2float(argvars, rettv)
17086 typval_T *argvars;
17087 typval_T *rettv;
17088{
17089 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17090
17091 if (*p == '+')
17092 p = skipwhite(p + 1);
17093 (void)string2float(p, &rettv->vval.v_float);
17094 rettv->v_type = VAR_FLOAT;
17095}
17096#endif
17097
Bram Moolenaar2c932302006-03-18 21:42:09 +000017098/*
17099 * "str2nr()" function
17100 */
17101 static void
17102f_str2nr(argvars, rettv)
17103 typval_T *argvars;
17104 typval_T *rettv;
17105{
17106 int base = 10;
17107 char_u *p;
17108 long n;
17109
17110 if (argvars[1].v_type != VAR_UNKNOWN)
17111 {
17112 base = get_tv_number(&argvars[1]);
17113 if (base != 8 && base != 10 && base != 16)
17114 {
17115 EMSG(_(e_invarg));
17116 return;
17117 }
17118 }
17119
17120 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017121 if (*p == '+')
17122 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017123 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17124 rettv->vval.v_number = n;
17125}
17126
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127#ifdef HAVE_STRFTIME
17128/*
17129 * "strftime({format}[, {time}])" function
17130 */
17131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017132f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017133 typval_T *argvars;
17134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017135{
17136 char_u result_buf[256];
17137 struct tm *curtime;
17138 time_t seconds;
17139 char_u *p;
17140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017141 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017143 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017144 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145 seconds = time(NULL);
17146 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017147 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148 curtime = localtime(&seconds);
17149 /* MSVC returns NULL for an invalid value of seconds. */
17150 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017151 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152 else
17153 {
17154# ifdef FEAT_MBYTE
17155 vimconv_T conv;
17156 char_u *enc;
17157
17158 conv.vc_type = CONV_NONE;
17159 enc = enc_locale();
17160 convert_setup(&conv, p_enc, enc);
17161 if (conv.vc_type != CONV_NONE)
17162 p = string_convert(&conv, p, NULL);
17163# endif
17164 if (p != NULL)
17165 (void)strftime((char *)result_buf, sizeof(result_buf),
17166 (char *)p, curtime);
17167 else
17168 result_buf[0] = NUL;
17169
17170# ifdef FEAT_MBYTE
17171 if (conv.vc_type != CONV_NONE)
17172 vim_free(p);
17173 convert_setup(&conv, enc, p_enc);
17174 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017175 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176 else
17177# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017178 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179
17180# ifdef FEAT_MBYTE
17181 /* Release conversion descriptors */
17182 convert_setup(&conv, NULL, NULL);
17183 vim_free(enc);
17184# endif
17185 }
17186}
17187#endif
17188
17189/*
17190 * "stridx()" function
17191 */
17192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017193f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017194 typval_T *argvars;
17195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196{
17197 char_u buf[NUMBUFLEN];
17198 char_u *needle;
17199 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017200 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017201 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017202 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017203
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017204 needle = get_tv_string_chk(&argvars[1]);
17205 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017206 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017207 if (needle == NULL || haystack == NULL)
17208 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209
Bram Moolenaar33570922005-01-25 22:26:29 +000017210 if (argvars[2].v_type != VAR_UNKNOWN)
17211 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017212 int error = FALSE;
17213
17214 start_idx = get_tv_number_chk(&argvars[2], &error);
17215 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017216 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017217 if (start_idx >= 0)
17218 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017219 }
17220
17221 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17222 if (pos != NULL)
17223 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224}
17225
17226/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017227 * "string()" function
17228 */
17229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017230f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017231 typval_T *argvars;
17232 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017233{
17234 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017235 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017237 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017238 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017239 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017240 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017241 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242}
17243
17244/*
17245 * "strlen()" function
17246 */
17247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017248f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017249 typval_T *argvars;
17250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017251{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017252 rettv->vval.v_number = (varnumber_T)(STRLEN(
17253 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017254}
17255
17256/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017257 * "strchars()" function
17258 */
17259 static void
17260f_strchars(argvars, rettv)
17261 typval_T *argvars;
17262 typval_T *rettv;
17263{
17264 char_u *s = get_tv_string(&argvars[0]);
17265#ifdef FEAT_MBYTE
17266 varnumber_T len = 0;
17267
17268 while (*s != NUL)
17269 {
17270 mb_cptr2char_adv(&s);
17271 ++len;
17272 }
17273 rettv->vval.v_number = len;
17274#else
17275 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17276#endif
17277}
17278
17279/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017280 * "strdisplaywidth()" function
17281 */
17282 static void
17283f_strdisplaywidth(argvars, rettv)
17284 typval_T *argvars;
17285 typval_T *rettv;
17286{
17287 char_u *s = get_tv_string(&argvars[0]);
17288 int col = 0;
17289
17290 if (argvars[1].v_type != VAR_UNKNOWN)
17291 col = get_tv_number(&argvars[1]);
17292
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017293 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017294}
17295
17296/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017297 * "strwidth()" function
17298 */
17299 static void
17300f_strwidth(argvars, rettv)
17301 typval_T *argvars;
17302 typval_T *rettv;
17303{
17304 char_u *s = get_tv_string(&argvars[0]);
17305
17306 rettv->vval.v_number = (varnumber_T)(
17307#ifdef FEAT_MBYTE
17308 mb_string2cells(s, -1)
17309#else
17310 STRLEN(s)
17311#endif
17312 );
17313}
17314
17315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316 * "strpart()" function
17317 */
17318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017319f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017320 typval_T *argvars;
17321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322{
17323 char_u *p;
17324 int n;
17325 int len;
17326 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017327 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017328
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017329 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330 slen = (int)STRLEN(p);
17331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017332 n = get_tv_number_chk(&argvars[1], &error);
17333 if (error)
17334 len = 0;
17335 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017336 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017337 else
17338 len = slen - n; /* default len: all bytes that are available. */
17339
17340 /*
17341 * Only return the overlap between the specified part and the actual
17342 * string.
17343 */
17344 if (n < 0)
17345 {
17346 len += n;
17347 n = 0;
17348 }
17349 else if (n > slen)
17350 n = slen;
17351 if (len < 0)
17352 len = 0;
17353 else if (n + len > slen)
17354 len = slen - n;
17355
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017356 rettv->v_type = VAR_STRING;
17357 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358}
17359
17360/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017361 * "strridx()" function
17362 */
17363 static void
17364f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017365 typval_T *argvars;
17366 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017367{
17368 char_u buf[NUMBUFLEN];
17369 char_u *needle;
17370 char_u *haystack;
17371 char_u *rest;
17372 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017373 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017374
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017375 needle = get_tv_string_chk(&argvars[1]);
17376 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017377
17378 rettv->vval.v_number = -1;
17379 if (needle == NULL || haystack == NULL)
17380 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017381
17382 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017383 if (argvars[2].v_type != VAR_UNKNOWN)
17384 {
17385 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017386 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017387 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017388 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017389 }
17390 else
17391 end_idx = haystack_len;
17392
Bram Moolenaar0d660222005-01-07 21:51:51 +000017393 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017394 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017395 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017396 lastmatch = haystack + end_idx;
17397 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017398 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017399 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017400 for (rest = haystack; *rest != '\0'; ++rest)
17401 {
17402 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017403 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017404 break;
17405 lastmatch = rest;
17406 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017407 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017408
17409 if (lastmatch == NULL)
17410 rettv->vval.v_number = -1;
17411 else
17412 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17413}
17414
17415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416 * "strtrans()" function
17417 */
17418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017419f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017420 typval_T *argvars;
17421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017423 rettv->v_type = VAR_STRING;
17424 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425}
17426
17427/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017428 * "submatch()" function
17429 */
17430 static void
17431f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017432 typval_T *argvars;
17433 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017434{
17435 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017436 rettv->vval.v_string =
17437 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017438}
17439
17440/*
17441 * "substitute()" function
17442 */
17443 static void
17444f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017445 typval_T *argvars;
17446 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017447{
17448 char_u patbuf[NUMBUFLEN];
17449 char_u subbuf[NUMBUFLEN];
17450 char_u flagsbuf[NUMBUFLEN];
17451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017452 char_u *str = get_tv_string_chk(&argvars[0]);
17453 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17454 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17455 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17456
Bram Moolenaar0d660222005-01-07 21:51:51 +000017457 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017458 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17459 rettv->vval.v_string = NULL;
17460 else
17461 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017462}
17463
17464/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017465 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017468f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017469 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471{
17472 int id = 0;
17473#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017474 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017475 long col;
17476 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017477 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017479 lnum = get_tv_lnum(argvars); /* -1 on type error */
17480 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17481 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017483 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017484 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017485 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486#endif
17487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017488 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489}
17490
17491/*
17492 * "synIDattr(id, what [, mode])" function
17493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017495f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017496 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498{
17499 char_u *p = NULL;
17500#ifdef FEAT_SYN_HL
17501 int id;
17502 char_u *what;
17503 char_u *mode;
17504 char_u modebuf[NUMBUFLEN];
17505 int modec;
17506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017507 id = get_tv_number(&argvars[0]);
17508 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017509 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017511 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017513 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514 modec = 0; /* replace invalid with current */
17515 }
17516 else
17517 {
17518#ifdef FEAT_GUI
17519 if (gui.in_use)
17520 modec = 'g';
17521 else
17522#endif
17523 if (t_colors > 1)
17524 modec = 'c';
17525 else
17526 modec = 't';
17527 }
17528
17529
17530 switch (TOLOWER_ASC(what[0]))
17531 {
17532 case 'b':
17533 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17534 p = highlight_color(id, what, modec);
17535 else /* bold */
17536 p = highlight_has_attr(id, HL_BOLD, modec);
17537 break;
17538
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017539 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017540 p = highlight_color(id, what, modec);
17541 break;
17542
17543 case 'i':
17544 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17545 p = highlight_has_attr(id, HL_INVERSE, modec);
17546 else /* italic */
17547 p = highlight_has_attr(id, HL_ITALIC, modec);
17548 break;
17549
17550 case 'n': /* name */
17551 p = get_highlight_name(NULL, id - 1);
17552 break;
17553
17554 case 'r': /* reverse */
17555 p = highlight_has_attr(id, HL_INVERSE, modec);
17556 break;
17557
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017558 case 's':
17559 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17560 p = highlight_color(id, what, modec);
17561 else /* standout */
17562 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563 break;
17564
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017565 case 'u':
17566 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17567 /* underline */
17568 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17569 else
17570 /* undercurl */
17571 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572 break;
17573 }
17574
17575 if (p != NULL)
17576 p = vim_strsave(p);
17577#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017578 rettv->v_type = VAR_STRING;
17579 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017580}
17581
17582/*
17583 * "synIDtrans(id)" function
17584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017586f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017587 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589{
17590 int id;
17591
17592#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017593 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594
17595 if (id > 0)
17596 id = syn_get_final_id(id);
17597 else
17598#endif
17599 id = 0;
17600
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017601 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602}
17603
17604/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017605 * "synconcealed(lnum, col)" function
17606 */
17607 static void
17608f_synconcealed(argvars, rettv)
17609 typval_T *argvars UNUSED;
17610 typval_T *rettv;
17611{
17612#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17613 long lnum;
17614 long col;
17615 int syntax_flags = 0;
17616 int cchar;
17617 int matchid = 0;
17618 char_u str[NUMBUFLEN];
17619#endif
17620
17621 rettv->v_type = VAR_LIST;
17622 rettv->vval.v_list = NULL;
17623
17624#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17625 lnum = get_tv_lnum(argvars); /* -1 on type error */
17626 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17627
17628 vim_memset(str, NUL, sizeof(str));
17629
17630 if (rettv_list_alloc(rettv) != FAIL)
17631 {
17632 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17633 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17634 && curwin->w_p_cole > 0)
17635 {
17636 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17637 syntax_flags = get_syntax_info(&matchid);
17638
17639 /* get the conceal character */
17640 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17641 {
17642 cchar = syn_get_sub_char();
17643 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17644 cchar = lcs_conceal;
17645 if (cchar != NUL)
17646 {
17647# ifdef FEAT_MBYTE
17648 if (has_mbyte)
17649 (*mb_char2bytes)(cchar, str);
17650 else
17651# endif
17652 str[0] = cchar;
17653 }
17654 }
17655 }
17656
17657 list_append_number(rettv->vval.v_list,
17658 (syntax_flags & HL_CONCEAL) != 0);
17659 /* -1 to auto-determine strlen */
17660 list_append_string(rettv->vval.v_list, str, -1);
17661 list_append_number(rettv->vval.v_list, matchid);
17662 }
17663#endif
17664}
17665
17666/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017667 * "synstack(lnum, col)" function
17668 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017669 static void
17670f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017671 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017672 typval_T *rettv;
17673{
17674#ifdef FEAT_SYN_HL
17675 long lnum;
17676 long col;
17677 int i;
17678 int id;
17679#endif
17680
17681 rettv->v_type = VAR_LIST;
17682 rettv->vval.v_list = NULL;
17683
17684#ifdef FEAT_SYN_HL
17685 lnum = get_tv_lnum(argvars); /* -1 on type error */
17686 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17687
17688 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017689 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017690 && rettv_list_alloc(rettv) != FAIL)
17691 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017692 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017693 for (i = 0; ; ++i)
17694 {
17695 id = syn_get_stack_item(i);
17696 if (id < 0)
17697 break;
17698 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17699 break;
17700 }
17701 }
17702#endif
17703}
17704
17705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706 * "system()" function
17707 */
17708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017709f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017710 typval_T *argvars;
17711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017713 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017715 char_u *infile = NULL;
17716 char_u buf[NUMBUFLEN];
17717 int err = FALSE;
17718 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017720 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017721 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017722
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017723 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017724 {
17725 /*
17726 * Write the string to a temp file, to be used for input of the shell
17727 * command.
17728 */
17729 if ((infile = vim_tempname('i')) == NULL)
17730 {
17731 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017732 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017733 }
17734
17735 fd = mch_fopen((char *)infile, WRITEBIN);
17736 if (fd == NULL)
17737 {
17738 EMSG2(_(e_notopen), infile);
17739 goto done;
17740 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017741 p = get_tv_string_buf_chk(&argvars[1], buf);
17742 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017743 {
17744 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017745 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017746 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017747 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17748 err = TRUE;
17749 if (fclose(fd) != 0)
17750 err = TRUE;
17751 if (err)
17752 {
17753 EMSG(_("E677: Error writing temp file"));
17754 goto done;
17755 }
17756 }
17757
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017758 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17759 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017760
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761#ifdef USE_CR
17762 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017763 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764 {
17765 char_u *s;
17766
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017767 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017768 {
17769 if (*s == CAR)
17770 *s = NL;
17771 }
17772 }
17773#else
17774# ifdef USE_CRNL
17775 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017776 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777 {
17778 char_u *s, *d;
17779
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017780 d = res;
17781 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782 {
17783 if (s[0] == CAR && s[1] == NL)
17784 ++s;
17785 *d++ = *s;
17786 }
17787 *d = NUL;
17788 }
17789# endif
17790#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017791
17792done:
17793 if (infile != NULL)
17794 {
17795 mch_remove(infile);
17796 vim_free(infile);
17797 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017798 rettv->v_type = VAR_STRING;
17799 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017800}
17801
17802/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017803 * "tabpagebuflist()" function
17804 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017805 static void
17806f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017807 typval_T *argvars UNUSED;
17808 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017809{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017810#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017811 tabpage_T *tp;
17812 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017813
17814 if (argvars[0].v_type == VAR_UNKNOWN)
17815 wp = firstwin;
17816 else
17817 {
17818 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17819 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017820 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017821 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017822 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017823 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017824 for (; wp != NULL; wp = wp->w_next)
17825 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017826 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017827 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017828 }
17829#endif
17830}
17831
17832
17833/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017834 * "tabpagenr()" function
17835 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017836 static void
17837f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017838 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017839 typval_T *rettv;
17840{
17841 int nr = 1;
17842#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017843 char_u *arg;
17844
17845 if (argvars[0].v_type != VAR_UNKNOWN)
17846 {
17847 arg = get_tv_string_chk(&argvars[0]);
17848 nr = 0;
17849 if (arg != NULL)
17850 {
17851 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017852 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017853 else
17854 EMSG2(_(e_invexpr2), arg);
17855 }
17856 }
17857 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017858 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017859#endif
17860 rettv->vval.v_number = nr;
17861}
17862
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017863
17864#ifdef FEAT_WINDOWS
17865static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17866
17867/*
17868 * Common code for tabpagewinnr() and winnr().
17869 */
17870 static int
17871get_winnr(tp, argvar)
17872 tabpage_T *tp;
17873 typval_T *argvar;
17874{
17875 win_T *twin;
17876 int nr = 1;
17877 win_T *wp;
17878 char_u *arg;
17879
17880 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17881 if (argvar->v_type != VAR_UNKNOWN)
17882 {
17883 arg = get_tv_string_chk(argvar);
17884 if (arg == NULL)
17885 nr = 0; /* type error; errmsg already given */
17886 else if (STRCMP(arg, "$") == 0)
17887 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17888 else if (STRCMP(arg, "#") == 0)
17889 {
17890 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17891 if (twin == NULL)
17892 nr = 0;
17893 }
17894 else
17895 {
17896 EMSG2(_(e_invexpr2), arg);
17897 nr = 0;
17898 }
17899 }
17900
17901 if (nr > 0)
17902 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17903 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017904 {
17905 if (wp == NULL)
17906 {
17907 /* didn't find it in this tabpage */
17908 nr = 0;
17909 break;
17910 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017911 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017912 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017913 return nr;
17914}
17915#endif
17916
17917/*
17918 * "tabpagewinnr()" function
17919 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017920 static void
17921f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017922 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017923 typval_T *rettv;
17924{
17925 int nr = 1;
17926#ifdef FEAT_WINDOWS
17927 tabpage_T *tp;
17928
17929 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17930 if (tp == NULL)
17931 nr = 0;
17932 else
17933 nr = get_winnr(tp, &argvars[1]);
17934#endif
17935 rettv->vval.v_number = nr;
17936}
17937
17938
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017939/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017940 * "tagfiles()" function
17941 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017942 static void
17943f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017944 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017945 typval_T *rettv;
17946{
Bram Moolenaard9462e32011-04-11 21:35:11 +020017947 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017948 tagname_T tn;
17949 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017950
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017951 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017952 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017953 fname = alloc(MAXPATHL);
17954 if (fname == NULL)
17955 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017956
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017957 for (first = TRUE; ; first = FALSE)
17958 if (get_tagfname(&tn, first, fname) == FAIL
17959 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017960 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017961 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020017962 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017963}
17964
17965/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017966 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017967 */
17968 static void
17969f_taglist(argvars, rettv)
17970 typval_T *argvars;
17971 typval_T *rettv;
17972{
17973 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017974
17975 tag_pattern = get_tv_string(&argvars[0]);
17976
17977 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017978 if (*tag_pattern == NUL)
17979 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017980
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017981 if (rettv_list_alloc(rettv) == OK)
17982 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017983}
17984
17985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017986 * "tempname()" function
17987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017989f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017990 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992{
17993 static int x = 'A';
17994
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017995 rettv->v_type = VAR_STRING;
17996 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997
17998 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17999 * names. Skip 'I' and 'O', they are used for shell redirection. */
18000 do
18001 {
18002 if (x == 'Z')
18003 x = '0';
18004 else if (x == '9')
18005 x = 'A';
18006 else
18007 {
18008#ifdef EBCDIC
18009 if (x == 'I')
18010 x = 'J';
18011 else if (x == 'R')
18012 x = 'S';
18013 else
18014#endif
18015 ++x;
18016 }
18017 } while (x == 'I' || x == 'O');
18018}
18019
18020/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018021 * "test(list)" function: Just checking the walls...
18022 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018023 static void
18024f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018025 typval_T *argvars UNUSED;
18026 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018027{
18028 /* Used for unit testing. Change the code below to your liking. */
18029#if 0
18030 listitem_T *li;
18031 list_T *l;
18032 char_u *bad, *good;
18033
18034 if (argvars[0].v_type != VAR_LIST)
18035 return;
18036 l = argvars[0].vval.v_list;
18037 if (l == NULL)
18038 return;
18039 li = l->lv_first;
18040 if (li == NULL)
18041 return;
18042 bad = get_tv_string(&li->li_tv);
18043 li = li->li_next;
18044 if (li == NULL)
18045 return;
18046 good = get_tv_string(&li->li_tv);
18047 rettv->vval.v_number = test_edit_score(bad, good);
18048#endif
18049}
18050
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018051#ifdef FEAT_FLOAT
18052/*
18053 * "tan()" function
18054 */
18055 static void
18056f_tan(argvars, rettv)
18057 typval_T *argvars;
18058 typval_T *rettv;
18059{
18060 float_T f;
18061
18062 rettv->v_type = VAR_FLOAT;
18063 if (get_float_arg(argvars, &f) == OK)
18064 rettv->vval.v_float = tan(f);
18065 else
18066 rettv->vval.v_float = 0.0;
18067}
18068
18069/*
18070 * "tanh()" function
18071 */
18072 static void
18073f_tanh(argvars, rettv)
18074 typval_T *argvars;
18075 typval_T *rettv;
18076{
18077 float_T f;
18078
18079 rettv->v_type = VAR_FLOAT;
18080 if (get_float_arg(argvars, &f) == OK)
18081 rettv->vval.v_float = tanh(f);
18082 else
18083 rettv->vval.v_float = 0.0;
18084}
18085#endif
18086
Bram Moolenaard52d9742005-08-21 22:20:28 +000018087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 * "tolower(string)" function
18089 */
18090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018091f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018092 typval_T *argvars;
18093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094{
18095 char_u *p;
18096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018097 p = vim_strsave(get_tv_string(&argvars[0]));
18098 rettv->v_type = VAR_STRING;
18099 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100
18101 if (p != NULL)
18102 while (*p != NUL)
18103 {
18104#ifdef FEAT_MBYTE
18105 int l;
18106
18107 if (enc_utf8)
18108 {
18109 int c, lc;
18110
18111 c = utf_ptr2char(p);
18112 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018113 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018114 /* TODO: reallocate string when byte count changes. */
18115 if (utf_char2len(lc) == l)
18116 utf_char2bytes(lc, p);
18117 p += l;
18118 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018119 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120 p += l; /* skip multi-byte character */
18121 else
18122#endif
18123 {
18124 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18125 ++p;
18126 }
18127 }
18128}
18129
18130/*
18131 * "toupper(string)" function
18132 */
18133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018134f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018135 typval_T *argvars;
18136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018137{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018138 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018139 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140}
18141
18142/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018143 * "tr(string, fromstr, tostr)" function
18144 */
18145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018146f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018147 typval_T *argvars;
18148 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018149{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018150 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018151 char_u *fromstr;
18152 char_u *tostr;
18153 char_u *p;
18154#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018155 int inlen;
18156 int fromlen;
18157 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018158 int idx;
18159 char_u *cpstr;
18160 int cplen;
18161 int first = TRUE;
18162#endif
18163 char_u buf[NUMBUFLEN];
18164 char_u buf2[NUMBUFLEN];
18165 garray_T ga;
18166
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018167 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018168 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18169 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018170
18171 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018172 rettv->v_type = VAR_STRING;
18173 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018174 if (fromstr == NULL || tostr == NULL)
18175 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018176 ga_init2(&ga, (int)sizeof(char), 80);
18177
18178#ifdef FEAT_MBYTE
18179 if (!has_mbyte)
18180#endif
18181 /* not multi-byte: fromstr and tostr must be the same length */
18182 if (STRLEN(fromstr) != STRLEN(tostr))
18183 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018184#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018185error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018186#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018187 EMSG2(_(e_invarg2), fromstr);
18188 ga_clear(&ga);
18189 return;
18190 }
18191
18192 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018193 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018194 {
18195#ifdef FEAT_MBYTE
18196 if (has_mbyte)
18197 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018198 inlen = (*mb_ptr2len)(in_str);
18199 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018200 cplen = inlen;
18201 idx = 0;
18202 for (p = fromstr; *p != NUL; p += fromlen)
18203 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018204 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018205 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018206 {
18207 for (p = tostr; *p != NUL; p += tolen)
18208 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018209 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018210 if (idx-- == 0)
18211 {
18212 cplen = tolen;
18213 cpstr = p;
18214 break;
18215 }
18216 }
18217 if (*p == NUL) /* tostr is shorter than fromstr */
18218 goto error;
18219 break;
18220 }
18221 ++idx;
18222 }
18223
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018224 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018225 {
18226 /* Check that fromstr and tostr have the same number of
18227 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018228 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018229 first = FALSE;
18230 for (p = tostr; *p != NUL; p += tolen)
18231 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018232 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018233 --idx;
18234 }
18235 if (idx != 0)
18236 goto error;
18237 }
18238
18239 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018240 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018241 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018242
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018243 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018244 }
18245 else
18246#endif
18247 {
18248 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018249 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018250 if (p != NULL)
18251 ga_append(&ga, tostr[p - fromstr]);
18252 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018253 ga_append(&ga, *in_str);
18254 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018255 }
18256 }
18257
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018258 /* add a terminating NUL */
18259 ga_grow(&ga, 1);
18260 ga_append(&ga, NUL);
18261
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018262 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018263}
18264
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018265#ifdef FEAT_FLOAT
18266/*
18267 * "trunc({float})" function
18268 */
18269 static void
18270f_trunc(argvars, rettv)
18271 typval_T *argvars;
18272 typval_T *rettv;
18273{
18274 float_T f;
18275
18276 rettv->v_type = VAR_FLOAT;
18277 if (get_float_arg(argvars, &f) == OK)
18278 /* trunc() is not in C90, use floor() or ceil() instead. */
18279 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18280 else
18281 rettv->vval.v_float = 0.0;
18282}
18283#endif
18284
Bram Moolenaar8299df92004-07-10 09:47:34 +000018285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286 * "type(expr)" function
18287 */
18288 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018289f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018290 typval_T *argvars;
18291 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018292{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018293 int n;
18294
18295 switch (argvars[0].v_type)
18296 {
18297 case VAR_NUMBER: n = 0; break;
18298 case VAR_STRING: n = 1; break;
18299 case VAR_FUNC: n = 2; break;
18300 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018301 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018302#ifdef FEAT_FLOAT
18303 case VAR_FLOAT: n = 5; break;
18304#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018305 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18306 }
18307 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308}
18309
18310/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018311 * "undofile(name)" function
18312 */
18313 static void
18314f_undofile(argvars, rettv)
18315 typval_T *argvars;
18316 typval_T *rettv;
18317{
18318 rettv->v_type = VAR_STRING;
18319#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018320 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018321 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018322
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018323 if (*fname == NUL)
18324 {
18325 /* If there is no file name there will be no undo file. */
18326 rettv->vval.v_string = NULL;
18327 }
18328 else
18329 {
18330 char_u *ffname = FullName_save(fname, FALSE);
18331
18332 if (ffname != NULL)
18333 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18334 vim_free(ffname);
18335 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018336 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018337#else
18338 rettv->vval.v_string = NULL;
18339#endif
18340}
18341
18342/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018343 * "undotree()" function
18344 */
18345 static void
18346f_undotree(argvars, rettv)
18347 typval_T *argvars UNUSED;
18348 typval_T *rettv;
18349{
18350 if (rettv_dict_alloc(rettv) == OK)
18351 {
18352 dict_T *dict = rettv->vval.v_dict;
18353 list_T *list;
18354
Bram Moolenaar730cde92010-06-27 05:18:54 +020018355 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018356 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018357 dict_add_nr_str(dict, "save_last",
18358 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018359 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18360 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018361 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018362
18363 list = list_alloc();
18364 if (list != NULL)
18365 {
18366 u_eval_tree(curbuf->b_u_oldhead, list);
18367 dict_add_list(dict, "entries", list);
18368 }
18369 }
18370}
18371
18372/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018373 * "values(dict)" function
18374 */
18375 static void
18376f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018377 typval_T *argvars;
18378 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018379{
18380 dict_list(argvars, rettv, 1);
18381}
18382
18383/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384 * "virtcol(string)" function
18385 */
18386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018387f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018388 typval_T *argvars;
18389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018390{
18391 colnr_T vcol = 0;
18392 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018393 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018395 fp = var2fpos(&argvars[0], FALSE, &fnum);
18396 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18397 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398 {
18399 getvvcol(curwin, fp, NULL, NULL, &vcol);
18400 ++vcol;
18401 }
18402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018403 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018404}
18405
18406/*
18407 * "visualmode()" function
18408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018410f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018411 typval_T *argvars UNUSED;
18412 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018413{
18414#ifdef FEAT_VISUAL
18415 char_u str[2];
18416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018417 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018418 str[0] = curbuf->b_visual_mode_eval;
18419 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018420 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018421
18422 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018423 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018424 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018425#endif
18426}
18427
18428/*
18429 * "winbufnr(nr)" function
18430 */
18431 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018432f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018433 typval_T *argvars;
18434 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435{
18436 win_T *wp;
18437
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018438 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018440 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018441 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018442 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018443}
18444
18445/*
18446 * "wincol()" function
18447 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018449f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018450 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452{
18453 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018454 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455}
18456
18457/*
18458 * "winheight(nr)" function
18459 */
18460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018461f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018462 typval_T *argvars;
18463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464{
18465 win_T *wp;
18466
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018467 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018469 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018471 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472}
18473
18474/*
18475 * "winline()" function
18476 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018478f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018479 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018481{
18482 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018483 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484}
18485
18486/*
18487 * "winnr()" function
18488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018490f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018491 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493{
18494 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018495
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018497 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018499 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018500}
18501
18502/*
18503 * "winrestcmd()" function
18504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018506f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018507 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018509{
18510#ifdef FEAT_WINDOWS
18511 win_T *wp;
18512 int winnr = 1;
18513 garray_T ga;
18514 char_u buf[50];
18515
18516 ga_init2(&ga, (int)sizeof(char), 70);
18517 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18518 {
18519 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18520 ga_concat(&ga, buf);
18521# ifdef FEAT_VERTSPLIT
18522 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18523 ga_concat(&ga, buf);
18524# endif
18525 ++winnr;
18526 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018527 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018529 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018530#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018531 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018533 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018534}
18535
18536/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018537 * "winrestview()" function
18538 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018539 static void
18540f_winrestview(argvars, rettv)
18541 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018542 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018543{
18544 dict_T *dict;
18545
18546 if (argvars[0].v_type != VAR_DICT
18547 || (dict = argvars[0].vval.v_dict) == NULL)
18548 EMSG(_(e_invarg));
18549 else
18550 {
18551 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18552 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18553#ifdef FEAT_VIRTUALEDIT
18554 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18555#endif
18556 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018557 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018558
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018559 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018560#ifdef FEAT_DIFF
18561 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18562#endif
18563 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18564 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18565
18566 check_cursor();
Bram Moolenaarab984db2012-06-06 16:29:10 +020018567 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018568
18569 if (curwin->w_topline == 0)
18570 curwin->w_topline = 1;
18571 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18572 curwin->w_topline = curbuf->b_ml.ml_line_count;
18573#ifdef FEAT_DIFF
18574 check_topfill(curwin, TRUE);
18575#endif
18576 }
18577}
18578
18579/*
18580 * "winsaveview()" function
18581 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018582 static void
18583f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018584 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018585 typval_T *rettv;
18586{
18587 dict_T *dict;
18588
Bram Moolenaara800b422010-06-27 01:15:55 +020018589 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018590 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018591 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018592
18593 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18594 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18595#ifdef FEAT_VIRTUALEDIT
18596 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18597#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018598 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018599 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18600
18601 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18602#ifdef FEAT_DIFF
18603 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18604#endif
18605 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18606 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18607}
18608
18609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018610 * "winwidth(nr)" function
18611 */
18612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018613f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018614 typval_T *argvars;
18615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616{
18617 win_T *wp;
18618
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018619 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018621 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622 else
18623#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018624 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018626 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627#endif
18628}
18629
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018631 * "writefile()" function
18632 */
18633 static void
18634f_writefile(argvars, rettv)
18635 typval_T *argvars;
18636 typval_T *rettv;
18637{
18638 int binary = FALSE;
18639 char_u *fname;
18640 FILE *fd;
18641 listitem_T *li;
18642 char_u *s;
18643 int ret = 0;
18644 int c;
18645
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018646 if (check_restricted() || check_secure())
18647 return;
18648
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018649 if (argvars[0].v_type != VAR_LIST)
18650 {
18651 EMSG2(_(e_listarg), "writefile()");
18652 return;
18653 }
18654 if (argvars[0].vval.v_list == NULL)
18655 return;
18656
18657 if (argvars[2].v_type != VAR_UNKNOWN
18658 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18659 binary = TRUE;
18660
18661 /* Always open the file in binary mode, library functions have a mind of
18662 * their own about CR-LF conversion. */
18663 fname = get_tv_string(&argvars[1]);
18664 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18665 {
18666 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18667 ret = -1;
18668 }
18669 else
18670 {
18671 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18672 li = li->li_next)
18673 {
18674 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18675 {
18676 if (*s == '\n')
18677 c = putc(NUL, fd);
18678 else
18679 c = putc(*s, fd);
18680 if (c == EOF)
18681 {
18682 ret = -1;
18683 break;
18684 }
18685 }
18686 if (!binary || li->li_next != NULL)
18687 if (putc('\n', fd) == EOF)
18688 {
18689 ret = -1;
18690 break;
18691 }
18692 if (ret < 0)
18693 {
18694 EMSG(_(e_write));
18695 break;
18696 }
18697 }
18698 fclose(fd);
18699 }
18700
18701 rettv->vval.v_number = ret;
18702}
18703
18704/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018705 * "xor(expr, expr)" function
18706 */
18707 static void
18708f_xor(argvars, rettv)
18709 typval_T *argvars;
18710 typval_T *rettv;
18711{
18712 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18713 ^ get_tv_number_chk(&argvars[1], NULL);
18714}
18715
18716
18717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018719 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720 */
18721 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018722var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018723 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018724 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018725 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018726{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018727 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018729 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730
Bram Moolenaara5525202006-03-02 22:52:09 +000018731 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018732 if (varp->v_type == VAR_LIST)
18733 {
18734 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018735 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018736 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018737 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018738
18739 l = varp->vval.v_list;
18740 if (l == NULL)
18741 return NULL;
18742
18743 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018744 pos.lnum = list_find_nr(l, 0L, &error);
18745 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018746 return NULL; /* invalid line number */
18747
18748 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018749 pos.col = list_find_nr(l, 1L, &error);
18750 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018751 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018752 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018753
18754 /* We accept "$" for the column number: last column. */
18755 li = list_find(l, 1L);
18756 if (li != NULL && li->li_tv.v_type == VAR_STRING
18757 && li->li_tv.vval.v_string != NULL
18758 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18759 pos.col = len + 1;
18760
Bram Moolenaara5525202006-03-02 22:52:09 +000018761 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018762 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018763 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018764 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018765
Bram Moolenaara5525202006-03-02 22:52:09 +000018766#ifdef FEAT_VIRTUALEDIT
18767 /* Get the virtual offset. Defaults to zero. */
18768 pos.coladd = list_find_nr(l, 2L, &error);
18769 if (error)
18770 pos.coladd = 0;
18771#endif
18772
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018773 return &pos;
18774 }
18775
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018776 name = get_tv_string_chk(varp);
18777 if (name == NULL)
18778 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018779 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018781#ifdef FEAT_VISUAL
18782 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18783 {
18784 if (VIsual_active)
18785 return &VIsual;
18786 return &curwin->w_cursor;
18787 }
18788#endif
18789 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018791 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18793 return NULL;
18794 return pp;
18795 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018796
18797#ifdef FEAT_VIRTUALEDIT
18798 pos.coladd = 0;
18799#endif
18800
Bram Moolenaar477933c2007-07-17 14:32:23 +000018801 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018802 {
18803 pos.col = 0;
18804 if (name[1] == '0') /* "w0": first visible line */
18805 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018806 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018807 pos.lnum = curwin->w_topline;
18808 return &pos;
18809 }
18810 else if (name[1] == '$') /* "w$": last visible line */
18811 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018812 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018813 pos.lnum = curwin->w_botline - 1;
18814 return &pos;
18815 }
18816 }
18817 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018819 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820 {
18821 pos.lnum = curbuf->b_ml.ml_line_count;
18822 pos.col = 0;
18823 }
18824 else
18825 {
18826 pos.lnum = curwin->w_cursor.lnum;
18827 pos.col = (colnr_T)STRLEN(ml_get_curline());
18828 }
18829 return &pos;
18830 }
18831 return NULL;
18832}
18833
18834/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018835 * Convert list in "arg" into a position and optional file number.
18836 * When "fnump" is NULL there is no file number, only 3 items.
18837 * Note that the column is passed on as-is, the caller may want to decrement
18838 * it to use 1 for the first column.
18839 * Return FAIL when conversion is not possible, doesn't check the position for
18840 * validity.
18841 */
18842 static int
18843list2fpos(arg, posp, fnump)
18844 typval_T *arg;
18845 pos_T *posp;
18846 int *fnump;
18847{
18848 list_T *l = arg->vval.v_list;
18849 long i = 0;
18850 long n;
18851
Bram Moolenaarbde35262006-07-23 20:12:24 +000018852 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18853 * when "fnump" isn't NULL and "coladd" is optional. */
18854 if (arg->v_type != VAR_LIST
18855 || l == NULL
18856 || l->lv_len < (fnump == NULL ? 2 : 3)
18857 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018858 return FAIL;
18859
18860 if (fnump != NULL)
18861 {
18862 n = list_find_nr(l, i++, NULL); /* fnum */
18863 if (n < 0)
18864 return FAIL;
18865 if (n == 0)
18866 n = curbuf->b_fnum; /* current buffer */
18867 *fnump = n;
18868 }
18869
18870 n = list_find_nr(l, i++, NULL); /* lnum */
18871 if (n < 0)
18872 return FAIL;
18873 posp->lnum = n;
18874
18875 n = list_find_nr(l, i++, NULL); /* col */
18876 if (n < 0)
18877 return FAIL;
18878 posp->col = n;
18879
18880#ifdef FEAT_VIRTUALEDIT
18881 n = list_find_nr(l, i, NULL);
18882 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018883 posp->coladd = 0;
18884 else
18885 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018886#endif
18887
18888 return OK;
18889}
18890
18891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892 * Get the length of an environment variable name.
18893 * Advance "arg" to the first character after the name.
18894 * Return 0 for error.
18895 */
18896 static int
18897get_env_len(arg)
18898 char_u **arg;
18899{
18900 char_u *p;
18901 int len;
18902
18903 for (p = *arg; vim_isIDc(*p); ++p)
18904 ;
18905 if (p == *arg) /* no name found */
18906 return 0;
18907
18908 len = (int)(p - *arg);
18909 *arg = p;
18910 return len;
18911}
18912
18913/*
18914 * Get the length of the name of a function or internal variable.
18915 * "arg" is advanced to the first non-white character after the name.
18916 * Return 0 if something is wrong.
18917 */
18918 static int
18919get_id_len(arg)
18920 char_u **arg;
18921{
18922 char_u *p;
18923 int len;
18924
18925 /* Find the end of the name. */
18926 for (p = *arg; eval_isnamec(*p); ++p)
18927 ;
18928 if (p == *arg) /* no name found */
18929 return 0;
18930
18931 len = (int)(p - *arg);
18932 *arg = skipwhite(p);
18933
18934 return len;
18935}
18936
18937/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018938 * Get the length of the name of a variable or function.
18939 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018941 * Return -1 if curly braces expansion failed.
18942 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018943 * If the name contains 'magic' {}'s, expand them and return the
18944 * expanded name in an allocated string via 'alias' - caller must free.
18945 */
18946 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018947get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948 char_u **arg;
18949 char_u **alias;
18950 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018951 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952{
18953 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954 char_u *p;
18955 char_u *expr_start;
18956 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957
18958 *alias = NULL; /* default to no alias */
18959
18960 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18961 && (*arg)[2] == (int)KE_SNR)
18962 {
18963 /* hard coded <SNR>, already translated */
18964 *arg += 3;
18965 return get_id_len(arg) + 3;
18966 }
18967 len = eval_fname_script(*arg);
18968 if (len > 0)
18969 {
18970 /* literal "<SID>", "s:" or "<SNR>" */
18971 *arg += len;
18972 }
18973
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018975 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018977 p = find_name_end(*arg, &expr_start, &expr_end,
18978 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979 if (expr_start != NULL)
18980 {
18981 char_u *temp_string;
18982
18983 if (!evaluate)
18984 {
18985 len += (int)(p - *arg);
18986 *arg = skipwhite(p);
18987 return len;
18988 }
18989
18990 /*
18991 * Include any <SID> etc in the expanded string:
18992 * Thus the -len here.
18993 */
18994 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18995 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018996 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018997 *alias = temp_string;
18998 *arg = skipwhite(p);
18999 return (int)STRLEN(temp_string);
19000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001
19002 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019003 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004 EMSG2(_(e_invexpr2), *arg);
19005
19006 return len;
19007}
19008
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019009/*
19010 * Find the end of a variable or function name, taking care of magic braces.
19011 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19012 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019013 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019014 * Return a pointer to just after the name. Equal to "arg" if there is no
19015 * valid name.
19016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019017 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019018find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019 char_u *arg;
19020 char_u **expr_start;
19021 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019022 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019023{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019024 int mb_nest = 0;
19025 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019026 char_u *p;
19027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019028 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019030 *expr_start = NULL;
19031 *expr_end = NULL;
19032 }
19033
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019034 /* Quick check for valid starting character. */
19035 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19036 return arg;
19037
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019038 for (p = arg; *p != NUL
19039 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019040 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019041 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019042 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019043 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019044 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019045 if (*p == '\'')
19046 {
19047 /* skip over 'string' to avoid counting [ and ] inside it. */
19048 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19049 ;
19050 if (*p == NUL)
19051 break;
19052 }
19053 else if (*p == '"')
19054 {
19055 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19056 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19057 if (*p == '\\' && p[1] != NUL)
19058 ++p;
19059 if (*p == NUL)
19060 break;
19061 }
19062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019063 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019065 if (*p == '[')
19066 ++br_nest;
19067 else if (*p == ']')
19068 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019070
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019071 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019073 if (*p == '{')
19074 {
19075 mb_nest++;
19076 if (expr_start != NULL && *expr_start == NULL)
19077 *expr_start = p;
19078 }
19079 else if (*p == '}')
19080 {
19081 mb_nest--;
19082 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19083 *expr_end = p;
19084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086 }
19087
19088 return p;
19089}
19090
19091/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019092 * Expands out the 'magic' {}'s in a variable/function name.
19093 * Note that this can call itself recursively, to deal with
19094 * constructs like foo{bar}{baz}{bam}
19095 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19096 * "in_start" ^
19097 * "expr_start" ^
19098 * "expr_end" ^
19099 * "in_end" ^
19100 *
19101 * Returns a new allocated string, which the caller must free.
19102 * Returns NULL for failure.
19103 */
19104 static char_u *
19105make_expanded_name(in_start, expr_start, expr_end, in_end)
19106 char_u *in_start;
19107 char_u *expr_start;
19108 char_u *expr_end;
19109 char_u *in_end;
19110{
19111 char_u c1;
19112 char_u *retval = NULL;
19113 char_u *temp_result;
19114 char_u *nextcmd = NULL;
19115
19116 if (expr_end == NULL || in_end == NULL)
19117 return NULL;
19118 *expr_start = NUL;
19119 *expr_end = NUL;
19120 c1 = *in_end;
19121 *in_end = NUL;
19122
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019123 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019124 if (temp_result != NULL && nextcmd == NULL)
19125 {
19126 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19127 + (in_end - expr_end) + 1));
19128 if (retval != NULL)
19129 {
19130 STRCPY(retval, in_start);
19131 STRCAT(retval, temp_result);
19132 STRCAT(retval, expr_end + 1);
19133 }
19134 }
19135 vim_free(temp_result);
19136
19137 *in_end = c1; /* put char back for error messages */
19138 *expr_start = '{';
19139 *expr_end = '}';
19140
19141 if (retval != NULL)
19142 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019143 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019144 if (expr_start != NULL)
19145 {
19146 /* Further expansion! */
19147 temp_result = make_expanded_name(retval, expr_start,
19148 expr_end, temp_result);
19149 vim_free(retval);
19150 retval = temp_result;
19151 }
19152 }
19153
19154 return retval;
19155}
19156
19157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019159 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 */
19161 static int
19162eval_isnamec(c)
19163 int c;
19164{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019165 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19166}
19167
19168/*
19169 * Return TRUE if character "c" can be used as the first character in a
19170 * variable or function name (excluding '{' and '}').
19171 */
19172 static int
19173eval_isnamec1(c)
19174 int c;
19175{
19176 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177}
19178
19179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019180 * Set number v: variable to "val".
19181 */
19182 void
19183set_vim_var_nr(idx, val)
19184 int idx;
19185 long val;
19186{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019187 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188}
19189
19190/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019191 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019192 */
19193 long
19194get_vim_var_nr(idx)
19195 int idx;
19196{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019197 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019198}
19199
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019200/*
19201 * Get string v: variable value. Uses a static buffer, can only be used once.
19202 */
19203 char_u *
19204get_vim_var_str(idx)
19205 int idx;
19206{
19207 return get_tv_string(&vimvars[idx].vv_tv);
19208}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019209
Bram Moolenaar071d4272004-06-13 20:20:40 +000019210/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019211 * Get List v: variable value. Caller must take care of reference count when
19212 * needed.
19213 */
19214 list_T *
19215get_vim_var_list(idx)
19216 int idx;
19217{
19218 return vimvars[idx].vv_list;
19219}
19220
19221/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019222 * Set v:char to character "c".
19223 */
19224 void
19225set_vim_var_char(c)
19226 int c;
19227{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019228 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019229
19230#ifdef FEAT_MBYTE
19231 if (has_mbyte)
19232 buf[(*mb_char2bytes)(c, buf)] = NUL;
19233 else
19234#endif
19235 {
19236 buf[0] = c;
19237 buf[1] = NUL;
19238 }
19239 set_vim_var_string(VV_CHAR, buf, -1);
19240}
19241
19242/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019243 * Set v:count to "count" and v:count1 to "count1".
19244 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245 */
19246 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019247set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019248 long count;
19249 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019250 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019252 if (set_prevcount)
19253 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019254 vimvars[VV_COUNT].vv_nr = count;
19255 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256}
19257
19258/*
19259 * Set string v: variable to a copy of "val".
19260 */
19261 void
19262set_vim_var_string(idx, val, len)
19263 int idx;
19264 char_u *val;
19265 int len; /* length of "val" to use or -1 (whole string) */
19266{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019267 /* Need to do this (at least) once, since we can't initialize a union.
19268 * Will always be invoked when "v:progname" is set. */
19269 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19270
Bram Moolenaare9a41262005-01-15 22:18:47 +000019271 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019273 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019274 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019275 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019276 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019277 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278}
19279
19280/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019281 * Set List v: variable to "val".
19282 */
19283 void
19284set_vim_var_list(idx, val)
19285 int idx;
19286 list_T *val;
19287{
19288 list_unref(vimvars[idx].vv_list);
19289 vimvars[idx].vv_list = val;
19290 if (val != NULL)
19291 ++val->lv_refcount;
19292}
19293
19294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295 * Set v:register if needed.
19296 */
19297 void
19298set_reg_var(c)
19299 int c;
19300{
19301 char_u regname;
19302
19303 if (c == 0 || c == ' ')
19304 regname = '"';
19305 else
19306 regname = c;
19307 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019308 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309 set_vim_var_string(VV_REG, &regname, 1);
19310}
19311
19312/*
19313 * Get or set v:exception. If "oldval" == NULL, return the current value.
19314 * Otherwise, restore the value to "oldval" and return NULL.
19315 * Must always be called in pairs to save and restore v:exception! Does not
19316 * take care of memory allocations.
19317 */
19318 char_u *
19319v_exception(oldval)
19320 char_u *oldval;
19321{
19322 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019323 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324
Bram Moolenaare9a41262005-01-15 22:18:47 +000019325 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326 return NULL;
19327}
19328
19329/*
19330 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19331 * Otherwise, restore the value to "oldval" and return NULL.
19332 * Must always be called in pairs to save and restore v:throwpoint! Does not
19333 * take care of memory allocations.
19334 */
19335 char_u *
19336v_throwpoint(oldval)
19337 char_u *oldval;
19338{
19339 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019340 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341
Bram Moolenaare9a41262005-01-15 22:18:47 +000019342 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343 return NULL;
19344}
19345
19346#if defined(FEAT_AUTOCMD) || defined(PROTO)
19347/*
19348 * Set v:cmdarg.
19349 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19350 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19351 * Must always be called in pairs!
19352 */
19353 char_u *
19354set_cmdarg(eap, oldarg)
19355 exarg_T *eap;
19356 char_u *oldarg;
19357{
19358 char_u *oldval;
19359 char_u *newval;
19360 unsigned len;
19361
Bram Moolenaare9a41262005-01-15 22:18:47 +000019362 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019363 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019365 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019366 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019367 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368 }
19369
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019370 if (eap->force_bin == FORCE_BIN)
19371 len = 6;
19372 else if (eap->force_bin == FORCE_NOBIN)
19373 len = 8;
19374 else
19375 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019376
19377 if (eap->read_edit)
19378 len += 7;
19379
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019380 if (eap->force_ff != 0)
19381 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19382# ifdef FEAT_MBYTE
19383 if (eap->force_enc != 0)
19384 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019385 if (eap->bad_char != 0)
19386 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019387# endif
19388
19389 newval = alloc(len + 1);
19390 if (newval == NULL)
19391 return NULL;
19392
19393 if (eap->force_bin == FORCE_BIN)
19394 sprintf((char *)newval, " ++bin");
19395 else if (eap->force_bin == FORCE_NOBIN)
19396 sprintf((char *)newval, " ++nobin");
19397 else
19398 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019399
19400 if (eap->read_edit)
19401 STRCAT(newval, " ++edit");
19402
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019403 if (eap->force_ff != 0)
19404 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19405 eap->cmd + eap->force_ff);
19406# ifdef FEAT_MBYTE
19407 if (eap->force_enc != 0)
19408 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19409 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019410 if (eap->bad_char == BAD_KEEP)
19411 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19412 else if (eap->bad_char == BAD_DROP)
19413 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19414 else if (eap->bad_char != 0)
19415 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019416# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019417 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019418 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019419}
19420#endif
19421
19422/*
19423 * Get the value of internal variable "name".
19424 * Return OK or FAIL.
19425 */
19426 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019427get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019428 char_u *name;
19429 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019430 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019431 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019432{
19433 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019434 typval_T *tv = NULL;
19435 typval_T atv;
19436 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019437 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019438
19439 /* truncate the name, so that we can use strcmp() */
19440 cc = name[len];
19441 name[len] = NUL;
19442
19443 /*
19444 * Check for "b:changedtick".
19445 */
19446 if (STRCMP(name, "b:changedtick") == 0)
19447 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019448 atv.v_type = VAR_NUMBER;
19449 atv.vval.v_number = curbuf->b_changedtick;
19450 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451 }
19452
19453 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454 * Check for user-defined variables.
19455 */
19456 else
19457 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019458 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019460 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461 }
19462
Bram Moolenaare9a41262005-01-15 22:18:47 +000019463 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019464 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019465 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019466 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019467 ret = FAIL;
19468 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019469 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019470 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019471
19472 name[len] = cc;
19473
19474 return ret;
19475}
19476
19477/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019478 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19479 * Also handle function call with Funcref variable: func(expr)
19480 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19481 */
19482 static int
19483handle_subscript(arg, rettv, evaluate, verbose)
19484 char_u **arg;
19485 typval_T *rettv;
19486 int evaluate; /* do more than finding the end */
19487 int verbose; /* give error messages */
19488{
19489 int ret = OK;
19490 dict_T *selfdict = NULL;
19491 char_u *s;
19492 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019493 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019494
19495 while (ret == OK
19496 && (**arg == '['
19497 || (**arg == '.' && rettv->v_type == VAR_DICT)
19498 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19499 && !vim_iswhite(*(*arg - 1)))
19500 {
19501 if (**arg == '(')
19502 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019503 /* need to copy the funcref so that we can clear rettv */
19504 functv = *rettv;
19505 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019506
19507 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019508 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019509 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019510 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19511 &len, evaluate, selfdict);
19512
19513 /* Clear the funcref afterwards, so that deleting it while
19514 * evaluating the arguments is possible (see test55). */
19515 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019516
19517 /* Stop the expression evaluation when immediately aborting on
19518 * error, or when an interrupt occurred or an exception was thrown
19519 * but not caught. */
19520 if (aborting())
19521 {
19522 if (ret == OK)
19523 clear_tv(rettv);
19524 ret = FAIL;
19525 }
19526 dict_unref(selfdict);
19527 selfdict = NULL;
19528 }
19529 else /* **arg == '[' || **arg == '.' */
19530 {
19531 dict_unref(selfdict);
19532 if (rettv->v_type == VAR_DICT)
19533 {
19534 selfdict = rettv->vval.v_dict;
19535 if (selfdict != NULL)
19536 ++selfdict->dv_refcount;
19537 }
19538 else
19539 selfdict = NULL;
19540 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19541 {
19542 clear_tv(rettv);
19543 ret = FAIL;
19544 }
19545 }
19546 }
19547 dict_unref(selfdict);
19548 return ret;
19549}
19550
19551/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019552 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019553 * value).
19554 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019555 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019556alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019557{
Bram Moolenaar33570922005-01-25 22:26:29 +000019558 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019559}
19560
19561/*
19562 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563 * The string "s" must have been allocated, it is consumed.
19564 * Return NULL for out of memory, the variable otherwise.
19565 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019566 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019567alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568 char_u *s;
19569{
Bram Moolenaar33570922005-01-25 22:26:29 +000019570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019572 rettv = alloc_tv();
19573 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019574 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019575 rettv->v_type = VAR_STRING;
19576 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019577 }
19578 else
19579 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019580 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019581}
19582
19583/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019584 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019585 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019586 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019587free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019588 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019589{
19590 if (varp != NULL)
19591 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019592 switch (varp->v_type)
19593 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019594 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019595 func_unref(varp->vval.v_string);
19596 /*FALLTHROUGH*/
19597 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019598 vim_free(varp->vval.v_string);
19599 break;
19600 case VAR_LIST:
19601 list_unref(varp->vval.v_list);
19602 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019603 case VAR_DICT:
19604 dict_unref(varp->vval.v_dict);
19605 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019606 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019607#ifdef FEAT_FLOAT
19608 case VAR_FLOAT:
19609#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019610 case VAR_UNKNOWN:
19611 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019612 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019613 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019614 break;
19615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 vim_free(varp);
19617 }
19618}
19619
19620/*
19621 * Free the memory for a variable value and set the value to NULL or 0.
19622 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019623 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019624clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019625 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626{
19627 if (varp != NULL)
19628 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019629 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019631 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019632 func_unref(varp->vval.v_string);
19633 /*FALLTHROUGH*/
19634 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019635 vim_free(varp->vval.v_string);
19636 varp->vval.v_string = NULL;
19637 break;
19638 case VAR_LIST:
19639 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019640 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019641 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019642 case VAR_DICT:
19643 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019644 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019645 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019646 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019647 varp->vval.v_number = 0;
19648 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019649#ifdef FEAT_FLOAT
19650 case VAR_FLOAT:
19651 varp->vval.v_float = 0.0;
19652 break;
19653#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019654 case VAR_UNKNOWN:
19655 break;
19656 default:
19657 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019659 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660 }
19661}
19662
19663/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019664 * Set the value of a variable to NULL without freeing items.
19665 */
19666 static void
19667init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019668 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019669{
19670 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019671 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019672}
19673
19674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019675 * Get the number value of a variable.
19676 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019677 * For incompatible types, return 0.
19678 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19679 * caller of incompatible types: it sets *denote to TRUE if "denote"
19680 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681 */
19682 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019683get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019684 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019686 int error = FALSE;
19687
19688 return get_tv_number_chk(varp, &error); /* return 0L on error */
19689}
19690
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019691 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019692get_tv_number_chk(varp, denote)
19693 typval_T *varp;
19694 int *denote;
19695{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019696 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019697
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019698 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019700 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019701 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019702#ifdef FEAT_FLOAT
19703 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019704 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019705 break;
19706#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019707 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019708 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019709 break;
19710 case VAR_STRING:
19711 if (varp->vval.v_string != NULL)
19712 vim_str2nr(varp->vval.v_string, NULL, NULL,
19713 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019714 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019715 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019716 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019717 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019718 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019719 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019720 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019721 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019722 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019723 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019724 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019725 if (denote == NULL) /* useful for values that must be unsigned */
19726 n = -1;
19727 else
19728 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019729 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730}
19731
19732/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019733 * Get the lnum from the first argument.
19734 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019735 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736 */
19737 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019738get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019739 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740{
Bram Moolenaar33570922005-01-25 22:26:29 +000019741 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019742 linenr_T lnum;
19743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019744 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745 if (lnum == 0) /* no valid number, try using line() */
19746 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019747 rettv.v_type = VAR_NUMBER;
19748 f_line(argvars, &rettv);
19749 lnum = rettv.vval.v_number;
19750 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751 }
19752 return lnum;
19753}
19754
19755/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019756 * Get the lnum from the first argument.
19757 * Also accepts "$", then "buf" is used.
19758 * Returns 0 on error.
19759 */
19760 static linenr_T
19761get_tv_lnum_buf(argvars, buf)
19762 typval_T *argvars;
19763 buf_T *buf;
19764{
19765 if (argvars[0].v_type == VAR_STRING
19766 && argvars[0].vval.v_string != NULL
19767 && argvars[0].vval.v_string[0] == '$'
19768 && buf != NULL)
19769 return buf->b_ml.ml_line_count;
19770 return get_tv_number_chk(&argvars[0], NULL);
19771}
19772
19773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774 * Get the string value of a variable.
19775 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019776 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19777 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778 * If the String variable has never been set, return an empty string.
19779 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019780 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19781 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782 */
19783 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019784get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019785 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786{
19787 static char_u mybuf[NUMBUFLEN];
19788
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019789 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790}
19791
19792 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019793get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019794 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019795 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019797 char_u *res = get_tv_string_buf_chk(varp, buf);
19798
19799 return res != NULL ? res : (char_u *)"";
19800}
19801
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019802 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019803get_tv_string_chk(varp)
19804 typval_T *varp;
19805{
19806 static char_u mybuf[NUMBUFLEN];
19807
19808 return get_tv_string_buf_chk(varp, mybuf);
19809}
19810
19811 static char_u *
19812get_tv_string_buf_chk(varp, buf)
19813 typval_T *varp;
19814 char_u *buf;
19815{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019816 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019818 case VAR_NUMBER:
19819 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19820 return buf;
19821 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019822 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019823 break;
19824 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019825 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019826 break;
19827 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019828 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019829 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019830#ifdef FEAT_FLOAT
19831 case VAR_FLOAT:
19832 EMSG(_("E806: using Float as a String"));
19833 break;
19834#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019835 case VAR_STRING:
19836 if (varp->vval.v_string != NULL)
19837 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019838 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019839 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019840 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019841 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019843 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019844}
19845
19846/*
19847 * Find variable "name" in the list of variables.
19848 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019849 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019850 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019851 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019852 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019853 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019854find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019855 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019856 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019858 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019859 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860
Bram Moolenaara7043832005-01-21 11:56:39 +000019861 ht = find_var_ht(name, &varname);
19862 if (htp != NULL)
19863 *htp = ht;
19864 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019866 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867}
19868
19869/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019870 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019871 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019873 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019874find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019875 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019876 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019877 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019878{
Bram Moolenaar33570922005-01-25 22:26:29 +000019879 hashitem_T *hi;
19880
19881 if (*varname == NUL)
19882 {
19883 /* Must be something like "s:", otherwise "ht" would be NULL. */
19884 switch (varname[-2])
19885 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019886 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019887 case 'g': return &globvars_var;
19888 case 'v': return &vimvars_var;
19889 case 'b': return &curbuf->b_bufvar;
19890 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019891#ifdef FEAT_WINDOWS
19892 case 't': return &curtab->tp_winvar;
19893#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019894 case 'l': return current_funccal == NULL
19895 ? NULL : &current_funccal->l_vars_var;
19896 case 'a': return current_funccal == NULL
19897 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019898 }
19899 return NULL;
19900 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019901
19902 hi = hash_find(ht, varname);
19903 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019904 {
19905 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019906 * worked find the variable again. Don't auto-load a script if it was
19907 * loaded already, otherwise it would be loaded every time when
19908 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019909 if (ht == &globvarht && !writing)
19910 {
19911 /* Note: script_autoload() may make "hi" invalid. It must either
19912 * be obtained again or not used. */
19913 if (!script_autoload(varname, FALSE) || aborting())
19914 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019915 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019916 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019917 if (HASHITEM_EMPTY(hi))
19918 return NULL;
19919 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019920 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019921}
19922
19923/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019924 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019925 * Set "varname" to the start of name without ':'.
19926 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019927 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019928find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929 char_u *name;
19930 char_u **varname;
19931{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019932 hashitem_T *hi;
19933
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 if (name[1] != ':')
19935 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019936 /* The name must not start with a colon or #. */
19937 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938 return NULL;
19939 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019940
19941 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019942 hi = hash_find(&compat_hashtab, name);
19943 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019944 return &compat_hashtab;
19945
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019947 return &globvarht; /* global variable */
19948 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949 }
19950 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019951 if (*name == 'g') /* global variable */
19952 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019953 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19954 */
19955 if (vim_strchr(name + 2, ':') != NULL
19956 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019957 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019958 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019959 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019960 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019961 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019962#ifdef FEAT_WINDOWS
19963 if (*name == 't') /* tab page variable */
19964 return &curtab->tp_vars.dv_hashtab;
19965#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019966 if (*name == 'v') /* v: variable */
19967 return &vimvarht;
19968 if (*name == 'a' && current_funccal != NULL) /* function argument */
19969 return &current_funccal->l_avars.dv_hashtab;
19970 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19971 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972 if (*name == 's' /* script variable */
19973 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19974 return &SCRIPT_VARS(current_SID);
19975 return NULL;
19976}
19977
19978/*
19979 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019980 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019981 * Returns NULL when it doesn't exist.
19982 */
19983 char_u *
19984get_var_value(name)
19985 char_u *name;
19986{
Bram Moolenaar33570922005-01-25 22:26:29 +000019987 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988
Bram Moolenaara7043832005-01-21 11:56:39 +000019989 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990 if (v == NULL)
19991 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019992 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993}
19994
19995/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019996 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997 * sourcing this script and when executing functions defined in the script.
19998 */
19999 void
20000new_script_vars(id)
20001 scid_T id;
20002{
Bram Moolenaara7043832005-01-21 11:56:39 +000020003 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020004 hashtab_T *ht;
20005 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020006
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20008 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020009 /* Re-allocating ga_data means that an ht_array pointing to
20010 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020011 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020012 for (i = 1; i <= ga_scripts.ga_len; ++i)
20013 {
20014 ht = &SCRIPT_VARS(i);
20015 if (ht->ht_mask == HT_INIT_SIZE - 1)
20016 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020017 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020018 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020019 }
20020
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021 while (ga_scripts.ga_len < id)
20022 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020023 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020024 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000020025 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 }
20028 }
20029}
20030
20031/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020032 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20033 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034 */
20035 void
Bram Moolenaar33570922005-01-25 22:26:29 +000020036init_var_dict(dict, dict_var)
20037 dict_T *dict;
20038 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039{
Bram Moolenaar33570922005-01-25 22:26:29 +000020040 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020041 dict->dv_lock = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020042 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020043 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020044 dict_var->di_tv.vval.v_dict = dict;
20045 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020046 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020047 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20048 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049}
20050
20051/*
20052 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020053 * Frees all allocated variables and the value they contain.
20054 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 */
20056 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020057vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020058 hashtab_T *ht;
20059{
20060 vars_clear_ext(ht, TRUE);
20061}
20062
20063/*
20064 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20065 */
20066 static void
20067vars_clear_ext(ht, free_val)
20068 hashtab_T *ht;
20069 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020070{
Bram Moolenaara7043832005-01-21 11:56:39 +000020071 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020072 hashitem_T *hi;
20073 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074
Bram Moolenaar33570922005-01-25 22:26:29 +000020075 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020076 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020077 for (hi = ht->ht_array; todo > 0; ++hi)
20078 {
20079 if (!HASHITEM_EMPTY(hi))
20080 {
20081 --todo;
20082
Bram Moolenaar33570922005-01-25 22:26:29 +000020083 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020084 * ht_array might change then. hash_clear() takes care of it
20085 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020086 v = HI2DI(hi);
20087 if (free_val)
20088 clear_tv(&v->di_tv);
20089 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20090 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020091 }
20092 }
20093 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020094 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095}
20096
Bram Moolenaara7043832005-01-21 11:56:39 +000020097/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020098 * Delete a variable from hashtab "ht" at item "hi".
20099 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020102delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020103 hashtab_T *ht;
20104 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105{
Bram Moolenaar33570922005-01-25 22:26:29 +000020106 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020107
20108 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020109 clear_tv(&di->di_tv);
20110 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111}
20112
20113/*
20114 * List the value of one internal variable.
20115 */
20116 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020117list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020118 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020120 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020122 char_u *tofree;
20123 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020124 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020125
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020126 current_copyID += COPYID_INC;
20127 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020128 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020129 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020130 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020131}
20132
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020134list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 char_u *prefix;
20136 char_u *name;
20137 int type;
20138 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020139 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140{
Bram Moolenaar31859182007-08-14 20:41:13 +000020141 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20142 msg_start();
20143 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144 if (name != NULL) /* "a:" vars don't have a name stored */
20145 msg_puts(name);
20146 msg_putchar(' ');
20147 msg_advance(22);
20148 if (type == VAR_NUMBER)
20149 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020150 else if (type == VAR_FUNC)
20151 msg_putchar('*');
20152 else if (type == VAR_LIST)
20153 {
20154 msg_putchar('[');
20155 if (*string == '[')
20156 ++string;
20157 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020158 else if (type == VAR_DICT)
20159 {
20160 msg_putchar('{');
20161 if (*string == '{')
20162 ++string;
20163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020164 else
20165 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020166
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020168
20169 if (type == VAR_FUNC)
20170 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020171 if (*first)
20172 {
20173 msg_clr_eos();
20174 *first = FALSE;
20175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176}
20177
20178/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020179 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180 * If the variable already exists, the value is updated.
20181 * Otherwise the variable is created.
20182 */
20183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020184set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020186 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020187 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188{
Bram Moolenaar33570922005-01-25 22:26:29 +000020189 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020190 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020191 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020192
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020193 ht = find_var_ht(name, &varname);
20194 if (ht == NULL || *varname == NUL)
20195 {
20196 EMSG2(_(e_illvar), name);
20197 return;
20198 }
20199 v = find_var_in_ht(ht, varname, TRUE);
20200
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020201 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20202 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020203
Bram Moolenaar33570922005-01-25 22:26:29 +000020204 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020205 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020206 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020207 if (var_check_ro(v->di_flags, name)
20208 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020209 return;
20210 if (v->di_tv.v_type != tv->v_type
20211 && !((v->di_tv.v_type == VAR_STRING
20212 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020213 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020214 || tv->v_type == VAR_NUMBER))
20215#ifdef FEAT_FLOAT
20216 && !((v->di_tv.v_type == VAR_NUMBER
20217 || v->di_tv.v_type == VAR_FLOAT)
20218 && (tv->v_type == VAR_NUMBER
20219 || tv->v_type == VAR_FLOAT))
20220#endif
20221 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020222 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020223 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020224 return;
20225 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020226
20227 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020228 * Handle setting internal v: variables separately: we don't change
20229 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020230 */
20231 if (ht == &vimvarht)
20232 {
20233 if (v->di_tv.v_type == VAR_STRING)
20234 {
20235 vim_free(v->di_tv.vval.v_string);
20236 if (copy || tv->v_type != VAR_STRING)
20237 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20238 else
20239 {
20240 /* Take over the string to avoid an extra alloc/free. */
20241 v->di_tv.vval.v_string = tv->vval.v_string;
20242 tv->vval.v_string = NULL;
20243 }
20244 }
20245 else if (v->di_tv.v_type != VAR_NUMBER)
20246 EMSG2(_(e_intern2), "set_var()");
20247 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020248 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020249 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020250 if (STRCMP(varname, "searchforward") == 0)
20251 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20252 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020253 return;
20254 }
20255
20256 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257 }
20258 else /* add a new variable */
20259 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020260 /* Can't add "v:" variable. */
20261 if (ht == &vimvarht)
20262 {
20263 EMSG2(_(e_illvar), name);
20264 return;
20265 }
20266
Bram Moolenaar92124a32005-06-17 22:03:40 +000020267 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020268 if (!valid_varname(varname))
20269 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020270
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020271 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20272 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020273 if (v == NULL)
20274 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020275 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020276 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020278 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279 return;
20280 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020281 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020283
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020284 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020285 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020286 else
20287 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020288 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020289 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020290 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292}
20293
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020294/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020295 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020296 * Also give an error message.
20297 */
20298 static int
20299var_check_ro(flags, name)
20300 int flags;
20301 char_u *name;
20302{
20303 if (flags & DI_FLAGS_RO)
20304 {
20305 EMSG2(_(e_readonlyvar), name);
20306 return TRUE;
20307 }
20308 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20309 {
20310 EMSG2(_(e_readonlysbx), name);
20311 return TRUE;
20312 }
20313 return FALSE;
20314}
20315
20316/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020317 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20318 * Also give an error message.
20319 */
20320 static int
20321var_check_fixed(flags, name)
20322 int flags;
20323 char_u *name;
20324{
20325 if (flags & DI_FLAGS_FIX)
20326 {
20327 EMSG2(_("E795: Cannot delete variable %s"), name);
20328 return TRUE;
20329 }
20330 return FALSE;
20331}
20332
20333/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020334 * Check if a funcref is assigned to a valid variable name.
20335 * Return TRUE and give an error if not.
20336 */
20337 static int
20338var_check_func_name(name, new_var)
20339 char_u *name; /* points to start of variable name */
20340 int new_var; /* TRUE when creating the variable */
20341{
20342 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20343 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20344 ? name[2] : name[0]))
20345 {
20346 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20347 name);
20348 return TRUE;
20349 }
20350 /* Don't allow hiding a function. When "v" is not NULL we might be
20351 * assigning another function to the same var, the type is checked
20352 * below. */
20353 if (new_var && function_exists(name))
20354 {
20355 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20356 name);
20357 return TRUE;
20358 }
20359 return FALSE;
20360}
20361
20362/*
20363 * Check if a variable name is valid.
20364 * Return FALSE and give an error if not.
20365 */
20366 static int
20367valid_varname(varname)
20368 char_u *varname;
20369{
20370 char_u *p;
20371
20372 for (p = varname; *p != NUL; ++p)
20373 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20374 && *p != AUTOLOAD_CHAR)
20375 {
20376 EMSG2(_(e_illvar), varname);
20377 return FALSE;
20378 }
20379 return TRUE;
20380}
20381
20382/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020383 * Return TRUE if typeval "tv" is set to be locked (immutable).
20384 * Also give an error message, using "name".
20385 */
20386 static int
20387tv_check_lock(lock, name)
20388 int lock;
20389 char_u *name;
20390{
20391 if (lock & VAR_LOCKED)
20392 {
20393 EMSG2(_("E741: Value is locked: %s"),
20394 name == NULL ? (char_u *)_("Unknown") : name);
20395 return TRUE;
20396 }
20397 if (lock & VAR_FIXED)
20398 {
20399 EMSG2(_("E742: Cannot change value of %s"),
20400 name == NULL ? (char_u *)_("Unknown") : name);
20401 return TRUE;
20402 }
20403 return FALSE;
20404}
20405
20406/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020407 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020408 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020409 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020410 * It is OK for "from" and "to" to point to the same item. This is used to
20411 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020412 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020413 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020414copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020415 typval_T *from;
20416 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020418 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020419 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020420 switch (from->v_type)
20421 {
20422 case VAR_NUMBER:
20423 to->vval.v_number = from->vval.v_number;
20424 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020425#ifdef FEAT_FLOAT
20426 case VAR_FLOAT:
20427 to->vval.v_float = from->vval.v_float;
20428 break;
20429#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020430 case VAR_STRING:
20431 case VAR_FUNC:
20432 if (from->vval.v_string == NULL)
20433 to->vval.v_string = NULL;
20434 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020435 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020436 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020437 if (from->v_type == VAR_FUNC)
20438 func_ref(to->vval.v_string);
20439 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020440 break;
20441 case VAR_LIST:
20442 if (from->vval.v_list == NULL)
20443 to->vval.v_list = NULL;
20444 else
20445 {
20446 to->vval.v_list = from->vval.v_list;
20447 ++to->vval.v_list->lv_refcount;
20448 }
20449 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020450 case VAR_DICT:
20451 if (from->vval.v_dict == NULL)
20452 to->vval.v_dict = NULL;
20453 else
20454 {
20455 to->vval.v_dict = from->vval.v_dict;
20456 ++to->vval.v_dict->dv_refcount;
20457 }
20458 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020459 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020460 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020461 break;
20462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463}
20464
20465/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020466 * Make a copy of an item.
20467 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020468 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20469 * reference to an already copied list/dict can be used.
20470 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020471 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020472 static int
20473item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020474 typval_T *from;
20475 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020476 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020477 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020478{
20479 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020480 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020481
Bram Moolenaar33570922005-01-25 22:26:29 +000020482 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020483 {
20484 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020485 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020486 }
20487 ++recurse;
20488
20489 switch (from->v_type)
20490 {
20491 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020492#ifdef FEAT_FLOAT
20493 case VAR_FLOAT:
20494#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020495 case VAR_STRING:
20496 case VAR_FUNC:
20497 copy_tv(from, to);
20498 break;
20499 case VAR_LIST:
20500 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020501 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020502 if (from->vval.v_list == NULL)
20503 to->vval.v_list = NULL;
20504 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20505 {
20506 /* use the copy made earlier */
20507 to->vval.v_list = from->vval.v_list->lv_copylist;
20508 ++to->vval.v_list->lv_refcount;
20509 }
20510 else
20511 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20512 if (to->vval.v_list == NULL)
20513 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020514 break;
20515 case VAR_DICT:
20516 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020517 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020518 if (from->vval.v_dict == NULL)
20519 to->vval.v_dict = NULL;
20520 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20521 {
20522 /* use the copy made earlier */
20523 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20524 ++to->vval.v_dict->dv_refcount;
20525 }
20526 else
20527 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20528 if (to->vval.v_dict == NULL)
20529 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020530 break;
20531 default:
20532 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020533 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020534 }
20535 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020536 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020537}
20538
20539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020540 * ":echo expr1 ..." print each argument separated with a space, add a
20541 * newline at the end.
20542 * ":echon expr1 ..." print each argument plain.
20543 */
20544 void
20545ex_echo(eap)
20546 exarg_T *eap;
20547{
20548 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020549 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020550 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551 char_u *p;
20552 int needclr = TRUE;
20553 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020554 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555
20556 if (eap->skip)
20557 ++emsg_skip;
20558 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20559 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020560 /* If eval1() causes an error message the text from the command may
20561 * still need to be cleared. E.g., "echo 22,44". */
20562 need_clr_eos = needclr;
20563
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020565 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566 {
20567 /*
20568 * Report the invalid expression unless the expression evaluation
20569 * has been cancelled due to an aborting error, an interrupt, or an
20570 * exception.
20571 */
20572 if (!aborting())
20573 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020574 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575 break;
20576 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020577 need_clr_eos = FALSE;
20578
Bram Moolenaar071d4272004-06-13 20:20:40 +000020579 if (!eap->skip)
20580 {
20581 if (atstart)
20582 {
20583 atstart = FALSE;
20584 /* Call msg_start() after eval1(), evaluating the expression
20585 * may cause a message to appear. */
20586 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020587 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020588 /* Mark the saved text as finishing the line, so that what
20589 * follows is displayed on a new line when scrolling back
20590 * at the more prompt. */
20591 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020594 }
20595 else if (eap->cmdidx == CMD_echo)
20596 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020597 current_copyID += COPYID_INC;
20598 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020599 if (p != NULL)
20600 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020601 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020602 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020603 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020604 if (*p != TAB && needclr)
20605 {
20606 /* remove any text still there from the command */
20607 msg_clr_eos();
20608 needclr = FALSE;
20609 }
20610 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611 }
20612 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020613 {
20614#ifdef FEAT_MBYTE
20615 if (has_mbyte)
20616 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020617 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020618
20619 (void)msg_outtrans_len_attr(p, i, echo_attr);
20620 p += i - 1;
20621 }
20622 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020623#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020624 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020627 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020628 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020629 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020630 arg = skipwhite(arg);
20631 }
20632 eap->nextcmd = check_nextcmd(arg);
20633
20634 if (eap->skip)
20635 --emsg_skip;
20636 else
20637 {
20638 /* remove text that may still be there from the command */
20639 if (needclr)
20640 msg_clr_eos();
20641 if (eap->cmdidx == CMD_echo)
20642 msg_end();
20643 }
20644}
20645
20646/*
20647 * ":echohl {name}".
20648 */
20649 void
20650ex_echohl(eap)
20651 exarg_T *eap;
20652{
20653 int id;
20654
20655 id = syn_name2id(eap->arg);
20656 if (id == 0)
20657 echo_attr = 0;
20658 else
20659 echo_attr = syn_id2attr(id);
20660}
20661
20662/*
20663 * ":execute expr1 ..." execute the result of an expression.
20664 * ":echomsg expr1 ..." Print a message
20665 * ":echoerr expr1 ..." Print an error
20666 * Each gets spaces around each argument and a newline at the end for
20667 * echo commands
20668 */
20669 void
20670ex_execute(eap)
20671 exarg_T *eap;
20672{
20673 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020674 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020675 int ret = OK;
20676 char_u *p;
20677 garray_T ga;
20678 int len;
20679 int save_did_emsg;
20680
20681 ga_init2(&ga, 1, 80);
20682
20683 if (eap->skip)
20684 ++emsg_skip;
20685 while (*arg != NUL && *arg != '|' && *arg != '\n')
20686 {
20687 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020688 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020689 {
20690 /*
20691 * Report the invalid expression unless the expression evaluation
20692 * has been cancelled due to an aborting error, an interrupt, or an
20693 * exception.
20694 */
20695 if (!aborting())
20696 EMSG2(_(e_invexpr2), p);
20697 ret = FAIL;
20698 break;
20699 }
20700
20701 if (!eap->skip)
20702 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020703 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020704 len = (int)STRLEN(p);
20705 if (ga_grow(&ga, len + 2) == FAIL)
20706 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020707 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708 ret = FAIL;
20709 break;
20710 }
20711 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020714 ga.ga_len += len;
20715 }
20716
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020717 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020718 arg = skipwhite(arg);
20719 }
20720
20721 if (ret != FAIL && ga.ga_data != NULL)
20722 {
20723 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020724 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020726 out_flush();
20727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020728 else if (eap->cmdidx == CMD_echoerr)
20729 {
20730 /* We don't want to abort following commands, restore did_emsg. */
20731 save_did_emsg = did_emsg;
20732 EMSG((char_u *)ga.ga_data);
20733 if (!force_abort)
20734 did_emsg = save_did_emsg;
20735 }
20736 else if (eap->cmdidx == CMD_execute)
20737 do_cmdline((char_u *)ga.ga_data,
20738 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20739 }
20740
20741 ga_clear(&ga);
20742
20743 if (eap->skip)
20744 --emsg_skip;
20745
20746 eap->nextcmd = check_nextcmd(arg);
20747}
20748
20749/*
20750 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20751 * "arg" points to the "&" or '+' when called, to "option" when returning.
20752 * Returns NULL when no option name found. Otherwise pointer to the char
20753 * after the option name.
20754 */
20755 static char_u *
20756find_option_end(arg, opt_flags)
20757 char_u **arg;
20758 int *opt_flags;
20759{
20760 char_u *p = *arg;
20761
20762 ++p;
20763 if (*p == 'g' && p[1] == ':')
20764 {
20765 *opt_flags = OPT_GLOBAL;
20766 p += 2;
20767 }
20768 else if (*p == 'l' && p[1] == ':')
20769 {
20770 *opt_flags = OPT_LOCAL;
20771 p += 2;
20772 }
20773 else
20774 *opt_flags = 0;
20775
20776 if (!ASCII_ISALPHA(*p))
20777 return NULL;
20778 *arg = p;
20779
20780 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20781 p += 4; /* termcap option */
20782 else
20783 while (ASCII_ISALPHA(*p))
20784 ++p;
20785 return p;
20786}
20787
20788/*
20789 * ":function"
20790 */
20791 void
20792ex_function(eap)
20793 exarg_T *eap;
20794{
20795 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020796 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797 int j;
20798 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020799 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800 char_u *name = NULL;
20801 char_u *p;
20802 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020803 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020804 garray_T newargs;
20805 garray_T newlines;
20806 int varargs = FALSE;
20807 int mustend = FALSE;
20808 int flags = 0;
20809 ufunc_T *fp;
20810 int indent;
20811 int nesting;
20812 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020813 dictitem_T *v;
20814 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020815 static int func_nr = 0; /* number for nameless function */
20816 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020817 hashtab_T *ht;
20818 int todo;
20819 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020820 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821
20822 /*
20823 * ":function" without argument: list functions.
20824 */
20825 if (ends_excmd(*eap->arg))
20826 {
20827 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020828 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020829 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020830 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020831 {
20832 if (!HASHITEM_EMPTY(hi))
20833 {
20834 --todo;
20835 fp = HI2UF(hi);
20836 if (!isdigit(*fp->uf_name))
20837 list_func_head(fp, FALSE);
20838 }
20839 }
20840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841 eap->nextcmd = check_nextcmd(eap->arg);
20842 return;
20843 }
20844
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020845 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020846 * ":function /pat": list functions matching pattern.
20847 */
20848 if (*eap->arg == '/')
20849 {
20850 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20851 if (!eap->skip)
20852 {
20853 regmatch_T regmatch;
20854
20855 c = *p;
20856 *p = NUL;
20857 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20858 *p = c;
20859 if (regmatch.regprog != NULL)
20860 {
20861 regmatch.rm_ic = p_ic;
20862
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020863 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020864 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20865 {
20866 if (!HASHITEM_EMPTY(hi))
20867 {
20868 --todo;
20869 fp = HI2UF(hi);
20870 if (!isdigit(*fp->uf_name)
20871 && vim_regexec(&regmatch, fp->uf_name, 0))
20872 list_func_head(fp, FALSE);
20873 }
20874 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020875 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020876 }
20877 }
20878 if (*p == '/')
20879 ++p;
20880 eap->nextcmd = check_nextcmd(p);
20881 return;
20882 }
20883
20884 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020885 * Get the function name. There are these situations:
20886 * func normal function name
20887 * "name" == func, "fudi.fd_dict" == NULL
20888 * dict.func new dictionary entry
20889 * "name" == NULL, "fudi.fd_dict" set,
20890 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20891 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020892 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020893 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20894 * dict.func existing dict entry that's not a Funcref
20895 * "name" == NULL, "fudi.fd_dict" set,
20896 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020898 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020899 name = trans_function_name(&p, eap->skip, 0, &fudi);
20900 paren = (vim_strchr(p, '(') != NULL);
20901 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902 {
20903 /*
20904 * Return on an invalid expression in braces, unless the expression
20905 * evaluation has been cancelled due to an aborting error, an
20906 * interrupt, or an exception.
20907 */
20908 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020909 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020910 if (!eap->skip && fudi.fd_newkey != NULL)
20911 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020912 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020915 else
20916 eap->skip = TRUE;
20917 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020918
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919 /* An error in a function call during evaluation of an expression in magic
20920 * braces should not cause the function not to be defined. */
20921 saved_did_emsg = did_emsg;
20922 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923
20924 /*
20925 * ":function func" with only function name: list function.
20926 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020927 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020928 {
20929 if (!ends_excmd(*skipwhite(p)))
20930 {
20931 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020932 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020933 }
20934 eap->nextcmd = check_nextcmd(p);
20935 if (eap->nextcmd != NULL)
20936 *p = NUL;
20937 if (!eap->skip && !got_int)
20938 {
20939 fp = find_func(name);
20940 if (fp != NULL)
20941 {
20942 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020943 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020945 if (FUNCLINE(fp, j) == NULL)
20946 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020947 msg_putchar('\n');
20948 msg_outnum((long)(j + 1));
20949 if (j < 9)
20950 msg_putchar(' ');
20951 if (j < 99)
20952 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020953 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954 out_flush(); /* show a line at a time */
20955 ui_breakcheck();
20956 }
20957 if (!got_int)
20958 {
20959 msg_putchar('\n');
20960 msg_puts((char_u *)" endfunction");
20961 }
20962 }
20963 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020964 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020965 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020966 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020967 }
20968
20969 /*
20970 * ":function name(arg1, arg2)" Define function.
20971 */
20972 p = skipwhite(p);
20973 if (*p != '(')
20974 {
20975 if (!eap->skip)
20976 {
20977 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020978 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979 }
20980 /* attempt to continue by skipping some text */
20981 if (vim_strchr(p, '(') != NULL)
20982 p = vim_strchr(p, '(');
20983 }
20984 p = skipwhite(p + 1);
20985
20986 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20987 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20988
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020989 if (!eap->skip)
20990 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020991 /* Check the name of the function. Unless it's a dictionary function
20992 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020993 if (name != NULL)
20994 arg = name;
20995 else
20996 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020997 if (arg != NULL && (fudi.fd_di == NULL
20998 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020999 {
21000 if (*arg == K_SPECIAL)
21001 j = 3;
21002 else
21003 j = 0;
21004 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21005 : eval_isnamec(arg[j])))
21006 ++j;
21007 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021008 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021009 }
21010 }
21011
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 /*
21013 * Isolate the arguments: "arg1, arg2, ...)"
21014 */
21015 while (*p != ')')
21016 {
21017 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21018 {
21019 varargs = TRUE;
21020 p += 3;
21021 mustend = TRUE;
21022 }
21023 else
21024 {
21025 arg = p;
21026 while (ASCII_ISALNUM(*p) || *p == '_')
21027 ++p;
21028 if (arg == p || isdigit(*arg)
21029 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21030 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21031 {
21032 if (!eap->skip)
21033 EMSG2(_("E125: Illegal argument: %s"), arg);
21034 break;
21035 }
21036 if (ga_grow(&newargs, 1) == FAIL)
21037 goto erret;
21038 c = *p;
21039 *p = NUL;
21040 arg = vim_strsave(arg);
21041 if (arg == NULL)
21042 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021043
21044 /* Check for duplicate argument name. */
21045 for (i = 0; i < newargs.ga_len; ++i)
21046 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21047 {
21048 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21049 goto erret;
21050 }
21051
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21053 *p = c;
21054 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055 if (*p == ',')
21056 ++p;
21057 else
21058 mustend = TRUE;
21059 }
21060 p = skipwhite(p);
21061 if (mustend && *p != ')')
21062 {
21063 if (!eap->skip)
21064 EMSG2(_(e_invarg2), eap->arg);
21065 break;
21066 }
21067 }
21068 ++p; /* skip the ')' */
21069
Bram Moolenaare9a41262005-01-15 22:18:47 +000021070 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071 for (;;)
21072 {
21073 p = skipwhite(p);
21074 if (STRNCMP(p, "range", 5) == 0)
21075 {
21076 flags |= FC_RANGE;
21077 p += 5;
21078 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021079 else if (STRNCMP(p, "dict", 4) == 0)
21080 {
21081 flags |= FC_DICT;
21082 p += 4;
21083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021084 else if (STRNCMP(p, "abort", 5) == 0)
21085 {
21086 flags |= FC_ABORT;
21087 p += 5;
21088 }
21089 else
21090 break;
21091 }
21092
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021093 /* When there is a line break use what follows for the function body.
21094 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21095 if (*p == '\n')
21096 line_arg = p + 1;
21097 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098 EMSG(_(e_trailing));
21099
21100 /*
21101 * Read the body of the function, until ":endfunction" is found.
21102 */
21103 if (KeyTyped)
21104 {
21105 /* Check if the function already exists, don't let the user type the
21106 * whole function before telling him it doesn't work! For a script we
21107 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021108 if (!eap->skip && !eap->forceit)
21109 {
21110 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21111 EMSG(_(e_funcdict));
21112 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021113 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021116 if (!eap->skip && did_emsg)
21117 goto erret;
21118
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119 msg_putchar('\n'); /* don't overwrite the function name */
21120 cmdline_row = msg_row;
21121 }
21122
21123 indent = 2;
21124 nesting = 0;
21125 for (;;)
21126 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021127 if (KeyTyped)
21128 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021130 sourcing_lnum_off = sourcing_lnum;
21131
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021132 if (line_arg != NULL)
21133 {
21134 /* Use eap->arg, split up in parts by line breaks. */
21135 theline = line_arg;
21136 p = vim_strchr(theline, '\n');
21137 if (p == NULL)
21138 line_arg += STRLEN(line_arg);
21139 else
21140 {
21141 *p = NUL;
21142 line_arg = p + 1;
21143 }
21144 }
21145 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 theline = getcmdline(':', 0L, indent);
21147 else
21148 theline = eap->getline(':', eap->cookie, indent);
21149 if (KeyTyped)
21150 lines_left = Rows - 1;
21151 if (theline == NULL)
21152 {
21153 EMSG(_("E126: Missing :endfunction"));
21154 goto erret;
21155 }
21156
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021157 /* Detect line continuation: sourcing_lnum increased more than one. */
21158 if (sourcing_lnum > sourcing_lnum_off + 1)
21159 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21160 else
21161 sourcing_lnum_off = 0;
21162
Bram Moolenaar071d4272004-06-13 20:20:40 +000021163 if (skip_until != NULL)
21164 {
21165 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21166 * don't check for ":endfunc". */
21167 if (STRCMP(theline, skip_until) == 0)
21168 {
21169 vim_free(skip_until);
21170 skip_until = NULL;
21171 }
21172 }
21173 else
21174 {
21175 /* skip ':' and blanks*/
21176 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21177 ;
21178
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021179 /* Check for "endfunction". */
21180 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021182 if (line_arg == NULL)
21183 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184 break;
21185 }
21186
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021187 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188 * at "end". */
21189 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21190 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021191 else if (STRNCMP(p, "if", 2) == 0
21192 || STRNCMP(p, "wh", 2) == 0
21193 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021194 || STRNCMP(p, "try", 3) == 0)
21195 indent += 2;
21196
21197 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021198 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021200 if (*p == '!')
21201 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202 p += eval_fname_script(p);
21203 if (ASCII_ISALPHA(*p))
21204 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021205 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206 if (*skipwhite(p) == '(')
21207 {
21208 ++nesting;
21209 indent += 2;
21210 }
21211 }
21212 }
21213
21214 /* Check for ":append" or ":insert". */
21215 p = skip_range(p, NULL);
21216 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21217 || (p[0] == 'i'
21218 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21219 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21220 skip_until = vim_strsave((char_u *)".");
21221
21222 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21223 arg = skipwhite(skiptowhite(p));
21224 if (arg[0] == '<' && arg[1] =='<'
21225 && ((p[0] == 'p' && p[1] == 'y'
21226 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21227 || (p[0] == 'p' && p[1] == 'e'
21228 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21229 || (p[0] == 't' && p[1] == 'c'
21230 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021231 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21232 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021233 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21234 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021235 || (p[0] == 'm' && p[1] == 'z'
21236 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 ))
21238 {
21239 /* ":python <<" continues until a dot, like ":append" */
21240 p = skipwhite(arg + 2);
21241 if (*p == NUL)
21242 skip_until = vim_strsave((char_u *)".");
21243 else
21244 skip_until = vim_strsave(p);
21245 }
21246 }
21247
21248 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021249 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021250 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021251 if (line_arg == NULL)
21252 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021254 }
21255
21256 /* Copy the line to newly allocated memory. get_one_sourceline()
21257 * allocates 250 bytes per line, this saves 80% on average. The cost
21258 * is an extra alloc/free. */
21259 p = vim_strsave(theline);
21260 if (p != NULL)
21261 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021262 if (line_arg == NULL)
21263 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021264 theline = p;
21265 }
21266
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021267 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21268
21269 /* Add NULL lines for continuation lines, so that the line count is
21270 * equal to the index in the growarray. */
21271 while (sourcing_lnum_off-- > 0)
21272 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021273
21274 /* Check for end of eap->arg. */
21275 if (line_arg != NULL && *line_arg == NUL)
21276 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021277 }
21278
21279 /* Don't define the function when skipping commands or when an error was
21280 * detected. */
21281 if (eap->skip || did_emsg)
21282 goto erret;
21283
21284 /*
21285 * If there are no errors, add the function
21286 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021287 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021288 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021289 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021290 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021291 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021292 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021293 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021294 goto erret;
21295 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021296
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021297 fp = find_func(name);
21298 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021299 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021300 if (!eap->forceit)
21301 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021302 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021303 goto erret;
21304 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021305 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021306 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021307 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021308 name);
21309 goto erret;
21310 }
21311 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021312 ga_clear_strings(&(fp->uf_args));
21313 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021314 vim_free(name);
21315 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021317 }
21318 else
21319 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021320 char numbuf[20];
21321
21322 fp = NULL;
21323 if (fudi.fd_newkey == NULL && !eap->forceit)
21324 {
21325 EMSG(_(e_funcdict));
21326 goto erret;
21327 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021328 if (fudi.fd_di == NULL)
21329 {
21330 /* Can't add a function to a locked dictionary */
21331 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21332 goto erret;
21333 }
21334 /* Can't change an existing function if it is locked */
21335 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21336 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021337
21338 /* Give the function a sequential number. Can only be used with a
21339 * Funcref! */
21340 vim_free(name);
21341 sprintf(numbuf, "%d", ++func_nr);
21342 name = vim_strsave((char_u *)numbuf);
21343 if (name == NULL)
21344 goto erret;
21345 }
21346
21347 if (fp == NULL)
21348 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021349 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021350 {
21351 int slen, plen;
21352 char_u *scriptname;
21353
21354 /* Check that the autoload name matches the script name. */
21355 j = FAIL;
21356 if (sourcing_name != NULL)
21357 {
21358 scriptname = autoload_name(name);
21359 if (scriptname != NULL)
21360 {
21361 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021362 plen = (int)STRLEN(p);
21363 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021364 if (slen > plen && fnamecmp(p,
21365 sourcing_name + slen - plen) == 0)
21366 j = OK;
21367 vim_free(scriptname);
21368 }
21369 }
21370 if (j == FAIL)
21371 {
21372 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21373 goto erret;
21374 }
21375 }
21376
21377 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021378 if (fp == NULL)
21379 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021380
21381 if (fudi.fd_dict != NULL)
21382 {
21383 if (fudi.fd_di == NULL)
21384 {
21385 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021386 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021387 if (fudi.fd_di == NULL)
21388 {
21389 vim_free(fp);
21390 goto erret;
21391 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021392 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21393 {
21394 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021395 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021396 goto erret;
21397 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021398 }
21399 else
21400 /* overwrite existing dict entry */
21401 clear_tv(&fudi.fd_di->di_tv);
21402 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021403 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021404 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021405 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021406
21407 /* behave like "dict" was used */
21408 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021409 }
21410
Bram Moolenaar071d4272004-06-13 20:20:40 +000021411 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021412 STRCPY(fp->uf_name, name);
21413 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021415 fp->uf_args = newargs;
21416 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021417#ifdef FEAT_PROFILE
21418 fp->uf_tml_count = NULL;
21419 fp->uf_tml_total = NULL;
21420 fp->uf_tml_self = NULL;
21421 fp->uf_profiling = FALSE;
21422 if (prof_def_func())
21423 func_do_profile(fp);
21424#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021425 fp->uf_varargs = varargs;
21426 fp->uf_flags = flags;
21427 fp->uf_calls = 0;
21428 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021429 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021430
21431erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432 ga_clear_strings(&newargs);
21433 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021434ret_free:
21435 vim_free(skip_until);
21436 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021438 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439}
21440
21441/*
21442 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021443 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021444 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021445 * flags:
21446 * TFN_INT: internal function name OK
21447 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021448 * Advances "pp" to just after the function name (if no error).
21449 */
21450 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021451trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452 char_u **pp;
21453 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021454 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021455 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021457 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458 char_u *start;
21459 char_u *end;
21460 int lead;
21461 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021462 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021463 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021464
21465 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021466 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021467 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021468
21469 /* Check for hard coded <SNR>: already translated function ID (from a user
21470 * command). */
21471 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21472 && (*pp)[2] == (int)KE_SNR)
21473 {
21474 *pp += 3;
21475 len = get_id_len(pp) + 3;
21476 return vim_strnsave(start, len);
21477 }
21478
21479 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21480 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021482 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021484
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021485 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21486 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021487 if (end == start)
21488 {
21489 if (!skip)
21490 EMSG(_("E129: Function name required"));
21491 goto theend;
21492 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021493 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021494 {
21495 /*
21496 * Report an invalid expression in braces, unless the expression
21497 * evaluation has been cancelled due to an aborting error, an
21498 * interrupt, or an exception.
21499 */
21500 if (!aborting())
21501 {
21502 if (end != NULL)
21503 EMSG2(_(e_invarg2), start);
21504 }
21505 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021506 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021507 goto theend;
21508 }
21509
21510 if (lv.ll_tv != NULL)
21511 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021512 if (fdp != NULL)
21513 {
21514 fdp->fd_dict = lv.ll_dict;
21515 fdp->fd_newkey = lv.ll_newkey;
21516 lv.ll_newkey = NULL;
21517 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021518 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021519 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21520 {
21521 name = vim_strsave(lv.ll_tv->vval.v_string);
21522 *pp = end;
21523 }
21524 else
21525 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021526 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21527 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021528 EMSG(_(e_funcref));
21529 else
21530 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021531 name = NULL;
21532 }
21533 goto theend;
21534 }
21535
21536 if (lv.ll_name == NULL)
21537 {
21538 /* Error found, but continue after the function name. */
21539 *pp = end;
21540 goto theend;
21541 }
21542
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021543 /* Check if the name is a Funcref. If so, use the value. */
21544 if (lv.ll_exp_name != NULL)
21545 {
21546 len = (int)STRLEN(lv.ll_exp_name);
21547 name = deref_func_name(lv.ll_exp_name, &len);
21548 if (name == lv.ll_exp_name)
21549 name = NULL;
21550 }
21551 else
21552 {
21553 len = (int)(end - *pp);
21554 name = deref_func_name(*pp, &len);
21555 if (name == *pp)
21556 name = NULL;
21557 }
21558 if (name != NULL)
21559 {
21560 name = vim_strsave(name);
21561 *pp = end;
21562 goto theend;
21563 }
21564
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021565 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021566 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021567 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021568 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21569 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21570 {
21571 /* When there was "s:" already or the name expanded to get a
21572 * leading "s:" then remove it. */
21573 lv.ll_name += 2;
21574 len -= 2;
21575 lead = 2;
21576 }
21577 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021578 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021579 {
21580 if (lead == 2) /* skip over "s:" */
21581 lv.ll_name += 2;
21582 len = (int)(end - lv.ll_name);
21583 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021584
21585 /*
21586 * Copy the function name to allocated memory.
21587 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21588 * Accept <SNR>123_name() outside a script.
21589 */
21590 if (skip)
21591 lead = 0; /* do nothing */
21592 else if (lead > 0)
21593 {
21594 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021595 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21596 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021597 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021598 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021599 if (current_SID <= 0)
21600 {
21601 EMSG(_(e_usingsid));
21602 goto theend;
21603 }
21604 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21605 lead += (int)STRLEN(sid_buf);
21606 }
21607 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021608 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021609 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021610 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021611 goto theend;
21612 }
21613 name = alloc((unsigned)(len + lead + 1));
21614 if (name != NULL)
21615 {
21616 if (lead > 0)
21617 {
21618 name[0] = K_SPECIAL;
21619 name[1] = KS_EXTRA;
21620 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021621 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021622 STRCPY(name + 3, sid_buf);
21623 }
21624 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21625 name[len + lead] = NUL;
21626 }
21627 *pp = end;
21628
21629theend:
21630 clear_lval(&lv);
21631 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632}
21633
21634/*
21635 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21636 * Return 2 if "p" starts with "s:".
21637 * Return 0 otherwise.
21638 */
21639 static int
21640eval_fname_script(p)
21641 char_u *p;
21642{
21643 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21644 || STRNICMP(p + 1, "SNR>", 4) == 0))
21645 return 5;
21646 if (p[0] == 's' && p[1] == ':')
21647 return 2;
21648 return 0;
21649}
21650
21651/*
21652 * Return TRUE if "p" starts with "<SID>" or "s:".
21653 * Only works if eval_fname_script() returned non-zero for "p"!
21654 */
21655 static int
21656eval_fname_sid(p)
21657 char_u *p;
21658{
21659 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21660}
21661
21662/*
21663 * List the head of the function: "name(arg1, arg2)".
21664 */
21665 static void
21666list_func_head(fp, indent)
21667 ufunc_T *fp;
21668 int indent;
21669{
21670 int j;
21671
21672 msg_start();
21673 if (indent)
21674 MSG_PUTS(" ");
21675 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021676 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677 {
21678 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021679 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680 }
21681 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021682 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021684 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021685 {
21686 if (j)
21687 MSG_PUTS(", ");
21688 msg_puts(FUNCARG(fp, j));
21689 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021690 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691 {
21692 if (j)
21693 MSG_PUTS(", ");
21694 MSG_PUTS("...");
21695 }
21696 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021697 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021698 if (p_verbose > 0)
21699 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700}
21701
21702/*
21703 * Find a function by name, return pointer to it in ufuncs.
21704 * Return NULL for unknown function.
21705 */
21706 static ufunc_T *
21707find_func(name)
21708 char_u *name;
21709{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021710 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021711
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021712 hi = hash_find(&func_hashtab, name);
21713 if (!HASHITEM_EMPTY(hi))
21714 return HI2UF(hi);
21715 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021716}
21717
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021718#if defined(EXITFREE) || defined(PROTO)
21719 void
21720free_all_functions()
21721{
21722 hashitem_T *hi;
21723
21724 /* Need to start all over every time, because func_free() may change the
21725 * hash table. */
21726 while (func_hashtab.ht_used > 0)
21727 for (hi = func_hashtab.ht_array; ; ++hi)
21728 if (!HASHITEM_EMPTY(hi))
21729 {
21730 func_free(HI2UF(hi));
21731 break;
21732 }
21733}
21734#endif
21735
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021736/*
21737 * Return TRUE if a function "name" exists.
21738 */
21739 static int
21740function_exists(name)
21741 char_u *name;
21742{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021743 char_u *nm = name;
21744 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021745 int n = FALSE;
21746
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021747 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021748 nm = skipwhite(nm);
21749
21750 /* Only accept "funcname", "funcname ", "funcname (..." and
21751 * "funcname(...", not "funcname!...". */
21752 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021753 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021754 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021755 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021756 else
21757 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021758 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021759 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021760 return n;
21761}
21762
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021763/*
21764 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021765 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021766 */
21767 static int
21768builtin_function(name)
21769 char_u *name;
21770{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021771 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21772 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021773}
21774
Bram Moolenaar05159a02005-02-26 23:04:13 +000021775#if defined(FEAT_PROFILE) || defined(PROTO)
21776/*
21777 * Start profiling function "fp".
21778 */
21779 static void
21780func_do_profile(fp)
21781 ufunc_T *fp;
21782{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021783 int len = fp->uf_lines.ga_len;
21784
21785 if (len == 0)
21786 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021787 fp->uf_tm_count = 0;
21788 profile_zero(&fp->uf_tm_self);
21789 profile_zero(&fp->uf_tm_total);
21790 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021791 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021792 if (fp->uf_tml_total == NULL)
21793 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021794 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021795 if (fp->uf_tml_self == NULL)
21796 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021797 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021798 fp->uf_tml_idx = -1;
21799 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21800 || fp->uf_tml_self == NULL)
21801 return; /* out of memory */
21802
21803 fp->uf_profiling = TRUE;
21804}
21805
21806/*
21807 * Dump the profiling results for all functions in file "fd".
21808 */
21809 void
21810func_dump_profile(fd)
21811 FILE *fd;
21812{
21813 hashitem_T *hi;
21814 int todo;
21815 ufunc_T *fp;
21816 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021817 ufunc_T **sorttab;
21818 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021819
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021820 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021821 if (todo == 0)
21822 return; /* nothing to dump */
21823
Bram Moolenaar73830342005-02-28 22:48:19 +000021824 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21825
Bram Moolenaar05159a02005-02-26 23:04:13 +000021826 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21827 {
21828 if (!HASHITEM_EMPTY(hi))
21829 {
21830 --todo;
21831 fp = HI2UF(hi);
21832 if (fp->uf_profiling)
21833 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021834 if (sorttab != NULL)
21835 sorttab[st_len++] = fp;
21836
Bram Moolenaar05159a02005-02-26 23:04:13 +000021837 if (fp->uf_name[0] == K_SPECIAL)
21838 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21839 else
21840 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21841 if (fp->uf_tm_count == 1)
21842 fprintf(fd, "Called 1 time\n");
21843 else
21844 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21845 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21846 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21847 fprintf(fd, "\n");
21848 fprintf(fd, "count total (s) self (s)\n");
21849
21850 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21851 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021852 if (FUNCLINE(fp, i) == NULL)
21853 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021854 prof_func_line(fd, fp->uf_tml_count[i],
21855 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021856 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21857 }
21858 fprintf(fd, "\n");
21859 }
21860 }
21861 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021862
21863 if (sorttab != NULL && st_len > 0)
21864 {
21865 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21866 prof_total_cmp);
21867 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21868 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21869 prof_self_cmp);
21870 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21871 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021872
21873 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021874}
Bram Moolenaar73830342005-02-28 22:48:19 +000021875
21876 static void
21877prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21878 FILE *fd;
21879 ufunc_T **sorttab;
21880 int st_len;
21881 char *title;
21882 int prefer_self; /* when equal print only self time */
21883{
21884 int i;
21885 ufunc_T *fp;
21886
21887 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21888 fprintf(fd, "count total (s) self (s) function\n");
21889 for (i = 0; i < 20 && i < st_len; ++i)
21890 {
21891 fp = sorttab[i];
21892 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21893 prefer_self);
21894 if (fp->uf_name[0] == K_SPECIAL)
21895 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21896 else
21897 fprintf(fd, " %s()\n", fp->uf_name);
21898 }
21899 fprintf(fd, "\n");
21900}
21901
21902/*
21903 * Print the count and times for one function or function line.
21904 */
21905 static void
21906prof_func_line(fd, count, total, self, prefer_self)
21907 FILE *fd;
21908 int count;
21909 proftime_T *total;
21910 proftime_T *self;
21911 int prefer_self; /* when equal print only self time */
21912{
21913 if (count > 0)
21914 {
21915 fprintf(fd, "%5d ", count);
21916 if (prefer_self && profile_equal(total, self))
21917 fprintf(fd, " ");
21918 else
21919 fprintf(fd, "%s ", profile_msg(total));
21920 if (!prefer_self && profile_equal(total, self))
21921 fprintf(fd, " ");
21922 else
21923 fprintf(fd, "%s ", profile_msg(self));
21924 }
21925 else
21926 fprintf(fd, " ");
21927}
21928
21929/*
21930 * Compare function for total time sorting.
21931 */
21932 static int
21933#ifdef __BORLANDC__
21934_RTLENTRYF
21935#endif
21936prof_total_cmp(s1, s2)
21937 const void *s1;
21938 const void *s2;
21939{
21940 ufunc_T *p1, *p2;
21941
21942 p1 = *(ufunc_T **)s1;
21943 p2 = *(ufunc_T **)s2;
21944 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21945}
21946
21947/*
21948 * Compare function for self time sorting.
21949 */
21950 static int
21951#ifdef __BORLANDC__
21952_RTLENTRYF
21953#endif
21954prof_self_cmp(s1, s2)
21955 const void *s1;
21956 const void *s2;
21957{
21958 ufunc_T *p1, *p2;
21959
21960 p1 = *(ufunc_T **)s1;
21961 p2 = *(ufunc_T **)s2;
21962 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21963}
21964
Bram Moolenaar05159a02005-02-26 23:04:13 +000021965#endif
21966
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021967/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021968 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021969 * Return TRUE if a package was loaded.
21970 */
21971 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021972script_autoload(name, reload)
21973 char_u *name;
21974 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021975{
21976 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021977 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021978 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021979 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021980
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021981 /* Return quickly when autoload disabled. */
21982 if (no_autoload)
21983 return FALSE;
21984
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021985 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021986 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021987 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021988 return FALSE;
21989
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021990 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021991
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021992 /* Find the name in the list of previously loaded package names. Skip
21993 * "autoload/", it's always the same. */
21994 for (i = 0; i < ga_loaded.ga_len; ++i)
21995 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21996 break;
21997 if (!reload && i < ga_loaded.ga_len)
21998 ret = FALSE; /* was loaded already */
21999 else
22000 {
22001 /* Remember the name if it wasn't loaded already. */
22002 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22003 {
22004 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22005 tofree = NULL;
22006 }
22007
22008 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022009 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022010 ret = TRUE;
22011 }
22012
22013 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022014 return ret;
22015}
22016
22017/*
22018 * Return the autoload script name for a function or variable name.
22019 * Returns NULL when out of memory.
22020 */
22021 static char_u *
22022autoload_name(name)
22023 char_u *name;
22024{
22025 char_u *p;
22026 char_u *scriptname;
22027
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022028 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022029 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22030 if (scriptname == NULL)
22031 return FALSE;
22032 STRCPY(scriptname, "autoload/");
22033 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022034 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022035 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022036 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022037 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022038 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022039}
22040
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22042
22043/*
22044 * Function given to ExpandGeneric() to obtain the list of user defined
22045 * function names.
22046 */
22047 char_u *
22048get_user_func_name(xp, idx)
22049 expand_T *xp;
22050 int idx;
22051{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022052 static long_u done;
22053 static hashitem_T *hi;
22054 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022055
22056 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022058 done = 0;
22059 hi = func_hashtab.ht_array;
22060 }
22061 if (done < func_hashtab.ht_used)
22062 {
22063 if (done++ > 0)
22064 ++hi;
22065 while (HASHITEM_EMPTY(hi))
22066 ++hi;
22067 fp = HI2UF(hi);
22068
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022069 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022070 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022071
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022072 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22073 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074
22075 cat_func_name(IObuff, fp);
22076 if (xp->xp_context != EXPAND_USER_FUNC)
22077 {
22078 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022079 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022080 STRCAT(IObuff, ")");
22081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082 return IObuff;
22083 }
22084 return NULL;
22085}
22086
22087#endif /* FEAT_CMDL_COMPL */
22088
22089/*
22090 * Copy the function name of "fp" to buffer "buf".
22091 * "buf" must be able to hold the function name plus three bytes.
22092 * Takes care of script-local function names.
22093 */
22094 static void
22095cat_func_name(buf, fp)
22096 char_u *buf;
22097 ufunc_T *fp;
22098{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022099 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100 {
22101 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022102 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022103 }
22104 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022105 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022106}
22107
22108/*
22109 * ":delfunction {name}"
22110 */
22111 void
22112ex_delfunction(eap)
22113 exarg_T *eap;
22114{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022115 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022116 char_u *p;
22117 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022118 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119
22120 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022121 name = trans_function_name(&p, eap->skip, 0, &fudi);
22122 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022124 {
22125 if (fudi.fd_dict != NULL && !eap->skip)
22126 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129 if (!ends_excmd(*skipwhite(p)))
22130 {
22131 vim_free(name);
22132 EMSG(_(e_trailing));
22133 return;
22134 }
22135 eap->nextcmd = check_nextcmd(p);
22136 if (eap->nextcmd != NULL)
22137 *p = NUL;
22138
22139 if (!eap->skip)
22140 fp = find_func(name);
22141 vim_free(name);
22142
22143 if (!eap->skip)
22144 {
22145 if (fp == NULL)
22146 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022147 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022148 return;
22149 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022150 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022151 {
22152 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22153 return;
22154 }
22155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022156 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022157 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022158 /* Delete the dict item that refers to the function, it will
22159 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022160 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022162 else
22163 func_free(fp);
22164 }
22165}
22166
22167/*
22168 * Free a function and remove it from the list of functions.
22169 */
22170 static void
22171func_free(fp)
22172 ufunc_T *fp;
22173{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022174 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022175
22176 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022177 ga_clear_strings(&(fp->uf_args));
22178 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022179#ifdef FEAT_PROFILE
22180 vim_free(fp->uf_tml_count);
22181 vim_free(fp->uf_tml_total);
22182 vim_free(fp->uf_tml_self);
22183#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022184
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022185 /* remove the function from the function hashtable */
22186 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22187 if (HASHITEM_EMPTY(hi))
22188 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022189 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022190 hash_remove(&func_hashtab, hi);
22191
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022192 vim_free(fp);
22193}
22194
22195/*
22196 * Unreference a Function: decrement the reference count and free it when it
22197 * becomes zero. Only for numbered functions.
22198 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022199 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022200func_unref(name)
22201 char_u *name;
22202{
22203 ufunc_T *fp;
22204
22205 if (name != NULL && isdigit(*name))
22206 {
22207 fp = find_func(name);
22208 if (fp == NULL)
22209 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022210 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022211 {
22212 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022213 * when "uf_calls" becomes zero. */
22214 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022215 func_free(fp);
22216 }
22217 }
22218}
22219
22220/*
22221 * Count a reference to a Function.
22222 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022223 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022224func_ref(name)
22225 char_u *name;
22226{
22227 ufunc_T *fp;
22228
22229 if (name != NULL && isdigit(*name))
22230 {
22231 fp = find_func(name);
22232 if (fp == NULL)
22233 EMSG2(_(e_intern2), "func_ref()");
22234 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022235 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022236 }
22237}
22238
22239/*
22240 * Call a user function.
22241 */
22242 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022243call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022244 ufunc_T *fp; /* pointer to function */
22245 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022246 typval_T *argvars; /* arguments */
22247 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022248 linenr_T firstline; /* first line of range */
22249 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022250 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022251{
Bram Moolenaar33570922005-01-25 22:26:29 +000022252 char_u *save_sourcing_name;
22253 linenr_T save_sourcing_lnum;
22254 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022255 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022256 int save_did_emsg;
22257 static int depth = 0;
22258 dictitem_T *v;
22259 int fixvar_idx = 0; /* index in fixvar[] */
22260 int i;
22261 int ai;
22262 char_u numbuf[NUMBUFLEN];
22263 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022264#ifdef FEAT_PROFILE
22265 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022266 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022268
22269 /* If depth of calling is getting too high, don't execute the function */
22270 if (depth >= p_mfd)
22271 {
22272 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022273 rettv->v_type = VAR_NUMBER;
22274 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022275 return;
22276 }
22277 ++depth;
22278
22279 line_breakcheck(); /* check for CTRL-C hit */
22280
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022281 fc = (funccall_T *)alloc(sizeof(funccall_T));
22282 fc->caller = current_funccal;
22283 current_funccal = fc;
22284 fc->func = fp;
22285 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022286 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022287 fc->linenr = 0;
22288 fc->returned = FALSE;
22289 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022291 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22292 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293
Bram Moolenaar33570922005-01-25 22:26:29 +000022294 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022295 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022296 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22297 * each argument variable and saves a lot of time.
22298 */
22299 /*
22300 * Init l: variables.
22301 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022302 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000022303 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022304 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022305 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22306 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022307 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022308 name = v->di_key;
22309 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022310 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022311 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022312 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022313 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022314 v->di_tv.vval.v_dict = selfdict;
22315 ++selfdict->dv_refcount;
22316 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022317
Bram Moolenaar33570922005-01-25 22:26:29 +000022318 /*
22319 * Init a: variables.
22320 * Set a:0 to "argcount".
22321 * Set a:000 to a list with room for the "..." arguments.
22322 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022323 init_var_dict(&fc->l_avars, &fc->l_avars_var);
22324 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022325 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022326 /* Use "name" to avoid a warning from some compiler that checks the
22327 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022328 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022329 name = v->di_key;
22330 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022331 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022332 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022333 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022334 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022335 v->di_tv.vval.v_list = &fc->l_varlist;
22336 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22337 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22338 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022339
22340 /*
22341 * Set a:firstline to "firstline" and a:lastline to "lastline".
22342 * Set a:name to named arguments.
22343 * Set a:N to the "..." arguments.
22344 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022345 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022346 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022347 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022348 (varnumber_T)lastline);
22349 for (i = 0; i < argcount; ++i)
22350 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022351 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022352 if (ai < 0)
22353 /* named argument a:name */
22354 name = FUNCARG(fp, i);
22355 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022356 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022357 /* "..." argument a:1, a:2, etc. */
22358 sprintf((char *)numbuf, "%d", ai + 1);
22359 name = numbuf;
22360 }
22361 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22362 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022363 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022364 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22365 }
22366 else
22367 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022368 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22369 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022370 if (v == NULL)
22371 break;
22372 v->di_flags = DI_FLAGS_RO;
22373 }
22374 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022375 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022376
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022377 /* Note: the values are copied directly to avoid alloc/free.
22378 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022379 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022380 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022381
22382 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22383 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022384 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22385 fc->l_listitems[ai].li_tv = argvars[i];
22386 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022387 }
22388 }
22389
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390 /* Don't redraw while executing the function. */
22391 ++RedrawingDisabled;
22392 save_sourcing_name = sourcing_name;
22393 save_sourcing_lnum = sourcing_lnum;
22394 sourcing_lnum = 1;
22395 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022396 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397 if (sourcing_name != NULL)
22398 {
22399 if (save_sourcing_name != NULL
22400 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22401 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22402 else
22403 STRCPY(sourcing_name, "function ");
22404 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22405
22406 if (p_verbose >= 12)
22407 {
22408 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022409 verbose_enter_scroll();
22410
Bram Moolenaar555b2802005-05-19 21:08:39 +000022411 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022412 if (p_verbose >= 14)
22413 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022415 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022416 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022417 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022418
22419 msg_puts((char_u *)"(");
22420 for (i = 0; i < argcount; ++i)
22421 {
22422 if (i > 0)
22423 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022424 if (argvars[i].v_type == VAR_NUMBER)
22425 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426 else
22427 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022428 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22429 if (s != NULL)
22430 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022431 if (vim_strsize(s) > MSG_BUF_CLEN)
22432 {
22433 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22434 s = buf;
22435 }
22436 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022437 vim_free(tofree);
22438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439 }
22440 }
22441 msg_puts((char_u *)")");
22442 }
22443 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022444
22445 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022446 --no_wait_return;
22447 }
22448 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022449#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022450 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022451 {
22452 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22453 func_do_profile(fp);
22454 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022455 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022456 {
22457 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022458 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022459 profile_zero(&fp->uf_tm_children);
22460 }
22461 script_prof_save(&wait_start);
22462 }
22463#endif
22464
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022466 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467 save_did_emsg = did_emsg;
22468 did_emsg = FALSE;
22469
22470 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022471 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22473
22474 --RedrawingDisabled;
22475
22476 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022477 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022478 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022479 clear_tv(rettv);
22480 rettv->v_type = VAR_NUMBER;
22481 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022482 }
22483
Bram Moolenaar05159a02005-02-26 23:04:13 +000022484#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022485 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022486 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022487 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022488 profile_end(&call_start);
22489 profile_sub_wait(&wait_start, &call_start);
22490 profile_add(&fp->uf_tm_total, &call_start);
22491 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022492 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022493 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022494 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22495 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022496 }
22497 }
22498#endif
22499
Bram Moolenaar071d4272004-06-13 20:20:40 +000022500 /* when being verbose, mention the return value */
22501 if (p_verbose >= 12)
22502 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022503 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022504 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022505
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022507 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022508 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022509 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022510 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022511 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022513 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022514 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022515 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022516 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022517
Bram Moolenaar555b2802005-05-19 21:08:39 +000022518 /* The value may be very long. Skip the middle part, so that we
22519 * have some idea how it starts and ends. smsg() would always
22520 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022521 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022522 if (s != NULL)
22523 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022524 if (vim_strsize(s) > MSG_BUF_CLEN)
22525 {
22526 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22527 s = buf;
22528 }
22529 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022530 vim_free(tofree);
22531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022532 }
22533 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022534
22535 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022536 --no_wait_return;
22537 }
22538
22539 vim_free(sourcing_name);
22540 sourcing_name = save_sourcing_name;
22541 sourcing_lnum = save_sourcing_lnum;
22542 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022543#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022544 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022545 script_prof_restore(&wait_start);
22546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547
22548 if (p_verbose >= 12 && sourcing_name != NULL)
22549 {
22550 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022551 verbose_enter_scroll();
22552
Bram Moolenaar555b2802005-05-19 21:08:39 +000022553 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022554 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022555
22556 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557 --no_wait_return;
22558 }
22559
22560 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022561 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022563
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022564 /* If the a:000 list and the l: and a: dicts are not referenced we can
22565 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022566 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22567 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22568 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22569 {
22570 free_funccal(fc, FALSE);
22571 }
22572 else
22573 {
22574 hashitem_T *hi;
22575 listitem_T *li;
22576 int todo;
22577
22578 /* "fc" is still in use. This can happen when returning "a:000" or
22579 * assigning "l:" to a global variable.
22580 * Link "fc" in the list for garbage collection later. */
22581 fc->caller = previous_funccal;
22582 previous_funccal = fc;
22583
22584 /* Make a copy of the a: variables, since we didn't do that above. */
22585 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22586 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22587 {
22588 if (!HASHITEM_EMPTY(hi))
22589 {
22590 --todo;
22591 v = HI2DI(hi);
22592 copy_tv(&v->di_tv, &v->di_tv);
22593 }
22594 }
22595
22596 /* Make a copy of the a:000 items, since we didn't do that above. */
22597 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22598 copy_tv(&li->li_tv, &li->li_tv);
22599 }
22600}
22601
22602/*
22603 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022604 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022605 */
22606 static int
22607can_free_funccal(fc, copyID)
22608 funccall_T *fc;
22609 int copyID;
22610{
22611 return (fc->l_varlist.lv_copyID != copyID
22612 && fc->l_vars.dv_copyID != copyID
22613 && fc->l_avars.dv_copyID != copyID);
22614}
22615
22616/*
22617 * Free "fc" and what it contains.
22618 */
22619 static void
22620free_funccal(fc, free_val)
22621 funccall_T *fc;
22622 int free_val; /* a: vars were allocated */
22623{
22624 listitem_T *li;
22625
22626 /* The a: variables typevals may not have been allocated, only free the
22627 * allocated variables. */
22628 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22629
22630 /* free all l: variables */
22631 vars_clear(&fc->l_vars.dv_hashtab);
22632
22633 /* Free the a:000 variables if they were allocated. */
22634 if (free_val)
22635 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22636 clear_tv(&li->li_tv);
22637
22638 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639}
22640
22641/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022642 * Add a number variable "name" to dict "dp" with value "nr".
22643 */
22644 static void
22645add_nr_var(dp, v, name, nr)
22646 dict_T *dp;
22647 dictitem_T *v;
22648 char *name;
22649 varnumber_T nr;
22650{
22651 STRCPY(v->di_key, name);
22652 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22653 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22654 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022655 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022656 v->di_tv.vval.v_number = nr;
22657}
22658
22659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 * ":return [expr]"
22661 */
22662 void
22663ex_return(eap)
22664 exarg_T *eap;
22665{
22666 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022667 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022668 int returning = FALSE;
22669
22670 if (current_funccal == NULL)
22671 {
22672 EMSG(_("E133: :return not inside a function"));
22673 return;
22674 }
22675
22676 if (eap->skip)
22677 ++emsg_skip;
22678
22679 eap->nextcmd = NULL;
22680 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022681 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022682 {
22683 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022684 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022685 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022686 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022687 }
22688 /* It's safer to return also on error. */
22689 else if (!eap->skip)
22690 {
22691 /*
22692 * Return unless the expression evaluation has been cancelled due to an
22693 * aborting error, an interrupt, or an exception.
22694 */
22695 if (!aborting())
22696 returning = do_return(eap, FALSE, TRUE, NULL);
22697 }
22698
22699 /* When skipping or the return gets pending, advance to the next command
22700 * in this line (!returning). Otherwise, ignore the rest of the line.
22701 * Following lines will be ignored by get_func_line(). */
22702 if (returning)
22703 eap->nextcmd = NULL;
22704 else if (eap->nextcmd == NULL) /* no argument */
22705 eap->nextcmd = check_nextcmd(arg);
22706
22707 if (eap->skip)
22708 --emsg_skip;
22709}
22710
22711/*
22712 * Return from a function. Possibly makes the return pending. Also called
22713 * for a pending return at the ":endtry" or after returning from an extra
22714 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022715 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022716 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022717 * FALSE when the return gets pending.
22718 */
22719 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022720do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721 exarg_T *eap;
22722 int reanimate;
22723 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022724 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725{
22726 int idx;
22727 struct condstack *cstack = eap->cstack;
22728
22729 if (reanimate)
22730 /* Undo the return. */
22731 current_funccal->returned = FALSE;
22732
22733 /*
22734 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22735 * not in its finally clause (which then is to be executed next) is found.
22736 * In this case, make the ":return" pending for execution at the ":endtry".
22737 * Otherwise, return normally.
22738 */
22739 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22740 if (idx >= 0)
22741 {
22742 cstack->cs_pending[idx] = CSTP_RETURN;
22743
22744 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022745 /* A pending return again gets pending. "rettv" points to an
22746 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022748 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749 else
22750 {
22751 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022752 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022754 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022756 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022757 {
22758 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022759 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022760 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022761 else
22762 EMSG(_(e_outofmem));
22763 }
22764 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022765 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022766
22767 if (reanimate)
22768 {
22769 /* The pending return value could be overwritten by a ":return"
22770 * without argument in a finally clause; reset the default
22771 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022772 current_funccal->rettv->v_type = VAR_NUMBER;
22773 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022774 }
22775 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022776 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022777 }
22778 else
22779 {
22780 current_funccal->returned = TRUE;
22781
22782 /* If the return is carried out now, store the return value. For
22783 * a return immediately after reanimation, the value is already
22784 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022785 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022786 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022787 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022788 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022789 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022790 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022791 }
22792 }
22793
22794 return idx < 0;
22795}
22796
22797/*
22798 * Free the variable with a pending return value.
22799 */
22800 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022801discard_pending_return(rettv)
22802 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022803{
Bram Moolenaar33570922005-01-25 22:26:29 +000022804 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022805}
22806
22807/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022808 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022809 * is an allocated string. Used by report_pending() for verbose messages.
22810 */
22811 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022812get_return_cmd(rettv)
22813 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022814{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022815 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022816 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022817 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022818
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022819 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022820 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022821 if (s == NULL)
22822 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022823
22824 STRCPY(IObuff, ":return ");
22825 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22826 if (STRLEN(s) + 8 >= IOSIZE)
22827 STRCPY(IObuff + IOSIZE - 4, "...");
22828 vim_free(tofree);
22829 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022830}
22831
22832/*
22833 * Get next function line.
22834 * Called by do_cmdline() to get the next line.
22835 * Returns allocated string, or NULL for end of function.
22836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022837 char_u *
22838get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022839 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022840 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022841 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022842{
Bram Moolenaar33570922005-01-25 22:26:29 +000022843 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022844 ufunc_T *fp = fcp->func;
22845 char_u *retval;
22846 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847
22848 /* If breakpoints have been added/deleted need to check for it. */
22849 if (fcp->dbg_tick != debug_tick)
22850 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022851 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022852 sourcing_lnum);
22853 fcp->dbg_tick = debug_tick;
22854 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022855#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022856 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022857 func_line_end(cookie);
22858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859
Bram Moolenaar05159a02005-02-26 23:04:13 +000022860 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022861 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22862 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022863 retval = NULL;
22864 else
22865 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022866 /* Skip NULL lines (continuation lines). */
22867 while (fcp->linenr < gap->ga_len
22868 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22869 ++fcp->linenr;
22870 if (fcp->linenr >= gap->ga_len)
22871 retval = NULL;
22872 else
22873 {
22874 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22875 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022876#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022877 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022878 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022879#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022881 }
22882
22883 /* Did we encounter a breakpoint? */
22884 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22885 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022886 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022888 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022889 sourcing_lnum);
22890 fcp->dbg_tick = debug_tick;
22891 }
22892
22893 return retval;
22894}
22895
Bram Moolenaar05159a02005-02-26 23:04:13 +000022896#if defined(FEAT_PROFILE) || defined(PROTO)
22897/*
22898 * Called when starting to read a function line.
22899 * "sourcing_lnum" must be correct!
22900 * When skipping lines it may not actually be executed, but we won't find out
22901 * until later and we need to store the time now.
22902 */
22903 void
22904func_line_start(cookie)
22905 void *cookie;
22906{
22907 funccall_T *fcp = (funccall_T *)cookie;
22908 ufunc_T *fp = fcp->func;
22909
22910 if (fp->uf_profiling && sourcing_lnum >= 1
22911 && sourcing_lnum <= fp->uf_lines.ga_len)
22912 {
22913 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022914 /* Skip continuation lines. */
22915 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22916 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022917 fp->uf_tml_execed = FALSE;
22918 profile_start(&fp->uf_tml_start);
22919 profile_zero(&fp->uf_tml_children);
22920 profile_get_wait(&fp->uf_tml_wait);
22921 }
22922}
22923
22924/*
22925 * Called when actually executing a function line.
22926 */
22927 void
22928func_line_exec(cookie)
22929 void *cookie;
22930{
22931 funccall_T *fcp = (funccall_T *)cookie;
22932 ufunc_T *fp = fcp->func;
22933
22934 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22935 fp->uf_tml_execed = TRUE;
22936}
22937
22938/*
22939 * Called when done with a function line.
22940 */
22941 void
22942func_line_end(cookie)
22943 void *cookie;
22944{
22945 funccall_T *fcp = (funccall_T *)cookie;
22946 ufunc_T *fp = fcp->func;
22947
22948 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22949 {
22950 if (fp->uf_tml_execed)
22951 {
22952 ++fp->uf_tml_count[fp->uf_tml_idx];
22953 profile_end(&fp->uf_tml_start);
22954 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022955 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022956 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22957 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022958 }
22959 fp->uf_tml_idx = -1;
22960 }
22961}
22962#endif
22963
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964/*
22965 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022966 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022967 */
22968 int
22969func_has_ended(cookie)
22970 void *cookie;
22971{
Bram Moolenaar33570922005-01-25 22:26:29 +000022972 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022973
22974 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22975 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022976 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022977 || fcp->returned);
22978}
22979
22980/*
22981 * return TRUE if cookie indicates a function which "abort"s on errors.
22982 */
22983 int
22984func_has_abort(cookie)
22985 void *cookie;
22986{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022987 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022988}
22989
22990#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22991typedef enum
22992{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022993 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22994 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22995 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996} var_flavour_T;
22997
22998static var_flavour_T var_flavour __ARGS((char_u *varname));
22999
23000 static var_flavour_T
23001var_flavour(varname)
23002 char_u *varname;
23003{
23004 char_u *p = varname;
23005
23006 if (ASCII_ISUPPER(*p))
23007 {
23008 while (*(++p))
23009 if (ASCII_ISLOWER(*p))
23010 return VAR_FLAVOUR_SESSION;
23011 return VAR_FLAVOUR_VIMINFO;
23012 }
23013 else
23014 return VAR_FLAVOUR_DEFAULT;
23015}
23016#endif
23017
23018#if defined(FEAT_VIMINFO) || defined(PROTO)
23019/*
23020 * Restore global vars that start with a capital from the viminfo file
23021 */
23022 int
23023read_viminfo_varlist(virp, writing)
23024 vir_T *virp;
23025 int writing;
23026{
23027 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023028 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023029 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030
23031 if (!writing && (find_viminfo_parameter('!') != NULL))
23032 {
23033 tab = vim_strchr(virp->vir_line + 1, '\t');
23034 if (tab != NULL)
23035 {
23036 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023037 switch (*tab)
23038 {
23039 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023040#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023041 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023042#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023043 case 'D': type = VAR_DICT; break;
23044 case 'L': type = VAR_LIST; break;
23045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046
23047 tab = vim_strchr(tab, '\t');
23048 if (tab != NULL)
23049 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023050 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023051 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023052 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023053 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023054#ifdef FEAT_FLOAT
23055 else if (type == VAR_FLOAT)
23056 (void)string2float(tab + 1, &tv.vval.v_float);
23057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023058 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023059 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023060 if (type == VAR_DICT || type == VAR_LIST)
23061 {
23062 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23063
23064 if (etv == NULL)
23065 /* Failed to parse back the dict or list, use it as a
23066 * string. */
23067 tv.v_type = VAR_STRING;
23068 else
23069 {
23070 vim_free(tv.vval.v_string);
23071 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023072 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023073 }
23074 }
23075
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023076 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023077
23078 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023079 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023080 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23081 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023082 }
23083 }
23084 }
23085
23086 return viminfo_readline(virp);
23087}
23088
23089/*
23090 * Write global vars that start with a capital to the viminfo file
23091 */
23092 void
23093write_viminfo_varlist(fp)
23094 FILE *fp;
23095{
Bram Moolenaar33570922005-01-25 22:26:29 +000023096 hashitem_T *hi;
23097 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023098 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023099 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023100 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023101 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023102 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103
23104 if (find_viminfo_parameter('!') == NULL)
23105 return;
23106
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023107 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023108
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023109 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023110 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023111 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023112 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023113 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023114 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023115 this_var = HI2DI(hi);
23116 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023117 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023118 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023119 {
23120 case VAR_STRING: s = "STR"; break;
23121 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023122#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023123 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023124#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023125 case VAR_DICT: s = "DIC"; break;
23126 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023127 default: continue;
23128 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023129 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023130 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023131 if (p != NULL)
23132 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023133 vim_free(tofree);
23134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023135 }
23136 }
23137}
23138#endif
23139
23140#if defined(FEAT_SESSION) || defined(PROTO)
23141 int
23142store_session_globals(fd)
23143 FILE *fd;
23144{
Bram Moolenaar33570922005-01-25 22:26:29 +000023145 hashitem_T *hi;
23146 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023147 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023148 char_u *p, *t;
23149
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023150 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023151 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023152 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023153 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023154 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023155 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023156 this_var = HI2DI(hi);
23157 if ((this_var->di_tv.v_type == VAR_NUMBER
23158 || this_var->di_tv.v_type == VAR_STRING)
23159 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023160 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023161 /* Escape special characters with a backslash. Turn a LF and
23162 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023163 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023164 (char_u *)"\\\"\n\r");
23165 if (p == NULL) /* out of memory */
23166 break;
23167 for (t = p; *t != NUL; ++t)
23168 if (*t == '\n')
23169 *t = 'n';
23170 else if (*t == '\r')
23171 *t = 'r';
23172 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023173 this_var->di_key,
23174 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23175 : ' ',
23176 p,
23177 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23178 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023179 || put_eol(fd) == FAIL)
23180 {
23181 vim_free(p);
23182 return FAIL;
23183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184 vim_free(p);
23185 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023186#ifdef FEAT_FLOAT
23187 else if (this_var->di_tv.v_type == VAR_FLOAT
23188 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23189 {
23190 float_T f = this_var->di_tv.vval.v_float;
23191 int sign = ' ';
23192
23193 if (f < 0)
23194 {
23195 f = -f;
23196 sign = '-';
23197 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023198 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023199 this_var->di_key, sign, f) < 0)
23200 || put_eol(fd) == FAIL)
23201 return FAIL;
23202 }
23203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023204 }
23205 }
23206 return OK;
23207}
23208#endif
23209
Bram Moolenaar661b1822005-07-28 22:36:45 +000023210/*
23211 * Display script name where an item was last set.
23212 * Should only be invoked when 'verbose' is non-zero.
23213 */
23214 void
23215last_set_msg(scriptID)
23216 scid_T scriptID;
23217{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023218 char_u *p;
23219
Bram Moolenaar661b1822005-07-28 22:36:45 +000023220 if (scriptID != 0)
23221 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023222 p = home_replace_save(NULL, get_scriptname(scriptID));
23223 if (p != NULL)
23224 {
23225 verbose_enter();
23226 MSG_PUTS(_("\n\tLast set from "));
23227 MSG_PUTS(p);
23228 vim_free(p);
23229 verbose_leave();
23230 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023231 }
23232}
23233
Bram Moolenaard812df62008-11-09 12:46:09 +000023234/*
23235 * List v:oldfiles in a nice way.
23236 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023237 void
23238ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023239 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023240{
23241 list_T *l = vimvars[VV_OLDFILES].vv_list;
23242 listitem_T *li;
23243 int nr = 0;
23244
23245 if (l == NULL)
23246 msg((char_u *)_("No old files"));
23247 else
23248 {
23249 msg_start();
23250 msg_scroll = TRUE;
23251 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23252 {
23253 msg_outnum((long)++nr);
23254 MSG_PUTS(": ");
23255 msg_outtrans(get_tv_string(&li->li_tv));
23256 msg_putchar('\n');
23257 out_flush(); /* output one line at a time */
23258 ui_breakcheck();
23259 }
23260 /* Assume "got_int" was set to truncate the listing. */
23261 got_int = FALSE;
23262
23263#ifdef FEAT_BROWSE_CMD
23264 if (cmdmod.browse)
23265 {
23266 quit_more = FALSE;
23267 nr = prompt_for_number(FALSE);
23268 msg_starthere();
23269 if (nr > 0)
23270 {
23271 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23272 (long)nr);
23273
23274 if (p != NULL)
23275 {
23276 p = expand_env_save(p);
23277 eap->arg = p;
23278 eap->cmdidx = CMD_edit;
23279 cmdmod.browse = FALSE;
23280 do_exedit(eap, NULL);
23281 vim_free(p);
23282 }
23283 }
23284 }
23285#endif
23286 }
23287}
23288
Bram Moolenaar071d4272004-06-13 20:20:40 +000023289#endif /* FEAT_EVAL */
23290
Bram Moolenaar071d4272004-06-13 20:20:40 +000023291
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023292#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023293
23294#ifdef WIN3264
23295/*
23296 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23297 */
23298static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23299static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23300static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23301
23302/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023303 * Get the short path (8.3) for the filename in "fnamep".
23304 * Only works for a valid file name.
23305 * When the path gets longer "fnamep" is changed and the allocated buffer
23306 * is put in "bufp".
23307 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23308 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309 */
23310 static int
23311get_short_pathname(fnamep, bufp, fnamelen)
23312 char_u **fnamep;
23313 char_u **bufp;
23314 int *fnamelen;
23315{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023316 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023317 char_u *newbuf;
23318
23319 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023320 l = GetShortPathName(*fnamep, *fnamep, len);
23321 if (l > len - 1)
23322 {
23323 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023324 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023325 newbuf = vim_strnsave(*fnamep, l);
23326 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023327 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023328
23329 vim_free(*bufp);
23330 *fnamep = *bufp = newbuf;
23331
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023332 /* Really should always succeed, as the buffer is big enough. */
23333 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023334 }
23335
23336 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023337 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023338}
23339
23340/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023341 * Get the short path (8.3) for the filename in "fname". The converted
23342 * path is returned in "bufp".
23343 *
23344 * Some of the directories specified in "fname" may not exist. This function
23345 * will shorten the existing directories at the beginning of the path and then
23346 * append the remaining non-existing path.
23347 *
23348 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023349 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023350 * bufp - Pointer to an allocated buffer for the filename.
23351 * fnamelen - Length of the filename pointed to by fname
23352 *
23353 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354 */
23355 static int
23356shortpath_for_invalid_fname(fname, bufp, fnamelen)
23357 char_u **fname;
23358 char_u **bufp;
23359 int *fnamelen;
23360{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023361 char_u *short_fname, *save_fname, *pbuf_unused;
23362 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023363 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023364 int old_len, len;
23365 int new_len, sfx_len;
23366 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023367
23368 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023369 old_len = *fnamelen;
23370 save_fname = vim_strnsave(*fname, old_len);
23371 pbuf_unused = NULL;
23372 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023373
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023374 endp = save_fname + old_len - 1; /* Find the end of the copy */
23375 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023376
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023377 /*
23378 * Try shortening the supplied path till it succeeds by removing one
23379 * directory at a time from the tail of the path.
23380 */
23381 len = 0;
23382 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023383 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023384 /* go back one path-separator */
23385 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23386 --endp;
23387 if (endp <= save_fname)
23388 break; /* processed the complete path */
23389
23390 /*
23391 * Replace the path separator with a NUL and try to shorten the
23392 * resulting path.
23393 */
23394 ch = *endp;
23395 *endp = 0;
23396 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023397 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023398 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23399 {
23400 retval = FAIL;
23401 goto theend;
23402 }
23403 *endp = ch; /* preserve the string */
23404
23405 if (len > 0)
23406 break; /* successfully shortened the path */
23407
23408 /* failed to shorten the path. Skip the path separator */
23409 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023410 }
23411
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023412 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023413 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023414 /*
23415 * Succeeded in shortening the path. Now concatenate the shortened
23416 * path with the remaining path at the tail.
23417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023418
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023419 /* Compute the length of the new path. */
23420 sfx_len = (int)(save_endp - endp) + 1;
23421 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023422
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023423 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023424 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023425 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023427 /* There is not enough space in the currently allocated string,
23428 * copy it to a buffer big enough. */
23429 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023430 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023431 {
23432 retval = FAIL;
23433 goto theend;
23434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023435 }
23436 else
23437 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023438 /* Transfer short_fname to the main buffer (it's big enough),
23439 * unless get_short_pathname() did its work in-place. */
23440 *fname = *bufp = save_fname;
23441 if (short_fname != save_fname)
23442 vim_strncpy(save_fname, short_fname, len);
23443 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023444 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023445
23446 /* concat the not-shortened part of the path */
23447 vim_strncpy(*fname + len, endp, sfx_len);
23448 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023449 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023450
23451theend:
23452 vim_free(pbuf_unused);
23453 vim_free(save_fname);
23454
23455 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023456}
23457
23458/*
23459 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023460 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023461 */
23462 static int
23463shortpath_for_partial(fnamep, bufp, fnamelen)
23464 char_u **fnamep;
23465 char_u **bufp;
23466 int *fnamelen;
23467{
23468 int sepcount, len, tflen;
23469 char_u *p;
23470 char_u *pbuf, *tfname;
23471 int hasTilde;
23472
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023473 /* Count up the path separators from the RHS.. so we know which part
23474 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023475 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023476 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477 if (vim_ispathsep(*p))
23478 ++sepcount;
23479
23480 /* Need full path first (use expand_env() to remove a "~/") */
23481 hasTilde = (**fnamep == '~');
23482 if (hasTilde)
23483 pbuf = tfname = expand_env_save(*fnamep);
23484 else
23485 pbuf = tfname = FullName_save(*fnamep, FALSE);
23486
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023487 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023488
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023489 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23490 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023491
23492 if (len == 0)
23493 {
23494 /* Don't have a valid filename, so shorten the rest of the
23495 * path if we can. This CAN give us invalid 8.3 filenames, but
23496 * there's not a lot of point in guessing what it might be.
23497 */
23498 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023499 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23500 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023501 }
23502
23503 /* Count the paths backward to find the beginning of the desired string. */
23504 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023505 {
23506#ifdef FEAT_MBYTE
23507 if (has_mbyte)
23508 p -= mb_head_off(tfname, p);
23509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023510 if (vim_ispathsep(*p))
23511 {
23512 if (sepcount == 0 || (hasTilde && sepcount == 1))
23513 break;
23514 else
23515 sepcount --;
23516 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023518 if (hasTilde)
23519 {
23520 --p;
23521 if (p >= tfname)
23522 *p = '~';
23523 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023524 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023525 }
23526 else
23527 ++p;
23528
23529 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23530 vim_free(*bufp);
23531 *fnamelen = (int)STRLEN(p);
23532 *bufp = pbuf;
23533 *fnamep = p;
23534
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023535 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023536}
23537#endif /* WIN3264 */
23538
23539/*
23540 * Adjust a filename, according to a string of modifiers.
23541 * *fnamep must be NUL terminated when called. When returning, the length is
23542 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023543 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023544 * When there is an error, *fnamep is set to NULL.
23545 */
23546 int
23547modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23548 char_u *src; /* string with modifiers */
23549 int *usedlen; /* characters after src that are used */
23550 char_u **fnamep; /* file name so far */
23551 char_u **bufp; /* buffer for allocated file name or NULL */
23552 int *fnamelen; /* length of fnamep */
23553{
23554 int valid = 0;
23555 char_u *tail;
23556 char_u *s, *p, *pbuf;
23557 char_u dirname[MAXPATHL];
23558 int c;
23559 int has_fullname = 0;
23560#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023561 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023562 int has_shortname = 0;
23563#endif
23564
23565repeat:
23566 /* ":p" - full path/file_name */
23567 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23568 {
23569 has_fullname = 1;
23570
23571 valid |= VALID_PATH;
23572 *usedlen += 2;
23573
23574 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23575 if ((*fnamep)[0] == '~'
23576#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23577 && ((*fnamep)[1] == '/'
23578# ifdef BACKSLASH_IN_FILENAME
23579 || (*fnamep)[1] == '\\'
23580# endif
23581 || (*fnamep)[1] == NUL)
23582
23583#endif
23584 )
23585 {
23586 *fnamep = expand_env_save(*fnamep);
23587 vim_free(*bufp); /* free any allocated file name */
23588 *bufp = *fnamep;
23589 if (*fnamep == NULL)
23590 return -1;
23591 }
23592
23593 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023594 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023595 {
23596 if (vim_ispathsep(*p)
23597 && p[1] == '.'
23598 && (p[2] == NUL
23599 || vim_ispathsep(p[2])
23600 || (p[2] == '.'
23601 && (p[3] == NUL || vim_ispathsep(p[3])))))
23602 break;
23603 }
23604
23605 /* FullName_save() is slow, don't use it when not needed. */
23606 if (*p != NUL || !vim_isAbsName(*fnamep))
23607 {
23608 *fnamep = FullName_save(*fnamep, *p != NUL);
23609 vim_free(*bufp); /* free any allocated file name */
23610 *bufp = *fnamep;
23611 if (*fnamep == NULL)
23612 return -1;
23613 }
23614
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023615#ifdef WIN3264
23616# if _WIN32_WINNT >= 0x0500
23617 if (vim_strchr(*fnamep, '~') != NULL)
23618 {
23619 /* Expand 8.3 filename to full path. Needed to make sure the same
23620 * file does not have two different names.
23621 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23622 p = alloc(_MAX_PATH + 1);
23623 if (p != NULL)
23624 {
23625 if (GetLongPathName(*fnamep, p, MAXPATHL))
23626 {
23627 vim_free(*bufp);
23628 *bufp = *fnamep = p;
23629 }
23630 else
23631 vim_free(p);
23632 }
23633 }
23634# endif
23635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023636 /* Append a path separator to a directory. */
23637 if (mch_isdir(*fnamep))
23638 {
23639 /* Make room for one or two extra characters. */
23640 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23641 vim_free(*bufp); /* free any allocated file name */
23642 *bufp = *fnamep;
23643 if (*fnamep == NULL)
23644 return -1;
23645 add_pathsep(*fnamep);
23646 }
23647 }
23648
23649 /* ":." - path relative to the current directory */
23650 /* ":~" - path relative to the home directory */
23651 /* ":8" - shortname path - postponed till after */
23652 while (src[*usedlen] == ':'
23653 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23654 {
23655 *usedlen += 2;
23656 if (c == '8')
23657 {
23658#ifdef WIN3264
23659 has_shortname = 1; /* Postpone this. */
23660#endif
23661 continue;
23662 }
23663 pbuf = NULL;
23664 /* Need full path first (use expand_env() to remove a "~/") */
23665 if (!has_fullname)
23666 {
23667 if (c == '.' && **fnamep == '~')
23668 p = pbuf = expand_env_save(*fnamep);
23669 else
23670 p = pbuf = FullName_save(*fnamep, FALSE);
23671 }
23672 else
23673 p = *fnamep;
23674
23675 has_fullname = 0;
23676
23677 if (p != NULL)
23678 {
23679 if (c == '.')
23680 {
23681 mch_dirname(dirname, MAXPATHL);
23682 s = shorten_fname(p, dirname);
23683 if (s != NULL)
23684 {
23685 *fnamep = s;
23686 if (pbuf != NULL)
23687 {
23688 vim_free(*bufp); /* free any allocated file name */
23689 *bufp = pbuf;
23690 pbuf = NULL;
23691 }
23692 }
23693 }
23694 else
23695 {
23696 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23697 /* Only replace it when it starts with '~' */
23698 if (*dirname == '~')
23699 {
23700 s = vim_strsave(dirname);
23701 if (s != NULL)
23702 {
23703 *fnamep = s;
23704 vim_free(*bufp);
23705 *bufp = s;
23706 }
23707 }
23708 }
23709 vim_free(pbuf);
23710 }
23711 }
23712
23713 tail = gettail(*fnamep);
23714 *fnamelen = (int)STRLEN(*fnamep);
23715
23716 /* ":h" - head, remove "/file_name", can be repeated */
23717 /* Don't remove the first "/" or "c:\" */
23718 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23719 {
23720 valid |= VALID_HEAD;
23721 *usedlen += 2;
23722 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023723 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023724 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023725 *fnamelen = (int)(tail - *fnamep);
23726#ifdef VMS
23727 if (*fnamelen > 0)
23728 *fnamelen += 1; /* the path separator is part of the path */
23729#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023730 if (*fnamelen == 0)
23731 {
23732 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23733 p = vim_strsave((char_u *)".");
23734 if (p == NULL)
23735 return -1;
23736 vim_free(*bufp);
23737 *bufp = *fnamep = tail = p;
23738 *fnamelen = 1;
23739 }
23740 else
23741 {
23742 while (tail > s && !after_pathsep(s, tail))
23743 mb_ptr_back(*fnamep, tail);
23744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023745 }
23746
23747 /* ":8" - shortname */
23748 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23749 {
23750 *usedlen += 2;
23751#ifdef WIN3264
23752 has_shortname = 1;
23753#endif
23754 }
23755
23756#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023757 /*
23758 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023759 */
23760 if (has_shortname)
23761 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023762 /* Copy the string if it is shortened by :h and when it wasn't copied
23763 * yet, because we are going to change it in place. Avoids changing
23764 * the buffer name for "%:8". */
23765 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023766 {
23767 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023768 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023769 return -1;
23770 vim_free(*bufp);
23771 *bufp = *fnamep = p;
23772 }
23773
23774 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023775 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023776 if (!has_fullname && !vim_isAbsName(*fnamep))
23777 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023778 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023779 return -1;
23780 }
23781 else
23782 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023783 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023784
Bram Moolenaardc935552011-08-17 15:23:23 +020023785 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023786 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023787 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023788 return -1;
23789
23790 if (l == 0)
23791 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023792 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023793 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023794 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023795 return -1;
23796 }
23797 *fnamelen = l;
23798 }
23799 }
23800#endif /* WIN3264 */
23801
23802 /* ":t" - tail, just the basename */
23803 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23804 {
23805 *usedlen += 2;
23806 *fnamelen -= (int)(tail - *fnamep);
23807 *fnamep = tail;
23808 }
23809
23810 /* ":e" - extension, can be repeated */
23811 /* ":r" - root, without extension, can be repeated */
23812 while (src[*usedlen] == ':'
23813 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23814 {
23815 /* find a '.' in the tail:
23816 * - for second :e: before the current fname
23817 * - otherwise: The last '.'
23818 */
23819 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23820 s = *fnamep - 2;
23821 else
23822 s = *fnamep + *fnamelen - 1;
23823 for ( ; s > tail; --s)
23824 if (s[0] == '.')
23825 break;
23826 if (src[*usedlen + 1] == 'e') /* :e */
23827 {
23828 if (s > tail)
23829 {
23830 *fnamelen += (int)(*fnamep - (s + 1));
23831 *fnamep = s + 1;
23832#ifdef VMS
23833 /* cut version from the extension */
23834 s = *fnamep + *fnamelen - 1;
23835 for ( ; s > *fnamep; --s)
23836 if (s[0] == ';')
23837 break;
23838 if (s > *fnamep)
23839 *fnamelen = s - *fnamep;
23840#endif
23841 }
23842 else if (*fnamep <= tail)
23843 *fnamelen = 0;
23844 }
23845 else /* :r */
23846 {
23847 if (s > tail) /* remove one extension */
23848 *fnamelen = (int)(s - *fnamep);
23849 }
23850 *usedlen += 2;
23851 }
23852
23853 /* ":s?pat?foo?" - substitute */
23854 /* ":gs?pat?foo?" - global substitute */
23855 if (src[*usedlen] == ':'
23856 && (src[*usedlen + 1] == 's'
23857 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23858 {
23859 char_u *str;
23860 char_u *pat;
23861 char_u *sub;
23862 int sep;
23863 char_u *flags;
23864 int didit = FALSE;
23865
23866 flags = (char_u *)"";
23867 s = src + *usedlen + 2;
23868 if (src[*usedlen + 1] == 'g')
23869 {
23870 flags = (char_u *)"g";
23871 ++s;
23872 }
23873
23874 sep = *s++;
23875 if (sep)
23876 {
23877 /* find end of pattern */
23878 p = vim_strchr(s, sep);
23879 if (p != NULL)
23880 {
23881 pat = vim_strnsave(s, (int)(p - s));
23882 if (pat != NULL)
23883 {
23884 s = p + 1;
23885 /* find end of substitution */
23886 p = vim_strchr(s, sep);
23887 if (p != NULL)
23888 {
23889 sub = vim_strnsave(s, (int)(p - s));
23890 str = vim_strnsave(*fnamep, *fnamelen);
23891 if (sub != NULL && str != NULL)
23892 {
23893 *usedlen = (int)(p + 1 - src);
23894 s = do_string_sub(str, pat, sub, flags);
23895 if (s != NULL)
23896 {
23897 *fnamep = s;
23898 *fnamelen = (int)STRLEN(s);
23899 vim_free(*bufp);
23900 *bufp = s;
23901 didit = TRUE;
23902 }
23903 }
23904 vim_free(sub);
23905 vim_free(str);
23906 }
23907 vim_free(pat);
23908 }
23909 }
23910 /* after using ":s", repeat all the modifiers */
23911 if (didit)
23912 goto repeat;
23913 }
23914 }
23915
23916 return valid;
23917}
23918
23919/*
23920 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23921 * "flags" can be "g" to do a global substitute.
23922 * Returns an allocated string, NULL for error.
23923 */
23924 char_u *
23925do_string_sub(str, pat, sub, flags)
23926 char_u *str;
23927 char_u *pat;
23928 char_u *sub;
23929 char_u *flags;
23930{
23931 int sublen;
23932 regmatch_T regmatch;
23933 int i;
23934 int do_all;
23935 char_u *tail;
23936 garray_T ga;
23937 char_u *ret;
23938 char_u *save_cpo;
23939
23940 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23941 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023942 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023943
23944 ga_init2(&ga, 1, 200);
23945
23946 do_all = (flags[0] == 'g');
23947
23948 regmatch.rm_ic = p_ic;
23949 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23950 if (regmatch.regprog != NULL)
23951 {
23952 tail = str;
23953 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23954 {
23955 /*
23956 * Get some space for a temporary buffer to do the substitution
23957 * into. It will contain:
23958 * - The text up to where the match is.
23959 * - The substituted text.
23960 * - The text after the match.
23961 */
23962 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23963 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23964 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23965 {
23966 ga_clear(&ga);
23967 break;
23968 }
23969
23970 /* copy the text up to where the match is */
23971 i = (int)(regmatch.startp[0] - tail);
23972 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23973 /* add the substituted text */
23974 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23975 + ga.ga_len + i, TRUE, TRUE, FALSE);
23976 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023977 /* avoid getting stuck on a match with an empty string */
23978 if (tail == regmatch.endp[0])
23979 {
23980 if (*tail == NUL)
23981 break;
23982 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23983 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023984 }
23985 else
23986 {
23987 tail = regmatch.endp[0];
23988 if (*tail == NUL)
23989 break;
23990 }
23991 if (!do_all)
23992 break;
23993 }
23994
23995 if (ga.ga_data != NULL)
23996 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23997
23998 vim_free(regmatch.regprog);
23999 }
24000
24001 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24002 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024003 if (p_cpo == empty_option)
24004 p_cpo = save_cpo;
24005 else
24006 /* Darn, evaluating {sub} expression changed the value. */
24007 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008
24009 return ret;
24010}
24011
24012#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */