blob: 7c575576548484a09cbffbb65a27c083a7ea6097 [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 Moolenaar9750bb12012-12-05 16:10:42 +0100671static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000674static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000676static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000683static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000684static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000685static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000686static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200688static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000689static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000691static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200692static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#ifdef FEAT_FLOAT
695static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200696static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000697#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000699static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000700static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
701static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000703#ifdef FEAT_FLOAT
704static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
706#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000707static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200708static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709#ifdef HAVE_STRFTIME
710static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
711#endif
712static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200718static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200719static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000725static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200726static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000727static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000728static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000729static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000730static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000731static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000732static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000734static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200735#ifdef FEAT_FLOAT
736static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
738#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000742#ifdef FEAT_FLOAT
743static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
744#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200746static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200747static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000757static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000760static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100761static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000763static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000764static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static int get_env_len __ARGS((char_u **arg));
766static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000767static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000768static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
769#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
770#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
771 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000772static 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 +0000773static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000774static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000775static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
776static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static typval_T *alloc_tv __ARGS((void));
778static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000779static void init_tv __ARGS((typval_T *varp));
780static long get_tv_number __ARGS((typval_T *varp));
781static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000782static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static char_u *get_tv_string __ARGS((typval_T *varp));
784static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000785static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000786static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000787static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
789static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
790static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000791static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
792static 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 +0000793static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
794static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000795static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200796static int var_check_func_name __ARGS((char_u *name, int new_var));
797static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000798static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000799static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000800static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
801static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
802static int eval_fname_script __ARGS((char_u *p));
803static int eval_fname_sid __ARGS((char_u *p));
804static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000805static ufunc_T *find_func __ARGS((char_u *name));
806static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000807static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808#ifdef FEAT_PROFILE
809static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000810static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
811static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
812static int
813# ifdef __BORLANDC__
814 _RTLENTRYF
815# endif
816 prof_total_cmp __ARGS((const void *s1, const void *s2));
817static int
818# ifdef __BORLANDC__
819 _RTLENTRYF
820# endif
821 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000822#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000823static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000824static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000825static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000826static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000827static 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 +0000828static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
829static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000831static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
832static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000833static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000834static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000835static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000836
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200837
838#ifdef EBCDIC
839static int compare_func_name __ARGS((const void *s1, const void *s2));
840static void sortFunctions __ARGS(());
841#endif
842
843
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000844/* Character used as separated in autoload function/variable names. */
845#define AUTOLOAD_CHAR '#'
846
Bram Moolenaar33570922005-01-25 22:26:29 +0000847/*
848 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000849 */
850 void
851eval_init()
852{
Bram Moolenaar33570922005-01-25 22:26:29 +0000853 int i;
854 struct vimvar *p;
855
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200856 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
857 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200858 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000859 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000860 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000861
862 for (i = 0; i < VV_LEN; ++i)
863 {
864 p = &vimvars[i];
865 STRCPY(p->vv_di.di_key, p->vv_name);
866 if (p->vv_flags & VV_RO)
867 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
868 else if (p->vv_flags & VV_RO_SBX)
869 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
870 else
871 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000872
873 /* add to v: scope dict, unless the value is not always available */
874 if (p->vv_type != VAR_UNKNOWN)
875 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000876 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000877 /* add to compat scope dict */
878 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000879 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000880 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200881 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200882
883#ifdef EBCDIC
884 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100885 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200886 */
887 sortFunctions();
888#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000889}
890
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000891#if defined(EXITFREE) || defined(PROTO)
892 void
893eval_clear()
894{
895 int i;
896 struct vimvar *p;
897
898 for (i = 0; i < VV_LEN; ++i)
899 {
900 p = &vimvars[i];
901 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000902 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000903 vim_free(p->vv_str);
904 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000905 }
906 else if (p->vv_di.di_tv.v_type == VAR_LIST)
907 {
908 list_unref(p->vv_list);
909 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000910 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911 }
912 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000913 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000914 hash_clear(&compat_hashtab);
915
Bram Moolenaard9fba312005-06-26 22:34:35 +0000916 free_scriptnames();
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200917 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000918
919 /* global variables */
920 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000921
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000922 /* autoloaded script names */
923 ga_clear_strings(&ga_loaded);
924
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200925 /* script-local variables */
926 for (i = 1; i <= ga_scripts.ga_len; ++i)
927 {
928 vars_clear(&SCRIPT_VARS(i));
929 vim_free(SCRIPT_SV(i));
930 }
931 ga_clear(&ga_scripts);
932
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000933 /* unreferenced lists and dicts */
934 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000935
936 /* functions */
937 free_all_functions();
938 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000939}
940#endif
941
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 * Return the name of the executed function.
944 */
945 char_u *
946func_name(cookie)
947 void *cookie;
948{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000949 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950}
951
952/*
953 * Return the address holding the next breakpoint line for a funccall cookie.
954 */
955 linenr_T *
956func_breakpoint(cookie)
957 void *cookie;
958{
Bram Moolenaar33570922005-01-25 22:26:29 +0000959 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960}
961
962/*
963 * Return the address holding the debug tick for a funccall cookie.
964 */
965 int *
966func_dbg_tick(cookie)
967 void *cookie;
968{
Bram Moolenaar33570922005-01-25 22:26:29 +0000969 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970}
971
972/*
973 * Return the nesting level for a funccall cookie.
974 */
975 int
976func_level(cookie)
977 void *cookie;
978{
Bram Moolenaar33570922005-01-25 22:26:29 +0000979 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980}
981
982/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000983funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000985/* pointer to list of previously used funccal, still around because some
986 * item in it is still being used. */
987funccall_T *previous_funccal = NULL;
988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989/*
990 * Return TRUE when a function was ended by a ":return" command.
991 */
992 int
993current_func_returned()
994{
995 return current_funccal->returned;
996}
997
998
999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 * Set an internal variable to a string value. Creates the variable if it does
1001 * not already exist.
1002 */
1003 void
1004set_internal_string_var(name, value)
1005 char_u *name;
1006 char_u *value;
1007{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001009 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
1011 val = vim_strsave(value);
1012 if (val != NULL)
1013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001014 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001015 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001017 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001018 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 }
1020 }
1021}
1022
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001023static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001024static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001025static char_u *redir_endp = NULL;
1026static char_u *redir_varname = NULL;
1027
1028/*
1029 * Start recording command output to a variable
1030 * Returns OK if successfully completed the setup. FAIL otherwise.
1031 */
1032 int
1033var_redir_start(name, append)
1034 char_u *name;
1035 int append; /* append to an existing variable */
1036{
1037 int save_emsg;
1038 int err;
1039 typval_T tv;
1040
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001041 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001042 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001043 {
1044 EMSG(_(e_invarg));
1045 return FAIL;
1046 }
1047
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001048 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049 redir_varname = vim_strsave(name);
1050 if (redir_varname == NULL)
1051 return FAIL;
1052
1053 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1054 if (redir_lval == NULL)
1055 {
1056 var_redir_stop();
1057 return FAIL;
1058 }
1059
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001060 /* The output is stored in growarray "redir_ga" until redirection ends. */
1061 ga_init2(&redir_ga, (int)sizeof(char), 500);
1062
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001064 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1065 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1067 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001068 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 if (redir_endp != NULL && *redir_endp != NUL)
1070 /* Trailing characters are present after the variable name */
1071 EMSG(_(e_trailing));
1072 else
1073 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001074 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 var_redir_stop();
1076 return FAIL;
1077 }
1078
1079 /* check if we can write to the variable: set it to or append an empty
1080 * string */
1081 save_emsg = did_emsg;
1082 did_emsg = FALSE;
1083 tv.v_type = VAR_STRING;
1084 tv.vval.v_string = (char_u *)"";
1085 if (append)
1086 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1087 else
1088 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001089 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001091 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 if (err)
1093 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001094 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095 var_redir_stop();
1096 return FAIL;
1097 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098
1099 return OK;
1100}
1101
1102/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001103 * Append "value[value_len]" to the variable set by var_redir_start().
1104 * The actual appending is postponed until redirection ends, because the value
1105 * appended may in fact be the string we write to, changing it may cause freed
1106 * memory to be used:
1107 * :redir => foo
1108 * :let foo
1109 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 */
1111 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001112var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001116 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117
1118 if (redir_lval == NULL)
1119 return;
1120
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001121 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001124 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001126 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 {
1128 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001129 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 }
1131 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133}
1134
1135/*
1136 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001137 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 */
1139 void
1140var_redir_stop()
1141{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142 typval_T tv;
1143
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 if (redir_lval != NULL)
1145 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001146 /* If there was no error: assign the text to the variable. */
1147 if (redir_endp != NULL)
1148 {
1149 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1150 tv.v_type = VAR_STRING;
1151 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001152 /* Call get_lval() again, if it's inside a Dict or List it may
1153 * have changed. */
1154 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1155 FALSE, FALSE, FALSE, FNE_CHECK_START);
1156 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1157 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1158 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001160
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001161 /* free the collected output */
1162 vim_free(redir_ga.ga_data);
1163 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001164
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 vim_free(redir_lval);
1166 redir_lval = NULL;
1167 }
1168 vim_free(redir_varname);
1169 redir_varname = NULL;
1170}
1171
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172# if defined(FEAT_MBYTE) || defined(PROTO)
1173 int
1174eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1175 char_u *enc_from;
1176 char_u *enc_to;
1177 char_u *fname_from;
1178 char_u *fname_to;
1179{
1180 int err = FALSE;
1181
1182 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1183 set_vim_var_string(VV_CC_TO, enc_to, -1);
1184 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1185 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1186 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1187 err = TRUE;
1188 set_vim_var_string(VV_CC_FROM, NULL, -1);
1189 set_vim_var_string(VV_CC_TO, NULL, -1);
1190 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1191 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1192
1193 if (err)
1194 return FAIL;
1195 return OK;
1196}
1197# endif
1198
1199# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1200 int
1201eval_printexpr(fname, args)
1202 char_u *fname;
1203 char_u *args;
1204{
1205 int err = FALSE;
1206
1207 set_vim_var_string(VV_FNAME_IN, fname, -1);
1208 set_vim_var_string(VV_CMDARG, args, -1);
1209 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1210 err = TRUE;
1211 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1212 set_vim_var_string(VV_CMDARG, NULL, -1);
1213
1214 if (err)
1215 {
1216 mch_remove(fname);
1217 return FAIL;
1218 }
1219 return OK;
1220}
1221# endif
1222
1223# if defined(FEAT_DIFF) || defined(PROTO)
1224 void
1225eval_diff(origfile, newfile, outfile)
1226 char_u *origfile;
1227 char_u *newfile;
1228 char_u *outfile;
1229{
1230 int err = FALSE;
1231
1232 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1233 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1234 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1235 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1236 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1237 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1238 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1239}
1240
1241 void
1242eval_patch(origfile, difffile, outfile)
1243 char_u *origfile;
1244 char_u *difffile;
1245 char_u *outfile;
1246{
1247 int err;
1248
1249 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1250 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1251 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1252 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1253 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1254 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1255 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1256}
1257# endif
1258
1259/*
1260 * Top level evaluation function, returning a boolean.
1261 * Sets "error" to TRUE if there was an error.
1262 * Return TRUE or FALSE.
1263 */
1264 int
1265eval_to_bool(arg, error, nextcmd, skip)
1266 char_u *arg;
1267 int *error;
1268 char_u **nextcmd;
1269 int skip; /* only parse, don't execute */
1270{
Bram Moolenaar33570922005-01-25 22:26:29 +00001271 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 int retval = FALSE;
1273
1274 if (skip)
1275 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001276 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 else
1279 {
1280 *error = FALSE;
1281 if (!skip)
1282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001283 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001284 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 }
1286 }
1287 if (skip)
1288 --emsg_skip;
1289
1290 return retval;
1291}
1292
1293/*
1294 * Top level evaluation function, returning a string. If "skip" is TRUE,
1295 * only parsing to "nextcmd" is done, without reporting errors. Return
1296 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1297 */
1298 char_u *
1299eval_to_string_skip(arg, nextcmd, skip)
1300 char_u *arg;
1301 char_u **nextcmd;
1302 int skip; /* only parse, don't execute */
1303{
Bram Moolenaar33570922005-01-25 22:26:29 +00001304 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 char_u *retval;
1306
1307 if (skip)
1308 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001309 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 retval = NULL;
1311 else
1312 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001313 retval = vim_strsave(get_tv_string(&tv));
1314 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 }
1316 if (skip)
1317 --emsg_skip;
1318
1319 return retval;
1320}
1321
1322/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001323 * Skip over an expression at "*pp".
1324 * Return FAIL for an error, OK otherwise.
1325 */
1326 int
1327skip_expr(pp)
1328 char_u **pp;
1329{
Bram Moolenaar33570922005-01-25 22:26:29 +00001330 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001331
1332 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001333 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001334}
1335
1336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001338 * When "convert" is TRUE convert a List into a sequence of lines and convert
1339 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 * Return pointer to allocated memory, or NULL for failure.
1341 */
1342 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001343eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 char_u *arg;
1345 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001346 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347{
Bram Moolenaar33570922005-01-25 22:26:29 +00001348 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001350 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001351#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001355 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 retval = NULL;
1357 else
1358 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001360 {
1361 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001362 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001363 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001364 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001365 if (tv.vval.v_list->lv_len > 0)
1366 ga_append(&ga, NL);
1367 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001368 ga_append(&ga, NUL);
1369 retval = (char_u *)ga.ga_data;
1370 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001371#ifdef FEAT_FLOAT
1372 else if (convert && tv.v_type == VAR_FLOAT)
1373 {
1374 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1375 retval = vim_strsave(numbuf);
1376 }
1377#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001378 else
1379 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 }
1382
1383 return retval;
1384}
1385
1386/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001387 * Call eval_to_string() without using current local variables and using
1388 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 */
1390 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 char_u *arg;
1393 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001394 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395{
1396 char_u *retval;
1397 void *save_funccalp;
1398
1399 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 if (use_sandbox)
1401 ++sandbox;
1402 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001403 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001404 if (use_sandbox)
1405 --sandbox;
1406 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 restore_funccal(save_funccalp);
1408 return retval;
1409}
1410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411/*
1412 * Top level evaluation function, returning a number.
1413 * Evaluates "expr" silently.
1414 * Returns -1 for an error.
1415 */
1416 int
1417eval_to_number(expr)
1418 char_u *expr;
1419{
Bram Moolenaar33570922005-01-25 22:26:29 +00001420 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001422 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
1424 ++emsg_off;
1425
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001426 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 retval = -1;
1428 else
1429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001430 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001431 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
1433 --emsg_off;
1434
1435 return retval;
1436}
1437
Bram Moolenaara40058a2005-07-11 22:42:07 +00001438/*
1439 * Prepare v: variable "idx" to be used.
1440 * Save the current typeval in "save_tv".
1441 * When not used yet add the variable to the v: hashtable.
1442 */
1443 static void
1444prepare_vimvar(idx, save_tv)
1445 int idx;
1446 typval_T *save_tv;
1447{
1448 *save_tv = vimvars[idx].vv_tv;
1449 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1450 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1451}
1452
1453/*
1454 * Restore v: variable "idx" to typeval "save_tv".
1455 * When no longer defined, remove the variable from the v: hashtable.
1456 */
1457 static void
1458restore_vimvar(idx, save_tv)
1459 int idx;
1460 typval_T *save_tv;
1461{
1462 hashitem_T *hi;
1463
Bram Moolenaara40058a2005-07-11 22:42:07 +00001464 vimvars[idx].vv_tv = *save_tv;
1465 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1466 {
1467 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1468 if (HASHITEM_EMPTY(hi))
1469 EMSG2(_(e_intern2), "restore_vimvar()");
1470 else
1471 hash_remove(&vimvarht, hi);
1472 }
1473}
1474
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001475#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001476/*
1477 * Evaluate an expression to a list with suggestions.
1478 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001479 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001480 */
1481 list_T *
1482eval_spell_expr(badword, expr)
1483 char_u *badword;
1484 char_u *expr;
1485{
1486 typval_T save_val;
1487 typval_T rettv;
1488 list_T *list = NULL;
1489 char_u *p = skipwhite(expr);
1490
1491 /* Set "v:val" to the bad word. */
1492 prepare_vimvar(VV_VAL, &save_val);
1493 vimvars[VV_VAL].vv_type = VAR_STRING;
1494 vimvars[VV_VAL].vv_str = badword;
1495 if (p_verbose == 0)
1496 ++emsg_off;
1497
1498 if (eval1(&p, &rettv, TRUE) == OK)
1499 {
1500 if (rettv.v_type != VAR_LIST)
1501 clear_tv(&rettv);
1502 else
1503 list = rettv.vval.v_list;
1504 }
1505
1506 if (p_verbose == 0)
1507 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001508 restore_vimvar(VV_VAL, &save_val);
1509
1510 return list;
1511}
1512
1513/*
1514 * "list" is supposed to contain two items: a word and a number. Return the
1515 * word in "pp" and the number as the return value.
1516 * Return -1 if anything isn't right.
1517 * Used to get the good word and score from the eval_spell_expr() result.
1518 */
1519 int
1520get_spellword(list, pp)
1521 list_T *list;
1522 char_u **pp;
1523{
1524 listitem_T *li;
1525
1526 li = list->lv_first;
1527 if (li == NULL)
1528 return -1;
1529 *pp = get_tv_string(&li->li_tv);
1530
1531 li = li->li_next;
1532 if (li == NULL)
1533 return -1;
1534 return get_tv_number(&li->li_tv);
1535}
1536#endif
1537
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001538/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001539 * Top level evaluation function.
1540 * Returns an allocated typval_T with the result.
1541 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001542 */
1543 typval_T *
1544eval_expr(arg, nextcmd)
1545 char_u *arg;
1546 char_u **nextcmd;
1547{
1548 typval_T *tv;
1549
1550 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001551 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001552 {
1553 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001554 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001555 }
1556
1557 return tv;
1558}
1559
1560
Bram Moolenaar4f688582007-07-24 12:34:30 +00001561#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1562 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001564 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001565 * Uses argv[argc] for the function arguments. Only Number and String
1566 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001567 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001569 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001570call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 char_u *func;
1572 int argc;
1573 char_u **argv;
1574 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001575 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001576 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577{
Bram Moolenaar33570922005-01-25 22:26:29 +00001578 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 long n;
1580 int len;
1581 int i;
1582 int doesrange;
1583 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001584 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001586 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001588 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
1590 for (i = 0; i < argc; i++)
1591 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001592 /* Pass a NULL or empty argument as an empty string */
1593 if (argv[i] == NULL || *argv[i] == NUL)
1594 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001595 argvars[i].v_type = VAR_STRING;
1596 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001597 continue;
1598 }
1599
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001600 if (str_arg_only)
1601 len = 0;
1602 else
1603 /* Recognize a number argument, the others must be strings. */
1604 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 if (len != 0 && len == (int)STRLEN(argv[i]))
1606 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001607 argvars[i].v_type = VAR_NUMBER;
1608 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
1610 else
1611 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001612 argvars[i].v_type = VAR_STRING;
1613 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 }
1615 }
1616
1617 if (safe)
1618 {
1619 save_funccalp = save_funccal();
1620 ++sandbox;
1621 }
1622
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1624 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001626 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 if (safe)
1628 {
1629 --sandbox;
1630 restore_funccal(save_funccalp);
1631 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001632 vim_free(argvars);
1633
1634 if (ret == FAIL)
1635 clear_tv(rettv);
1636
1637 return ret;
1638}
1639
Bram Moolenaar4f688582007-07-24 12:34:30 +00001640# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001641/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001642 * Call vimL function "func" and return the result as a string.
1643 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001644 * Uses argv[argc] for the function arguments.
1645 */
1646 void *
1647call_func_retstr(func, argc, argv, safe)
1648 char_u *func;
1649 int argc;
1650 char_u **argv;
1651 int safe; /* use the sandbox */
1652{
1653 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001654 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001655
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001656 /* All arguments are passed as strings, no conversion to number. */
1657 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001658 return NULL;
1659
1660 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001661 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 return retval;
1663}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001664# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001665
Bram Moolenaar4f688582007-07-24 12:34:30 +00001666# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001667/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001668 * Call vimL function "func" and return the result as a number.
1669 * Returns -1 when calling the function fails.
1670 * Uses argv[argc] for the function arguments.
1671 */
1672 long
1673call_func_retnr(func, argc, argv, safe)
1674 char_u *func;
1675 int argc;
1676 char_u **argv;
1677 int safe; /* use the sandbox */
1678{
1679 typval_T rettv;
1680 long retval;
1681
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001682 /* All arguments are passed as strings, no conversion to number. */
1683 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001684 return -1;
1685
1686 retval = get_tv_number_chk(&rettv, NULL);
1687 clear_tv(&rettv);
1688 return retval;
1689}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001690# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001691
1692/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001693 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001694 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001695 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696 */
1697 void *
1698call_func_retlist(func, argc, argv, safe)
1699 char_u *func;
1700 int argc;
1701 char_u **argv;
1702 int safe; /* use the sandbox */
1703{
1704 typval_T rettv;
1705
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001706 /* All arguments are passed as strings, no conversion to number. */
1707 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708 return NULL;
1709
1710 if (rettv.v_type != VAR_LIST)
1711 {
1712 clear_tv(&rettv);
1713 return NULL;
1714 }
1715
1716 return rettv.vval.v_list;
1717}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718#endif
1719
Bram Moolenaar4f688582007-07-24 12:34:30 +00001720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721/*
1722 * Save the current function call pointer, and set it to NULL.
1723 * Used when executing autocommands and for ":source".
1724 */
1725 void *
1726save_funccal()
1727{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001728 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 current_funccal = NULL;
1731 return (void *)fc;
1732}
1733
1734 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001735restore_funccal(vfc)
1736 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001738 funccall_T *fc = (funccall_T *)vfc;
1739
1740 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741}
1742
Bram Moolenaar05159a02005-02-26 23:04:13 +00001743#if defined(FEAT_PROFILE) || defined(PROTO)
1744/*
1745 * Prepare profiling for entering a child or something else that is not
1746 * counted for the script/function itself.
1747 * Should always be called in pair with prof_child_exit().
1748 */
1749 void
1750prof_child_enter(tm)
1751 proftime_T *tm; /* place to store waittime */
1752{
1753 funccall_T *fc = current_funccal;
1754
1755 if (fc != NULL && fc->func->uf_profiling)
1756 profile_start(&fc->prof_child);
1757 script_prof_save(tm);
1758}
1759
1760/*
1761 * Take care of time spent in a child.
1762 * Should always be called after prof_child_enter().
1763 */
1764 void
1765prof_child_exit(tm)
1766 proftime_T *tm; /* where waittime was stored */
1767{
1768 funccall_T *fc = current_funccal;
1769
1770 if (fc != NULL && fc->func->uf_profiling)
1771 {
1772 profile_end(&fc->prof_child);
1773 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1774 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1775 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1776 }
1777 script_prof_restore(tm);
1778}
1779#endif
1780
1781
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782#ifdef FEAT_FOLDING
1783/*
1784 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1785 * it in "*cp". Doesn't give error messages.
1786 */
1787 int
1788eval_foldexpr(arg, cp)
1789 char_u *arg;
1790 int *cp;
1791{
Bram Moolenaar33570922005-01-25 22:26:29 +00001792 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 int retval;
1794 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001795 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1796 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
1798 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001799 if (use_sandbox)
1800 ++sandbox;
1801 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001803 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 retval = 0;
1805 else
1806 {
1807 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808 if (tv.v_type == VAR_NUMBER)
1809 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001810 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 retval = 0;
1812 else
1813 {
1814 /* If the result is a string, check if there is a non-digit before
1815 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001816 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 if (!VIM_ISDIGIT(*s) && *s != '-')
1818 *cp = *s++;
1819 retval = atol((char *)s);
1820 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001821 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 }
1823 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001824 if (use_sandbox)
1825 --sandbox;
1826 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827
1828 return retval;
1829}
1830#endif
1831
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 * ":let" list all variable values
1834 * ":let var1 var2" list variable values
1835 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001836 * ":let var += expr" assignment command.
1837 * ":let var -= expr" assignment command.
1838 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 */
1841 void
1842ex_let(eap)
1843 exarg_T *eap;
1844{
1845 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001847 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001849 int var_count = 0;
1850 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001851 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001852 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001853 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
Bram Moolenaardb552d602006-03-23 22:59:57 +00001855 argend = skip_var_list(arg, &var_count, &semicolon);
1856 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001858 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1859 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001860 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001863 /*
1864 * ":let" without "=": list variables
1865 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001866 if (*arg == '[')
1867 EMSG(_(e_invarg));
1868 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001870 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001872 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001874 list_glob_vars(&first);
1875 list_buf_vars(&first);
1876 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001877#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001878 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001879#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001880 list_script_vars(&first);
1881 list_func_vars(&first);
1882 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 eap->nextcmd = check_nextcmd(arg);
1885 }
1886 else
1887 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001888 op[0] = '=';
1889 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001890 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001891 {
1892 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1893 op[0] = expr[-1]; /* +=, -= or .= */
1894 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001895 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 if (eap->skip)
1898 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 if (eap->skip)
1901 {
1902 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 --emsg_skip;
1905 }
1906 else if (i != FAIL)
1907 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001908 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001909 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 }
1912 }
1913}
1914
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915/*
1916 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1917 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001918 * When "nextchars" is not NULL it points to a string with characters that
1919 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1920 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 * Returns OK or FAIL;
1922 */
1923 static int
1924ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1925 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001926 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927 int copy; /* copy values from "tv", don't move */
1928 int semicolon; /* from skip_var_list() */
1929 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001930 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931{
1932 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001933 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001935 listitem_T *item;
1936 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937
1938 if (*arg != '[')
1939 {
1940 /*
1941 * ":let var = expr" or ":for var in list"
1942 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001943 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944 return FAIL;
1945 return OK;
1946 }
1947
1948 /*
1949 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1950 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001951 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 {
1953 EMSG(_(e_listreq));
1954 return FAIL;
1955 }
1956
1957 i = list_len(l);
1958 if (semicolon == 0 && var_count < i)
1959 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001960 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961 return FAIL;
1962 }
1963 if (var_count - semicolon > i)
1964 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001965 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 return FAIL;
1967 }
1968
1969 item = l->lv_first;
1970 while (*arg != ']')
1971 {
1972 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001973 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 item = item->li_next;
1975 if (arg == NULL)
1976 return FAIL;
1977
1978 arg = skipwhite(arg);
1979 if (*arg == ';')
1980 {
1981 /* Put the rest of the list (may be empty) in the var after ';'.
1982 * Create a new list for this. */
1983 l = list_alloc();
1984 if (l == NULL)
1985 return FAIL;
1986 while (item != NULL)
1987 {
1988 list_append_tv(l, &item->li_tv);
1989 item = item->li_next;
1990 }
1991
1992 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001993 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 ltv.vval.v_list = l;
1995 l->lv_refcount = 1;
1996
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001997 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1998 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 clear_tv(&ltv);
2000 if (arg == NULL)
2001 return FAIL;
2002 break;
2003 }
2004 else if (*arg != ',' && *arg != ']')
2005 {
2006 EMSG2(_(e_intern2), "ex_let_vars()");
2007 return FAIL;
2008 }
2009 }
2010
2011 return OK;
2012}
2013
2014/*
2015 * Skip over assignable variable "var" or list of variables "[var, var]".
2016 * Used for ":let varvar = expr" and ":for varvar in expr".
2017 * For "[var, var]" increment "*var_count" for each variable.
2018 * for "[var, var; var]" set "semicolon".
2019 * Return NULL for an error.
2020 */
2021 static char_u *
2022skip_var_list(arg, var_count, semicolon)
2023 char_u *arg;
2024 int *var_count;
2025 int *semicolon;
2026{
2027 char_u *p, *s;
2028
2029 if (*arg == '[')
2030 {
2031 /* "[var, var]": find the matching ']'. */
2032 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002033 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002034 {
2035 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2036 s = skip_var_one(p);
2037 if (s == p)
2038 {
2039 EMSG2(_(e_invarg2), p);
2040 return NULL;
2041 }
2042 ++*var_count;
2043
2044 p = skipwhite(s);
2045 if (*p == ']')
2046 break;
2047 else if (*p == ';')
2048 {
2049 if (*semicolon == 1)
2050 {
2051 EMSG(_("Double ; in list of variables"));
2052 return NULL;
2053 }
2054 *semicolon = 1;
2055 }
2056 else if (*p != ',')
2057 {
2058 EMSG2(_(e_invarg2), p);
2059 return NULL;
2060 }
2061 }
2062 return p + 1;
2063 }
2064 else
2065 return skip_var_one(arg);
2066}
2067
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002068/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002069 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002070 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002071 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002072 static char_u *
2073skip_var_one(arg)
2074 char_u *arg;
2075{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002076 if (*arg == '@' && arg[1] != NUL)
2077 return arg + 2;
2078 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2079 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002080}
2081
Bram Moolenaara7043832005-01-21 11:56:39 +00002082/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002083 * List variables for hashtab "ht" with prefix "prefix".
2084 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002085 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002086 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002087list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002088 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002090 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002091 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002092{
Bram Moolenaar33570922005-01-25 22:26:29 +00002093 hashitem_T *hi;
2094 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002095 int todo;
2096
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002097 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002098 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2099 {
2100 if (!HASHITEM_EMPTY(hi))
2101 {
2102 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002103 di = HI2DI(hi);
2104 if (empty || di->di_tv.v_type != VAR_STRING
2105 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002106 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002107 }
2108 }
2109}
2110
2111/*
2112 * List global variables.
2113 */
2114 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002115list_glob_vars(first)
2116 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002117{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002118 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002119}
2120
2121/*
2122 * List buffer variables.
2123 */
2124 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125list_buf_vars(first)
2126 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002127{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002128 char_u numbuf[NUMBUFLEN];
2129
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002130 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2131 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002132
2133 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002134 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2135 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002136}
2137
2138/*
2139 * List window variables.
2140 */
2141 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002142list_win_vars(first)
2143 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002144{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2146 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002147}
2148
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002149#ifdef FEAT_WINDOWS
2150/*
2151 * List tab page variables.
2152 */
2153 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154list_tab_vars(first)
2155 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002156{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2158 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002159}
2160#endif
2161
Bram Moolenaara7043832005-01-21 11:56:39 +00002162/*
2163 * List Vim variables.
2164 */
2165 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166list_vim_vars(first)
2167 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002169 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002170}
2171
2172/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002173 * List script-local variables, if there is a script.
2174 */
2175 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176list_script_vars(first)
2177 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002178{
2179 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2181 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002182}
2183
2184/*
2185 * List function variables, if there is a function.
2186 */
2187 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188list_func_vars(first)
2189 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002190{
2191 if (current_funccal != NULL)
2192 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002194}
2195
2196/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 * List variables in "arg".
2198 */
2199 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002200list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 exarg_T *eap;
2202 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002203 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204{
2205 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002206 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002208 char_u *name_start;
2209 char_u *arg_subsc;
2210 char_u *tofree;
2211 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212
2213 while (!ends_excmd(*arg) && !got_int)
2214 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002215 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002217 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2219 {
2220 emsg_severe = TRUE;
2221 EMSG(_(e_trailing));
2222 break;
2223 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002227 /* get_name_len() takes care of expanding curly braces */
2228 name_start = name = arg;
2229 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2230 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 /* This is mainly to keep test 49 working: when expanding
2233 * curly braces fails overrule the exception error message. */
2234 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 emsg_severe = TRUE;
2237 EMSG2(_(e_invarg2), arg);
2238 break;
2239 }
2240 error = TRUE;
2241 }
2242 else
2243 {
2244 if (tofree != NULL)
2245 name = tofree;
2246 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 else
2249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 /* handle d.key, l[idx], f(expr) */
2251 arg_subsc = arg;
2252 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002253 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002259 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002260 case 'g': list_glob_vars(first); break;
2261 case 'b': list_buf_vars(first); break;
2262 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002263#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002264 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002265#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002266 case 'v': list_vim_vars(first); break;
2267 case 's': list_script_vars(first); break;
2268 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002269 default:
2270 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002271 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002272 }
2273 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 {
2275 char_u numbuf[NUMBUFLEN];
2276 char_u *tf;
2277 int c;
2278 char_u *s;
2279
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002280 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281 c = *arg;
2282 *arg = NUL;
2283 list_one_var_a((char_u *)"",
2284 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002285 tv.v_type,
2286 s == NULL ? (char_u *)"" : s,
2287 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 *arg = c;
2289 vim_free(tf);
2290 }
2291 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002292 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 }
2294 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295
2296 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002298
2299 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 }
2301
2302 return arg;
2303}
2304
2305/*
2306 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2307 * Returns a pointer to the char just after the var name.
2308 * Returns NULL if there is an error.
2309 */
2310 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002311ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002313 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002315 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002316 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317{
2318 int c1;
2319 char_u *name;
2320 char_u *p;
2321 char_u *arg_end = NULL;
2322 int len;
2323 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002324 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325
2326 /*
2327 * ":let $VAR = expr": Set environment variable.
2328 */
2329 if (*arg == '$')
2330 {
2331 /* Find the end of the name. */
2332 ++arg;
2333 name = arg;
2334 len = get_env_len(&arg);
2335 if (len == 0)
2336 EMSG2(_(e_invarg2), name - 1);
2337 else
2338 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002339 if (op != NULL && (*op == '+' || *op == '-'))
2340 EMSG2(_(e_letwrong), op);
2341 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002342 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002344 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345 {
2346 c1 = name[len];
2347 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002348 p = get_tv_string_chk(tv);
2349 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002350 {
2351 int mustfree = FALSE;
2352 char_u *s = vim_getenv(name, &mustfree);
2353
2354 if (s != NULL)
2355 {
2356 p = tofree = concat_str(s, p);
2357 if (mustfree)
2358 vim_free(s);
2359 }
2360 }
2361 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002363 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002364 if (STRICMP(name, "HOME") == 0)
2365 init_homedir();
2366 else if (didset_vim && STRICMP(name, "VIM") == 0)
2367 didset_vim = FALSE;
2368 else if (didset_vimruntime
2369 && STRICMP(name, "VIMRUNTIME") == 0)
2370 didset_vimruntime = FALSE;
2371 arg_end = arg;
2372 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002374 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375 }
2376 }
2377 }
2378
2379 /*
2380 * ":let &option = expr": Set option value.
2381 * ":let &l:option = expr": Set local option value.
2382 * ":let &g:option = expr": Set global option value.
2383 */
2384 else if (*arg == '&')
2385 {
2386 /* Find the end of the name. */
2387 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002388 if (p == NULL || (endchars != NULL
2389 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 EMSG(_(e_letunexp));
2391 else
2392 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002393 long n;
2394 int opt_type;
2395 long numval;
2396 char_u *stringval = NULL;
2397 char_u *s;
2398
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002399 c1 = *p;
2400 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002401
2402 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002403 s = get_tv_string_chk(tv); /* != NULL if number or string */
2404 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405 {
2406 opt_type = get_option_value(arg, &numval,
2407 &stringval, opt_flags);
2408 if ((opt_type == 1 && *op == '.')
2409 || (opt_type == 0 && *op != '.'))
2410 EMSG2(_(e_letwrong), op);
2411 else
2412 {
2413 if (opt_type == 1) /* number */
2414 {
2415 if (*op == '+')
2416 n = numval + n;
2417 else
2418 n = numval - n;
2419 }
2420 else if (opt_type == 0 && stringval != NULL) /* string */
2421 {
2422 s = concat_str(stringval, s);
2423 vim_free(stringval);
2424 stringval = s;
2425 }
2426 }
2427 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002428 if (s != NULL)
2429 {
2430 set_option_value(arg, n, s, opt_flags);
2431 arg_end = p;
2432 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002434 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002435 }
2436 }
2437
2438 /*
2439 * ":let @r = expr": Set register contents.
2440 */
2441 else if (*arg == '@')
2442 {
2443 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 if (op != NULL && (*op == '+' || *op == '-'))
2445 EMSG2(_(e_letwrong), op);
2446 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002447 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002448 EMSG(_(e_letunexp));
2449 else
2450 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002451 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 char_u *s;
2453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002454 p = get_tv_string_chk(tv);
2455 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002457 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 if (s != NULL)
2459 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002460 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 vim_free(s);
2462 }
2463 }
2464 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002465 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002467 arg_end = arg + 1;
2468 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002469 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 }
2471 }
2472
2473 /*
2474 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002477 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002479 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002481 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2485 EMSG(_(e_letunexp));
2486 else
2487 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 arg_end = p;
2490 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002491 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002493 }
2494
2495 else
2496 EMSG2(_(e_invarg2), arg);
2497
2498 return arg_end;
2499}
2500
2501/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002502 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2503 */
2504 static int
2505check_changedtick(arg)
2506 char_u *arg;
2507{
2508 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2509 {
2510 EMSG2(_(e_readonlyvar), arg);
2511 return TRUE;
2512 }
2513 return FALSE;
2514}
2515
2516/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 * Get an lval: variable, Dict item or List item that can be assigned a value
2518 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2519 * "name.key", "name.key[expr]" etc.
2520 * Indexing only works if "name" is an existing List or Dictionary.
2521 * "name" points to the start of the name.
2522 * If "rettv" is not NULL it points to the value to be assigned.
2523 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2524 * wrong; must end in space or cmd separator.
2525 *
2526 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002527 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529 */
2530 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002531get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002532 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 typval_T *rettv;
2534 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 int unlet;
2536 int skip;
2537 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002538 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002539{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002541 char_u *expr_start, *expr_end;
2542 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002543 dictitem_T *v;
2544 typval_T var1;
2545 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002547 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002548 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002549 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002550 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002551
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002553 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554
2555 if (skip)
2556 {
2557 /* When skipping just find the end of the name. */
2558 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002559 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 }
2561
2562 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002563 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 if (expr_start != NULL)
2565 {
2566 /* Don't expand the name when we already know there is an error. */
2567 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2568 && *p != '[' && *p != '.')
2569 {
2570 EMSG(_(e_trailing));
2571 return NULL;
2572 }
2573
2574 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2575 if (lp->ll_exp_name == NULL)
2576 {
2577 /* Report an invalid expression in braces, unless the
2578 * expression evaluation has been cancelled due to an
2579 * aborting error, an interrupt, or an exception. */
2580 if (!aborting() && !quiet)
2581 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002582 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 EMSG2(_(e_invarg2), name);
2584 return NULL;
2585 }
2586 }
2587 lp->ll_name = lp->ll_exp_name;
2588 }
2589 else
2590 lp->ll_name = name;
2591
2592 /* Without [idx] or .key we are done. */
2593 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2594 return p;
2595
2596 cc = *p;
2597 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002598 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 if (v == NULL && !quiet)
2600 EMSG2(_(e_undefvar), lp->ll_name);
2601 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002602 if (v == NULL)
2603 return NULL;
2604
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 /*
2606 * Loop until no more [idx] or .key is following.
2607 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002608 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2612 && !(lp->ll_tv->v_type == VAR_DICT
2613 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 if (!quiet)
2616 EMSG(_("E689: Can only index a List or Dictionary"));
2617 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002618 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002620 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 if (!quiet)
2622 EMSG(_("E708: [:] must come last"));
2623 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002624 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002625
Bram Moolenaar8c711452005-01-14 21:53:12 +00002626 len = -1;
2627 if (*p == '.')
2628 {
2629 key = p + 1;
2630 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2631 ;
2632 if (len == 0)
2633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (!quiet)
2635 EMSG(_(e_emptykey));
2636 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002637 }
2638 p = key + len;
2639 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002640 else
2641 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002643 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 if (*p == ':')
2645 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002646 else
2647 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 empty1 = FALSE;
2649 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002651 if (get_tv_string_chk(&var1) == NULL)
2652 {
2653 /* not a number or string */
2654 clear_tv(&var1);
2655 return NULL;
2656 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 }
2658
2659 /* Optionally get the second index [ :expr]. */
2660 if (*p == ':')
2661 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002665 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002666 if (!empty1)
2667 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002669 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 if (rettv != NULL && (rettv->v_type != VAR_LIST
2671 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 if (!quiet)
2674 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 if (!empty1)
2676 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 }
2679 p = skipwhite(p + 1);
2680 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 else
2683 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2686 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 if (!empty1)
2688 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002691 if (get_tv_string_chk(&var2) == NULL)
2692 {
2693 /* not a number or string */
2694 if (!empty1)
2695 clear_tv(&var1);
2696 clear_tv(&var2);
2697 return NULL;
2698 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002701 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 if (!quiet)
2708 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 if (!empty1)
2710 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 }
2715
2716 /* Skip to past ']'. */
2717 ++p;
2718 }
2719
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 {
2722 if (len == -1)
2723 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002725 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 if (*key == NUL)
2727 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 if (!quiet)
2729 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 }
2733 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002734 lp->ll_list = NULL;
2735 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002736 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002737
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002738 /* When assigning to a scope dictionary check that a function and
2739 * variable name is valid (only variable name unless it is l: or
2740 * g: dictionary). Disallow overwriting a builtin function. */
2741 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002742 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002743 int prevval;
2744 int wrong;
2745
2746 if (len != -1)
2747 {
2748 prevval = key[len];
2749 key[len] = NUL;
2750 }
2751 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2752 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002753 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002754 || !valid_varname(key);
2755 if (len != -1)
2756 key[len] = prevval;
2757 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002758 return NULL;
2759 }
2760
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002763 /* Can't add "v:" variable. */
2764 if (lp->ll_dict == &vimvardict)
2765 {
2766 EMSG2(_(e_illvar), name);
2767 return NULL;
2768 }
2769
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002770 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002774 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 if (len == -1)
2776 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 }
2779 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 if (len == -1)
2784 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 p = NULL;
2787 break;
2788 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002789 /* existing variable, need to check if it can be changed */
2790 else if (var_check_ro(lp->ll_di->di_flags, name))
2791 return NULL;
2792
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 if (len == -1)
2794 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 }
2797 else
2798 {
2799 /*
2800 * Get the number and item for the only or first index of the List.
2801 */
2802 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 else
2805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002806 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 clear_tv(&var1);
2808 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002809 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 lp->ll_list = lp->ll_tv->vval.v_list;
2811 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2812 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002814 if (lp->ll_n1 < 0)
2815 {
2816 lp->ll_n1 = 0;
2817 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2818 }
2819 }
2820 if (lp->ll_li == NULL)
2821 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002824 if (!quiet)
2825 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 }
2828
2829 /*
2830 * May need to find the item or absolute index for the second
2831 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 * When no index given: "lp->ll_empty2" is TRUE.
2833 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002837 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002843 {
2844 if (!quiet)
2845 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002847 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 }
2850
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2852 if (lp->ll_n1 < 0)
2853 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2854 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002855 {
2856 if (!quiet)
2857 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002859 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002860 }
2861
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002863 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002864 }
2865
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 return p;
2867}
2868
2869/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002870 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 */
2872 static void
2873clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002874 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875{
2876 vim_free(lp->ll_exp_name);
2877 vim_free(lp->ll_newkey);
2878}
2879
2880/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002881 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002883 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 */
2885 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002886set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002887 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002889 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002891 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892{
2893 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 listitem_T *ri;
2895 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896
2897 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002898 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002900 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 cc = *endp;
2902 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002903 if (op != NULL && *op != '=')
2904 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002905 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002906
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002907 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002908 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002909 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002910 {
2911 if (tv_op(&tv, rettv, op) == OK)
2912 set_var(lp->ll_name, &tv, FALSE);
2913 clear_tv(&tv);
2914 }
2915 }
2916 else
2917 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002919 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002921 else if (tv_check_lock(lp->ll_newkey == NULL
2922 ? lp->ll_tv->v_lock
2923 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2924 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 else if (lp->ll_range)
2926 {
2927 /*
2928 * Assign the List values to the list items.
2929 */
2930 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002931 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002932 if (op != NULL && *op != '=')
2933 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2934 else
2935 {
2936 clear_tv(&lp->ll_li->li_tv);
2937 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2938 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 ri = ri->li_next;
2940 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2941 break;
2942 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002943 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002945 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002946 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 ri = NULL;
2948 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002949 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002950 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 lp->ll_li = lp->ll_li->li_next;
2952 ++lp->ll_n1;
2953 }
2954 if (ri != NULL)
2955 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002956 else if (lp->ll_empty2
2957 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 : lp->ll_n1 != lp->ll_n2)
2959 EMSG(_("E711: List value has not enough items"));
2960 }
2961 else
2962 {
2963 /*
2964 * Assign to a List or Dictionary item.
2965 */
2966 if (lp->ll_newkey != NULL)
2967 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002968 if (op != NULL && *op != '=')
2969 {
2970 EMSG2(_(e_letwrong), op);
2971 return;
2972 }
2973
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002974 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002975 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 if (di == NULL)
2977 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002978 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2979 {
2980 vim_free(di);
2981 return;
2982 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002983 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002984 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002985 else if (op != NULL && *op != '=')
2986 {
2987 tv_op(lp->ll_tv, rettv, op);
2988 return;
2989 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002990 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002991 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002992
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 /*
2994 * Assign the value to the variable or list item.
2995 */
2996 if (copy)
2997 copy_tv(rettv, lp->ll_tv);
2998 else
2999 {
3000 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003001 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003003 }
3004 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003005}
3006
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003007/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003008 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3009 * Returns OK or FAIL.
3010 */
3011 static int
3012tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003013 typval_T *tv1;
3014 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003015 char_u *op;
3016{
3017 long n;
3018 char_u numbuf[NUMBUFLEN];
3019 char_u *s;
3020
3021 /* Can't do anything with a Funcref or a Dict on the right. */
3022 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3023 {
3024 switch (tv1->v_type)
3025 {
3026 case VAR_DICT:
3027 case VAR_FUNC:
3028 break;
3029
3030 case VAR_LIST:
3031 if (*op != '+' || tv2->v_type != VAR_LIST)
3032 break;
3033 /* List += List */
3034 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3035 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3036 return OK;
3037
3038 case VAR_NUMBER:
3039 case VAR_STRING:
3040 if (tv2->v_type == VAR_LIST)
3041 break;
3042 if (*op == '+' || *op == '-')
3043 {
3044 /* nr += nr or nr -= nr*/
3045 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003046#ifdef FEAT_FLOAT
3047 if (tv2->v_type == VAR_FLOAT)
3048 {
3049 float_T f = n;
3050
3051 if (*op == '+')
3052 f += tv2->vval.v_float;
3053 else
3054 f -= tv2->vval.v_float;
3055 clear_tv(tv1);
3056 tv1->v_type = VAR_FLOAT;
3057 tv1->vval.v_float = f;
3058 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003059 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003060#endif
3061 {
3062 if (*op == '+')
3063 n += get_tv_number(tv2);
3064 else
3065 n -= get_tv_number(tv2);
3066 clear_tv(tv1);
3067 tv1->v_type = VAR_NUMBER;
3068 tv1->vval.v_number = n;
3069 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003070 }
3071 else
3072 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003073 if (tv2->v_type == VAR_FLOAT)
3074 break;
3075
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003076 /* str .= str */
3077 s = get_tv_string(tv1);
3078 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3079 clear_tv(tv1);
3080 tv1->v_type = VAR_STRING;
3081 tv1->vval.v_string = s;
3082 }
3083 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003084
3085#ifdef FEAT_FLOAT
3086 case VAR_FLOAT:
3087 {
3088 float_T f;
3089
3090 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3091 && tv2->v_type != VAR_NUMBER
3092 && tv2->v_type != VAR_STRING))
3093 break;
3094 if (tv2->v_type == VAR_FLOAT)
3095 f = tv2->vval.v_float;
3096 else
3097 f = get_tv_number(tv2);
3098 if (*op == '+')
3099 tv1->vval.v_float += f;
3100 else
3101 tv1->vval.v_float -= f;
3102 }
3103 return OK;
3104#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003105 }
3106 }
3107
3108 EMSG2(_(e_letwrong), op);
3109 return FAIL;
3110}
3111
3112/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003113 * Add a watcher to a list.
3114 */
3115 static void
3116list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003117 list_T *l;
3118 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003119{
3120 lw->lw_next = l->lv_watch;
3121 l->lv_watch = lw;
3122}
3123
3124/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003125 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003126 * No warning when it isn't found...
3127 */
3128 static void
3129list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003130 list_T *l;
3131 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003132{
Bram Moolenaar33570922005-01-25 22:26:29 +00003133 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134
3135 lwp = &l->lv_watch;
3136 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3137 {
3138 if (lw == lwrem)
3139 {
3140 *lwp = lw->lw_next;
3141 break;
3142 }
3143 lwp = &lw->lw_next;
3144 }
3145}
3146
3147/*
3148 * Just before removing an item from a list: advance watchers to the next
3149 * item.
3150 */
3151 static void
3152list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003153 list_T *l;
3154 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003155{
Bram Moolenaar33570922005-01-25 22:26:29 +00003156 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003157
3158 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3159 if (lw->lw_item == item)
3160 lw->lw_item = item->li_next;
3161}
3162
3163/*
3164 * Evaluate the expression used in a ":for var in expr" command.
3165 * "arg" points to "var".
3166 * Set "*errp" to TRUE for an error, FALSE otherwise;
3167 * Return a pointer that holds the info. Null when there is an error.
3168 */
3169 void *
3170eval_for_line(arg, errp, nextcmdp, skip)
3171 char_u *arg;
3172 int *errp;
3173 char_u **nextcmdp;
3174 int skip;
3175{
Bram Moolenaar33570922005-01-25 22:26:29 +00003176 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003177 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003178 typval_T tv;
3179 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180
3181 *errp = TRUE; /* default: there is an error */
3182
Bram Moolenaar33570922005-01-25 22:26:29 +00003183 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184 if (fi == NULL)
3185 return NULL;
3186
3187 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3188 if (expr == NULL)
3189 return fi;
3190
3191 expr = skipwhite(expr);
3192 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3193 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003194 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195 return fi;
3196 }
3197
3198 if (skip)
3199 ++emsg_skip;
3200 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3201 {
3202 *errp = FALSE;
3203 if (!skip)
3204 {
3205 l = tv.vval.v_list;
3206 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003207 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003209 clear_tv(&tv);
3210 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003211 else
3212 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003213 /* No need to increment the refcount, it's already set for the
3214 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215 fi->fi_list = l;
3216 list_add_watch(l, &fi->fi_lw);
3217 fi->fi_lw.lw_item = l->lv_first;
3218 }
3219 }
3220 }
3221 if (skip)
3222 --emsg_skip;
3223
3224 return fi;
3225}
3226
3227/*
3228 * Use the first item in a ":for" list. Advance to the next.
3229 * Assign the values to the variable (list). "arg" points to the first one.
3230 * Return TRUE when a valid item was found, FALSE when at end of list or
3231 * something wrong.
3232 */
3233 int
3234next_for_item(fi_void, arg)
3235 void *fi_void;
3236 char_u *arg;
3237{
Bram Moolenaar33570922005-01-25 22:26:29 +00003238 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003240 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241
3242 item = fi->fi_lw.lw_item;
3243 if (item == NULL)
3244 result = FALSE;
3245 else
3246 {
3247 fi->fi_lw.lw_item = item->li_next;
3248 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3249 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3250 }
3251 return result;
3252}
3253
3254/*
3255 * Free the structure used to store info used by ":for".
3256 */
3257 void
3258free_for_info(fi_void)
3259 void *fi_void;
3260{
Bram Moolenaar33570922005-01-25 22:26:29 +00003261 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003263 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003264 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003265 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003266 list_unref(fi->fi_list);
3267 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003268 vim_free(fi);
3269}
3270
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3272
3273 void
3274set_context_for_expression(xp, arg, cmdidx)
3275 expand_T *xp;
3276 char_u *arg;
3277 cmdidx_T cmdidx;
3278{
3279 int got_eq = FALSE;
3280 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003281 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 if (cmdidx == CMD_let)
3284 {
3285 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003286 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003287 {
3288 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003289 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003290 {
3291 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003292 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293 if (vim_iswhite(*p))
3294 break;
3295 }
3296 return;
3297 }
3298 }
3299 else
3300 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3301 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 while ((xp->xp_pattern = vim_strpbrk(arg,
3303 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3304 {
3305 c = *xp->xp_pattern;
3306 if (c == '&')
3307 {
3308 c = xp->xp_pattern[1];
3309 if (c == '&')
3310 {
3311 ++xp->xp_pattern;
3312 xp->xp_context = cmdidx != CMD_let || got_eq
3313 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3314 }
3315 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003316 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003318 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3319 xp->xp_pattern += 2;
3320
3321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 }
3323 else if (c == '$')
3324 {
3325 /* environment variable */
3326 xp->xp_context = EXPAND_ENV_VARS;
3327 }
3328 else if (c == '=')
3329 {
3330 got_eq = TRUE;
3331 xp->xp_context = EXPAND_EXPRESSION;
3332 }
3333 else if (c == '<'
3334 && xp->xp_context == EXPAND_FUNCTIONS
3335 && vim_strchr(xp->xp_pattern, '(') == NULL)
3336 {
3337 /* Function name can start with "<SNR>" */
3338 break;
3339 }
3340 else if (cmdidx != CMD_let || got_eq)
3341 {
3342 if (c == '"') /* string */
3343 {
3344 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3345 if (c == '\\' && xp->xp_pattern[1] != NUL)
3346 ++xp->xp_pattern;
3347 xp->xp_context = EXPAND_NOTHING;
3348 }
3349 else if (c == '\'') /* literal string */
3350 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003351 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3353 /* skip */ ;
3354 xp->xp_context = EXPAND_NOTHING;
3355 }
3356 else if (c == '|')
3357 {
3358 if (xp->xp_pattern[1] == '|')
3359 {
3360 ++xp->xp_pattern;
3361 xp->xp_context = EXPAND_EXPRESSION;
3362 }
3363 else
3364 xp->xp_context = EXPAND_COMMANDS;
3365 }
3366 else
3367 xp->xp_context = EXPAND_EXPRESSION;
3368 }
3369 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003370 /* Doesn't look like something valid, expand as an expression
3371 * anyway. */
3372 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 arg = xp->xp_pattern;
3374 if (*arg != NUL)
3375 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3376 /* skip */ ;
3377 }
3378 xp->xp_pattern = arg;
3379}
3380
3381#endif /* FEAT_CMDL_COMPL */
3382
3383/*
3384 * ":1,25call func(arg1, arg2)" function call.
3385 */
3386 void
3387ex_call(eap)
3388 exarg_T *eap;
3389{
3390 char_u *arg = eap->arg;
3391 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003393 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003395 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 linenr_T lnum;
3397 int doesrange;
3398 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003399 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003401 if (eap->skip)
3402 {
3403 /* trans_function_name() doesn't work well when skipping, use eval0()
3404 * instead to skip to any following command, e.g. for:
3405 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003406 ++emsg_skip;
3407 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3408 clear_tv(&rettv);
3409 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003410 return;
3411 }
3412
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003413 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003414 if (fudi.fd_newkey != NULL)
3415 {
3416 /* Still need to give an error message for missing key. */
3417 EMSG2(_(e_dictkey), fudi.fd_newkey);
3418 vim_free(fudi.fd_newkey);
3419 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003420 if (tofree == NULL)
3421 return;
3422
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003423 /* Increase refcount on dictionary, it could get deleted when evaluating
3424 * the arguments. */
3425 if (fudi.fd_dict != NULL)
3426 ++fudi.fd_dict->dv_refcount;
3427
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003428 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003429 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003430 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
Bram Moolenaar532c7802005-01-27 14:44:31 +00003432 /* Skip white space to allow ":call func ()". Not good, but required for
3433 * backward compatibility. */
3434 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003435 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
3437 if (*startarg != '(')
3438 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003439 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 goto end;
3441 }
3442
3443 /*
3444 * When skipping, evaluate the function once, to find the end of the
3445 * arguments.
3446 * When the function takes a range, this is discovered after the first
3447 * call, and the loop is broken.
3448 */
3449 if (eap->skip)
3450 {
3451 ++emsg_skip;
3452 lnum = eap->line2; /* do it once, also with an invalid range */
3453 }
3454 else
3455 lnum = eap->line1;
3456 for ( ; lnum <= eap->line2; ++lnum)
3457 {
3458 if (!eap->skip && eap->addr_count > 0)
3459 {
3460 curwin->w_cursor.lnum = lnum;
3461 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003462#ifdef FEAT_VIRTUALEDIT
3463 curwin->w_cursor.coladd = 0;
3464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 }
3466 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003467 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003468 eap->line1, eap->line2, &doesrange,
3469 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 {
3471 failed = TRUE;
3472 break;
3473 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003474
3475 /* Handle a function returning a Funcref, Dictionary or List. */
3476 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3477 {
3478 failed = TRUE;
3479 break;
3480 }
3481
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003482 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 if (doesrange || eap->skip)
3484 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003485
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003487 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003488 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003489 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 if (aborting())
3491 break;
3492 }
3493 if (eap->skip)
3494 --emsg_skip;
3495
3496 if (!failed)
3497 {
3498 /* Check for trailing illegal characters and a following command. */
3499 if (!ends_excmd(*arg))
3500 {
3501 emsg_severe = TRUE;
3502 EMSG(_(e_trailing));
3503 }
3504 else
3505 eap->nextcmd = check_nextcmd(arg);
3506 }
3507
3508end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003509 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003510 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511}
3512
3513/*
3514 * ":unlet[!] var1 ... " command.
3515 */
3516 void
3517ex_unlet(eap)
3518 exarg_T *eap;
3519{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003520 ex_unletlock(eap, eap->arg, 0);
3521}
3522
3523/*
3524 * ":lockvar" and ":unlockvar" commands
3525 */
3526 void
3527ex_lockvar(eap)
3528 exarg_T *eap;
3529{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003531 int deep = 2;
3532
3533 if (eap->forceit)
3534 deep = -1;
3535 else if (vim_isdigit(*arg))
3536 {
3537 deep = getdigits(&arg);
3538 arg = skipwhite(arg);
3539 }
3540
3541 ex_unletlock(eap, arg, deep);
3542}
3543
3544/*
3545 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3546 */
3547 static void
3548ex_unletlock(eap, argstart, deep)
3549 exarg_T *eap;
3550 char_u *argstart;
3551 int deep;
3552{
3553 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003556 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557
3558 do
3559 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003560 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003561 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3562 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003563 if (lv.ll_name == NULL)
3564 error = TRUE; /* error but continue parsing */
3565 if (name_end == NULL || (!vim_iswhite(*name_end)
3566 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003568 if (name_end != NULL)
3569 {
3570 emsg_severe = TRUE;
3571 EMSG(_(e_trailing));
3572 }
3573 if (!(eap->skip || error))
3574 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 break;
3576 }
3577
3578 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003579 {
3580 if (eap->cmdidx == CMD_unlet)
3581 {
3582 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3583 error = TRUE;
3584 }
3585 else
3586 {
3587 if (do_lock_var(&lv, name_end, deep,
3588 eap->cmdidx == CMD_lockvar) == FAIL)
3589 error = TRUE;
3590 }
3591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003593 if (!eap->skip)
3594 clear_lval(&lv);
3595
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 arg = skipwhite(name_end);
3597 } while (!ends_excmd(*arg));
3598
3599 eap->nextcmd = check_nextcmd(arg);
3600}
3601
Bram Moolenaar8c711452005-01-14 21:53:12 +00003602 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003603do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003604 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003605 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003606 int forceit;
3607{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003608 int ret = OK;
3609 int cc;
3610
3611 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003612 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613 cc = *name_end;
3614 *name_end = NUL;
3615
3616 /* Normal name or expanded name. */
3617 if (check_changedtick(lp->ll_name))
3618 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003619 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003620 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003622 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003623 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3624 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 else if (lp->ll_range)
3626 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003627 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003628
3629 /* Delete a range of List items. */
3630 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3631 {
3632 li = lp->ll_li->li_next;
3633 listitem_remove(lp->ll_list, lp->ll_li);
3634 lp->ll_li = li;
3635 ++lp->ll_n1;
3636 }
3637 }
3638 else
3639 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003640 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003641 /* unlet a List item. */
3642 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003643 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003645 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 }
3647
3648 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003649}
3650
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651/*
3652 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003653 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 */
3655 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003656do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003658 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659{
Bram Moolenaar33570922005-01-25 22:26:29 +00003660 hashtab_T *ht;
3661 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003662 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003663 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
Bram Moolenaar33570922005-01-25 22:26:29 +00003665 ht = find_var_ht(name, &varname);
3666 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003668 hi = hash_find(ht, varname);
3669 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003670 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003671 di = HI2DI(hi);
3672 if (var_check_fixed(di->di_flags, name)
3673 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003674 return FAIL;
3675 delete_var(ht, hi);
3676 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003679 if (forceit)
3680 return OK;
3681 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 return FAIL;
3683}
3684
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003685/*
3686 * Lock or unlock variable indicated by "lp".
3687 * "deep" is the levels to go (-1 for unlimited);
3688 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3689 */
3690 static int
3691do_lock_var(lp, name_end, deep, lock)
3692 lval_T *lp;
3693 char_u *name_end;
3694 int deep;
3695 int lock;
3696{
3697 int ret = OK;
3698 int cc;
3699 dictitem_T *di;
3700
3701 if (deep == 0) /* nothing to do */
3702 return OK;
3703
3704 if (lp->ll_tv == NULL)
3705 {
3706 cc = *name_end;
3707 *name_end = NUL;
3708
3709 /* Normal name or expanded name. */
3710 if (check_changedtick(lp->ll_name))
3711 ret = FAIL;
3712 else
3713 {
3714 di = find_var(lp->ll_name, NULL);
3715 if (di == NULL)
3716 ret = FAIL;
3717 else
3718 {
3719 if (lock)
3720 di->di_flags |= DI_FLAGS_LOCK;
3721 else
3722 di->di_flags &= ~DI_FLAGS_LOCK;
3723 item_lock(&di->di_tv, deep, lock);
3724 }
3725 }
3726 *name_end = cc;
3727 }
3728 else if (lp->ll_range)
3729 {
3730 listitem_T *li = lp->ll_li;
3731
3732 /* (un)lock a range of List items. */
3733 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3734 {
3735 item_lock(&li->li_tv, deep, lock);
3736 li = li->li_next;
3737 ++lp->ll_n1;
3738 }
3739 }
3740 else if (lp->ll_list != NULL)
3741 /* (un)lock a List item. */
3742 item_lock(&lp->ll_li->li_tv, deep, lock);
3743 else
3744 /* un(lock) a Dictionary item. */
3745 item_lock(&lp->ll_di->di_tv, deep, lock);
3746
3747 return ret;
3748}
3749
3750/*
3751 * Lock or unlock an item. "deep" is nr of levels to go.
3752 */
3753 static void
3754item_lock(tv, deep, lock)
3755 typval_T *tv;
3756 int deep;
3757 int lock;
3758{
3759 static int recurse = 0;
3760 list_T *l;
3761 listitem_T *li;
3762 dict_T *d;
3763 hashitem_T *hi;
3764 int todo;
3765
3766 if (recurse >= DICT_MAXNEST)
3767 {
3768 EMSG(_("E743: variable nested too deep for (un)lock"));
3769 return;
3770 }
3771 if (deep == 0)
3772 return;
3773 ++recurse;
3774
3775 /* lock/unlock the item itself */
3776 if (lock)
3777 tv->v_lock |= VAR_LOCKED;
3778 else
3779 tv->v_lock &= ~VAR_LOCKED;
3780
3781 switch (tv->v_type)
3782 {
3783 case VAR_LIST:
3784 if ((l = tv->vval.v_list) != NULL)
3785 {
3786 if (lock)
3787 l->lv_lock |= VAR_LOCKED;
3788 else
3789 l->lv_lock &= ~VAR_LOCKED;
3790 if (deep < 0 || deep > 1)
3791 /* recursive: lock/unlock the items the List contains */
3792 for (li = l->lv_first; li != NULL; li = li->li_next)
3793 item_lock(&li->li_tv, deep - 1, lock);
3794 }
3795 break;
3796 case VAR_DICT:
3797 if ((d = tv->vval.v_dict) != NULL)
3798 {
3799 if (lock)
3800 d->dv_lock |= VAR_LOCKED;
3801 else
3802 d->dv_lock &= ~VAR_LOCKED;
3803 if (deep < 0 || deep > 1)
3804 {
3805 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003806 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003807 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3808 {
3809 if (!HASHITEM_EMPTY(hi))
3810 {
3811 --todo;
3812 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3813 }
3814 }
3815 }
3816 }
3817 }
3818 --recurse;
3819}
3820
Bram Moolenaara40058a2005-07-11 22:42:07 +00003821/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003822 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3823 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003824 */
3825 static int
3826tv_islocked(tv)
3827 typval_T *tv;
3828{
3829 return (tv->v_lock & VAR_LOCKED)
3830 || (tv->v_type == VAR_LIST
3831 && tv->vval.v_list != NULL
3832 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3833 || (tv->v_type == VAR_DICT
3834 && tv->vval.v_dict != NULL
3835 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3836}
3837
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3839/*
3840 * Delete all "menutrans_" variables.
3841 */
3842 void
3843del_menutrans_vars()
3844{
Bram Moolenaar33570922005-01-25 22:26:29 +00003845 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003846 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847
Bram Moolenaar33570922005-01-25 22:26:29 +00003848 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003849 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003850 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003851 {
3852 if (!HASHITEM_EMPTY(hi))
3853 {
3854 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003855 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3856 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003857 }
3858 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003859 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860}
3861#endif
3862
3863#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3864
3865/*
3866 * Local string buffer for the next two functions to store a variable name
3867 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3868 * get_user_var_name().
3869 */
3870
3871static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3872
3873static char_u *varnamebuf = NULL;
3874static int varnamebuflen = 0;
3875
3876/*
3877 * Function to concatenate a prefix and a variable name.
3878 */
3879 static char_u *
3880cat_prefix_varname(prefix, name)
3881 int prefix;
3882 char_u *name;
3883{
3884 int len;
3885
3886 len = (int)STRLEN(name) + 3;
3887 if (len > varnamebuflen)
3888 {
3889 vim_free(varnamebuf);
3890 len += 10; /* some additional space */
3891 varnamebuf = alloc(len);
3892 if (varnamebuf == NULL)
3893 {
3894 varnamebuflen = 0;
3895 return NULL;
3896 }
3897 varnamebuflen = len;
3898 }
3899 *varnamebuf = prefix;
3900 varnamebuf[1] = ':';
3901 STRCPY(varnamebuf + 2, name);
3902 return varnamebuf;
3903}
3904
3905/*
3906 * Function given to ExpandGeneric() to obtain the list of user defined
3907 * (global/buffer/window/built-in) variable names.
3908 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 char_u *
3910get_user_var_name(xp, idx)
3911 expand_T *xp;
3912 int idx;
3913{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003914 static long_u gdone;
3915 static long_u bdone;
3916 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003917#ifdef FEAT_WINDOWS
3918 static long_u tdone;
3919#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003920 static int vidx;
3921 static hashitem_T *hi;
3922 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923
3924 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003925 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003926 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003927#ifdef FEAT_WINDOWS
3928 tdone = 0;
3929#endif
3930 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003931
3932 /* Global variables */
3933 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003935 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003937 else
3938 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003939 while (HASHITEM_EMPTY(hi))
3940 ++hi;
3941 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3942 return cat_prefix_varname('g', hi->hi_key);
3943 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003945
3946 /* b: variables */
3947 ht = &curbuf->b_vars.dv_hashtab;
3948 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003952 else
3953 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003954 while (HASHITEM_EMPTY(hi))
3955 ++hi;
3956 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003958 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003960 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 return (char_u *)"b:changedtick";
3962 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003963
3964 /* w: variables */
3965 ht = &curwin->w_vars.dv_hashtab;
3966 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003968 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003969 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003970 else
3971 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003972 while (HASHITEM_EMPTY(hi))
3973 ++hi;
3974 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003976
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003977#ifdef FEAT_WINDOWS
3978 /* t: variables */
3979 ht = &curtab->tp_vars.dv_hashtab;
3980 if (tdone < ht->ht_used)
3981 {
3982 if (tdone++ == 0)
3983 hi = ht->ht_array;
3984 else
3985 ++hi;
3986 while (HASHITEM_EMPTY(hi))
3987 ++hi;
3988 return cat_prefix_varname('t', hi->hi_key);
3989 }
3990#endif
3991
Bram Moolenaar33570922005-01-25 22:26:29 +00003992 /* v: variables */
3993 if (vidx < VV_LEN)
3994 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995
3996 vim_free(varnamebuf);
3997 varnamebuf = NULL;
3998 varnamebuflen = 0;
3999 return NULL;
4000}
4001
4002#endif /* FEAT_CMDL_COMPL */
4003
4004/*
4005 * types for expressions.
4006 */
4007typedef enum
4008{
4009 TYPE_UNKNOWN = 0
4010 , TYPE_EQUAL /* == */
4011 , TYPE_NEQUAL /* != */
4012 , TYPE_GREATER /* > */
4013 , TYPE_GEQUAL /* >= */
4014 , TYPE_SMALLER /* < */
4015 , TYPE_SEQUAL /* <= */
4016 , TYPE_MATCH /* =~ */
4017 , TYPE_NOMATCH /* !~ */
4018} exptype_T;
4019
4020/*
4021 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004022 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4024 */
4025
4026/*
4027 * Handle zero level expression.
4028 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004029 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004030 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 * Return OK or FAIL.
4032 */
4033 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004034eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 char_u **nextcmd;
4038 int evaluate;
4039{
4040 int ret;
4041 char_u *p;
4042
4043 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004044 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 if (ret == FAIL || !ends_excmd(*p))
4046 {
4047 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004048 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 /*
4050 * Report the invalid expression unless the expression evaluation has
4051 * been cancelled due to an aborting error, an interrupt, or an
4052 * exception.
4053 */
4054 if (!aborting())
4055 EMSG2(_(e_invexpr2), arg);
4056 ret = FAIL;
4057 }
4058 if (nextcmd != NULL)
4059 *nextcmd = check_nextcmd(p);
4060
4061 return ret;
4062}
4063
4064/*
4065 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004066 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 *
4068 * "arg" must point to the first non-white of the expression.
4069 * "arg" is advanced to the next non-white after the recognized expression.
4070 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004071 * Note: "rettv.v_lock" is not set.
4072 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 * Return OK or FAIL.
4074 */
4075 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004076eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 int evaluate;
4080{
4081 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004082 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
4084 /*
4085 * Get the first variable.
4086 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004087 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 return FAIL;
4089
4090 if ((*arg)[0] == '?')
4091 {
4092 result = FALSE;
4093 if (evaluate)
4094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004095 int error = FALSE;
4096
4097 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004099 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004100 if (error)
4101 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 }
4103
4104 /*
4105 * Get the second variable.
4106 */
4107 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004108 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 return FAIL;
4110
4111 /*
4112 * Check for the ":".
4113 */
4114 if ((*arg)[0] != ':')
4115 {
4116 EMSG(_("E109: Missing ':' after '?'"));
4117 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004118 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 return FAIL;
4120 }
4121
4122 /*
4123 * Get the third variable.
4124 */
4125 *arg = skipwhite(*arg + 1);
4126 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4127 {
4128 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 return FAIL;
4131 }
4132 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 }
4135
4136 return OK;
4137}
4138
4139/*
4140 * Handle first level expression:
4141 * expr2 || expr2 || expr2 logical OR
4142 *
4143 * "arg" must point to the first non-white of the expression.
4144 * "arg" is advanced to the next non-white after the recognized expression.
4145 *
4146 * Return OK or FAIL.
4147 */
4148 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004149eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 int evaluate;
4153{
Bram Moolenaar33570922005-01-25 22:26:29 +00004154 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 long result;
4156 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004157 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158
4159 /*
4160 * Get the first variable.
4161 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004162 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 return FAIL;
4164
4165 /*
4166 * Repeat until there is no following "||".
4167 */
4168 first = TRUE;
4169 result = FALSE;
4170 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4171 {
4172 if (evaluate && first)
4173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004174 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004177 if (error)
4178 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 first = FALSE;
4180 }
4181
4182 /*
4183 * Get the second variable.
4184 */
4185 *arg = skipwhite(*arg + 2);
4186 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4187 return FAIL;
4188
4189 /*
4190 * Compute the result.
4191 */
4192 if (evaluate && !result)
4193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004194 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004196 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004197 if (error)
4198 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 }
4200 if (evaluate)
4201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004202 rettv->v_type = VAR_NUMBER;
4203 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 }
4205 }
4206
4207 return OK;
4208}
4209
4210/*
4211 * Handle second level expression:
4212 * expr3 && expr3 && expr3 logical AND
4213 *
4214 * "arg" must point to the first non-white of the expression.
4215 * "arg" is advanced to the next non-white after the recognized expression.
4216 *
4217 * Return OK or FAIL.
4218 */
4219 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004220eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 int evaluate;
4224{
Bram Moolenaar33570922005-01-25 22:26:29 +00004225 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 long result;
4227 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004228 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229
4230 /*
4231 * Get the first variable.
4232 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004233 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 return FAIL;
4235
4236 /*
4237 * Repeat until there is no following "&&".
4238 */
4239 first = TRUE;
4240 result = TRUE;
4241 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4242 {
4243 if (evaluate && first)
4244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004245 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004248 if (error)
4249 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 first = FALSE;
4251 }
4252
4253 /*
4254 * Get the second variable.
4255 */
4256 *arg = skipwhite(*arg + 2);
4257 if (eval4(arg, &var2, evaluate && result) == FAIL)
4258 return FAIL;
4259
4260 /*
4261 * Compute the result.
4262 */
4263 if (evaluate && result)
4264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004265 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004267 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004268 if (error)
4269 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
4271 if (evaluate)
4272 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 rettv->v_type = VAR_NUMBER;
4274 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276 }
4277
4278 return OK;
4279}
4280
4281/*
4282 * Handle third level expression:
4283 * var1 == var2
4284 * var1 =~ var2
4285 * var1 != var2
4286 * var1 !~ var2
4287 * var1 > var2
4288 * var1 >= var2
4289 * var1 < var2
4290 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004291 * var1 is var2
4292 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 *
4294 * "arg" must point to the first non-white of the expression.
4295 * "arg" is advanced to the next non-white after the recognized expression.
4296 *
4297 * Return OK or FAIL.
4298 */
4299 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004300eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 int evaluate;
4304{
Bram Moolenaar33570922005-01-25 22:26:29 +00004305 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 char_u *p;
4307 int i;
4308 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004309 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 int len = 2;
4311 long n1, n2;
4312 char_u *s1, *s2;
4313 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4314 regmatch_T regmatch;
4315 int ic;
4316 char_u *save_cpo;
4317
4318 /*
4319 * Get the first variable.
4320 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004321 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 return FAIL;
4323
4324 p = *arg;
4325 switch (p[0])
4326 {
4327 case '=': if (p[1] == '=')
4328 type = TYPE_EQUAL;
4329 else if (p[1] == '~')
4330 type = TYPE_MATCH;
4331 break;
4332 case '!': if (p[1] == '=')
4333 type = TYPE_NEQUAL;
4334 else if (p[1] == '~')
4335 type = TYPE_NOMATCH;
4336 break;
4337 case '>': if (p[1] != '=')
4338 {
4339 type = TYPE_GREATER;
4340 len = 1;
4341 }
4342 else
4343 type = TYPE_GEQUAL;
4344 break;
4345 case '<': if (p[1] != '=')
4346 {
4347 type = TYPE_SMALLER;
4348 len = 1;
4349 }
4350 else
4351 type = TYPE_SEQUAL;
4352 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004353 case 'i': if (p[1] == 's')
4354 {
4355 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4356 len = 5;
4357 if (!vim_isIDc(p[len]))
4358 {
4359 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4360 type_is = TRUE;
4361 }
4362 }
4363 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 }
4365
4366 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004367 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 */
4369 if (type != TYPE_UNKNOWN)
4370 {
4371 /* extra question mark appended: ignore case */
4372 if (p[len] == '?')
4373 {
4374 ic = TRUE;
4375 ++len;
4376 }
4377 /* extra '#' appended: match case */
4378 else if (p[len] == '#')
4379 {
4380 ic = FALSE;
4381 ++len;
4382 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004383 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 else
4385 ic = p_ic;
4386
4387 /*
4388 * Get the second variable.
4389 */
4390 *arg = skipwhite(p + len);
4391 if (eval5(arg, &var2, evaluate) == FAIL)
4392 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004393 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 return FAIL;
4395 }
4396
4397 if (evaluate)
4398 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004399 if (type_is && rettv->v_type != var2.v_type)
4400 {
4401 /* For "is" a different type always means FALSE, for "notis"
4402 * it means TRUE. */
4403 n1 = (type == TYPE_NEQUAL);
4404 }
4405 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4406 {
4407 if (type_is)
4408 {
4409 n1 = (rettv->v_type == var2.v_type
4410 && rettv->vval.v_list == var2.vval.v_list);
4411 if (type == TYPE_NEQUAL)
4412 n1 = !n1;
4413 }
4414 else if (rettv->v_type != var2.v_type
4415 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4416 {
4417 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004418 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004419 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004420 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004421 clear_tv(rettv);
4422 clear_tv(&var2);
4423 return FAIL;
4424 }
4425 else
4426 {
4427 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004428 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4429 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004430 if (type == TYPE_NEQUAL)
4431 n1 = !n1;
4432 }
4433 }
4434
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004435 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4436 {
4437 if (type_is)
4438 {
4439 n1 = (rettv->v_type == var2.v_type
4440 && rettv->vval.v_dict == var2.vval.v_dict);
4441 if (type == TYPE_NEQUAL)
4442 n1 = !n1;
4443 }
4444 else if (rettv->v_type != var2.v_type
4445 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4446 {
4447 if (rettv->v_type != var2.v_type)
4448 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4449 else
4450 EMSG(_("E736: Invalid operation for Dictionary"));
4451 clear_tv(rettv);
4452 clear_tv(&var2);
4453 return FAIL;
4454 }
4455 else
4456 {
4457 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004458 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4459 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004460 if (type == TYPE_NEQUAL)
4461 n1 = !n1;
4462 }
4463 }
4464
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004465 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4466 {
4467 if (rettv->v_type != var2.v_type
4468 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4469 {
4470 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004471 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004472 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004473 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004474 clear_tv(rettv);
4475 clear_tv(&var2);
4476 return FAIL;
4477 }
4478 else
4479 {
4480 /* Compare two Funcrefs for being equal or unequal. */
4481 if (rettv->vval.v_string == NULL
4482 || var2.vval.v_string == NULL)
4483 n1 = FALSE;
4484 else
4485 n1 = STRCMP(rettv->vval.v_string,
4486 var2.vval.v_string) == 0;
4487 if (type == TYPE_NEQUAL)
4488 n1 = !n1;
4489 }
4490 }
4491
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004492#ifdef FEAT_FLOAT
4493 /*
4494 * If one of the two variables is a float, compare as a float.
4495 * When using "=~" or "!~", always compare as string.
4496 */
4497 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4498 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4499 {
4500 float_T f1, f2;
4501
4502 if (rettv->v_type == VAR_FLOAT)
4503 f1 = rettv->vval.v_float;
4504 else
4505 f1 = get_tv_number(rettv);
4506 if (var2.v_type == VAR_FLOAT)
4507 f2 = var2.vval.v_float;
4508 else
4509 f2 = get_tv_number(&var2);
4510 n1 = FALSE;
4511 switch (type)
4512 {
4513 case TYPE_EQUAL: n1 = (f1 == f2); break;
4514 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4515 case TYPE_GREATER: n1 = (f1 > f2); break;
4516 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4517 case TYPE_SMALLER: n1 = (f1 < f2); break;
4518 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4519 case TYPE_UNKNOWN:
4520 case TYPE_MATCH:
4521 case TYPE_NOMATCH: break; /* avoid gcc warning */
4522 }
4523 }
4524#endif
4525
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 /*
4527 * If one of the two variables is a number, compare as a number.
4528 * When using "=~" or "!~", always compare as string.
4529 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004530 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4532 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004533 n1 = get_tv_number(rettv);
4534 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 switch (type)
4536 {
4537 case TYPE_EQUAL: n1 = (n1 == n2); break;
4538 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4539 case TYPE_GREATER: n1 = (n1 > n2); break;
4540 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4541 case TYPE_SMALLER: n1 = (n1 < n2); break;
4542 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4543 case TYPE_UNKNOWN:
4544 case TYPE_MATCH:
4545 case TYPE_NOMATCH: break; /* avoid gcc warning */
4546 }
4547 }
4548 else
4549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004550 s1 = get_tv_string_buf(rettv, buf1);
4551 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4553 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4554 else
4555 i = 0;
4556 n1 = FALSE;
4557 switch (type)
4558 {
4559 case TYPE_EQUAL: n1 = (i == 0); break;
4560 case TYPE_NEQUAL: n1 = (i != 0); break;
4561 case TYPE_GREATER: n1 = (i > 0); break;
4562 case TYPE_GEQUAL: n1 = (i >= 0); break;
4563 case TYPE_SMALLER: n1 = (i < 0); break;
4564 case TYPE_SEQUAL: n1 = (i <= 0); break;
4565
4566 case TYPE_MATCH:
4567 case TYPE_NOMATCH:
4568 /* avoid 'l' flag in 'cpoptions' */
4569 save_cpo = p_cpo;
4570 p_cpo = (char_u *)"";
4571 regmatch.regprog = vim_regcomp(s2,
4572 RE_MAGIC + RE_STRING);
4573 regmatch.rm_ic = ic;
4574 if (regmatch.regprog != NULL)
4575 {
4576 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4577 vim_free(regmatch.regprog);
4578 if (type == TYPE_NOMATCH)
4579 n1 = !n1;
4580 }
4581 p_cpo = save_cpo;
4582 break;
4583
4584 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4585 }
4586 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004587 clear_tv(rettv);
4588 clear_tv(&var2);
4589 rettv->v_type = VAR_NUMBER;
4590 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 }
4592 }
4593
4594 return OK;
4595}
4596
4597/*
4598 * Handle fourth level expression:
4599 * + number addition
4600 * - number subtraction
4601 * . string concatenation
4602 *
4603 * "arg" must point to the first non-white of the expression.
4604 * "arg" is advanced to the next non-white after the recognized expression.
4605 *
4606 * Return OK or FAIL.
4607 */
4608 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004609eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 int evaluate;
4613{
Bram Moolenaar33570922005-01-25 22:26:29 +00004614 typval_T var2;
4615 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 int op;
4617 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004618#ifdef FEAT_FLOAT
4619 float_T f1 = 0, f2 = 0;
4620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 char_u *s1, *s2;
4622 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4623 char_u *p;
4624
4625 /*
4626 * Get the first variable.
4627 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004628 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 return FAIL;
4630
4631 /*
4632 * Repeat computing, until no '+', '-' or '.' is following.
4633 */
4634 for (;;)
4635 {
4636 op = **arg;
4637 if (op != '+' && op != '-' && op != '.')
4638 break;
4639
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004640 if ((op != '+' || rettv->v_type != VAR_LIST)
4641#ifdef FEAT_FLOAT
4642 && (op == '.' || rettv->v_type != VAR_FLOAT)
4643#endif
4644 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004645 {
4646 /* For "list + ...", an illegal use of the first operand as
4647 * a number cannot be determined before evaluating the 2nd
4648 * operand: if this is also a list, all is ok.
4649 * For "something . ...", "something - ..." or "non-list + ...",
4650 * we know that the first operand needs to be a string or number
4651 * without evaluating the 2nd operand. So check before to avoid
4652 * side effects after an error. */
4653 if (evaluate && get_tv_string_chk(rettv) == NULL)
4654 {
4655 clear_tv(rettv);
4656 return FAIL;
4657 }
4658 }
4659
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 /*
4661 * Get the second variable.
4662 */
4663 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004664 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004666 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 return FAIL;
4668 }
4669
4670 if (evaluate)
4671 {
4672 /*
4673 * Compute the result.
4674 */
4675 if (op == '.')
4676 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004677 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4678 s2 = get_tv_string_buf_chk(&var2, buf2);
4679 if (s2 == NULL) /* type error ? */
4680 {
4681 clear_tv(rettv);
4682 clear_tv(&var2);
4683 return FAIL;
4684 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004685 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004686 clear_tv(rettv);
4687 rettv->v_type = VAR_STRING;
4688 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004690 else if (op == '+' && rettv->v_type == VAR_LIST
4691 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004692 {
4693 /* concatenate Lists */
4694 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4695 &var3) == FAIL)
4696 {
4697 clear_tv(rettv);
4698 clear_tv(&var2);
4699 return FAIL;
4700 }
4701 clear_tv(rettv);
4702 *rettv = var3;
4703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 else
4705 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004706 int error = FALSE;
4707
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004708#ifdef FEAT_FLOAT
4709 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004710 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004711 f1 = rettv->vval.v_float;
4712 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004713 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004714 else
4715#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004716 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004717 n1 = get_tv_number_chk(rettv, &error);
4718 if (error)
4719 {
4720 /* This can only happen for "list + non-list". For
4721 * "non-list + ..." or "something - ...", we returned
4722 * before evaluating the 2nd operand. */
4723 clear_tv(rettv);
4724 return FAIL;
4725 }
4726#ifdef FEAT_FLOAT
4727 if (var2.v_type == VAR_FLOAT)
4728 f1 = n1;
4729#endif
4730 }
4731#ifdef FEAT_FLOAT
4732 if (var2.v_type == VAR_FLOAT)
4733 {
4734 f2 = var2.vval.v_float;
4735 n2 = 0;
4736 }
4737 else
4738#endif
4739 {
4740 n2 = get_tv_number_chk(&var2, &error);
4741 if (error)
4742 {
4743 clear_tv(rettv);
4744 clear_tv(&var2);
4745 return FAIL;
4746 }
4747#ifdef FEAT_FLOAT
4748 if (rettv->v_type == VAR_FLOAT)
4749 f2 = n2;
4750#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004751 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004752 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004753
4754#ifdef FEAT_FLOAT
4755 /* If there is a float on either side the result is a float. */
4756 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4757 {
4758 if (op == '+')
4759 f1 = f1 + f2;
4760 else
4761 f1 = f1 - f2;
4762 rettv->v_type = VAR_FLOAT;
4763 rettv->vval.v_float = f1;
4764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004766#endif
4767 {
4768 if (op == '+')
4769 n1 = n1 + n2;
4770 else
4771 n1 = n1 - n2;
4772 rettv->v_type = VAR_NUMBER;
4773 rettv->vval.v_number = n1;
4774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004776 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 }
4778 }
4779 return OK;
4780}
4781
4782/*
4783 * Handle fifth level expression:
4784 * * number multiplication
4785 * / number division
4786 * % number modulo
4787 *
4788 * "arg" must point to the first non-white of the expression.
4789 * "arg" is advanced to the next non-white after the recognized expression.
4790 *
4791 * Return OK or FAIL.
4792 */
4793 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004794eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004798 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799{
Bram Moolenaar33570922005-01-25 22:26:29 +00004800 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 int op;
4802 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004803#ifdef FEAT_FLOAT
4804 int use_float = FALSE;
4805 float_T f1 = 0, f2;
4806#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004807 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808
4809 /*
4810 * Get the first variable.
4811 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004812 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 return FAIL;
4814
4815 /*
4816 * Repeat computing, until no '*', '/' or '%' is following.
4817 */
4818 for (;;)
4819 {
4820 op = **arg;
4821 if (op != '*' && op != '/' && op != '%')
4822 break;
4823
4824 if (evaluate)
4825 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004826#ifdef FEAT_FLOAT
4827 if (rettv->v_type == VAR_FLOAT)
4828 {
4829 f1 = rettv->vval.v_float;
4830 use_float = TRUE;
4831 n1 = 0;
4832 }
4833 else
4834#endif
4835 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004836 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004837 if (error)
4838 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 }
4840 else
4841 n1 = 0;
4842
4843 /*
4844 * Get the second variable.
4845 */
4846 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004847 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 return FAIL;
4849
4850 if (evaluate)
4851 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004852#ifdef FEAT_FLOAT
4853 if (var2.v_type == VAR_FLOAT)
4854 {
4855 if (!use_float)
4856 {
4857 f1 = n1;
4858 use_float = TRUE;
4859 }
4860 f2 = var2.vval.v_float;
4861 n2 = 0;
4862 }
4863 else
4864#endif
4865 {
4866 n2 = get_tv_number_chk(&var2, &error);
4867 clear_tv(&var2);
4868 if (error)
4869 return FAIL;
4870#ifdef FEAT_FLOAT
4871 if (use_float)
4872 f2 = n2;
4873#endif
4874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875
4876 /*
4877 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004878 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004880#ifdef FEAT_FLOAT
4881 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004883 if (op == '*')
4884 f1 = f1 * f2;
4885 else if (op == '/')
4886 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004887# ifdef VMS
4888 /* VMS crashes on divide by zero, work around it */
4889 if (f2 == 0.0)
4890 {
4891 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004892 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004893 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004894 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004895 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004896 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004897 }
4898 else
4899 f1 = f1 / f2;
4900# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901 /* We rely on the floating point library to handle divide
4902 * by zero to result in "inf" and not a crash. */
4903 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004904# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004907 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004908 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004909 return FAIL;
4910 }
4911 rettv->v_type = VAR_FLOAT;
4912 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 }
4914 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004917 if (op == '*')
4918 n1 = n1 * n2;
4919 else if (op == '/')
4920 {
4921 if (n2 == 0) /* give an error message? */
4922 {
4923 if (n1 == 0)
4924 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4925 else if (n1 < 0)
4926 n1 = -0x7fffffffL;
4927 else
4928 n1 = 0x7fffffffL;
4929 }
4930 else
4931 n1 = n1 / n2;
4932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004934 {
4935 if (n2 == 0) /* give an error message? */
4936 n1 = 0;
4937 else
4938 n1 = n1 % n2;
4939 }
4940 rettv->v_type = VAR_NUMBER;
4941 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 }
4944 }
4945
4946 return OK;
4947}
4948
4949/*
4950 * Handle sixth level expression:
4951 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004952 * "string" string constant
4953 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 * &option-name option value
4955 * @r register contents
4956 * identifier variable value
4957 * function() function call
4958 * $VAR environment variable
4959 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004960 * [expr, expr] List
4961 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 *
4963 * Also handle:
4964 * ! in front logical NOT
4965 * - in front unary minus
4966 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004967 * trailing [] subscript in String or List
4968 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 *
4970 * "arg" must point to the first non-white of the expression.
4971 * "arg" is advanced to the next non-white after the recognized expression.
4972 *
4973 * Return OK or FAIL.
4974 */
4975 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004976eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004980 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 long n;
4983 int len;
4984 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 char_u *start_leader, *end_leader;
4986 int ret = OK;
4987 char_u *alias;
4988
4989 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004990 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004991 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004993 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994
4995 /*
4996 * Skip '!' and '-' characters. They are handled later.
4997 */
4998 start_leader = *arg;
4999 while (**arg == '!' || **arg == '-' || **arg == '+')
5000 *arg = skipwhite(*arg + 1);
5001 end_leader = *arg;
5002
5003 switch (**arg)
5004 {
5005 /*
5006 * Number constant.
5007 */
5008 case '0':
5009 case '1':
5010 case '2':
5011 case '3':
5012 case '4':
5013 case '5':
5014 case '6':
5015 case '7':
5016 case '8':
5017 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005018 {
5019#ifdef FEAT_FLOAT
5020 char_u *p = skipdigits(*arg + 1);
5021 int get_float = FALSE;
5022
5023 /* We accept a float when the format matches
5024 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005025 * strict to avoid backwards compatibility problems.
5026 * Don't look for a float after the "." operator, so that
5027 * ":let vers = 1.2.3" doesn't fail. */
5028 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005030 get_float = TRUE;
5031 p = skipdigits(p + 2);
5032 if (*p == 'e' || *p == 'E')
5033 {
5034 ++p;
5035 if (*p == '-' || *p == '+')
5036 ++p;
5037 if (!vim_isdigit(*p))
5038 get_float = FALSE;
5039 else
5040 p = skipdigits(p + 1);
5041 }
5042 if (ASCII_ISALPHA(*p) || *p == '.')
5043 get_float = FALSE;
5044 }
5045 if (get_float)
5046 {
5047 float_T f;
5048
5049 *arg += string2float(*arg, &f);
5050 if (evaluate)
5051 {
5052 rettv->v_type = VAR_FLOAT;
5053 rettv->vval.v_float = f;
5054 }
5055 }
5056 else
5057#endif
5058 {
5059 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5060 *arg += len;
5061 if (evaluate)
5062 {
5063 rettv->v_type = VAR_NUMBER;
5064 rettv->vval.v_number = n;
5065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 }
5067 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069
5070 /*
5071 * String constant: "string".
5072 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005073 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 break;
5075
5076 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005079 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005080 break;
5081
5082 /*
5083 * List: [expr, expr]
5084 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005085 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 break;
5087
5088 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005089 * Dictionary: {key: val, key: val}
5090 */
5091 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5092 break;
5093
5094 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005095 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005097 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 break;
5099
5100 /*
5101 * Environment variable: $VAR.
5102 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005103 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 break;
5105
5106 /*
5107 * Register contents: @r.
5108 */
5109 case '@': ++*arg;
5110 if (evaluate)
5111 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005112 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005113 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
5115 if (**arg != NUL)
5116 ++*arg;
5117 break;
5118
5119 /*
5120 * nested expression: (expression).
5121 */
5122 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005123 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 if (**arg == ')')
5125 ++*arg;
5126 else if (ret == OK)
5127 {
5128 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005129 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 ret = FAIL;
5131 }
5132 break;
5133
Bram Moolenaar8c711452005-01-14 21:53:12 +00005134 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 break;
5136 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005137
5138 if (ret == NOTDONE)
5139 {
5140 /*
5141 * Must be a variable or function name.
5142 * Can also be a curly-braces kind of name: {expr}.
5143 */
5144 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005145 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 if (alias != NULL)
5147 s = alias;
5148
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005149 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 ret = FAIL;
5151 else
5152 {
5153 if (**arg == '(') /* recursive! */
5154 {
5155 /* If "s" is the name of a variable of type VAR_FUNC
5156 * use its contents. */
5157 s = deref_func_name(s, &len);
5158
5159 /* Invoke the function. */
5160 ret = get_func_tv(s, len, rettv, arg,
5161 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005162 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163 /* Stop the expression evaluation when immediately
5164 * aborting on error, or when an interrupt occurred or
5165 * an exception was thrown but not caught. */
5166 if (aborting())
5167 {
5168 if (ret == OK)
5169 clear_tv(rettv);
5170 ret = FAIL;
5171 }
5172 }
5173 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005174 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005175 else
5176 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005178 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005179 }
5180
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 *arg = skipwhite(*arg);
5182
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005183 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5184 * expr(expr). */
5185 if (ret == OK)
5186 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187
5188 /*
5189 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5190 */
5191 if (ret == OK && evaluate && end_leader > start_leader)
5192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005193 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005194 int val = 0;
5195#ifdef FEAT_FLOAT
5196 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005197
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005198 if (rettv->v_type == VAR_FLOAT)
5199 f = rettv->vval.v_float;
5200 else
5201#endif
5202 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005203 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005205 clear_tv(rettv);
5206 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208 else
5209 {
5210 while (end_leader > start_leader)
5211 {
5212 --end_leader;
5213 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005214 {
5215#ifdef FEAT_FLOAT
5216 if (rettv->v_type == VAR_FLOAT)
5217 f = !f;
5218 else
5219#endif
5220 val = !val;
5221 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005222 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005223 {
5224#ifdef FEAT_FLOAT
5225 if (rettv->v_type == VAR_FLOAT)
5226 f = -f;
5227 else
5228#endif
5229 val = -val;
5230 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005231 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005232#ifdef FEAT_FLOAT
5233 if (rettv->v_type == VAR_FLOAT)
5234 {
5235 clear_tv(rettv);
5236 rettv->vval.v_float = f;
5237 }
5238 else
5239#endif
5240 {
5241 clear_tv(rettv);
5242 rettv->v_type = VAR_NUMBER;
5243 rettv->vval.v_number = val;
5244 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 }
5247
5248 return ret;
5249}
5250
5251/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005252 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5253 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5255 */
5256 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005257eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005259 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005261 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262{
5263 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005264 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005265 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 long len = -1;
5267 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005271 if (rettv->v_type == VAR_FUNC
5272#ifdef FEAT_FLOAT
5273 || rettv->v_type == VAR_FLOAT
5274#endif
5275 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005277 if (verbose)
5278 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 return FAIL;
5280 }
5281
Bram Moolenaar8c711452005-01-14 21:53:12 +00005282 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 /*
5285 * dict.name
5286 */
5287 key = *arg + 1;
5288 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5289 ;
5290 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 }
5294 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005296 /*
5297 * something[idx]
5298 *
5299 * Get the (first) variable from inside the [].
5300 */
5301 *arg = skipwhite(*arg + 1);
5302 if (**arg == ':')
5303 empty1 = TRUE;
5304 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5305 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005306 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5307 {
5308 /* not a number or string */
5309 clear_tv(&var1);
5310 return FAIL;
5311 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005312
5313 /*
5314 * Get the second variable from inside the [:].
5315 */
5316 if (**arg == ':')
5317 {
5318 range = TRUE;
5319 *arg = skipwhite(*arg + 1);
5320 if (**arg == ']')
5321 empty2 = TRUE;
5322 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5323 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005324 if (!empty1)
5325 clear_tv(&var1);
5326 return FAIL;
5327 }
5328 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5329 {
5330 /* not a number or string */
5331 if (!empty1)
5332 clear_tv(&var1);
5333 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005334 return FAIL;
5335 }
5336 }
5337
5338 /* Check for the ']'. */
5339 if (**arg != ']')
5340 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005341 if (verbose)
5342 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005343 clear_tv(&var1);
5344 if (range)
5345 clear_tv(&var2);
5346 return FAIL;
5347 }
5348 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 }
5350
5351 if (evaluate)
5352 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005353 n1 = 0;
5354 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005356 n1 = get_tv_number(&var1);
5357 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 }
5359 if (range)
5360 {
5361 if (empty2)
5362 n2 = -1;
5363 else
5364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005365 n2 = get_tv_number(&var2);
5366 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367 }
5368 }
5369
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005370 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371 {
5372 case VAR_NUMBER:
5373 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005374 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 len = (long)STRLEN(s);
5376 if (range)
5377 {
5378 /* The resulting variable is a substring. If the indexes
5379 * are out of range the result is empty. */
5380 if (n1 < 0)
5381 {
5382 n1 = len + n1;
5383 if (n1 < 0)
5384 n1 = 0;
5385 }
5386 if (n2 < 0)
5387 n2 = len + n2;
5388 else if (n2 >= len)
5389 n2 = len;
5390 if (n1 >= len || n2 < 0 || n1 > n2)
5391 s = NULL;
5392 else
5393 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5394 }
5395 else
5396 {
5397 /* The resulting variable is a string of a single
5398 * character. If the index is too big or negative the
5399 * result is empty. */
5400 if (n1 >= len || n1 < 0)
5401 s = NULL;
5402 else
5403 s = vim_strnsave(s + n1, 1);
5404 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005405 clear_tv(rettv);
5406 rettv->v_type = VAR_STRING;
5407 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 break;
5409
5410 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005411 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412 if (n1 < 0)
5413 n1 = len + n1;
5414 if (!empty1 && (n1 < 0 || n1 >= len))
5415 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005416 /* For a range we allow invalid values and return an empty
5417 * list. A list index out of range is an error. */
5418 if (!range)
5419 {
5420 if (verbose)
5421 EMSGN(_(e_listidx), n1);
5422 return FAIL;
5423 }
5424 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005425 }
5426 if (range)
5427 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005428 list_T *l;
5429 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430
5431 if (n2 < 0)
5432 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005433 else if (n2 >= len)
5434 n2 = len - 1;
5435 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005436 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 l = list_alloc();
5438 if (l == NULL)
5439 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005440 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 n1 <= n2; ++n1)
5442 {
5443 if (list_append_tv(l, &item->li_tv) == FAIL)
5444 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005445 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 return FAIL;
5447 }
5448 item = item->li_next;
5449 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005450 clear_tv(rettv);
5451 rettv->v_type = VAR_LIST;
5452 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005453 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 }
5455 else
5456 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005457 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005458 clear_tv(rettv);
5459 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 }
5461 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005462
5463 case VAR_DICT:
5464 if (range)
5465 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005466 if (verbose)
5467 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005468 if (len == -1)
5469 clear_tv(&var1);
5470 return FAIL;
5471 }
5472 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005473 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005474
5475 if (len == -1)
5476 {
5477 key = get_tv_string(&var1);
5478 if (*key == NUL)
5479 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005480 if (verbose)
5481 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005482 clear_tv(&var1);
5483 return FAIL;
5484 }
5485 }
5486
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005487 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005488
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005489 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005490 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005491 if (len == -1)
5492 clear_tv(&var1);
5493 if (item == NULL)
5494 return FAIL;
5495
5496 copy_tv(&item->di_tv, &var1);
5497 clear_tv(rettv);
5498 *rettv = var1;
5499 }
5500 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 }
5502 }
5503
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 return OK;
5505}
5506
5507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 * Get an option value.
5509 * "arg" points to the '&' or '+' before the option name.
5510 * "arg" is advanced to character after the option name.
5511 * Return OK or FAIL.
5512 */
5513 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005514get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005516 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 int evaluate;
5518{
5519 char_u *option_end;
5520 long numval;
5521 char_u *stringval;
5522 int opt_type;
5523 int c;
5524 int working = (**arg == '+'); /* has("+option") */
5525 int ret = OK;
5526 int opt_flags;
5527
5528 /*
5529 * Isolate the option name and find its value.
5530 */
5531 option_end = find_option_end(arg, &opt_flags);
5532 if (option_end == NULL)
5533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005534 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 EMSG2(_("E112: Option name missing: %s"), *arg);
5536 return FAIL;
5537 }
5538
5539 if (!evaluate)
5540 {
5541 *arg = option_end;
5542 return OK;
5543 }
5544
5545 c = *option_end;
5546 *option_end = NUL;
5547 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005548 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549
5550 if (opt_type == -3) /* invalid name */
5551 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 EMSG2(_("E113: Unknown option: %s"), *arg);
5554 ret = FAIL;
5555 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005556 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 {
5558 if (opt_type == -2) /* hidden string option */
5559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560 rettv->v_type = VAR_STRING;
5561 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 }
5563 else if (opt_type == -1) /* hidden number option */
5564 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005565 rettv->v_type = VAR_NUMBER;
5566 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 }
5568 else if (opt_type == 1) /* number option */
5569 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005570 rettv->v_type = VAR_NUMBER;
5571 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 }
5573 else /* string option */
5574 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 rettv->v_type = VAR_STRING;
5576 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 }
5578 }
5579 else if (working && (opt_type == -2 || opt_type == -1))
5580 ret = FAIL;
5581
5582 *option_end = c; /* put back for error messages */
5583 *arg = option_end;
5584
5585 return ret;
5586}
5587
5588/*
5589 * Allocate a variable for a string constant.
5590 * Return OK or FAIL.
5591 */
5592 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005593get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 int evaluate;
5597{
5598 char_u *p;
5599 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 int extra = 0;
5601
5602 /*
5603 * Find the end of the string, skipping backslashed characters.
5604 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005605 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 {
5607 if (*p == '\\' && p[1] != NUL)
5608 {
5609 ++p;
5610 /* A "\<x>" form occupies at least 4 characters, and produces up
5611 * to 6 characters: reserve space for 2 extra */
5612 if (*p == '<')
5613 extra += 2;
5614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 }
5616
5617 if (*p != '"')
5618 {
5619 EMSG2(_("E114: Missing quote: %s"), *arg);
5620 return FAIL;
5621 }
5622
5623 /* If only parsing, set *arg and return here */
5624 if (!evaluate)
5625 {
5626 *arg = p + 1;
5627 return OK;
5628 }
5629
5630 /*
5631 * Copy the string into allocated memory, handling backslashed
5632 * characters.
5633 */
5634 name = alloc((unsigned)(p - *arg + extra));
5635 if (name == NULL)
5636 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005637 rettv->v_type = VAR_STRING;
5638 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 {
5642 if (*p == '\\')
5643 {
5644 switch (*++p)
5645 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005646 case 'b': *name++ = BS; ++p; break;
5647 case 'e': *name++ = ESC; ++p; break;
5648 case 'f': *name++ = FF; ++p; break;
5649 case 'n': *name++ = NL; ++p; break;
5650 case 'r': *name++ = CAR; ++p; break;
5651 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652
5653 case 'X': /* hex: "\x1", "\x12" */
5654 case 'x':
5655 case 'u': /* Unicode: "\u0023" */
5656 case 'U':
5657 if (vim_isxdigit(p[1]))
5658 {
5659 int n, nr;
5660 int c = toupper(*p);
5661
5662 if (c == 'X')
5663 n = 2;
5664 else
5665 n = 4;
5666 nr = 0;
5667 while (--n >= 0 && vim_isxdigit(p[1]))
5668 {
5669 ++p;
5670 nr = (nr << 4) + hex2nr(*p);
5671 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005672 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673#ifdef FEAT_MBYTE
5674 /* For "\u" store the number according to
5675 * 'encoding'. */
5676 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 else
5679#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005680 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 break;
5683
5684 /* octal: "\1", "\12", "\123" */
5685 case '0':
5686 case '1':
5687 case '2':
5688 case '3':
5689 case '4':
5690 case '5':
5691 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005692 case '7': *name = *p++ - '0';
5693 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005695 *name = (*name << 3) + *p++ - '0';
5696 if (*p >= '0' && *p <= '7')
5697 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 break;
5701
5702 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005703 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 if (extra != 0)
5705 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 break;
5708 }
5709 /* FALLTHROUGH */
5710
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 break;
5713 }
5714 }
5715 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005719 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 *arg = p + 1;
5721
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 return OK;
5723}
5724
5725/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005726 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 * Return OK or FAIL.
5728 */
5729 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005730get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 int evaluate;
5734{
5735 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005736 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005737 int reduce = 0;
5738
5739 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005741 */
5742 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5743 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005747 break;
5748 ++reduce;
5749 ++p;
5750 }
5751 }
5752
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005754 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005756 return FAIL;
5757 }
5758
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005760 if (!evaluate)
5761 {
5762 *arg = p + 1;
5763 return OK;
5764 }
5765
5766 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005768 */
5769 str = alloc((unsigned)((p - *arg) - reduce));
5770 if (str == NULL)
5771 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772 rettv->v_type = VAR_STRING;
5773 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005774
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005776 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005778 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005780 break;
5781 ++p;
5782 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005784 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005786 *arg = p + 1;
5787
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005788 return OK;
5789}
5790
5791/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005792 * Allocate a variable for a List and fill it from "*arg".
5793 * Return OK or FAIL.
5794 */
5795 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005796get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005797 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005798 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005799 int evaluate;
5800{
Bram Moolenaar33570922005-01-25 22:26:29 +00005801 list_T *l = NULL;
5802 typval_T tv;
5803 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804
5805 if (evaluate)
5806 {
5807 l = list_alloc();
5808 if (l == NULL)
5809 return FAIL;
5810 }
5811
5812 *arg = skipwhite(*arg + 1);
5813 while (**arg != ']' && **arg != NUL)
5814 {
5815 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5816 goto failret;
5817 if (evaluate)
5818 {
5819 item = listitem_alloc();
5820 if (item != NULL)
5821 {
5822 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005823 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005824 list_append(l, item);
5825 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005826 else
5827 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 }
5829
5830 if (**arg == ']')
5831 break;
5832 if (**arg != ',')
5833 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835 goto failret;
5836 }
5837 *arg = skipwhite(*arg + 1);
5838 }
5839
5840 if (**arg != ']')
5841 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005843failret:
5844 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005845 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005846 return FAIL;
5847 }
5848
5849 *arg = skipwhite(*arg + 1);
5850 if (evaluate)
5851 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005852 rettv->v_type = VAR_LIST;
5853 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854 ++l->lv_refcount;
5855 }
5856
5857 return OK;
5858}
5859
5860/*
5861 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005862 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005863 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005864 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865list_alloc()
5866{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005867 list_T *l;
5868
5869 l = (list_T *)alloc_clear(sizeof(list_T));
5870 if (l != NULL)
5871 {
5872 /* Prepend the list to the list of lists for garbage collection. */
5873 if (first_list != NULL)
5874 first_list->lv_used_prev = l;
5875 l->lv_used_prev = NULL;
5876 l->lv_used_next = first_list;
5877 first_list = l;
5878 }
5879 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880}
5881
5882/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005883 * Allocate an empty list for a return value.
5884 * Returns OK or FAIL.
5885 */
5886 static int
5887rettv_list_alloc(rettv)
5888 typval_T *rettv;
5889{
5890 list_T *l = list_alloc();
5891
5892 if (l == NULL)
5893 return FAIL;
5894
5895 rettv->vval.v_list = l;
5896 rettv->v_type = VAR_LIST;
5897 ++l->lv_refcount;
5898 return OK;
5899}
5900
5901/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005902 * Unreference a list: decrement the reference count and free it when it
5903 * becomes zero.
5904 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005905 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005907 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005909 if (l != NULL && --l->lv_refcount <= 0)
5910 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911}
5912
5913/*
5914 * Free a list, including all items it points to.
5915 * Ignores the reference count.
5916 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005917 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005918list_free(l, recurse)
5919 list_T *l;
5920 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921{
Bram Moolenaar33570922005-01-25 22:26:29 +00005922 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005923
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005924 /* Remove the list from the list of lists for garbage collection. */
5925 if (l->lv_used_prev == NULL)
5926 first_list = l->lv_used_next;
5927 else
5928 l->lv_used_prev->lv_used_next = l->lv_used_next;
5929 if (l->lv_used_next != NULL)
5930 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5931
Bram Moolenaard9fba312005-06-26 22:34:35 +00005932 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005934 /* Remove the item before deleting it. */
5935 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005936 if (recurse || (item->li_tv.v_type != VAR_LIST
5937 && item->li_tv.v_type != VAR_DICT))
5938 clear_tv(&item->li_tv);
5939 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 }
5941 vim_free(l);
5942}
5943
5944/*
5945 * Allocate a list item.
5946 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005947 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948listitem_alloc()
5949{
Bram Moolenaar33570922005-01-25 22:26:29 +00005950 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951}
5952
5953/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005954 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955 */
5956 static void
5957listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005958 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005960 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961 vim_free(item);
5962}
5963
5964/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005965 * Remove a list item from a List and free it. Also clears the value.
5966 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005967 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005968listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005969 list_T *l;
5970 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005971{
5972 list_remove(l, item, item);
5973 listitem_free(item);
5974}
5975
5976/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977 * Get the number of items in a list.
5978 */
5979 static long
5980list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005981 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 if (l == NULL)
5984 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005985 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986}
5987
5988/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005989 * Return TRUE when two lists have exactly the same values.
5990 */
5991 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005992list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005993 list_T *l1;
5994 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005995 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005996 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005997{
Bram Moolenaar33570922005-01-25 22:26:29 +00005998 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005999
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006000 if (l1 == NULL || l2 == NULL)
6001 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006002 if (l1 == l2)
6003 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006004 if (list_len(l1) != list_len(l2))
6005 return FALSE;
6006
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006007 for (item1 = l1->lv_first, item2 = l2->lv_first;
6008 item1 != NULL && item2 != NULL;
6009 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006010 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006011 return FALSE;
6012 return item1 == NULL && item2 == NULL;
6013}
6014
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006015#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6016 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006017/*
6018 * Return the dictitem that an entry in a hashtable points to.
6019 */
6020 dictitem_T *
6021dict_lookup(hi)
6022 hashitem_T *hi;
6023{
6024 return HI2DI(hi);
6025}
6026#endif
6027
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006028/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006029 * Return TRUE when two dictionaries have exactly the same key/values.
6030 */
6031 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006032dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 dict_T *d1;
6034 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006035 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006036 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006037{
Bram Moolenaar33570922005-01-25 22:26:29 +00006038 hashitem_T *hi;
6039 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006040 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006041
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006042 if (d1 == NULL || d2 == NULL)
6043 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006044 if (d1 == d2)
6045 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006046 if (dict_len(d1) != dict_len(d2))
6047 return FALSE;
6048
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006049 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006050 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006051 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006052 if (!HASHITEM_EMPTY(hi))
6053 {
6054 item2 = dict_find(d2, hi->hi_key, -1);
6055 if (item2 == NULL)
6056 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006057 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006058 return FALSE;
6059 --todo;
6060 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006061 }
6062 return TRUE;
6063}
6064
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006065static int tv_equal_recurse_limit;
6066
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006067/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006068 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006069 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006070 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006071 */
6072 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006073tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006074 typval_T *tv1;
6075 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006076 int ic; /* ignore case */
6077 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006078{
6079 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006080 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006081 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006082 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006083
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006084 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006085 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006086
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006087 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006088 * recursiveness to a limit. We guess they are equal then.
6089 * A fixed limit has the problem of still taking an awful long time.
6090 * Reduce the limit every time running into it. That should work fine for
6091 * deeply linked structures that are not recursively linked and catch
6092 * recursiveness quickly. */
6093 if (!recursive)
6094 tv_equal_recurse_limit = 1000;
6095 if (recursive_cnt >= tv_equal_recurse_limit)
6096 {
6097 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006098 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006099 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006100
6101 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006103 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006104 ++recursive_cnt;
6105 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6106 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006107 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006108
6109 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006110 ++recursive_cnt;
6111 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6112 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006113 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006114
6115 case VAR_FUNC:
6116 return (tv1->vval.v_string != NULL
6117 && tv2->vval.v_string != NULL
6118 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6119
6120 case VAR_NUMBER:
6121 return tv1->vval.v_number == tv2->vval.v_number;
6122
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006123#ifdef FEAT_FLOAT
6124 case VAR_FLOAT:
6125 return tv1->vval.v_float == tv2->vval.v_float;
6126#endif
6127
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006128 case VAR_STRING:
6129 s1 = get_tv_string_buf(tv1, buf1);
6130 s2 = get_tv_string_buf(tv2, buf2);
6131 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006132 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006133
6134 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006135 return TRUE;
6136}
6137
6138/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006139 * Locate item with index "n" in list "l" and return it.
6140 * A negative index is counted from the end; -1 is the last item.
6141 * Returns NULL when "n" is out of range.
6142 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006143 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006144list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006145 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006146 long n;
6147{
Bram Moolenaar33570922005-01-25 22:26:29 +00006148 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149 long idx;
6150
6151 if (l == NULL)
6152 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006153
6154 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006155 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006156 n = l->lv_len + n;
6157
6158 /* Check for index out of range. */
6159 if (n < 0 || n >= l->lv_len)
6160 return NULL;
6161
6162 /* When there is a cached index may start search from there. */
6163 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006164 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006165 if (n < l->lv_idx / 2)
6166 {
6167 /* closest to the start of the list */
6168 item = l->lv_first;
6169 idx = 0;
6170 }
6171 else if (n > (l->lv_idx + l->lv_len) / 2)
6172 {
6173 /* closest to the end of the list */
6174 item = l->lv_last;
6175 idx = l->lv_len - 1;
6176 }
6177 else
6178 {
6179 /* closest to the cached index */
6180 item = l->lv_idx_item;
6181 idx = l->lv_idx;
6182 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006183 }
6184 else
6185 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006186 if (n < l->lv_len / 2)
6187 {
6188 /* closest to the start of the list */
6189 item = l->lv_first;
6190 idx = 0;
6191 }
6192 else
6193 {
6194 /* closest to the end of the list */
6195 item = l->lv_last;
6196 idx = l->lv_len - 1;
6197 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006198 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006199
6200 while (n > idx)
6201 {
6202 /* search forward */
6203 item = item->li_next;
6204 ++idx;
6205 }
6206 while (n < idx)
6207 {
6208 /* search backward */
6209 item = item->li_prev;
6210 --idx;
6211 }
6212
6213 /* cache the used index */
6214 l->lv_idx = idx;
6215 l->lv_idx_item = item;
6216
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006217 return item;
6218}
6219
6220/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006221 * Get list item "l[idx]" as a number.
6222 */
6223 static long
6224list_find_nr(l, idx, errorp)
6225 list_T *l;
6226 long idx;
6227 int *errorp; /* set to TRUE when something wrong */
6228{
6229 listitem_T *li;
6230
6231 li = list_find(l, idx);
6232 if (li == NULL)
6233 {
6234 if (errorp != NULL)
6235 *errorp = TRUE;
6236 return -1L;
6237 }
6238 return get_tv_number_chk(&li->li_tv, errorp);
6239}
6240
6241/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006242 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6243 */
6244 char_u *
6245list_find_str(l, idx)
6246 list_T *l;
6247 long idx;
6248{
6249 listitem_T *li;
6250
6251 li = list_find(l, idx - 1);
6252 if (li == NULL)
6253 {
6254 EMSGN(_(e_listidx), idx);
6255 return NULL;
6256 }
6257 return get_tv_string(&li->li_tv);
6258}
6259
6260/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006261 * Locate "item" list "l" and return its index.
6262 * Returns -1 when "item" is not in the list.
6263 */
6264 static long
6265list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006266 list_T *l;
6267 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006268{
6269 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006270 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006271
6272 if (l == NULL)
6273 return -1;
6274 idx = 0;
6275 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6276 ++idx;
6277 if (li == NULL)
6278 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006279 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006280}
6281
6282/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006283 * Append item "item" to the end of list "l".
6284 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006285 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006287 list_T *l;
6288 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006289{
6290 if (l->lv_last == NULL)
6291 {
6292 /* empty list */
6293 l->lv_first = item;
6294 l->lv_last = item;
6295 item->li_prev = NULL;
6296 }
6297 else
6298 {
6299 l->lv_last->li_next = item;
6300 item->li_prev = l->lv_last;
6301 l->lv_last = item;
6302 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006303 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006304 item->li_next = NULL;
6305}
6306
6307/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006308 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006309 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006310 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006311 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006313 list_T *l;
6314 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006316 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006317
Bram Moolenaar05159a02005-02-26 23:04:13 +00006318 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006319 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006320 copy_tv(tv, &li->li_tv);
6321 list_append(l, li);
6322 return OK;
6323}
6324
6325/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006326 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006327 * Return FAIL when out of memory.
6328 */
6329 int
6330list_append_dict(list, dict)
6331 list_T *list;
6332 dict_T *dict;
6333{
6334 listitem_T *li = listitem_alloc();
6335
6336 if (li == NULL)
6337 return FAIL;
6338 li->li_tv.v_type = VAR_DICT;
6339 li->li_tv.v_lock = 0;
6340 li->li_tv.vval.v_dict = dict;
6341 list_append(list, li);
6342 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006343 return OK;
6344}
6345
6346/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006347 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006348 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006349 * Returns FAIL when out of memory.
6350 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006351 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006352list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006353 list_T *l;
6354 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006355 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006356{
6357 listitem_T *li = listitem_alloc();
6358
6359 if (li == NULL)
6360 return FAIL;
6361 list_append(l, li);
6362 li->li_tv.v_type = VAR_STRING;
6363 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006364 if (str == NULL)
6365 li->li_tv.vval.v_string = NULL;
6366 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006367 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006368 return FAIL;
6369 return OK;
6370}
6371
6372/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006373 * Append "n" to list "l".
6374 * Returns FAIL when out of memory.
6375 */
6376 static int
6377list_append_number(l, n)
6378 list_T *l;
6379 varnumber_T n;
6380{
6381 listitem_T *li;
6382
6383 li = listitem_alloc();
6384 if (li == NULL)
6385 return FAIL;
6386 li->li_tv.v_type = VAR_NUMBER;
6387 li->li_tv.v_lock = 0;
6388 li->li_tv.vval.v_number = n;
6389 list_append(l, li);
6390 return OK;
6391}
6392
6393/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006395 * If "item" is NULL append at the end.
6396 * Return FAIL when out of memory.
6397 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006398 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006399list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006400 list_T *l;
6401 typval_T *tv;
6402 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403{
Bram Moolenaar33570922005-01-25 22:26:29 +00006404 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006405
6406 if (ni == NULL)
6407 return FAIL;
6408 copy_tv(tv, &ni->li_tv);
6409 if (item == NULL)
6410 /* Append new item at end of list. */
6411 list_append(l, ni);
6412 else
6413 {
6414 /* Insert new item before existing item. */
6415 ni->li_prev = item->li_prev;
6416 ni->li_next = item;
6417 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006418 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006419 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006420 ++l->lv_idx;
6421 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006423 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006425 l->lv_idx_item = NULL;
6426 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006427 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006428 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 }
6430 return OK;
6431}
6432
6433/*
6434 * Extend "l1" with "l2".
6435 * If "bef" is NULL append at the end, otherwise insert before this item.
6436 * Returns FAIL when out of memory.
6437 */
6438 static int
6439list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006440 list_T *l1;
6441 list_T *l2;
6442 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006443{
Bram Moolenaar33570922005-01-25 22:26:29 +00006444 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006445 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006446
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006447 /* We also quit the loop when we have inserted the original item count of
6448 * the list, avoid a hang when we extend a list with itself. */
6449 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006450 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6451 return FAIL;
6452 return OK;
6453}
6454
6455/*
6456 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6457 * Return FAIL when out of memory.
6458 */
6459 static int
6460list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006461 list_T *l1;
6462 list_T *l2;
6463 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464{
Bram Moolenaar33570922005-01-25 22:26:29 +00006465 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006466
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006467 if (l1 == NULL || l2 == NULL)
6468 return FAIL;
6469
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006470 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006471 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006472 if (l == NULL)
6473 return FAIL;
6474 tv->v_type = VAR_LIST;
6475 tv->vval.v_list = l;
6476
6477 /* append all items from the second list */
6478 return list_extend(l, l2, NULL);
6479}
6480
6481/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006482 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006483 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006484 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006485 * Returns NULL when out of memory.
6486 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006487 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006488list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006489 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006490 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006491 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006492{
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 list_T *copy;
6494 listitem_T *item;
6495 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006496
6497 if (orig == NULL)
6498 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006499
6500 copy = list_alloc();
6501 if (copy != NULL)
6502 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006503 if (copyID != 0)
6504 {
6505 /* Do this before adding the items, because one of the items may
6506 * refer back to this list. */
6507 orig->lv_copyID = copyID;
6508 orig->lv_copylist = copy;
6509 }
6510 for (item = orig->lv_first; item != NULL && !got_int;
6511 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006512 {
6513 ni = listitem_alloc();
6514 if (ni == NULL)
6515 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006516 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006517 {
6518 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6519 {
6520 vim_free(ni);
6521 break;
6522 }
6523 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006525 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006526 list_append(copy, ni);
6527 }
6528 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 if (item != NULL)
6530 {
6531 list_unref(copy);
6532 copy = NULL;
6533 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006534 }
6535
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006536 return copy;
6537}
6538
6539/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006541 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006542 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006543 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006544list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006545 list_T *l;
6546 listitem_T *item;
6547 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006548{
Bram Moolenaar33570922005-01-25 22:26:29 +00006549 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006551 /* notify watchers */
6552 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006553 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006554 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555 list_fix_watch(l, ip);
6556 if (ip == item2)
6557 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006558 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559
6560 if (item2->li_next == NULL)
6561 l->lv_last = item->li_prev;
6562 else
6563 item2->li_next->li_prev = item->li_prev;
6564 if (item->li_prev == NULL)
6565 l->lv_first = item2->li_next;
6566 else
6567 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006568 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006569}
6570
6571/*
6572 * Return an allocated string with the string representation of a list.
6573 * May return NULL.
6574 */
6575 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006576list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006577 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006578 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579{
6580 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581
6582 if (tv->vval.v_list == NULL)
6583 return NULL;
6584 ga_init2(&ga, (int)sizeof(char), 80);
6585 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006586 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006587 {
6588 vim_free(ga.ga_data);
6589 return NULL;
6590 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591 ga_append(&ga, ']');
6592 ga_append(&ga, NUL);
6593 return (char_u *)ga.ga_data;
6594}
6595
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006596typedef struct join_S {
6597 char_u *s;
6598 char_u *tofree;
6599} join_T;
6600
6601 static int
6602list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6603 garray_T *gap; /* to store the result in */
6604 list_T *l;
6605 char_u *sep;
6606 int echo_style;
6607 int copyID;
6608 garray_T *join_gap; /* to keep each list item string */
6609{
6610 int i;
6611 join_T *p;
6612 int len;
6613 int sumlen = 0;
6614 int first = TRUE;
6615 char_u *tofree;
6616 char_u numbuf[NUMBUFLEN];
6617 listitem_T *item;
6618 char_u *s;
6619
6620 /* Stringify each item in the list. */
6621 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6622 {
6623 if (echo_style)
6624 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6625 else
6626 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6627 if (s == NULL)
6628 return FAIL;
6629
6630 len = (int)STRLEN(s);
6631 sumlen += len;
6632
6633 ga_grow(join_gap, 1);
6634 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6635 if (tofree != NULL || s != numbuf)
6636 {
6637 p->s = s;
6638 p->tofree = tofree;
6639 }
6640 else
6641 {
6642 p->s = vim_strnsave(s, len);
6643 p->tofree = p->s;
6644 }
6645
6646 line_breakcheck();
6647 }
6648
6649 /* Allocate result buffer with its total size, avoid re-allocation and
6650 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6651 if (join_gap->ga_len >= 2)
6652 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6653 if (ga_grow(gap, sumlen + 2) == FAIL)
6654 return FAIL;
6655
6656 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6657 {
6658 if (first)
6659 first = FALSE;
6660 else
6661 ga_concat(gap, sep);
6662 p = ((join_T *)join_gap->ga_data) + i;
6663
6664 if (p->s != NULL)
6665 ga_concat(gap, p->s);
6666 line_breakcheck();
6667 }
6668
6669 return OK;
6670}
6671
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006673 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006674 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006675 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006676 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006677 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006678list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006679 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006680 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006681 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006682 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006683 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006684{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006685 garray_T join_ga;
6686 int retval;
6687 join_T *p;
6688 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006689
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006690 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6691 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6692
6693 /* Dispose each item in join_ga. */
6694 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006695 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006696 p = (join_T *)join_ga.ga_data;
6697 for (i = 0; i < join_ga.ga_len; ++i)
6698 {
6699 vim_free(p->tofree);
6700 ++p;
6701 }
6702 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006703 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006704
6705 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006706}
6707
6708/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006709 * Garbage collection for lists and dictionaries.
6710 *
6711 * We use reference counts to be able to free most items right away when they
6712 * are no longer used. But for composite items it's possible that it becomes
6713 * unused while the reference count is > 0: When there is a recursive
6714 * reference. Example:
6715 * :let l = [1, 2, 3]
6716 * :let d = {9: l}
6717 * :let l[1] = d
6718 *
6719 * Since this is quite unusual we handle this with garbage collection: every
6720 * once in a while find out which lists and dicts are not referenced from any
6721 * variable.
6722 *
6723 * Here is a good reference text about garbage collection (refers to Python
6724 * but it applies to all reference-counting mechanisms):
6725 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006726 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006727
6728/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006729 * Do garbage collection for lists and dicts.
6730 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006731 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006732 int
6733garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006734{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006735 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006736 buf_T *buf;
6737 win_T *wp;
6738 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006739 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006740 int did_free;
6741 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006742#ifdef FEAT_WINDOWS
6743 tabpage_T *tp;
6744#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006745
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006746 /* Only do this once. */
6747 want_garbage_collect = FALSE;
6748 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006749 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006750
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006751 /* We advance by two because we add one for items referenced through
6752 * previous_funccal. */
6753 current_copyID += COPYID_INC;
6754 copyID = current_copyID;
6755
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006756 /*
6757 * 1. Go through all accessible variables and mark all lists and dicts
6758 * with copyID.
6759 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006760
6761 /* Don't free variables in the previous_funccal list unless they are only
6762 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006763 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006764 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6765 {
6766 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6767 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6768 }
6769
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 /* script-local variables */
6771 for (i = 1; i <= ga_scripts.ga_len; ++i)
6772 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6773
6774 /* buffer-local variables */
6775 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6776 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6777
6778 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006779 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006780 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6781
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006782#ifdef FEAT_WINDOWS
6783 /* tabpage-local variables */
6784 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6785 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6786#endif
6787
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006788 /* global variables */
6789 set_ref_in_ht(&globvarht, copyID);
6790
6791 /* function-local variables */
6792 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6793 {
6794 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6795 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6796 }
6797
Bram Moolenaard812df62008-11-09 12:46:09 +00006798 /* v: vars */
6799 set_ref_in_ht(&vimvarht, copyID);
6800
Bram Moolenaar1dced572012-04-05 16:54:08 +02006801#ifdef FEAT_LUA
6802 set_ref_in_lua(copyID);
6803#endif
6804
Bram Moolenaardb913952012-06-29 12:54:53 +02006805#ifdef FEAT_PYTHON
6806 set_ref_in_python(copyID);
6807#endif
6808
6809#ifdef FEAT_PYTHON3
6810 set_ref_in_python3(copyID);
6811#endif
6812
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006813 /*
6814 * 2. Free lists and dictionaries that are not referenced.
6815 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006816 did_free = free_unref_items(copyID);
6817
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006818 /*
6819 * 3. Check if any funccal can be freed now.
6820 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006821 for (pfc = &previous_funccal; *pfc != NULL; )
6822 {
6823 if (can_free_funccal(*pfc, copyID))
6824 {
6825 fc = *pfc;
6826 *pfc = fc->caller;
6827 free_funccal(fc, TRUE);
6828 did_free = TRUE;
6829 did_free_funccal = TRUE;
6830 }
6831 else
6832 pfc = &(*pfc)->caller;
6833 }
6834 if (did_free_funccal)
6835 /* When a funccal was freed some more items might be garbage
6836 * collected, so run again. */
6837 (void)garbage_collect();
6838
6839 return did_free;
6840}
6841
6842/*
6843 * Free lists and dictionaries that are no longer referenced.
6844 */
6845 static int
6846free_unref_items(copyID)
6847 int copyID;
6848{
6849 dict_T *dd;
6850 list_T *ll;
6851 int did_free = FALSE;
6852
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006853 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006854 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006855 */
6856 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006857 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006858 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006859 /* Free the Dictionary and ordinary items it contains, but don't
6860 * recurse into Lists and Dictionaries, they will be in the list
6861 * of dicts or list of lists. */
6862 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006863 did_free = TRUE;
6864
6865 /* restart, next dict may also have been freed */
6866 dd = first_dict;
6867 }
6868 else
6869 dd = dd->dv_used_next;
6870
6871 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006872 * Go through the list of lists and free items without the copyID.
6873 * But don't free a list that has a watcher (used in a for loop), these
6874 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875 */
6876 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006877 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6878 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006879 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006880 /* Free the List and ordinary items it contains, but don't recurse
6881 * into Lists and Dictionaries, they will be in the list of dicts
6882 * or list of lists. */
6883 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006884 did_free = TRUE;
6885
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006886 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006887 ll = first_list;
6888 }
6889 else
6890 ll = ll->lv_used_next;
6891
6892 return did_free;
6893}
6894
6895/*
6896 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6897 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006898 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006899set_ref_in_ht(ht, copyID)
6900 hashtab_T *ht;
6901 int copyID;
6902{
6903 int todo;
6904 hashitem_T *hi;
6905
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006906 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 for (hi = ht->ht_array; todo > 0; ++hi)
6908 if (!HASHITEM_EMPTY(hi))
6909 {
6910 --todo;
6911 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6912 }
6913}
6914
6915/*
6916 * Mark all lists and dicts referenced through list "l" with "copyID".
6917 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006918 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919set_ref_in_list(l, copyID)
6920 list_T *l;
6921 int copyID;
6922{
6923 listitem_T *li;
6924
6925 for (li = l->lv_first; li != NULL; li = li->li_next)
6926 set_ref_in_item(&li->li_tv, copyID);
6927}
6928
6929/*
6930 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6931 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006932 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006933set_ref_in_item(tv, copyID)
6934 typval_T *tv;
6935 int copyID;
6936{
6937 dict_T *dd;
6938 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006939
6940 switch (tv->v_type)
6941 {
6942 case VAR_DICT:
6943 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006944 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006945 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006946 /* Didn't see this dict yet. */
6947 dd->dv_copyID = copyID;
6948 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006949 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006950 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006951
6952 case VAR_LIST:
6953 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006954 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006955 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006956 /* Didn't see this list yet. */
6957 ll->lv_copyID = copyID;
6958 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006959 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006960 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006961 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006962 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006963}
6964
6965/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006966 * Allocate an empty header for a dictionary.
6967 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006968 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006969dict_alloc()
6970{
Bram Moolenaar33570922005-01-25 22:26:29 +00006971 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006972
Bram Moolenaar33570922005-01-25 22:26:29 +00006973 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006974 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006975 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006976 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006977 if (first_dict != NULL)
6978 first_dict->dv_used_prev = d;
6979 d->dv_used_next = first_dict;
6980 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006981 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006982
Bram Moolenaar33570922005-01-25 22:26:29 +00006983 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006984 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006985 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006986 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006987 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006988 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006989 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006990}
6991
6992/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006993 * Allocate an empty dict for a return value.
6994 * Returns OK or FAIL.
6995 */
6996 static int
6997rettv_dict_alloc(rettv)
6998 typval_T *rettv;
6999{
7000 dict_T *d = dict_alloc();
7001
7002 if (d == NULL)
7003 return FAIL;
7004
7005 rettv->vval.v_dict = d;
7006 rettv->v_type = VAR_DICT;
7007 ++d->dv_refcount;
7008 return OK;
7009}
7010
7011
7012/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007013 * Unreference a Dictionary: decrement the reference count and free it when it
7014 * becomes zero.
7015 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007016 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007017dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007018 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007019{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007020 if (d != NULL && --d->dv_refcount <= 0)
7021 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007022}
7023
7024/*
7025 * Free a Dictionary, including all items it contains.
7026 * Ignores the reference count.
7027 */
7028 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007029dict_free(d, recurse)
7030 dict_T *d;
7031 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007032{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007033 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007034 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007035 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007036
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 /* Remove the dict from the list of dicts for garbage collection. */
7038 if (d->dv_used_prev == NULL)
7039 first_dict = d->dv_used_next;
7040 else
7041 d->dv_used_prev->dv_used_next = d->dv_used_next;
7042 if (d->dv_used_next != NULL)
7043 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7044
7045 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007046 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007047 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007048 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007049 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007050 if (!HASHITEM_EMPTY(hi))
7051 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007052 /* Remove the item before deleting it, just in case there is
7053 * something recursive causing trouble. */
7054 di = HI2DI(hi);
7055 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007056 if (recurse || (di->di_tv.v_type != VAR_LIST
7057 && di->di_tv.v_type != VAR_DICT))
7058 clear_tv(&di->di_tv);
7059 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007060 --todo;
7061 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007062 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007063 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007064 vim_free(d);
7065}
7066
7067/*
7068 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007069 * The "key" is copied to the new item.
7070 * Note that the value of the item "di_tv" still needs to be initialized!
7071 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007072 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007073 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007074dictitem_alloc(key)
7075 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076{
Bram Moolenaar33570922005-01-25 22:26:29 +00007077 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007078
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007079 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007080 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007081 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007082 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007083 di->di_flags = 0;
7084 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007085 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007086}
7087
7088/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007089 * Make a copy of a Dictionary item.
7090 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007091 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007092dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007093 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007094{
Bram Moolenaar33570922005-01-25 22:26:29 +00007095 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007096
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007097 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7098 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007099 if (di != NULL)
7100 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007101 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007102 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007103 copy_tv(&org->di_tv, &di->di_tv);
7104 }
7105 return di;
7106}
7107
7108/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007109 * Remove item "item" from Dictionary "dict" and free it.
7110 */
7111 static void
7112dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007113 dict_T *dict;
7114 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007115{
Bram Moolenaar33570922005-01-25 22:26:29 +00007116 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007117
Bram Moolenaar33570922005-01-25 22:26:29 +00007118 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007119 if (HASHITEM_EMPTY(hi))
7120 EMSG2(_(e_intern2), "dictitem_remove()");
7121 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007122 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007123 dictitem_free(item);
7124}
7125
7126/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007127 * Free a dict item. Also clears the value.
7128 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007129 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007130dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007131 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007132{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007133 clear_tv(&item->di_tv);
7134 vim_free(item);
7135}
7136
7137/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007138 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7139 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007140 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007141 * Returns NULL when out of memory.
7142 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007143 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007144dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007146 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007147 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007148{
Bram Moolenaar33570922005-01-25 22:26:29 +00007149 dict_T *copy;
7150 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007151 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007152 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007153
7154 if (orig == NULL)
7155 return NULL;
7156
7157 copy = dict_alloc();
7158 if (copy != NULL)
7159 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007160 if (copyID != 0)
7161 {
7162 orig->dv_copyID = copyID;
7163 orig->dv_copydict = copy;
7164 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007165 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007166 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007167 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007168 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007169 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007170 --todo;
7171
7172 di = dictitem_alloc(hi->hi_key);
7173 if (di == NULL)
7174 break;
7175 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007176 {
7177 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7178 copyID) == FAIL)
7179 {
7180 vim_free(di);
7181 break;
7182 }
7183 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007184 else
7185 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7186 if (dict_add(copy, di) == FAIL)
7187 {
7188 dictitem_free(di);
7189 break;
7190 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007191 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007192 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007193
Bram Moolenaare9a41262005-01-15 22:18:47 +00007194 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007195 if (todo > 0)
7196 {
7197 dict_unref(copy);
7198 copy = NULL;
7199 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007200 }
7201
7202 return copy;
7203}
7204
7205/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007206 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007207 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007208 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007209 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007210dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007211 dict_T *d;
7212 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213{
Bram Moolenaar33570922005-01-25 22:26:29 +00007214 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215}
7216
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007218 * Add a number or string entry to dictionary "d".
7219 * When "str" is NULL use number "nr", otherwise use "str".
7220 * Returns FAIL when out of memory and when key already exists.
7221 */
7222 int
7223dict_add_nr_str(d, key, nr, str)
7224 dict_T *d;
7225 char *key;
7226 long nr;
7227 char_u *str;
7228{
7229 dictitem_T *item;
7230
7231 item = dictitem_alloc((char_u *)key);
7232 if (item == NULL)
7233 return FAIL;
7234 item->di_tv.v_lock = 0;
7235 if (str == NULL)
7236 {
7237 item->di_tv.v_type = VAR_NUMBER;
7238 item->di_tv.vval.v_number = nr;
7239 }
7240 else
7241 {
7242 item->di_tv.v_type = VAR_STRING;
7243 item->di_tv.vval.v_string = vim_strsave(str);
7244 }
7245 if (dict_add(d, item) == FAIL)
7246 {
7247 dictitem_free(item);
7248 return FAIL;
7249 }
7250 return OK;
7251}
7252
7253/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007254 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007255 * Returns FAIL when out of memory and when key already exists.
7256 */
7257 int
7258dict_add_list(d, key, list)
7259 dict_T *d;
7260 char *key;
7261 list_T *list;
7262{
7263 dictitem_T *item;
7264
7265 item = dictitem_alloc((char_u *)key);
7266 if (item == NULL)
7267 return FAIL;
7268 item->di_tv.v_lock = 0;
7269 item->di_tv.v_type = VAR_LIST;
7270 item->di_tv.vval.v_list = list;
7271 if (dict_add(d, item) == FAIL)
7272 {
7273 dictitem_free(item);
7274 return FAIL;
7275 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007276 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007277 return OK;
7278}
7279
7280/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007281 * Get the number of items in a Dictionary.
7282 */
7283 static long
7284dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007285 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007286{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007287 if (d == NULL)
7288 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007289 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007290}
7291
7292/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007293 * Find item "key[len]" in Dictionary "d".
7294 * If "len" is negative use strlen(key).
7295 * Returns NULL when not found.
7296 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007297 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007298dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007300 char_u *key;
7301 int len;
7302{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303#define AKEYLEN 200
7304 char_u buf[AKEYLEN];
7305 char_u *akey;
7306 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007307 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007308
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309 if (len < 0)
7310 akey = key;
7311 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007312 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313 tofree = akey = vim_strnsave(key, len);
7314 if (akey == NULL)
7315 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007316 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317 else
7318 {
7319 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007320 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321 akey = buf;
7322 }
7323
Bram Moolenaar33570922005-01-25 22:26:29 +00007324 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 vim_free(tofree);
7326 if (HASHITEM_EMPTY(hi))
7327 return NULL;
7328 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007329}
7330
7331/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007332 * Get a string item from a dictionary.
7333 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007334 * Returns NULL if the entry doesn't exist or out of memory.
7335 */
7336 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007337get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007338 dict_T *d;
7339 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007340 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007341{
7342 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007343 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007344
7345 di = dict_find(d, key, -1);
7346 if (di == NULL)
7347 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007348 s = get_tv_string(&di->di_tv);
7349 if (save && s != NULL)
7350 s = vim_strsave(s);
7351 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007352}
7353
7354/*
7355 * Get a number item from a dictionary.
7356 * Returns 0 if the entry doesn't exist or out of memory.
7357 */
7358 long
7359get_dict_number(d, key)
7360 dict_T *d;
7361 char_u *key;
7362{
7363 dictitem_T *di;
7364
7365 di = dict_find(d, key, -1);
7366 if (di == NULL)
7367 return 0;
7368 return get_tv_number(&di->di_tv);
7369}
7370
7371/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007372 * Return an allocated string with the string representation of a Dictionary.
7373 * May return NULL.
7374 */
7375 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007376dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007377 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007378 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007379{
7380 garray_T ga;
7381 int first = TRUE;
7382 char_u *tofree;
7383 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007384 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007385 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007387 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007388
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007389 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390 return NULL;
7391 ga_init2(&ga, (int)sizeof(char), 80);
7392 ga_append(&ga, '{');
7393
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007394 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007395 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007396 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007397 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007398 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007399 --todo;
7400
7401 if (first)
7402 first = FALSE;
7403 else
7404 ga_concat(&ga, (char_u *)", ");
7405
7406 tofree = string_quote(hi->hi_key, FALSE);
7407 if (tofree != NULL)
7408 {
7409 ga_concat(&ga, tofree);
7410 vim_free(tofree);
7411 }
7412 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007413 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007414 if (s != NULL)
7415 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007417 if (s == NULL)
7418 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007419 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007421 if (todo > 0)
7422 {
7423 vim_free(ga.ga_data);
7424 return NULL;
7425 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007426
7427 ga_append(&ga, '}');
7428 ga_append(&ga, NUL);
7429 return (char_u *)ga.ga_data;
7430}
7431
7432/*
7433 * Allocate a variable for a Dictionary and fill it from "*arg".
7434 * Return OK or FAIL. Returns NOTDONE for {expr}.
7435 */
7436 static int
7437get_dict_tv(arg, rettv, evaluate)
7438 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007439 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440 int evaluate;
7441{
Bram Moolenaar33570922005-01-25 22:26:29 +00007442 dict_T *d = NULL;
7443 typval_T tvkey;
7444 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007445 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007446 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007447 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007448 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449
7450 /*
7451 * First check if it's not a curly-braces thing: {expr}.
7452 * Must do this without evaluating, otherwise a function may be called
7453 * twice. Unfortunately this means we need to call eval1() twice for the
7454 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007455 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007456 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007457 if (*start != '}')
7458 {
7459 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7460 return FAIL;
7461 if (*start == '}')
7462 return NOTDONE;
7463 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007464
7465 if (evaluate)
7466 {
7467 d = dict_alloc();
7468 if (d == NULL)
7469 return FAIL;
7470 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007471 tvkey.v_type = VAR_UNKNOWN;
7472 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007473
7474 *arg = skipwhite(*arg + 1);
7475 while (**arg != '}' && **arg != NUL)
7476 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007477 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007478 goto failret;
7479 if (**arg != ':')
7480 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007481 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007482 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007483 goto failret;
7484 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007485 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007486 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007487 key = get_tv_string_buf_chk(&tvkey, buf);
7488 if (key == NULL || *key == NUL)
7489 {
7490 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7491 if (key != NULL)
7492 EMSG(_(e_emptykey));
7493 clear_tv(&tvkey);
7494 goto failret;
7495 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007496 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497
7498 *arg = skipwhite(*arg + 1);
7499 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7500 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007501 if (evaluate)
7502 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007503 goto failret;
7504 }
7505 if (evaluate)
7506 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007507 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508 if (item != NULL)
7509 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007510 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007511 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007512 clear_tv(&tv);
7513 goto failret;
7514 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515 item = dictitem_alloc(key);
7516 clear_tv(&tvkey);
7517 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007519 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007520 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007521 if (dict_add(d, item) == FAIL)
7522 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007523 }
7524 }
7525
7526 if (**arg == '}')
7527 break;
7528 if (**arg != ',')
7529 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007530 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007531 goto failret;
7532 }
7533 *arg = skipwhite(*arg + 1);
7534 }
7535
7536 if (**arg != '}')
7537 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007538 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539failret:
7540 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007541 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007542 return FAIL;
7543 }
7544
7545 *arg = skipwhite(*arg + 1);
7546 if (evaluate)
7547 {
7548 rettv->v_type = VAR_DICT;
7549 rettv->vval.v_dict = d;
7550 ++d->dv_refcount;
7551 }
7552
7553 return OK;
7554}
7555
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007557 * Return a string with the string representation of a variable.
7558 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007559 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007560 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007561 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007562 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007563 */
7564 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007565echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007567 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007568 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007569 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007570{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007571 static int recurse = 0;
7572 char_u *r = NULL;
7573
Bram Moolenaar33570922005-01-25 22:26:29 +00007574 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007575 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007576 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007577 *tofree = NULL;
7578 return NULL;
7579 }
7580 ++recurse;
7581
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007582 switch (tv->v_type)
7583 {
7584 case VAR_FUNC:
7585 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007586 r = tv->vval.v_string;
7587 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007588
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007589 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007590 if (tv->vval.v_list == NULL)
7591 {
7592 *tofree = NULL;
7593 r = NULL;
7594 }
7595 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7596 {
7597 *tofree = NULL;
7598 r = (char_u *)"[...]";
7599 }
7600 else
7601 {
7602 tv->vval.v_list->lv_copyID = copyID;
7603 *tofree = list2string(tv, copyID);
7604 r = *tofree;
7605 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007606 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007607
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007609 if (tv->vval.v_dict == NULL)
7610 {
7611 *tofree = NULL;
7612 r = NULL;
7613 }
7614 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7615 {
7616 *tofree = NULL;
7617 r = (char_u *)"{...}";
7618 }
7619 else
7620 {
7621 tv->vval.v_dict->dv_copyID = copyID;
7622 *tofree = dict2string(tv, copyID);
7623 r = *tofree;
7624 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007625 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007626
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007627 case VAR_STRING:
7628 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007629 *tofree = NULL;
7630 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007631 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007632
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007633#ifdef FEAT_FLOAT
7634 case VAR_FLOAT:
7635 *tofree = NULL;
7636 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7637 r = numbuf;
7638 break;
7639#endif
7640
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007641 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007642 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007643 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007644 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007645
7646 --recurse;
7647 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007648}
7649
7650/*
7651 * Return a string with the string representation of a variable.
7652 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7653 * "numbuf" is used for a number.
7654 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007655 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007656 */
7657 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007658tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007659 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007660 char_u **tofree;
7661 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007662 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007663{
7664 switch (tv->v_type)
7665 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007666 case VAR_FUNC:
7667 *tofree = string_quote(tv->vval.v_string, TRUE);
7668 return *tofree;
7669 case VAR_STRING:
7670 *tofree = string_quote(tv->vval.v_string, FALSE);
7671 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007672#ifdef FEAT_FLOAT
7673 case VAR_FLOAT:
7674 *tofree = NULL;
7675 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7676 return numbuf;
7677#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007678 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007679 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007681 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007682 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007683 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007684 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007685 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007686}
7687
7688/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007689 * Return string "str" in ' quotes, doubling ' characters.
7690 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007692 */
7693 static char_u *
7694string_quote(str, function)
7695 char_u *str;
7696 int function;
7697{
Bram Moolenaar33570922005-01-25 22:26:29 +00007698 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007699 char_u *p, *r, *s;
7700
Bram Moolenaar33570922005-01-25 22:26:29 +00007701 len = (function ? 13 : 3);
7702 if (str != NULL)
7703 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007704 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007705 for (p = str; *p != NUL; mb_ptr_adv(p))
7706 if (*p == '\'')
7707 ++len;
7708 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007709 s = r = alloc(len);
7710 if (r != NULL)
7711 {
7712 if (function)
7713 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007714 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007715 r += 10;
7716 }
7717 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007719 if (str != NULL)
7720 for (p = str; *p != NUL; )
7721 {
7722 if (*p == '\'')
7723 *r++ = '\'';
7724 MB_COPY_CHAR(p, r);
7725 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007727 if (function)
7728 *r++ = ')';
7729 *r++ = NUL;
7730 }
7731 return s;
7732}
7733
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007734#ifdef FEAT_FLOAT
7735/*
7736 * Convert the string "text" to a floating point number.
7737 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7738 * this always uses a decimal point.
7739 * Returns the length of the text that was consumed.
7740 */
7741 static int
7742string2float(text, value)
7743 char_u *text;
7744 float_T *value; /* result stored here */
7745{
7746 char *s = (char *)text;
7747 float_T f;
7748
7749 f = strtod(s, &s);
7750 *value = f;
7751 return (int)((char_u *)s - text);
7752}
7753#endif
7754
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 * Get the value of an environment variable.
7757 * "arg" is pointing to the '$'. It is advanced to after the name.
7758 * If the environment variable was not set, silently assume it is empty.
7759 * Always return OK.
7760 */
7761 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007762get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 int evaluate;
7766{
7767 char_u *string = NULL;
7768 int len;
7769 int cc;
7770 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007771 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772
7773 ++*arg;
7774 name = *arg;
7775 len = get_env_len(arg);
7776 if (evaluate)
7777 {
7778 if (len != 0)
7779 {
7780 cc = name[len];
7781 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007782 /* first try vim_getenv(), fast for normal environment vars */
7783 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007785 {
7786 if (!mustfree)
7787 string = vim_strsave(string);
7788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 else
7790 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007791 if (mustfree)
7792 vim_free(string);
7793
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 /* next try expanding things like $VIM and ${HOME} */
7795 string = expand_env_save(name - 1);
7796 if (string != NULL && *string == '$')
7797 {
7798 vim_free(string);
7799 string = NULL;
7800 }
7801 }
7802 name[len] = cc;
7803 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007804 rettv->v_type = VAR_STRING;
7805 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 }
7807
7808 return OK;
7809}
7810
7811/*
7812 * Array with names and number of arguments of all internal functions
7813 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7814 */
7815static struct fst
7816{
7817 char *f_name; /* function name */
7818 char f_min_argc; /* minimal number of arguments */
7819 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007820 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007821 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822} functions[] =
7823{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007824#ifdef FEAT_FLOAT
7825 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007826 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007827#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007828 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007829 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 {"append", 2, 2, f_append},
7831 {"argc", 0, 0, f_argc},
7832 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007833 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007834#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007835 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007836 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007837 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007840 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 {"bufexists", 1, 1, f_bufexists},
7842 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7843 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7844 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7845 {"buflisted", 1, 1, f_buflisted},
7846 {"bufloaded", 1, 1, f_bufloaded},
7847 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007848 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 {"bufwinnr", 1, 1, f_bufwinnr},
7850 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007851 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007852 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007853#ifdef FEAT_FLOAT
7854 {"ceil", 1, 1, f_ceil},
7855#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007856 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 {"char2nr", 1, 1, f_char2nr},
7858 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007859 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007861#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007862 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007863 {"complete_add", 1, 1, f_complete_add},
7864 {"complete_check", 0, 0, f_complete_check},
7865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007867 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007868#ifdef FEAT_FLOAT
7869 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007870 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007871#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007872 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007874 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007875 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 {"delete", 1, 1, f_delete},
7877 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007878 {"diff_filler", 1, 1, f_diff_filler},
7879 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007880 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007882 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 {"eventhandler", 0, 0, f_eventhandler},
7884 {"executable", 1, 1, f_executable},
7885 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007886#ifdef FEAT_FLOAT
7887 {"exp", 1, 1, f_exp},
7888#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007889 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007890 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007891 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7893 {"filereadable", 1, 1, f_filereadable},
7894 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007895 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007896 {"finddir", 1, 3, f_finddir},
7897 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007898#ifdef FEAT_FLOAT
7899 {"float2nr", 1, 1, f_float2nr},
7900 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007901 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007902#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007903 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {"fnamemodify", 2, 2, f_fnamemodify},
7905 {"foldclosed", 1, 1, f_foldclosed},
7906 {"foldclosedend", 1, 1, f_foldclosedend},
7907 {"foldlevel", 1, 1, f_foldlevel},
7908 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007909 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007911 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007912 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007913 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007914 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 {"getbufvar", 2, 2, f_getbufvar},
7916 {"getchar", 0, 1, f_getchar},
7917 {"getcharmod", 0, 0, f_getcharmod},
7918 {"getcmdline", 0, 0, f_getcmdline},
7919 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007920 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007922 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007923 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {"getfsize", 1, 1, f_getfsize},
7925 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007926 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007927 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007928 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007929 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007930 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007931 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007932 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007933 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007935 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007936 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 {"getwinposx", 0, 0, f_getwinposx},
7938 {"getwinposy", 0, 0, f_getwinposy},
7939 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007940 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007941 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007943 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007944 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007945 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7947 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7948 {"histadd", 2, 2, f_histadd},
7949 {"histdel", 1, 2, f_histdel},
7950 {"histget", 1, 2, f_histget},
7951 {"histnr", 1, 1, f_histnr},
7952 {"hlID", 1, 1, f_hlID},
7953 {"hlexists", 1, 1, f_hlexists},
7954 {"hostname", 0, 0, f_hostname},
7955 {"iconv", 3, 3, f_iconv},
7956 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007957 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007958 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007960 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {"inputrestore", 0, 0, f_inputrestore},
7962 {"inputsave", 0, 0, f_inputsave},
7963 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007964 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007965 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007967 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007968 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007969 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007970 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007972 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {"libcall", 3, 3, f_libcall},
7974 {"libcallnr", 3, 3, f_libcallnr},
7975 {"line", 1, 1, f_line},
7976 {"line2byte", 1, 1, f_line2byte},
7977 {"lispindent", 1, 1, f_lispindent},
7978 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007979#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007980 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007981 {"log10", 1, 1, f_log10},
7982#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007983#ifdef FEAT_LUA
7984 {"luaeval", 1, 2, f_luaeval},
7985#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007986 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007987 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007988 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007989 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007990 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007991 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007992 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007993 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007994 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007995 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007996 {"max", 1, 1, f_max},
7997 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007998#ifdef vim_mkdir
7999 {"mkdir", 1, 3, f_mkdir},
8000#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008001 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008002#ifdef FEAT_MZSCHEME
8003 {"mzeval", 1, 1, f_mzeval},
8004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 {"nextnonblank", 1, 1, f_nextnonblank},
8006 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008007 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008008 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008009#ifdef FEAT_FLOAT
8010 {"pow", 2, 2, f_pow},
8011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008013 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008014 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008015#ifdef FEAT_PYTHON3
8016 {"py3eval", 1, 1, f_py3eval},
8017#endif
8018#ifdef FEAT_PYTHON
8019 {"pyeval", 1, 1, f_pyeval},
8020#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008021 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008022 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008023 {"reltime", 0, 2, f_reltime},
8024 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 {"remote_expr", 2, 3, f_remote_expr},
8026 {"remote_foreground", 1, 1, f_remote_foreground},
8027 {"remote_peek", 1, 2, f_remote_peek},
8028 {"remote_read", 1, 1, f_remote_read},
8029 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008030 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008032 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008034 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008035#ifdef FEAT_FLOAT
8036 {"round", 1, 1, f_round},
8037#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008038 {"screencol", 0, 0, f_screencol},
8039 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008040 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008041 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008042 {"searchpair", 3, 7, f_searchpair},
8043 {"searchpairpos", 3, 7, f_searchpairpos},
8044 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 {"server2client", 2, 2, f_server2client},
8046 {"serverlist", 0, 0, f_serverlist},
8047 {"setbufvar", 3, 3, f_setbufvar},
8048 {"setcmdpos", 1, 1, f_setcmdpos},
8049 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008050 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008051 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008052 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008053 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008055 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008056 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008058 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008059 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008061#ifdef FEAT_FLOAT
8062 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008063 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008064#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008065 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008066 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008067 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008068 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008069 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008070#ifdef FEAT_FLOAT
8071 {"sqrt", 1, 1, f_sqrt},
8072 {"str2float", 1, 1, f_str2float},
8073#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008074 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008075 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008076 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077#ifdef HAVE_STRFTIME
8078 {"strftime", 1, 2, f_strftime},
8079#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008080 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008081 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 {"strlen", 1, 1, f_strlen},
8083 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008084 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008086 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 {"submatch", 1, 1, f_submatch},
8088 {"substitute", 4, 4, f_substitute},
8089 {"synID", 3, 3, f_synID},
8090 {"synIDattr", 2, 3, f_synIDattr},
8091 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008092 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008093 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008094 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008095 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008096 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008097 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008098 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008099 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008100#ifdef FEAT_FLOAT
8101 {"tan", 1, 1, f_tan},
8102 {"tanh", 1, 1, f_tanh},
8103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008105 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"tolower", 1, 1, f_tolower},
8107 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008108 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008109#ifdef FEAT_FLOAT
8110 {"trunc", 1, 1, f_trunc},
8111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008113 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008114 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008115 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 {"virtcol", 1, 1, f_virtcol},
8117 {"visualmode", 0, 1, f_visualmode},
8118 {"winbufnr", 1, 1, f_winbufnr},
8119 {"wincol", 0, 0, f_wincol},
8120 {"winheight", 1, 1, f_winheight},
8121 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008122 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008124 {"winrestview", 1, 1, f_winrestview},
8125 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008127 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008128 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129};
8130
8131#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8132
8133/*
8134 * Function given to ExpandGeneric() to obtain the list of internal
8135 * or user defined function names.
8136 */
8137 char_u *
8138get_function_name(xp, idx)
8139 expand_T *xp;
8140 int idx;
8141{
8142 static int intidx = -1;
8143 char_u *name;
8144
8145 if (idx == 0)
8146 intidx = -1;
8147 if (intidx < 0)
8148 {
8149 name = get_user_func_name(xp, idx);
8150 if (name != NULL)
8151 return name;
8152 }
8153 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8154 {
8155 STRCPY(IObuff, functions[intidx].f_name);
8156 STRCAT(IObuff, "(");
8157 if (functions[intidx].f_max_argc == 0)
8158 STRCAT(IObuff, ")");
8159 return IObuff;
8160 }
8161
8162 return NULL;
8163}
8164
8165/*
8166 * Function given to ExpandGeneric() to obtain the list of internal or
8167 * user defined variable or function names.
8168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 char_u *
8170get_expr_name(xp, idx)
8171 expand_T *xp;
8172 int idx;
8173{
8174 static int intidx = -1;
8175 char_u *name;
8176
8177 if (idx == 0)
8178 intidx = -1;
8179 if (intidx < 0)
8180 {
8181 name = get_function_name(xp, idx);
8182 if (name != NULL)
8183 return name;
8184 }
8185 return get_user_var_name(xp, ++intidx);
8186}
8187
8188#endif /* FEAT_CMDL_COMPL */
8189
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008190#if defined(EBCDIC) || defined(PROTO)
8191/*
8192 * Compare struct fst by function name.
8193 */
8194 static int
8195compare_func_name(s1, s2)
8196 const void *s1;
8197 const void *s2;
8198{
8199 struct fst *p1 = (struct fst *)s1;
8200 struct fst *p2 = (struct fst *)s2;
8201
8202 return STRCMP(p1->f_name, p2->f_name);
8203}
8204
8205/*
8206 * Sort the function table by function name.
8207 * The sorting of the table above is ASCII dependant.
8208 * On machines using EBCDIC we have to sort it.
8209 */
8210 static void
8211sortFunctions()
8212{
8213 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8214
8215 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8216}
8217#endif
8218
8219
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220/*
8221 * Find internal function in table above.
8222 * Return index, or -1 if not found
8223 */
8224 static int
8225find_internal_func(name)
8226 char_u *name; /* name of the function */
8227{
8228 int first = 0;
8229 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8230 int cmp;
8231 int x;
8232
8233 /*
8234 * Find the function name in the table. Binary search.
8235 */
8236 while (first <= last)
8237 {
8238 x = first + ((unsigned)(last - first) >> 1);
8239 cmp = STRCMP(name, functions[x].f_name);
8240 if (cmp < 0)
8241 last = x - 1;
8242 else if (cmp > 0)
8243 first = x + 1;
8244 else
8245 return x;
8246 }
8247 return -1;
8248}
8249
8250/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008251 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8252 * name it contains, otherwise return "name".
8253 */
8254 static char_u *
8255deref_func_name(name, lenp)
8256 char_u *name;
8257 int *lenp;
8258{
Bram Moolenaar33570922005-01-25 22:26:29 +00008259 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008260 int cc;
8261
8262 cc = name[*lenp];
8263 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008264 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008265 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008266 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008267 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008268 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008269 {
8270 *lenp = 0;
8271 return (char_u *)""; /* just in case */
8272 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008273 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008274 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008275 }
8276
8277 return name;
8278}
8279
8280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 * Allocate a variable for the result of a function.
8282 * Return OK or FAIL.
8283 */
8284 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008285get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8286 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 char_u *name; /* name of the function */
8288 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 char_u **arg; /* argument, pointing to the '(' */
8291 linenr_T firstline; /* first line of range */
8292 linenr_T lastline; /* last line of range */
8293 int *doesrange; /* return: function handled range */
8294 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008295 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296{
8297 char_u *argp;
8298 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008299 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 int argcount = 0; /* number of arguments found */
8301
8302 /*
8303 * Get the arguments.
8304 */
8305 argp = *arg;
8306 while (argcount < MAX_FUNC_ARGS)
8307 {
8308 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8309 if (*argp == ')' || *argp == ',' || *argp == NUL)
8310 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8312 {
8313 ret = FAIL;
8314 break;
8315 }
8316 ++argcount;
8317 if (*argp != ',')
8318 break;
8319 }
8320 if (*argp == ')')
8321 ++argp;
8322 else
8323 ret = FAIL;
8324
8325 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008326 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008327 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008329 {
8330 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008331 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008332 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008333 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335
8336 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008337 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338
8339 *arg = skipwhite(argp);
8340 return ret;
8341}
8342
8343
8344/*
8345 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008346 * Return OK when the function can't be called, FAIL otherwise.
8347 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 */
8349 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008350call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008351 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008352 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008354 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008356 typval_T *argvars; /* vars for arguments, must have "argcount"
8357 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 linenr_T firstline; /* first line of range */
8359 linenr_T lastline; /* last line of range */
8360 int *doesrange; /* return: function handled range */
8361 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008362 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363{
8364 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365#define ERROR_UNKNOWN 0
8366#define ERROR_TOOMANY 1
8367#define ERROR_TOOFEW 2
8368#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008369#define ERROR_DICT 4
8370#define ERROR_NONE 5
8371#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 int error = ERROR_NONE;
8373 int i;
8374 int llen;
8375 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376#define FLEN_FIXED 40
8377 char_u fname_buf[FLEN_FIXED + 1];
8378 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008379 char_u *name;
8380
8381 /* Make a copy of the name, if it comes from a funcref variable it could
8382 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008383 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008384 if (name == NULL)
8385 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386
8387 /*
8388 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8389 * Change <SNR>123_name() to K_SNR 123_name().
8390 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 llen = eval_fname_script(name);
8393 if (llen > 0)
8394 {
8395 fname_buf[0] = K_SPECIAL;
8396 fname_buf[1] = KS_EXTRA;
8397 fname_buf[2] = (int)KE_SNR;
8398 i = 3;
8399 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8400 {
8401 if (current_SID <= 0)
8402 error = ERROR_SCRIPT;
8403 else
8404 {
8405 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8406 i = (int)STRLEN(fname_buf);
8407 }
8408 }
8409 if (i + STRLEN(name + llen) < FLEN_FIXED)
8410 {
8411 STRCPY(fname_buf + i, name + llen);
8412 fname = fname_buf;
8413 }
8414 else
8415 {
8416 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8417 if (fname == NULL)
8418 error = ERROR_OTHER;
8419 else
8420 {
8421 mch_memmove(fname, fname_buf, (size_t)i);
8422 STRCPY(fname + i, name + llen);
8423 }
8424 }
8425 }
8426 else
8427 fname = name;
8428
8429 *doesrange = FALSE;
8430
8431
8432 /* execute the function if no errors detected and executing */
8433 if (evaluate && error == ERROR_NONE)
8434 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008435 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8436 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 error = ERROR_UNKNOWN;
8438
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008439 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 {
8441 /*
8442 * User defined function.
8443 */
8444 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008445
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008447 /* Trigger FuncUndefined event, may load the function. */
8448 if (fp == NULL
8449 && apply_autocmds(EVENT_FUNCUNDEFINED,
8450 fname, fname, TRUE, NULL)
8451 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008453 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 fp = find_func(fname);
8455 }
8456#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008457 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008458 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008459 {
8460 /* loaded a package, search for the function again */
8461 fp = find_func(fname);
8462 }
8463
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 if (fp != NULL)
8465 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008466 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008468 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008470 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008472 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008473 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 else
8475 {
8476 /*
8477 * Call the user function.
8478 * Save and restore search patterns, script variables and
8479 * redo buffer.
8480 */
8481 save_search_patterns();
8482 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008483 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008484 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008485 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008486 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8487 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8488 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008489 /* Function was unreferenced while being used, free it
8490 * now. */
8491 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 restoreRedobuff();
8493 restore_search_patterns();
8494 error = ERROR_NONE;
8495 }
8496 }
8497 }
8498 else
8499 {
8500 /*
8501 * Find the function name in the table, call its implementation.
8502 */
8503 i = find_internal_func(fname);
8504 if (i >= 0)
8505 {
8506 if (argcount < functions[i].f_min_argc)
8507 error = ERROR_TOOFEW;
8508 else if (argcount > functions[i].f_max_argc)
8509 error = ERROR_TOOMANY;
8510 else
8511 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008512 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008513 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 error = ERROR_NONE;
8515 }
8516 }
8517 }
8518 /*
8519 * The function call (or "FuncUndefined" autocommand sequence) might
8520 * have been aborted by an error, an interrupt, or an explicitly thrown
8521 * exception that has not been caught so far. This situation can be
8522 * tested for by calling aborting(). For an error in an internal
8523 * function or for the "E132" error in call_user_func(), however, the
8524 * throw point at which the "force_abort" flag (temporarily reset by
8525 * emsg()) is normally updated has not been reached yet. We need to
8526 * update that flag first to make aborting() reliable.
8527 */
8528 update_force_abort();
8529 }
8530 if (error == ERROR_NONE)
8531 ret = OK;
8532
8533 /*
8534 * Report an error unless the argument evaluation or function call has been
8535 * cancelled due to an aborting error, an interrupt, or an exception.
8536 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008537 if (!aborting())
8538 {
8539 switch (error)
8540 {
8541 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008542 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008543 break;
8544 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008545 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008546 break;
8547 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008548 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008549 name);
8550 break;
8551 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008552 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008553 name);
8554 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008555 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008556 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008557 name);
8558 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008559 }
8560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 if (fname != name && fname != fname_buf)
8563 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008564 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565
8566 return ret;
8567}
8568
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008569/*
8570 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008571 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008572 */
8573 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008574emsg_funcname(ermsg, name)
8575 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008576 char_u *name;
8577{
8578 char_u *p;
8579
8580 if (*name == K_SPECIAL)
8581 p = concat_str((char_u *)"<SNR>", name + 3);
8582 else
8583 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008584 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008585 if (p != name)
8586 vim_free(p);
8587}
8588
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008589/*
8590 * Return TRUE for a non-zero Number and a non-empty String.
8591 */
8592 static int
8593non_zero_arg(argvars)
8594 typval_T *argvars;
8595{
8596 return ((argvars[0].v_type == VAR_NUMBER
8597 && argvars[0].vval.v_number != 0)
8598 || (argvars[0].v_type == VAR_STRING
8599 && argvars[0].vval.v_string != NULL
8600 && *argvars[0].vval.v_string != NUL));
8601}
8602
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603/*********************************************
8604 * Implementation of the built-in functions
8605 */
8606
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008607#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008608static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8609
8610/*
8611 * Get the float value of "argvars[0]" into "f".
8612 * Returns FAIL when the argument is not a Number or Float.
8613 */
8614 static int
8615get_float_arg(argvars, f)
8616 typval_T *argvars;
8617 float_T *f;
8618{
8619 if (argvars[0].v_type == VAR_FLOAT)
8620 {
8621 *f = argvars[0].vval.v_float;
8622 return OK;
8623 }
8624 if (argvars[0].v_type == VAR_NUMBER)
8625 {
8626 *f = (float_T)argvars[0].vval.v_number;
8627 return OK;
8628 }
8629 EMSG(_("E808: Number or Float required"));
8630 return FAIL;
8631}
8632
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008633/*
8634 * "abs(expr)" function
8635 */
8636 static void
8637f_abs(argvars, rettv)
8638 typval_T *argvars;
8639 typval_T *rettv;
8640{
8641 if (argvars[0].v_type == VAR_FLOAT)
8642 {
8643 rettv->v_type = VAR_FLOAT;
8644 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8645 }
8646 else
8647 {
8648 varnumber_T n;
8649 int error = FALSE;
8650
8651 n = get_tv_number_chk(&argvars[0], &error);
8652 if (error)
8653 rettv->vval.v_number = -1;
8654 else if (n > 0)
8655 rettv->vval.v_number = n;
8656 else
8657 rettv->vval.v_number = -n;
8658 }
8659}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008660
8661/*
8662 * "acos()" function
8663 */
8664 static void
8665f_acos(argvars, rettv)
8666 typval_T *argvars;
8667 typval_T *rettv;
8668{
8669 float_T f;
8670
8671 rettv->v_type = VAR_FLOAT;
8672 if (get_float_arg(argvars, &f) == OK)
8673 rettv->vval.v_float = acos(f);
8674 else
8675 rettv->vval.v_float = 0.0;
8676}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008677#endif
8678
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008680 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 */
8682 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008683f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008684 typval_T *argvars;
8685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686{
Bram Moolenaar33570922005-01-25 22:26:29 +00008687 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008689 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008690 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008692 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008693 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008694 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008695 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008696 }
8697 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008698 EMSG(_(e_listreq));
8699}
8700
8701/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008702 * "and(expr, expr)" function
8703 */
8704 static void
8705f_and(argvars, rettv)
8706 typval_T *argvars;
8707 typval_T *rettv;
8708{
8709 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8710 & get_tv_number_chk(&argvars[1], NULL);
8711}
8712
8713/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008714 * "append(lnum, string/list)" function
8715 */
8716 static void
8717f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008718 typval_T *argvars;
8719 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008720{
8721 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008722 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008723 list_T *l = NULL;
8724 listitem_T *li = NULL;
8725 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008726 long added = 0;
8727
Bram Moolenaar0d660222005-01-07 21:51:51 +00008728 lnum = get_tv_lnum(argvars);
8729 if (lnum >= 0
8730 && lnum <= curbuf->b_ml.ml_line_count
8731 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008732 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008733 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008734 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008735 l = argvars[1].vval.v_list;
8736 if (l == NULL)
8737 return;
8738 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008739 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008740 for (;;)
8741 {
8742 if (l == NULL)
8743 tv = &argvars[1]; /* append a string */
8744 else if (li == NULL)
8745 break; /* end of list */
8746 else
8747 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008748 line = get_tv_string_chk(tv);
8749 if (line == NULL) /* type error */
8750 {
8751 rettv->vval.v_number = 1; /* Failed */
8752 break;
8753 }
8754 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008755 ++added;
8756 if (l == NULL)
8757 break;
8758 li = li->li_next;
8759 }
8760
8761 appended_lines_mark(lnum, added);
8762 if (curwin->w_cursor.lnum > lnum)
8763 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008765 else
8766 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767}
8768
8769/*
8770 * "argc()" function
8771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008773f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008774 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008777 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778}
8779
8780/*
8781 * "argidx()" function
8782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008784f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008785 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008788 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789}
8790
8791/*
8792 * "argv(nr)" function
8793 */
8794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008795f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008796 typval_T *argvars;
8797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798{
8799 int idx;
8800
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008801 if (argvars[0].v_type != VAR_UNKNOWN)
8802 {
8803 idx = get_tv_number_chk(&argvars[0], NULL);
8804 if (idx >= 0 && idx < ARGCOUNT)
8805 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8806 else
8807 rettv->vval.v_string = NULL;
8808 rettv->v_type = VAR_STRING;
8809 }
8810 else if (rettv_list_alloc(rettv) == OK)
8811 for (idx = 0; idx < ARGCOUNT; ++idx)
8812 list_append_string(rettv->vval.v_list,
8813 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814}
8815
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008816#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008817/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008818 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008819 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008820 static void
8821f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008822 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008823 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008824{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008825 float_T f;
8826
8827 rettv->v_type = VAR_FLOAT;
8828 if (get_float_arg(argvars, &f) == OK)
8829 rettv->vval.v_float = asin(f);
8830 else
8831 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008832}
8833
8834/*
8835 * "atan()" function
8836 */
8837 static void
8838f_atan(argvars, rettv)
8839 typval_T *argvars;
8840 typval_T *rettv;
8841{
8842 float_T f;
8843
8844 rettv->v_type = VAR_FLOAT;
8845 if (get_float_arg(argvars, &f) == OK)
8846 rettv->vval.v_float = atan(f);
8847 else
8848 rettv->vval.v_float = 0.0;
8849}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008850
8851/*
8852 * "atan2()" function
8853 */
8854 static void
8855f_atan2(argvars, rettv)
8856 typval_T *argvars;
8857 typval_T *rettv;
8858{
8859 float_T fx, fy;
8860
8861 rettv->v_type = VAR_FLOAT;
8862 if (get_float_arg(argvars, &fx) == OK
8863 && get_float_arg(&argvars[1], &fy) == OK)
8864 rettv->vval.v_float = atan2(fx, fy);
8865 else
8866 rettv->vval.v_float = 0.0;
8867}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008868#endif
8869
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870/*
8871 * "browse(save, title, initdir, default)" function
8872 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008874f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008875 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877{
8878#ifdef FEAT_BROWSE
8879 int save;
8880 char_u *title;
8881 char_u *initdir;
8882 char_u *defname;
8883 char_u buf[NUMBUFLEN];
8884 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008885 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008887 save = get_tv_number_chk(&argvars[0], &error);
8888 title = get_tv_string_chk(&argvars[1]);
8889 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8890 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008892 if (error || title == NULL || initdir == NULL || defname == NULL)
8893 rettv->vval.v_string = NULL;
8894 else
8895 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008896 do_browse(save ? BROWSE_SAVE : 0,
8897 title, defname, NULL, initdir, NULL, curbuf);
8898#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008899 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008900#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008901 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008902}
8903
8904/*
8905 * "browsedir(title, initdir)" function
8906 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008908f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008909 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008910 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008911{
8912#ifdef FEAT_BROWSE
8913 char_u *title;
8914 char_u *initdir;
8915 char_u buf[NUMBUFLEN];
8916
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008917 title = get_tv_string_chk(&argvars[0]);
8918 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008919
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008920 if (title == NULL || initdir == NULL)
8921 rettv->vval.v_string = NULL;
8922 else
8923 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008924 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008926 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008928 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929}
8930
Bram Moolenaar33570922005-01-25 22:26:29 +00008931static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008932
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933/*
8934 * Find a buffer by number or exact name.
8935 */
8936 static buf_T *
8937find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008938 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939{
8940 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008942 if (avar->v_type == VAR_NUMBER)
8943 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008944 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008946 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008947 if (buf == NULL)
8948 {
8949 /* No full path name match, try a match with a URL or a "nofile"
8950 * buffer, these don't use the full path. */
8951 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8952 if (buf->b_fname != NULL
8953 && (path_with_url(buf->b_fname)
8954#ifdef FEAT_QUICKFIX
8955 || bt_nofile(buf)
8956#endif
8957 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008958 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008959 break;
8960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 }
8962 return buf;
8963}
8964
8965/*
8966 * "bufexists(expr)" function
8967 */
8968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008970 typval_T *argvars;
8971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008973 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974}
8975
8976/*
8977 * "buflisted(expr)" function
8978 */
8979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008980f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008981 typval_T *argvars;
8982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983{
8984 buf_T *buf;
8985
8986 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008987 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988}
8989
8990/*
8991 * "bufloaded(expr)" function
8992 */
8993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008994f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008995 typval_T *argvars;
8996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997{
8998 buf_T *buf;
8999
9000 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009001 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002}
9003
Bram Moolenaar33570922005-01-25 22:26:29 +00009004static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009005
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006/*
9007 * Get buffer by number or pattern.
9008 */
9009 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009010get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009011 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009013 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 int save_magic;
9015 char_u *save_cpo;
9016 buf_T *buf;
9017
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009018 if (tv->v_type == VAR_NUMBER)
9019 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009020 if (tv->v_type != VAR_STRING)
9021 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 if (name == NULL || *name == NUL)
9023 return curbuf;
9024 if (name[0] == '$' && name[1] == NUL)
9025 return lastbuf;
9026
9027 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9028 save_magic = p_magic;
9029 p_magic = TRUE;
9030 save_cpo = p_cpo;
9031 p_cpo = (char_u *)"";
9032
9033 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
9034 TRUE, FALSE));
9035
9036 p_magic = save_magic;
9037 p_cpo = save_cpo;
9038
9039 /* If not found, try expanding the name, like done for bufexists(). */
9040 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042
9043 return buf;
9044}
9045
9046/*
9047 * "bufname(expr)" function
9048 */
9049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009050f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009051 typval_T *argvars;
9052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053{
9054 buf_T *buf;
9055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009056 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009058 buf = get_buf_tv(&argvars[0]);
9059 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009063 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 --emsg_off;
9065}
9066
9067/*
9068 * "bufnr(expr)" function
9069 */
9070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009072 typval_T *argvars;
9073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074{
9075 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009076 int error = FALSE;
9077 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009079 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009081 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009082 --emsg_off;
9083
9084 /* If the buffer isn't found and the second argument is not zero create a
9085 * new buffer. */
9086 if (buf == NULL
9087 && argvars[1].v_type != VAR_UNKNOWN
9088 && get_tv_number_chk(&argvars[1], &error) != 0
9089 && !error
9090 && (name = get_tv_string_chk(&argvars[0])) != NULL
9091 && !error)
9092 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9093
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009095 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009097 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098}
9099
9100/*
9101 * "bufwinnr(nr)" function
9102 */
9103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009104f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009105 typval_T *argvars;
9106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107{
9108#ifdef FEAT_WINDOWS
9109 win_T *wp;
9110 int winnr = 0;
9111#endif
9112 buf_T *buf;
9113
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009114 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009116 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117#ifdef FEAT_WINDOWS
9118 for (wp = firstwin; wp; wp = wp->w_next)
9119 {
9120 ++winnr;
9121 if (wp->w_buffer == buf)
9122 break;
9123 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009124 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009126 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127#endif
9128 --emsg_off;
9129}
9130
9131/*
9132 * "byte2line(byte)" function
9133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009135f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009136 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138{
9139#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009140 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141#else
9142 long boff = 0;
9143
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009144 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009146 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009148 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 (linenr_T)0, &boff);
9150#endif
9151}
9152
9153/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009154 * "byteidx()" function
9155 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009157f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009158 typval_T *argvars;
9159 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009160{
9161#ifdef FEAT_MBYTE
9162 char_u *t;
9163#endif
9164 char_u *str;
9165 long idx;
9166
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009167 str = get_tv_string_chk(&argvars[0]);
9168 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009169 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009170 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009171 return;
9172
9173#ifdef FEAT_MBYTE
9174 t = str;
9175 for ( ; idx > 0; idx--)
9176 {
9177 if (*t == NUL) /* EOL reached */
9178 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009179 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009180 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009181 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009182#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009183 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009185#endif
9186}
9187
Bram Moolenaardb913952012-06-29 12:54:53 +02009188 int
9189func_call(name, args, selfdict, rettv)
9190 char_u *name;
9191 typval_T *args;
9192 dict_T *selfdict;
9193 typval_T *rettv;
9194{
9195 listitem_T *item;
9196 typval_T argv[MAX_FUNC_ARGS + 1];
9197 int argc = 0;
9198 int dummy;
9199 int r = 0;
9200
9201 for (item = args->vval.v_list->lv_first; item != NULL;
9202 item = item->li_next)
9203 {
9204 if (argc == MAX_FUNC_ARGS)
9205 {
9206 EMSG(_("E699: Too many arguments"));
9207 break;
9208 }
9209 /* Make a copy of each argument. This is needed to be able to set
9210 * v_lock to VAR_FIXED in the copy without changing the original list.
9211 */
9212 copy_tv(&item->li_tv, &argv[argc++]);
9213 }
9214
9215 if (item == NULL)
9216 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9217 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9218 &dummy, TRUE, selfdict);
9219
9220 /* Free the arguments. */
9221 while (argc > 0)
9222 clear_tv(&argv[--argc]);
9223
9224 return r;
9225}
9226
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009227/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009228 * "call(func, arglist)" function
9229 */
9230 static void
9231f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009232 typval_T *argvars;
9233 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009234{
9235 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009236 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009237
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009238 if (argvars[1].v_type != VAR_LIST)
9239 {
9240 EMSG(_(e_listreq));
9241 return;
9242 }
9243 if (argvars[1].vval.v_list == NULL)
9244 return;
9245
9246 if (argvars[0].v_type == VAR_FUNC)
9247 func = argvars[0].vval.v_string;
9248 else
9249 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009250 if (*func == NUL)
9251 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009252
Bram Moolenaare9a41262005-01-15 22:18:47 +00009253 if (argvars[2].v_type != VAR_UNKNOWN)
9254 {
9255 if (argvars[2].v_type != VAR_DICT)
9256 {
9257 EMSG(_(e_dictreq));
9258 return;
9259 }
9260 selfdict = argvars[2].vval.v_dict;
9261 }
9262
Bram Moolenaardb913952012-06-29 12:54:53 +02009263 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009264}
9265
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009266#ifdef FEAT_FLOAT
9267/*
9268 * "ceil({float})" function
9269 */
9270 static void
9271f_ceil(argvars, rettv)
9272 typval_T *argvars;
9273 typval_T *rettv;
9274{
9275 float_T f;
9276
9277 rettv->v_type = VAR_FLOAT;
9278 if (get_float_arg(argvars, &f) == OK)
9279 rettv->vval.v_float = ceil(f);
9280 else
9281 rettv->vval.v_float = 0.0;
9282}
9283#endif
9284
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009285/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009286 * "changenr()" function
9287 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009288 static void
9289f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009290 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009291 typval_T *rettv;
9292{
9293 rettv->vval.v_number = curbuf->b_u_seq_cur;
9294}
9295
9296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 * "char2nr(string)" function
9298 */
9299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009300f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009301 typval_T *argvars;
9302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303{
9304#ifdef FEAT_MBYTE
9305 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009306 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 else
9308#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009309 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310}
9311
9312/*
9313 * "cindent(lnum)" function
9314 */
9315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009316f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009317 typval_T *argvars;
9318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319{
9320#ifdef FEAT_CINDENT
9321 pos_T pos;
9322 linenr_T lnum;
9323
9324 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009325 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9327 {
9328 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009329 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 curwin->w_cursor = pos;
9331 }
9332 else
9333#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009334 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335}
9336
9337/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009338 * "clearmatches()" function
9339 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009340 static void
9341f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009342 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009343 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009344{
9345#ifdef FEAT_SEARCH_EXTRA
9346 clear_matches(curwin);
9347#endif
9348}
9349
9350/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 * "col(string)" function
9352 */
9353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009354f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009355 typval_T *argvars;
9356 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357{
9358 colnr_T col = 0;
9359 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009360 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009362 fp = var2fpos(&argvars[0], FALSE, &fnum);
9363 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 {
9365 if (fp->col == MAXCOL)
9366 {
9367 /* '> can be MAXCOL, get the length of the line then */
9368 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009369 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 else
9371 col = MAXCOL;
9372 }
9373 else
9374 {
9375 col = fp->col + 1;
9376#ifdef FEAT_VIRTUALEDIT
9377 /* col(".") when the cursor is on the NUL at the end of the line
9378 * because of "coladd" can be seen as an extra column. */
9379 if (virtual_active() && fp == &curwin->w_cursor)
9380 {
9381 char_u *p = ml_get_cursor();
9382
9383 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9384 curwin->w_virtcol - curwin->w_cursor.coladd))
9385 {
9386# ifdef FEAT_MBYTE
9387 int l;
9388
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009389 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390 col += l;
9391# else
9392 if (*p != NUL && p[1] == NUL)
9393 ++col;
9394# endif
9395 }
9396 }
9397#endif
9398 }
9399 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009400 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401}
9402
Bram Moolenaar572cb562005-08-05 21:35:02 +00009403#if defined(FEAT_INS_EXPAND)
9404/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009405 * "complete()" function
9406 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009407 static void
9408f_complete(argvars, rettv)
9409 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009410 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009411{
9412 int startcol;
9413
9414 if ((State & INSERT) == 0)
9415 {
9416 EMSG(_("E785: complete() can only be used in Insert mode"));
9417 return;
9418 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009419
9420 /* Check for undo allowed here, because if something was already inserted
9421 * the line was already saved for undo and this check isn't done. */
9422 if (!undo_allowed())
9423 return;
9424
Bram Moolenaarade00832006-03-10 21:46:58 +00009425 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9426 {
9427 EMSG(_(e_invarg));
9428 return;
9429 }
9430
9431 startcol = get_tv_number_chk(&argvars[0], NULL);
9432 if (startcol <= 0)
9433 return;
9434
9435 set_completion(startcol - 1, argvars[1].vval.v_list);
9436}
9437
9438/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009439 * "complete_add()" function
9440 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009441 static void
9442f_complete_add(argvars, rettv)
9443 typval_T *argvars;
9444 typval_T *rettv;
9445{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009446 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009447}
9448
9449/*
9450 * "complete_check()" function
9451 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009452 static void
9453f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009454 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009455 typval_T *rettv;
9456{
9457 int saved = RedrawingDisabled;
9458
9459 RedrawingDisabled = 0;
9460 ins_compl_check_keys(0);
9461 rettv->vval.v_number = compl_interrupted;
9462 RedrawingDisabled = saved;
9463}
9464#endif
9465
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466/*
9467 * "confirm(message, buttons[, default [, type]])" function
9468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009470f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009471 typval_T *argvars UNUSED;
9472 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473{
9474#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9475 char_u *message;
9476 char_u *buttons = NULL;
9477 char_u buf[NUMBUFLEN];
9478 char_u buf2[NUMBUFLEN];
9479 int def = 1;
9480 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009481 char_u *typestr;
9482 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009484 message = get_tv_string_chk(&argvars[0]);
9485 if (message == NULL)
9486 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009487 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009489 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9490 if (buttons == NULL)
9491 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009492 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009494 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009495 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009497 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9498 if (typestr == NULL)
9499 error = TRUE;
9500 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009502 switch (TOUPPER_ASC(*typestr))
9503 {
9504 case 'E': type = VIM_ERROR; break;
9505 case 'Q': type = VIM_QUESTION; break;
9506 case 'I': type = VIM_INFO; break;
9507 case 'W': type = VIM_WARNING; break;
9508 case 'G': type = VIM_GENERIC; break;
9509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 }
9511 }
9512 }
9513 }
9514
9515 if (buttons == NULL || *buttons == NUL)
9516 buttons = (char_u *)_("&Ok");
9517
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009518 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009519 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009520 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521#endif
9522}
9523
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009524/*
9525 * "copy()" function
9526 */
9527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009528f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009529 typval_T *argvars;
9530 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009531{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009532 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009533}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009535#ifdef FEAT_FLOAT
9536/*
9537 * "cos()" function
9538 */
9539 static void
9540f_cos(argvars, rettv)
9541 typval_T *argvars;
9542 typval_T *rettv;
9543{
9544 float_T f;
9545
9546 rettv->v_type = VAR_FLOAT;
9547 if (get_float_arg(argvars, &f) == OK)
9548 rettv->vval.v_float = cos(f);
9549 else
9550 rettv->vval.v_float = 0.0;
9551}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009552
9553/*
9554 * "cosh()" function
9555 */
9556 static void
9557f_cosh(argvars, rettv)
9558 typval_T *argvars;
9559 typval_T *rettv;
9560{
9561 float_T f;
9562
9563 rettv->v_type = VAR_FLOAT;
9564 if (get_float_arg(argvars, &f) == OK)
9565 rettv->vval.v_float = cosh(f);
9566 else
9567 rettv->vval.v_float = 0.0;
9568}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009569#endif
9570
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009572 * "count()" function
9573 */
9574 static void
9575f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009576 typval_T *argvars;
9577 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009578{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009579 long n = 0;
9580 int ic = FALSE;
9581
Bram Moolenaare9a41262005-01-15 22:18:47 +00009582 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009583 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009584 listitem_T *li;
9585 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009586 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009587
Bram Moolenaare9a41262005-01-15 22:18:47 +00009588 if ((l = argvars[0].vval.v_list) != NULL)
9589 {
9590 li = l->lv_first;
9591 if (argvars[2].v_type != VAR_UNKNOWN)
9592 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009593 int error = FALSE;
9594
9595 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009596 if (argvars[3].v_type != VAR_UNKNOWN)
9597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009598 idx = get_tv_number_chk(&argvars[3], &error);
9599 if (!error)
9600 {
9601 li = list_find(l, idx);
9602 if (li == NULL)
9603 EMSGN(_(e_listidx), idx);
9604 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009605 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009606 if (error)
9607 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009608 }
9609
9610 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009611 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009612 ++n;
9613 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009614 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009615 else if (argvars[0].v_type == VAR_DICT)
9616 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009617 int todo;
9618 dict_T *d;
9619 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009620
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009621 if ((d = argvars[0].vval.v_dict) != NULL)
9622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009623 int error = FALSE;
9624
Bram Moolenaare9a41262005-01-15 22:18:47 +00009625 if (argvars[2].v_type != VAR_UNKNOWN)
9626 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009627 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009628 if (argvars[3].v_type != VAR_UNKNOWN)
9629 EMSG(_(e_invarg));
9630 }
9631
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009632 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009633 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009634 {
9635 if (!HASHITEM_EMPTY(hi))
9636 {
9637 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009638 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009639 ++n;
9640 }
9641 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009642 }
9643 }
9644 else
9645 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009646 rettv->vval.v_number = n;
9647}
9648
9649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9651 *
9652 * Checks the existence of a cscope connection.
9653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009656 typval_T *argvars UNUSED;
9657 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658{
9659#ifdef FEAT_CSCOPE
9660 int num = 0;
9661 char_u *dbpath = NULL;
9662 char_u *prepend = NULL;
9663 char_u buf[NUMBUFLEN];
9664
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009665 if (argvars[0].v_type != VAR_UNKNOWN
9666 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009668 num = (int)get_tv_number(&argvars[0]);
9669 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009670 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009671 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 }
9673
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675#endif
9676}
9677
9678/*
9679 * "cursor(lnum, col)" function
9680 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009681 * Moves the cursor to the specified line and column.
9682 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009685f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009686 typval_T *argvars;
9687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688{
9689 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009690#ifdef FEAT_VIRTUALEDIT
9691 long coladd = 0;
9692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009694 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009695 if (argvars[1].v_type == VAR_UNKNOWN)
9696 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009697 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009698
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009699 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009700 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009701 line = pos.lnum;
9702 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009703#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009704 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009705#endif
9706 }
9707 else
9708 {
9709 line = get_tv_lnum(argvars);
9710 col = get_tv_number_chk(&argvars[1], NULL);
9711#ifdef FEAT_VIRTUALEDIT
9712 if (argvars[2].v_type != VAR_UNKNOWN)
9713 coladd = get_tv_number_chk(&argvars[2], NULL);
9714#endif
9715 }
9716 if (line < 0 || col < 0
9717#ifdef FEAT_VIRTUALEDIT
9718 || coladd < 0
9719#endif
9720 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009721 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 if (line > 0)
9723 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 if (col > 0)
9725 curwin->w_cursor.col = col - 1;
9726#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009727 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728#endif
9729
9730 /* Make sure the cursor is in a valid position. */
9731 check_cursor();
9732#ifdef FEAT_MBYTE
9733 /* Correct cursor for multi-byte character. */
9734 if (has_mbyte)
9735 mb_adjust_cursor();
9736#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009737
9738 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009739 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740}
9741
9742/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009743 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 */
9745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009746f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009747 typval_T *argvars;
9748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009750 int noref = 0;
9751
9752 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009753 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009754 if (noref < 0 || noref > 1)
9755 EMSG(_(e_invarg));
9756 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009757 {
9758 current_copyID += COPYID_INC;
9759 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761}
9762
9763/*
9764 * "delete()" function
9765 */
9766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009767f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009768 typval_T *argvars;
9769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
9771 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009772 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775}
9776
9777/*
9778 * "did_filetype()" function
9779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009781f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009782 typval_T *argvars UNUSED;
9783 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784{
9785#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009786 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787#endif
9788}
9789
9790/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009791 * "diff_filler()" function
9792 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009794f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009795 typval_T *argvars UNUSED;
9796 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009797{
9798#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009799 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009800#endif
9801}
9802
9803/*
9804 * "diff_hlID()" function
9805 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009807f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009808 typval_T *argvars UNUSED;
9809 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009810{
9811#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009812 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009813 static linenr_T prev_lnum = 0;
9814 static int changedtick = 0;
9815 static int fnum = 0;
9816 static int change_start = 0;
9817 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009818 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009819 int filler_lines;
9820 int col;
9821
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009822 if (lnum < 0) /* ignore type error in {lnum} arg */
9823 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009824 if (lnum != prev_lnum
9825 || changedtick != curbuf->b_changedtick
9826 || fnum != curbuf->b_fnum)
9827 {
9828 /* New line, buffer, change: need to get the values. */
9829 filler_lines = diff_check(curwin, lnum);
9830 if (filler_lines < 0)
9831 {
9832 if (filler_lines == -1)
9833 {
9834 change_start = MAXCOL;
9835 change_end = -1;
9836 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9837 hlID = HLF_ADD; /* added line */
9838 else
9839 hlID = HLF_CHD; /* changed line */
9840 }
9841 else
9842 hlID = HLF_ADD; /* added line */
9843 }
9844 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009845 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009846 prev_lnum = lnum;
9847 changedtick = curbuf->b_changedtick;
9848 fnum = curbuf->b_fnum;
9849 }
9850
9851 if (hlID == HLF_CHD || hlID == HLF_TXD)
9852 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009853 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009854 if (col >= change_start && col <= change_end)
9855 hlID = HLF_TXD; /* changed text */
9856 else
9857 hlID = HLF_CHD; /* changed line */
9858 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009859 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009860#endif
9861}
9862
9863/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009864 * "empty({expr})" function
9865 */
9866 static void
9867f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009868 typval_T *argvars;
9869 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009870{
9871 int n;
9872
9873 switch (argvars[0].v_type)
9874 {
9875 case VAR_STRING:
9876 case VAR_FUNC:
9877 n = argvars[0].vval.v_string == NULL
9878 || *argvars[0].vval.v_string == NUL;
9879 break;
9880 case VAR_NUMBER:
9881 n = argvars[0].vval.v_number == 0;
9882 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009883#ifdef FEAT_FLOAT
9884 case VAR_FLOAT:
9885 n = argvars[0].vval.v_float == 0.0;
9886 break;
9887#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009888 case VAR_LIST:
9889 n = argvars[0].vval.v_list == NULL
9890 || argvars[0].vval.v_list->lv_first == NULL;
9891 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009892 case VAR_DICT:
9893 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009894 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009895 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009896 default:
9897 EMSG2(_(e_intern2), "f_empty()");
9898 n = 0;
9899 }
9900
9901 rettv->vval.v_number = n;
9902}
9903
9904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 * "escape({string}, {chars})" function
9906 */
9907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009908f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009909 typval_T *argvars;
9910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911{
9912 char_u buf[NUMBUFLEN];
9913
Bram Moolenaar758711c2005-02-02 23:11:38 +00009914 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9915 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009916 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917}
9918
9919/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009920 * "eval()" function
9921 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009922 static void
9923f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009924 typval_T *argvars;
9925 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009926{
9927 char_u *s;
9928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009929 s = get_tv_string_chk(&argvars[0]);
9930 if (s != NULL)
9931 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009933 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9934 {
9935 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009936 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009937 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009938 else if (*s != NUL)
9939 EMSG(_(e_trailing));
9940}
9941
9942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 * "eventhandler()" function
9944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009946f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009947 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009948 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009950 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951}
9952
9953/*
9954 * "executable()" function
9955 */
9956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009957f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009958 typval_T *argvars;
9959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009961 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962}
9963
9964/*
9965 * "exists()" function
9966 */
9967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009968f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009969 typval_T *argvars;
9970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971{
9972 char_u *p;
9973 char_u *name;
9974 int n = FALSE;
9975 int len = 0;
9976
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009977 no_autoload = TRUE;
9978
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009979 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 if (*p == '$') /* environment variable */
9981 {
9982 /* first try "normal" environment variables (fast) */
9983 if (mch_getenv(p + 1) != NULL)
9984 n = TRUE;
9985 else
9986 {
9987 /* try expanding things like $VIM and ${HOME} */
9988 p = expand_env_save(p);
9989 if (p != NULL && *p != '$')
9990 n = TRUE;
9991 vim_free(p);
9992 }
9993 }
9994 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009995 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009996 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009997 if (*skipwhite(p) != NUL)
9998 n = FALSE; /* trailing garbage */
9999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 else if (*p == '*') /* internal or user defined function */
10001 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010002 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 }
10004 else if (*p == ':')
10005 {
10006 n = cmd_exists(p + 1);
10007 }
10008 else if (*p == '#')
10009 {
10010#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010011 if (p[1] == '#')
10012 n = autocmd_supported(p + 2);
10013 else
10014 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015#endif
10016 }
10017 else /* internal variable */
10018 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010019 char_u *tofree;
10020 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010022 /* get_name_len() takes care of expanding curly braces */
10023 name = p;
10024 len = get_name_len(&p, &tofree, TRUE, FALSE);
10025 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010027 if (tofree != NULL)
10028 name = tofree;
10029 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10030 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010032 /* handle d.key, l[idx], f(expr) */
10033 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10034 if (n)
10035 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 }
10037 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010038 if (*p != NUL)
10039 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010041 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 }
10043
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010044 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010045
10046 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047}
10048
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010049#ifdef FEAT_FLOAT
10050/*
10051 * "exp()" function
10052 */
10053 static void
10054f_exp(argvars, rettv)
10055 typval_T *argvars;
10056 typval_T *rettv;
10057{
10058 float_T f;
10059
10060 rettv->v_type = VAR_FLOAT;
10061 if (get_float_arg(argvars, &f) == OK)
10062 rettv->vval.v_float = exp(f);
10063 else
10064 rettv->vval.v_float = 0.0;
10065}
10066#endif
10067
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068/*
10069 * "expand()" function
10070 */
10071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010072f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010073 typval_T *argvars;
10074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075{
10076 char_u *s;
10077 int len;
10078 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010079 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010081 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010082 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010084 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010085 if (argvars[1].v_type != VAR_UNKNOWN
10086 && argvars[2].v_type != VAR_UNKNOWN
10087 && get_tv_number_chk(&argvars[2], &error)
10088 && !error)
10089 {
10090 rettv->v_type = VAR_LIST;
10091 rettv->vval.v_list = NULL;
10092 }
10093
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 if (*s == '%' || *s == '#' || *s == '<')
10096 {
10097 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010098 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010100 if (rettv->v_type == VAR_LIST)
10101 {
10102 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10103 list_append_string(rettv->vval.v_list, result, -1);
10104 else
10105 vim_free(result);
10106 }
10107 else
10108 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 }
10110 else
10111 {
10112 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010113 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010114 if (argvars[1].v_type != VAR_UNKNOWN
10115 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010116 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010117 if (!error)
10118 {
10119 ExpandInit(&xpc);
10120 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010121 if (p_wic)
10122 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010123 if (rettv->v_type == VAR_STRING)
10124 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10125 options, WILD_ALL);
10126 else if (rettv_list_alloc(rettv) != FAIL)
10127 {
10128 int i;
10129
10130 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10131 for (i = 0; i < xpc.xp_numfiles; i++)
10132 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10133 ExpandCleanup(&xpc);
10134 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010135 }
10136 else
10137 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 }
10139}
10140
10141/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010142 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010143 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010144 */
10145 static void
10146f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010147 typval_T *argvars;
10148 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010149{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010150 char *arg_errmsg = N_("extend() argument");
10151
Bram Moolenaare9a41262005-01-15 22:18:47 +000010152 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010153 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010154 list_T *l1, *l2;
10155 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010156 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010157 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010158
Bram Moolenaare9a41262005-01-15 22:18:47 +000010159 l1 = argvars[0].vval.v_list;
10160 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010161 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010162 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010163 {
10164 if (argvars[2].v_type != VAR_UNKNOWN)
10165 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010166 before = get_tv_number_chk(&argvars[2], &error);
10167 if (error)
10168 return; /* type error; errmsg already given */
10169
Bram Moolenaar758711c2005-02-02 23:11:38 +000010170 if (before == l1->lv_len)
10171 item = NULL;
10172 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010173 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010174 item = list_find(l1, before);
10175 if (item == NULL)
10176 {
10177 EMSGN(_(e_listidx), before);
10178 return;
10179 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010180 }
10181 }
10182 else
10183 item = NULL;
10184 list_extend(l1, l2, item);
10185
Bram Moolenaare9a41262005-01-15 22:18:47 +000010186 copy_tv(&argvars[0], rettv);
10187 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010188 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010189 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10190 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010191 dict_T *d1, *d2;
10192 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010193 char_u *action;
10194 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010195 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010196 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010197
10198 d1 = argvars[0].vval.v_dict;
10199 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010200 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010201 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010202 {
10203 /* Check the third argument. */
10204 if (argvars[2].v_type != VAR_UNKNOWN)
10205 {
10206 static char *(av[]) = {"keep", "force", "error"};
10207
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010208 action = get_tv_string_chk(&argvars[2]);
10209 if (action == NULL)
10210 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010211 for (i = 0; i < 3; ++i)
10212 if (STRCMP(action, av[i]) == 0)
10213 break;
10214 if (i == 3)
10215 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010216 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010217 return;
10218 }
10219 }
10220 else
10221 action = (char_u *)"force";
10222
10223 /* Go over all entries in the second dict and add them to the
10224 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010225 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010226 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010227 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010228 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010229 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010230 --todo;
10231 di1 = dict_find(d1, hi2->hi_key, -1);
Bram Moolenaarbdb62052012-07-16 17:31:53 +020010232 if (d1->dv_scope != 0)
10233 {
10234 /* Disallow replacing a builtin function in l: and g:.
10235 * Check the key to be valid when adding to any
10236 * scope. */
10237 if (d1->dv_scope == VAR_DEF_SCOPE
10238 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10239 && var_check_func_name(hi2->hi_key,
10240 di1 == NULL))
10241 break;
10242 if (!valid_varname(hi2->hi_key))
10243 break;
10244 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010245 if (di1 == NULL)
10246 {
10247 di1 = dictitem_copy(HI2DI(hi2));
10248 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10249 dictitem_free(di1);
10250 }
10251 else if (*action == 'e')
10252 {
10253 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10254 break;
10255 }
Bram Moolenaar2fc88022012-05-18 12:07:05 +020010256 else if (*action == 'f' && HI2DI(hi2) != di1)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010257 {
10258 clear_tv(&di1->di_tv);
10259 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10260 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010261 }
10262 }
10263
Bram Moolenaare9a41262005-01-15 22:18:47 +000010264 copy_tv(&argvars[0], rettv);
10265 }
10266 }
10267 else
10268 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010269}
10270
10271/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010272 * "feedkeys()" function
10273 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010274 static void
10275f_feedkeys(argvars, rettv)
10276 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010277 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010278{
10279 int remap = TRUE;
10280 char_u *keys, *flags;
10281 char_u nbuf[NUMBUFLEN];
10282 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010283 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010284
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010285 /* This is not allowed in the sandbox. If the commands would still be
10286 * executed in the sandbox it would be OK, but it probably happens later,
10287 * when "sandbox" is no longer set. */
10288 if (check_secure())
10289 return;
10290
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010291 keys = get_tv_string(&argvars[0]);
10292 if (*keys != NUL)
10293 {
10294 if (argvars[1].v_type != VAR_UNKNOWN)
10295 {
10296 flags = get_tv_string_buf(&argvars[1], nbuf);
10297 for ( ; *flags != NUL; ++flags)
10298 {
10299 switch (*flags)
10300 {
10301 case 'n': remap = FALSE; break;
10302 case 'm': remap = TRUE; break;
10303 case 't': typed = TRUE; break;
10304 }
10305 }
10306 }
10307
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010308 /* Need to escape K_SPECIAL and CSI before putting the string in the
10309 * typeahead buffer. */
10310 keys_esc = vim_strsave_escape_csi(keys);
10311 if (keys_esc != NULL)
10312 {
10313 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010314 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010315 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010316 if (vgetc_busy)
10317 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010318 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010319 }
10320}
10321
10322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 * "filereadable()" function
10324 */
10325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010326f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010327 typval_T *argvars;
10328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010330 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 char_u *p;
10332 int n;
10333
Bram Moolenaarc236c162008-07-13 17:41:49 +000010334#ifndef O_NONBLOCK
10335# define O_NONBLOCK 0
10336#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010337 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010338 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10339 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 {
10341 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010342 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 }
10344 else
10345 n = FALSE;
10346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010347 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348}
10349
10350/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010351 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 * rights to write into.
10353 */
10354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010355f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010356 typval_T *argvars;
10357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010359 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360}
10361
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010362static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010363
10364 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010365findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010366 typval_T *argvars;
10367 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010368 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010369{
10370#ifdef FEAT_SEARCHPATH
10371 char_u *fname;
10372 char_u *fresult = NULL;
10373 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10374 char_u *p;
10375 char_u pathbuf[NUMBUFLEN];
10376 int count = 1;
10377 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010378 int error = FALSE;
10379#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010380
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010381 rettv->vval.v_string = NULL;
10382 rettv->v_type = VAR_STRING;
10383
10384#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010385 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010386
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010387 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010388 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010389 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10390 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010391 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010392 else
10393 {
10394 if (*p != NUL)
10395 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010396
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010397 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010398 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010399 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010400 }
10401
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010402 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10403 error = TRUE;
10404
10405 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010407 do
10408 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010409 if (rettv->v_type == VAR_STRING)
10410 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010411 fresult = find_file_in_path_option(first ? fname : NULL,
10412 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010413 0, first, path,
10414 find_what,
10415 curbuf->b_ffname,
10416 find_what == FINDFILE_DIR
10417 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010418 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010419
10420 if (fresult != NULL && rettv->v_type == VAR_LIST)
10421 list_append_string(rettv->vval.v_list, fresult, -1);
10422
10423 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010424 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010425
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010426 if (rettv->v_type == VAR_STRING)
10427 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010428#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010429}
10430
Bram Moolenaar33570922005-01-25 22:26:29 +000010431static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10432static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010433
10434/*
10435 * Implementation of map() and filter().
10436 */
10437 static void
10438filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010439 typval_T *argvars;
10440 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010441 int map;
10442{
10443 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010444 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010445 listitem_T *li, *nli;
10446 list_T *l = NULL;
10447 dictitem_T *di;
10448 hashtab_T *ht;
10449 hashitem_T *hi;
10450 dict_T *d = NULL;
10451 typval_T save_val;
10452 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010453 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010454 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010455 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10456 char *arg_errmsg = (map ? N_("map() argument")
10457 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010458 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010459 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010460
Bram Moolenaare9a41262005-01-15 22:18:47 +000010461 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010462 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010463 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010464 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010465 return;
10466 }
10467 else if (argvars[0].v_type == VAR_DICT)
10468 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010469 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010470 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010471 return;
10472 }
10473 else
10474 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010475 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010476 return;
10477 }
10478
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010479 expr = get_tv_string_buf_chk(&argvars[1], buf);
10480 /* On type errors, the preceding call has already displayed an error
10481 * message. Avoid a misleading error message for an empty string that
10482 * was not passed as argument. */
10483 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010484 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010485 prepare_vimvar(VV_VAL, &save_val);
10486 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010487
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010488 /* We reset "did_emsg" to be able to detect whether an error
10489 * occurred during evaluation of the expression. */
10490 save_did_emsg = did_emsg;
10491 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010492
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010493 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010494 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010495 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010496 vimvars[VV_KEY].vv_type = VAR_STRING;
10497
10498 ht = &d->dv_hashtab;
10499 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010500 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010501 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010502 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010503 if (!HASHITEM_EMPTY(hi))
10504 {
10505 --todo;
10506 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010507 if (tv_check_lock(di->di_tv.v_lock,
10508 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010509 break;
10510 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010511 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010512 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010513 break;
10514 if (!map && rem)
10515 dictitem_remove(d, di);
10516 clear_tv(&vimvars[VV_KEY].vv_tv);
10517 }
10518 }
10519 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010520 }
10521 else
10522 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010523 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10524
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010525 for (li = l->lv_first; li != NULL; li = nli)
10526 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010527 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010528 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010529 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010530 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010531 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010532 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010533 break;
10534 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010535 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010536 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010537 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010538 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010539
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010540 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010541 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010542
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010543 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010544 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010545
10546 copy_tv(&argvars[0], rettv);
10547}
10548
10549 static int
10550filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010551 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010552 char_u *expr;
10553 int map;
10554 int *remp;
10555{
Bram Moolenaar33570922005-01-25 22:26:29 +000010556 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010557 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010558 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010559
Bram Moolenaar33570922005-01-25 22:26:29 +000010560 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010561 s = expr;
10562 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010563 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010564 if (*s != NUL) /* check for trailing chars after expr */
10565 {
10566 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010567 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010568 }
10569 if (map)
10570 {
10571 /* map(): replace the list item value */
10572 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010573 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010574 *tv = rettv;
10575 }
10576 else
10577 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010578 int error = FALSE;
10579
Bram Moolenaare9a41262005-01-15 22:18:47 +000010580 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010581 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010582 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010583 /* On type error, nothing has been removed; return FAIL to stop the
10584 * loop. The error message was given by get_tv_number_chk(). */
10585 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010586 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010587 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010588 retval = OK;
10589theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010590 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010591 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010592}
10593
10594/*
10595 * "filter()" function
10596 */
10597 static void
10598f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010599 typval_T *argvars;
10600 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010601{
10602 filter_map(argvars, rettv, FALSE);
10603}
10604
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010605/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010606 * "finddir({fname}[, {path}[, {count}]])" function
10607 */
10608 static void
10609f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010610 typval_T *argvars;
10611 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010612{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010613 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010614}
10615
10616/*
10617 * "findfile({fname}[, {path}[, {count}]])" function
10618 */
10619 static void
10620f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010621 typval_T *argvars;
10622 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010623{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010624 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010625}
10626
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010627#ifdef FEAT_FLOAT
10628/*
10629 * "float2nr({float})" function
10630 */
10631 static void
10632f_float2nr(argvars, rettv)
10633 typval_T *argvars;
10634 typval_T *rettv;
10635{
10636 float_T f;
10637
10638 if (get_float_arg(argvars, &f) == OK)
10639 {
10640 if (f < -0x7fffffff)
10641 rettv->vval.v_number = -0x7fffffff;
10642 else if (f > 0x7fffffff)
10643 rettv->vval.v_number = 0x7fffffff;
10644 else
10645 rettv->vval.v_number = (varnumber_T)f;
10646 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010647}
10648
10649/*
10650 * "floor({float})" function
10651 */
10652 static void
10653f_floor(argvars, rettv)
10654 typval_T *argvars;
10655 typval_T *rettv;
10656{
10657 float_T f;
10658
10659 rettv->v_type = VAR_FLOAT;
10660 if (get_float_arg(argvars, &f) == OK)
10661 rettv->vval.v_float = floor(f);
10662 else
10663 rettv->vval.v_float = 0.0;
10664}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010665
10666/*
10667 * "fmod()" function
10668 */
10669 static void
10670f_fmod(argvars, rettv)
10671 typval_T *argvars;
10672 typval_T *rettv;
10673{
10674 float_T fx, fy;
10675
10676 rettv->v_type = VAR_FLOAT;
10677 if (get_float_arg(argvars, &fx) == OK
10678 && get_float_arg(&argvars[1], &fy) == OK)
10679 rettv->vval.v_float = fmod(fx, fy);
10680 else
10681 rettv->vval.v_float = 0.0;
10682}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010683#endif
10684
Bram Moolenaar0d660222005-01-07 21:51:51 +000010685/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010686 * "fnameescape({string})" function
10687 */
10688 static void
10689f_fnameescape(argvars, rettv)
10690 typval_T *argvars;
10691 typval_T *rettv;
10692{
10693 rettv->vval.v_string = vim_strsave_fnameescape(
10694 get_tv_string(&argvars[0]), FALSE);
10695 rettv->v_type = VAR_STRING;
10696}
10697
10698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 * "fnamemodify({fname}, {mods})" function
10700 */
10701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010702f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010703 typval_T *argvars;
10704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705{
10706 char_u *fname;
10707 char_u *mods;
10708 int usedlen = 0;
10709 int len;
10710 char_u *fbuf = NULL;
10711 char_u buf[NUMBUFLEN];
10712
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010713 fname = get_tv_string_chk(&argvars[0]);
10714 mods = get_tv_string_buf_chk(&argvars[1], buf);
10715 if (fname == NULL || mods == NULL)
10716 fname = NULL;
10717 else
10718 {
10719 len = (int)STRLEN(fname);
10720 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010723 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010725 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010727 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 vim_free(fbuf);
10729}
10730
Bram Moolenaar33570922005-01-25 22:26:29 +000010731static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732
10733/*
10734 * "foldclosed()" function
10735 */
10736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010737foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010738 typval_T *argvars;
10739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740 int end;
10741{
10742#ifdef FEAT_FOLDING
10743 linenr_T lnum;
10744 linenr_T first, last;
10745
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010746 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10748 {
10749 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10750 {
10751 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010754 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 return;
10756 }
10757 }
10758#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010759 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760}
10761
10762/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010763 * "foldclosed()" function
10764 */
10765 static void
10766f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010767 typval_T *argvars;
10768 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010769{
10770 foldclosed_both(argvars, rettv, FALSE);
10771}
10772
10773/*
10774 * "foldclosedend()" function
10775 */
10776 static void
10777f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010778 typval_T *argvars;
10779 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010780{
10781 foldclosed_both(argvars, rettv, TRUE);
10782}
10783
10784/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 * "foldlevel()" function
10786 */
10787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010788f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010789 typval_T *argvars;
10790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791{
10792#ifdef FEAT_FOLDING
10793 linenr_T lnum;
10794
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010795 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010797 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799}
10800
10801/*
10802 * "foldtext()" function
10803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010805f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010806 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808{
10809#ifdef FEAT_FOLDING
10810 linenr_T lnum;
10811 char_u *s;
10812 char_u *r;
10813 int len;
10814 char *txt;
10815#endif
10816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010817 rettv->v_type = VAR_STRING;
10818 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010820 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10821 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10822 <= curbuf->b_ml.ml_line_count
10823 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 {
10825 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010826 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10827 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 {
10829 if (!linewhite(lnum))
10830 break;
10831 ++lnum;
10832 }
10833
10834 /* Find interesting text in this line. */
10835 s = skipwhite(ml_get(lnum));
10836 /* skip C comment-start */
10837 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010838 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010840 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010841 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010842 {
10843 s = skipwhite(ml_get(lnum + 1));
10844 if (*s == '*')
10845 s = skipwhite(s + 1);
10846 }
10847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 txt = _("+-%s%3ld lines: ");
10849 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010850 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 + 20 /* for %3ld */
10852 + STRLEN(s))); /* concatenated */
10853 if (r != NULL)
10854 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010855 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10856 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10857 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 len = (int)STRLEN(r);
10859 STRCAT(r, s);
10860 /* remove 'foldmarker' and 'commentstring' */
10861 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010862 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863 }
10864 }
10865#endif
10866}
10867
10868/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010869 * "foldtextresult(lnum)" function
10870 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010872f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010873 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010874 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010875{
10876#ifdef FEAT_FOLDING
10877 linenr_T lnum;
10878 char_u *text;
10879 char_u buf[51];
10880 foldinfo_T foldinfo;
10881 int fold_count;
10882#endif
10883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010884 rettv->v_type = VAR_STRING;
10885 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010886#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010887 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010888 /* treat illegal types and illegal string values for {lnum} the same */
10889 if (lnum < 0)
10890 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010891 fold_count = foldedCount(curwin, lnum, &foldinfo);
10892 if (fold_count > 0)
10893 {
10894 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10895 &foldinfo, buf);
10896 if (text == buf)
10897 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010898 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010899 }
10900#endif
10901}
10902
10903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 * "foreground()" function
10905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010907f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010908 typval_T *argvars UNUSED;
10909 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911#ifdef FEAT_GUI
10912 if (gui.in_use)
10913 gui_mch_set_foreground();
10914#else
10915# ifdef WIN32
10916 win32_set_foreground();
10917# endif
10918#endif
10919}
10920
10921/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010922 * "function()" function
10923 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010925f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010926 typval_T *argvars;
10927 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010928{
10929 char_u *s;
10930
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010931 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010932 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010933 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010934 /* Don't check an autoload name for existence here. */
10935 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010936 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010937 else
10938 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010939 rettv->vval.v_string = vim_strsave(s);
10940 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010941 }
10942}
10943
10944/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010945 * "garbagecollect()" function
10946 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010947 static void
10948f_garbagecollect(argvars, rettv)
10949 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010950 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010951{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010952 /* This is postponed until we are back at the toplevel, because we may be
10953 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10954 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010955
10956 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10957 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010958}
10959
10960/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010961 * "get()" function
10962 */
10963 static void
10964f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010965 typval_T *argvars;
10966 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010967{
Bram Moolenaar33570922005-01-25 22:26:29 +000010968 listitem_T *li;
10969 list_T *l;
10970 dictitem_T *di;
10971 dict_T *d;
10972 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010973
Bram Moolenaare9a41262005-01-15 22:18:47 +000010974 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010975 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010976 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010977 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010978 int error = FALSE;
10979
10980 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10981 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010982 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010983 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010984 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010985 else if (argvars[0].v_type == VAR_DICT)
10986 {
10987 if ((d = argvars[0].vval.v_dict) != NULL)
10988 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010989 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010990 if (di != NULL)
10991 tv = &di->di_tv;
10992 }
10993 }
10994 else
10995 EMSG2(_(e_listdictarg), "get()");
10996
10997 if (tv == NULL)
10998 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010999 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011000 copy_tv(&argvars[2], rettv);
11001 }
11002 else
11003 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011004}
11005
Bram Moolenaar342337a2005-07-21 21:11:17 +000011006static 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 +000011007
11008/*
11009 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011010 * Return a range (from start to end) of lines in rettv from the specified
11011 * buffer.
11012 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011013 */
11014 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011015get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011016 buf_T *buf;
11017 linenr_T start;
11018 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011019 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011020 typval_T *rettv;
11021{
11022 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011023
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011024 if (retlist && rettv_list_alloc(rettv) == FAIL)
11025 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011026
11027 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11028 return;
11029
11030 if (!retlist)
11031 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011032 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11033 p = ml_get_buf(buf, start, FALSE);
11034 else
11035 p = (char_u *)"";
11036
11037 rettv->v_type = VAR_STRING;
11038 rettv->vval.v_string = vim_strsave(p);
11039 }
11040 else
11041 {
11042 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011043 return;
11044
11045 if (start < 1)
11046 start = 1;
11047 if (end > buf->b_ml.ml_line_count)
11048 end = buf->b_ml.ml_line_count;
11049 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011050 if (list_append_string(rettv->vval.v_list,
11051 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011052 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011053 }
11054}
11055
11056/*
11057 * "getbufline()" function
11058 */
11059 static void
11060f_getbufline(argvars, rettv)
11061 typval_T *argvars;
11062 typval_T *rettv;
11063{
11064 linenr_T lnum;
11065 linenr_T end;
11066 buf_T *buf;
11067
11068 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11069 ++emsg_off;
11070 buf = get_buf_tv(&argvars[0]);
11071 --emsg_off;
11072
Bram Moolenaar661b1822005-07-28 22:36:45 +000011073 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011074 if (argvars[2].v_type == VAR_UNKNOWN)
11075 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011076 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011077 end = get_tv_lnum_buf(&argvars[2], buf);
11078
Bram Moolenaar342337a2005-07-21 21:11:17 +000011079 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011080}
11081
Bram Moolenaar0d660222005-01-07 21:51:51 +000011082/*
11083 * "getbufvar()" function
11084 */
11085 static void
11086f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011087 typval_T *argvars;
11088 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011089{
11090 buf_T *buf;
11091 buf_T *save_curbuf;
11092 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011093 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011094
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011095 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11096 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011097 ++emsg_off;
11098 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011099
11100 rettv->v_type = VAR_STRING;
11101 rettv->vval.v_string = NULL;
11102
11103 if (buf != NULL && varname != NULL)
11104 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011105 /* set curbuf to be our buf, temporarily */
11106 save_curbuf = curbuf;
11107 curbuf = buf;
11108
Bram Moolenaar0d660222005-01-07 21:51:51 +000011109 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011110 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010011111 else if (STRCMP(varname, "changedtick") == 0)
11112 {
11113 rettv->v_type = VAR_NUMBER;
11114 rettv->vval.v_number = curbuf->b_changedtick;
11115 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011116 else
11117 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011118 if (*varname == NUL)
11119 /* let getbufvar({nr}, "") return the "b:" dictionary. The
11120 * scope prefix before the NUL byte is required by
11121 * find_var_in_ht(). */
11122 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011123 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000011124 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011125 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011126 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011127 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011128
11129 /* restore previous notion of curbuf */
11130 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011131 }
11132
11133 --emsg_off;
11134}
11135
11136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 * "getchar()" function
11138 */
11139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011141 typval_T *argvars;
11142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143{
11144 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011145 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011147 /* Position the cursor. Needed after a message that ends in a space. */
11148 windgoto(msg_row, msg_col);
11149
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150 ++no_mapping;
11151 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011152 for (;;)
11153 {
11154 if (argvars[0].v_type == VAR_UNKNOWN)
11155 /* getchar(): blocking wait. */
11156 n = safe_vgetc();
11157 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11158 /* getchar(1): only check if char avail */
11159 n = vpeekc();
11160 else if (error || vpeekc() == NUL)
11161 /* illegal argument or getchar(0) and no char avail: return zero */
11162 n = 0;
11163 else
11164 /* getchar(0) and char avail: return char */
11165 n = safe_vgetc();
11166 if (n == K_IGNORE)
11167 continue;
11168 break;
11169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170 --no_mapping;
11171 --allow_keys;
11172
Bram Moolenaar219b8702006-11-01 14:32:36 +000011173 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11174 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11175 vimvars[VV_MOUSE_COL].vv_nr = 0;
11176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011177 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 if (IS_SPECIAL(n) || mod_mask != 0)
11179 {
11180 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11181 int i = 0;
11182
11183 /* Turn a special key into three bytes, plus modifier. */
11184 if (mod_mask != 0)
11185 {
11186 temp[i++] = K_SPECIAL;
11187 temp[i++] = KS_MODIFIER;
11188 temp[i++] = mod_mask;
11189 }
11190 if (IS_SPECIAL(n))
11191 {
11192 temp[i++] = K_SPECIAL;
11193 temp[i++] = K_SECOND(n);
11194 temp[i++] = K_THIRD(n);
11195 }
11196#ifdef FEAT_MBYTE
11197 else if (has_mbyte)
11198 i += (*mb_char2bytes)(n, temp + i);
11199#endif
11200 else
11201 temp[i++] = n;
11202 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011203 rettv->v_type = VAR_STRING;
11204 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011205
11206#ifdef FEAT_MOUSE
11207 if (n == K_LEFTMOUSE
11208 || n == K_LEFTMOUSE_NM
11209 || n == K_LEFTDRAG
11210 || n == K_LEFTRELEASE
11211 || n == K_LEFTRELEASE_NM
11212 || n == K_MIDDLEMOUSE
11213 || n == K_MIDDLEDRAG
11214 || n == K_MIDDLERELEASE
11215 || n == K_RIGHTMOUSE
11216 || n == K_RIGHTDRAG
11217 || n == K_RIGHTRELEASE
11218 || n == K_X1MOUSE
11219 || n == K_X1DRAG
11220 || n == K_X1RELEASE
11221 || n == K_X2MOUSE
11222 || n == K_X2DRAG
11223 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011224 || n == K_MOUSELEFT
11225 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011226 || n == K_MOUSEDOWN
11227 || n == K_MOUSEUP)
11228 {
11229 int row = mouse_row;
11230 int col = mouse_col;
11231 win_T *win;
11232 linenr_T lnum;
11233# ifdef FEAT_WINDOWS
11234 win_T *wp;
11235# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011236 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011237
11238 if (row >= 0 && col >= 0)
11239 {
11240 /* Find the window at the mouse coordinates and compute the
11241 * text position. */
11242 win = mouse_find_win(&row, &col);
11243 (void)mouse_comp_pos(win, &row, &col, &lnum);
11244# ifdef FEAT_WINDOWS
11245 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011246 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011247# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011248 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011249 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11250 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11251 }
11252 }
11253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 }
11255}
11256
11257/*
11258 * "getcharmod()" function
11259 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011261f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011262 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011265 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266}
11267
11268/*
11269 * "getcmdline()" function
11270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011272f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011273 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011274 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011276 rettv->v_type = VAR_STRING;
11277 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278}
11279
11280/*
11281 * "getcmdpos()" function
11282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011284f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011285 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011288 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289}
11290
11291/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011292 * "getcmdtype()" function
11293 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011294 static void
11295f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011296 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011297 typval_T *rettv;
11298{
11299 rettv->v_type = VAR_STRING;
11300 rettv->vval.v_string = alloc(2);
11301 if (rettv->vval.v_string != NULL)
11302 {
11303 rettv->vval.v_string[0] = get_cmdline_type();
11304 rettv->vval.v_string[1] = NUL;
11305 }
11306}
11307
11308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309 * "getcwd()" function
11310 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011312f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011313 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011316 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011319 rettv->vval.v_string = NULL;
11320 cwd = alloc(MAXPATHL);
11321 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011323 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11324 {
11325 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011327 if (rettv->vval.v_string != NULL)
11328 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011330 }
11331 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 }
11333}
11334
11335/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011336 * "getfontname()" function
11337 */
11338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011339f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011340 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011341 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011342{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011343 rettv->v_type = VAR_STRING;
11344 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011345#ifdef FEAT_GUI
11346 if (gui.in_use)
11347 {
11348 GuiFont font;
11349 char_u *name = NULL;
11350
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011351 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011352 {
11353 /* Get the "Normal" font. Either the name saved by
11354 * hl_set_font_name() or from the font ID. */
11355 font = gui.norm_font;
11356 name = hl_get_font_name();
11357 }
11358 else
11359 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011360 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011361 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11362 return;
11363 font = gui_mch_get_font(name, FALSE);
11364 if (font == NOFONT)
11365 return; /* Invalid font name, return empty string. */
11366 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011367 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011368 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011369 gui_mch_free_font(font);
11370 }
11371#endif
11372}
11373
11374/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011375 * "getfperm({fname})" function
11376 */
11377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011378f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011379 typval_T *argvars;
11380 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011381{
11382 char_u *fname;
11383 struct stat st;
11384 char_u *perm = NULL;
11385 char_u flags[] = "rwx";
11386 int i;
11387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011391 if (mch_stat((char *)fname, &st) >= 0)
11392 {
11393 perm = vim_strsave((char_u *)"---------");
11394 if (perm != NULL)
11395 {
11396 for (i = 0; i < 9; i++)
11397 {
11398 if (st.st_mode & (1 << (8 - i)))
11399 perm[i] = flags[i % 3];
11400 }
11401 }
11402 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011403 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011404}
11405
11406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407 * "getfsize({fname})" function
11408 */
11409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011410f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011411 typval_T *argvars;
11412 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413{
11414 char_u *fname;
11415 struct stat st;
11416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011417 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011419 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420
11421 if (mch_stat((char *)fname, &st) >= 0)
11422 {
11423 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011424 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011426 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011427 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011428
11429 /* non-perfect check for overflow */
11430 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11431 rettv->vval.v_number = -2;
11432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 }
11434 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011435 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436}
11437
11438/*
11439 * "getftime({fname})" function
11440 */
11441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011442f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011443 typval_T *argvars;
11444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445{
11446 char_u *fname;
11447 struct stat st;
11448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450
11451 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011452 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011454 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455}
11456
11457/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011458 * "getftype({fname})" function
11459 */
11460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011461f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011462 typval_T *argvars;
11463 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011464{
11465 char_u *fname;
11466 struct stat st;
11467 char_u *type = NULL;
11468 char *t;
11469
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011470 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011473 if (mch_lstat((char *)fname, &st) >= 0)
11474 {
11475#ifdef S_ISREG
11476 if (S_ISREG(st.st_mode))
11477 t = "file";
11478 else if (S_ISDIR(st.st_mode))
11479 t = "dir";
11480# ifdef S_ISLNK
11481 else if (S_ISLNK(st.st_mode))
11482 t = "link";
11483# endif
11484# ifdef S_ISBLK
11485 else if (S_ISBLK(st.st_mode))
11486 t = "bdev";
11487# endif
11488# ifdef S_ISCHR
11489 else if (S_ISCHR(st.st_mode))
11490 t = "cdev";
11491# endif
11492# ifdef S_ISFIFO
11493 else if (S_ISFIFO(st.st_mode))
11494 t = "fifo";
11495# endif
11496# ifdef S_ISSOCK
11497 else if (S_ISSOCK(st.st_mode))
11498 t = "fifo";
11499# endif
11500 else
11501 t = "other";
11502#else
11503# ifdef S_IFMT
11504 switch (st.st_mode & S_IFMT)
11505 {
11506 case S_IFREG: t = "file"; break;
11507 case S_IFDIR: t = "dir"; break;
11508# ifdef S_IFLNK
11509 case S_IFLNK: t = "link"; break;
11510# endif
11511# ifdef S_IFBLK
11512 case S_IFBLK: t = "bdev"; break;
11513# endif
11514# ifdef S_IFCHR
11515 case S_IFCHR: t = "cdev"; break;
11516# endif
11517# ifdef S_IFIFO
11518 case S_IFIFO: t = "fifo"; break;
11519# endif
11520# ifdef S_IFSOCK
11521 case S_IFSOCK: t = "socket"; break;
11522# endif
11523 default: t = "other";
11524 }
11525# else
11526 if (mch_isdir(fname))
11527 t = "dir";
11528 else
11529 t = "file";
11530# endif
11531#endif
11532 type = vim_strsave((char_u *)t);
11533 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011534 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011535}
11536
11537/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011538 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011539 */
11540 static void
11541f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011542 typval_T *argvars;
11543 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011544{
11545 linenr_T lnum;
11546 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011547 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011548
11549 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011550 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011551 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011552 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011553 retlist = FALSE;
11554 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011555 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011556 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011557 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011558 retlist = TRUE;
11559 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011560
Bram Moolenaar342337a2005-07-21 21:11:17 +000011561 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011562}
11563
11564/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011565 * "getmatches()" function
11566 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011567 static void
11568f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011569 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011570 typval_T *rettv;
11571{
11572#ifdef FEAT_SEARCH_EXTRA
11573 dict_T *dict;
11574 matchitem_T *cur = curwin->w_match_head;
11575
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011576 if (rettv_list_alloc(rettv) == OK)
11577 {
11578 while (cur != NULL)
11579 {
11580 dict = dict_alloc();
11581 if (dict == NULL)
11582 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011583 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11584 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11585 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11586 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11587 list_append_dict(rettv->vval.v_list, dict);
11588 cur = cur->next;
11589 }
11590 }
11591#endif
11592}
11593
11594/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011595 * "getpid()" function
11596 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011597 static void
11598f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011599 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011600 typval_T *rettv;
11601{
11602 rettv->vval.v_number = mch_get_pid();
11603}
11604
11605/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011606 * "getpos(string)" function
11607 */
11608 static void
11609f_getpos(argvars, rettv)
11610 typval_T *argvars;
11611 typval_T *rettv;
11612{
11613 pos_T *fp;
11614 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011615 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011616
11617 if (rettv_list_alloc(rettv) == OK)
11618 {
11619 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011620 fp = var2fpos(&argvars[0], TRUE, &fnum);
11621 if (fnum != -1)
11622 list_append_number(l, (varnumber_T)fnum);
11623 else
11624 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011625 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11626 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011627 list_append_number(l, (fp != NULL)
11628 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011629 : (varnumber_T)0);
11630 list_append_number(l,
11631#ifdef FEAT_VIRTUALEDIT
11632 (fp != NULL) ? (varnumber_T)fp->coladd :
11633#endif
11634 (varnumber_T)0);
11635 }
11636 else
11637 rettv->vval.v_number = FALSE;
11638}
11639
11640/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011641 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011642 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011643 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011644f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011645 typval_T *argvars UNUSED;
11646 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011647{
11648#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011649 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011650#endif
11651
Bram Moolenaar2641f772005-03-25 21:58:17 +000011652#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011653 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011654 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011655 wp = NULL;
11656 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11657 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011658 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011659 if (wp == NULL)
11660 return;
11661 }
11662
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011663 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011664 }
11665#endif
11666}
11667
11668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669 * "getreg()" function
11670 */
11671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011672f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011673 typval_T *argvars;
11674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675{
11676 char_u *strregname;
11677 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011678 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011679 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011681 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011683 strregname = get_tv_string_chk(&argvars[0]);
11684 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011685 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011686 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011689 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690 regname = (strregname == NULL ? '"' : *strregname);
11691 if (regname == 0)
11692 regname = '"';
11693
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011694 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011695 rettv->vval.v_string = error ? NULL :
11696 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697}
11698
11699/*
11700 * "getregtype()" function
11701 */
11702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011703f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011704 typval_T *argvars;
11705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706{
11707 char_u *strregname;
11708 int regname;
11709 char_u buf[NUMBUFLEN + 2];
11710 long reglen = 0;
11711
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011712 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011713 {
11714 strregname = get_tv_string_chk(&argvars[0]);
11715 if (strregname == NULL) /* type error; errmsg already given */
11716 {
11717 rettv->v_type = VAR_STRING;
11718 rettv->vval.v_string = NULL;
11719 return;
11720 }
11721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722 else
11723 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011724 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011725
11726 regname = (strregname == NULL ? '"' : *strregname);
11727 if (regname == 0)
11728 regname = '"';
11729
11730 buf[0] = NUL;
11731 buf[1] = NUL;
11732 switch (get_reg_type(regname, &reglen))
11733 {
11734 case MLINE: buf[0] = 'V'; break;
11735 case MCHAR: buf[0] = 'v'; break;
11736#ifdef FEAT_VISUAL
11737 case MBLOCK:
11738 buf[0] = Ctrl_V;
11739 sprintf((char *)buf + 1, "%ld", reglen + 1);
11740 break;
11741#endif
11742 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011743 rettv->v_type = VAR_STRING;
11744 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011745}
11746
11747/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011748 * "gettabvar()" function
11749 */
11750 static void
11751f_gettabvar(argvars, rettv)
11752 typval_T *argvars;
11753 typval_T *rettv;
11754{
11755 tabpage_T *tp;
11756 dictitem_T *v;
11757 char_u *varname;
11758
11759 rettv->v_type = VAR_STRING;
11760 rettv->vval.v_string = NULL;
11761
11762 varname = get_tv_string_chk(&argvars[1]);
11763 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11764 if (tp != NULL && varname != NULL)
11765 {
11766 /* look up the variable */
11767 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11768 if (v != NULL)
11769 copy_tv(&v->di_tv, rettv);
11770 }
11771}
11772
11773/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011774 * "gettabwinvar()" function
11775 */
11776 static void
11777f_gettabwinvar(argvars, rettv)
11778 typval_T *argvars;
11779 typval_T *rettv;
11780{
11781 getwinvar(argvars, rettv, 1);
11782}
11783
11784/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785 * "getwinposx()" function
11786 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011788f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011789 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011792 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793#ifdef FEAT_GUI
11794 if (gui.in_use)
11795 {
11796 int x, y;
11797
11798 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011799 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800 }
11801#endif
11802}
11803
11804/*
11805 * "getwinposy()" function
11806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011808f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011809 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011812 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813#ifdef FEAT_GUI
11814 if (gui.in_use)
11815 {
11816 int x, y;
11817
11818 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011819 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820 }
11821#endif
11822}
11823
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011824/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011825 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011826 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011827 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011828find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011829 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011830 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011831{
11832#ifdef FEAT_WINDOWS
11833 win_T *wp;
11834#endif
11835 int nr;
11836
11837 nr = get_tv_number_chk(vp, NULL);
11838
11839#ifdef FEAT_WINDOWS
11840 if (nr < 0)
11841 return NULL;
11842 if (nr == 0)
11843 return curwin;
11844
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011845 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11846 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011847 if (--nr <= 0)
11848 break;
11849 return wp;
11850#else
11851 if (nr == 0 || nr == 1)
11852 return curwin;
11853 return NULL;
11854#endif
11855}
11856
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857/*
11858 * "getwinvar()" function
11859 */
11860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011861f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011862 typval_T *argvars;
11863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011865 getwinvar(argvars, rettv, 0);
11866}
11867
11868/*
11869 * getwinvar() and gettabwinvar()
11870 */
11871 static void
11872getwinvar(argvars, rettv, off)
11873 typval_T *argvars;
11874 typval_T *rettv;
11875 int off; /* 1 for gettabwinvar() */
11876{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 win_T *win, *oldcurwin;
11878 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011879 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011880 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011882#ifdef FEAT_WINDOWS
11883 if (off == 1)
11884 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11885 else
11886 tp = curtab;
11887#endif
11888 win = find_win_by_nr(&argvars[off], tp);
11889 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011890 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011892 rettv->v_type = VAR_STRING;
11893 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894
11895 if (win != NULL && varname != NULL)
11896 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011897 /* Set curwin to be our win, temporarily. Also set curbuf, so
11898 * that we can get buffer-local options. */
11899 oldcurwin = curwin;
11900 curwin = win;
11901 curbuf = win->w_buffer;
11902
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011904 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905 else
11906 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011907 if (*varname == NUL)
11908 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11909 * scope prefix before the NUL byte is required by
11910 * find_var_in_ht(). */
11911 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011913 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011915 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011917
11918 /* restore previous notion of curwin */
11919 curwin = oldcurwin;
11920 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 }
11922
11923 --emsg_off;
11924}
11925
11926/*
11927 * "glob()" function
11928 */
11929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011930f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011931 typval_T *argvars;
11932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011934 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011936 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011938 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011939 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011940 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011941 if (argvars[1].v_type != VAR_UNKNOWN)
11942 {
11943 if (get_tv_number_chk(&argvars[1], &error))
11944 options |= WILD_KEEP_ALL;
11945 if (argvars[2].v_type != VAR_UNKNOWN
11946 && get_tv_number_chk(&argvars[2], &error))
11947 {
11948 rettv->v_type = VAR_LIST;
11949 rettv->vval.v_list = NULL;
11950 }
11951 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011952 if (!error)
11953 {
11954 ExpandInit(&xpc);
11955 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011956 if (p_wic)
11957 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011958 if (rettv->v_type == VAR_STRING)
11959 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011960 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011961 else if (rettv_list_alloc(rettv) != FAIL)
11962 {
11963 int i;
11964
11965 ExpandOne(&xpc, get_tv_string(&argvars[0]),
11966 NULL, options, WILD_ALL_KEEP);
11967 for (i = 0; i < xpc.xp_numfiles; i++)
11968 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11969
11970 ExpandCleanup(&xpc);
11971 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011972 }
11973 else
11974 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975}
11976
11977/*
11978 * "globpath()" function
11979 */
11980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011981f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011982 typval_T *argvars;
11983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011985 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011987 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011988 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011990 /* When the optional second argument is non-zero, don't remove matches
11991 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11992 if (argvars[2].v_type != VAR_UNKNOWN
11993 && get_tv_number_chk(&argvars[2], &error))
11994 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011995 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011996 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011997 rettv->vval.v_string = NULL;
11998 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011999 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12000 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001}
12002
12003/*
12004 * "has()" function
12005 */
12006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012007f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012008 typval_T *argvars;
12009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010{
12011 int i;
12012 char_u *name;
12013 int n = FALSE;
12014 static char *(has_list[]) =
12015 {
12016#ifdef AMIGA
12017 "amiga",
12018# ifdef FEAT_ARP
12019 "arp",
12020# endif
12021#endif
12022#ifdef __BEOS__
12023 "beos",
12024#endif
12025#ifdef MSDOS
12026# ifdef DJGPP
12027 "dos32",
12028# else
12029 "dos16",
12030# endif
12031#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012032#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033 "mac",
12034#endif
12035#if defined(MACOS_X_UNIX)
12036 "macunix",
12037#endif
12038#ifdef OS2
12039 "os2",
12040#endif
12041#ifdef __QNX__
12042 "qnx",
12043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044#ifdef UNIX
12045 "unix",
12046#endif
12047#ifdef VMS
12048 "vms",
12049#endif
12050#ifdef WIN16
12051 "win16",
12052#endif
12053#ifdef WIN32
12054 "win32",
12055#endif
12056#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12057 "win32unix",
12058#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012059#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060 "win64",
12061#endif
12062#ifdef EBCDIC
12063 "ebcdic",
12064#endif
12065#ifndef CASE_INSENSITIVE_FILENAME
12066 "fname_case",
12067#endif
12068#ifdef FEAT_ARABIC
12069 "arabic",
12070#endif
12071#ifdef FEAT_AUTOCMD
12072 "autocmd",
12073#endif
12074#ifdef FEAT_BEVAL
12075 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012076# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12077 "balloon_multiline",
12078# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079#endif
12080#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12081 "builtin_terms",
12082# ifdef ALL_BUILTIN_TCAPS
12083 "all_builtin_terms",
12084# endif
12085#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012086#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12087 || defined(FEAT_GUI_W32) \
12088 || defined(FEAT_GUI_MOTIF))
12089 "browsefilter",
12090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091#ifdef FEAT_BYTEOFF
12092 "byte_offset",
12093#endif
12094#ifdef FEAT_CINDENT
12095 "cindent",
12096#endif
12097#ifdef FEAT_CLIENTSERVER
12098 "clientserver",
12099#endif
12100#ifdef FEAT_CLIPBOARD
12101 "clipboard",
12102#endif
12103#ifdef FEAT_CMDL_COMPL
12104 "cmdline_compl",
12105#endif
12106#ifdef FEAT_CMDHIST
12107 "cmdline_hist",
12108#endif
12109#ifdef FEAT_COMMENTS
12110 "comments",
12111#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012112#ifdef FEAT_CONCEAL
12113 "conceal",
12114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115#ifdef FEAT_CRYPT
12116 "cryptv",
12117#endif
12118#ifdef FEAT_CSCOPE
12119 "cscope",
12120#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012121#ifdef FEAT_CURSORBIND
12122 "cursorbind",
12123#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012124#ifdef CURSOR_SHAPE
12125 "cursorshape",
12126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127#ifdef DEBUG
12128 "debug",
12129#endif
12130#ifdef FEAT_CON_DIALOG
12131 "dialog_con",
12132#endif
12133#ifdef FEAT_GUI_DIALOG
12134 "dialog_gui",
12135#endif
12136#ifdef FEAT_DIFF
12137 "diff",
12138#endif
12139#ifdef FEAT_DIGRAPHS
12140 "digraphs",
12141#endif
12142#ifdef FEAT_DND
12143 "dnd",
12144#endif
12145#ifdef FEAT_EMACS_TAGS
12146 "emacs_tags",
12147#endif
12148 "eval", /* always present, of course! */
12149#ifdef FEAT_EX_EXTRA
12150 "ex_extra",
12151#endif
12152#ifdef FEAT_SEARCH_EXTRA
12153 "extra_search",
12154#endif
12155#ifdef FEAT_FKMAP
12156 "farsi",
12157#endif
12158#ifdef FEAT_SEARCHPATH
12159 "file_in_path",
12160#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012161#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012162 "filterpipe",
12163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164#ifdef FEAT_FIND_ID
12165 "find_in_path",
12166#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012167#ifdef FEAT_FLOAT
12168 "float",
12169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012170#ifdef FEAT_FOLDING
12171 "folding",
12172#endif
12173#ifdef FEAT_FOOTER
12174 "footer",
12175#endif
12176#if !defined(USE_SYSTEM) && defined(UNIX)
12177 "fork",
12178#endif
12179#ifdef FEAT_GETTEXT
12180 "gettext",
12181#endif
12182#ifdef FEAT_GUI
12183 "gui",
12184#endif
12185#ifdef FEAT_GUI_ATHENA
12186# ifdef FEAT_GUI_NEXTAW
12187 "gui_neXtaw",
12188# else
12189 "gui_athena",
12190# endif
12191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192#ifdef FEAT_GUI_GTK
12193 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012196#ifdef FEAT_GUI_GNOME
12197 "gui_gnome",
12198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199#ifdef FEAT_GUI_MAC
12200 "gui_mac",
12201#endif
12202#ifdef FEAT_GUI_MOTIF
12203 "gui_motif",
12204#endif
12205#ifdef FEAT_GUI_PHOTON
12206 "gui_photon",
12207#endif
12208#ifdef FEAT_GUI_W16
12209 "gui_win16",
12210#endif
12211#ifdef FEAT_GUI_W32
12212 "gui_win32",
12213#endif
12214#ifdef FEAT_HANGULIN
12215 "hangul_input",
12216#endif
12217#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12218 "iconv",
12219#endif
12220#ifdef FEAT_INS_EXPAND
12221 "insert_expand",
12222#endif
12223#ifdef FEAT_JUMPLIST
12224 "jumplist",
12225#endif
12226#ifdef FEAT_KEYMAP
12227 "keymap",
12228#endif
12229#ifdef FEAT_LANGMAP
12230 "langmap",
12231#endif
12232#ifdef FEAT_LIBCALL
12233 "libcall",
12234#endif
12235#ifdef FEAT_LINEBREAK
12236 "linebreak",
12237#endif
12238#ifdef FEAT_LISP
12239 "lispindent",
12240#endif
12241#ifdef FEAT_LISTCMDS
12242 "listcmds",
12243#endif
12244#ifdef FEAT_LOCALMAP
12245 "localmap",
12246#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012247#ifdef FEAT_LUA
12248# ifndef DYNAMIC_LUA
12249 "lua",
12250# endif
12251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252#ifdef FEAT_MENU
12253 "menu",
12254#endif
12255#ifdef FEAT_SESSION
12256 "mksession",
12257#endif
12258#ifdef FEAT_MODIFY_FNAME
12259 "modify_fname",
12260#endif
12261#ifdef FEAT_MOUSE
12262 "mouse",
12263#endif
12264#ifdef FEAT_MOUSESHAPE
12265 "mouseshape",
12266#endif
12267#if defined(UNIX) || defined(VMS)
12268# ifdef FEAT_MOUSE_DEC
12269 "mouse_dec",
12270# endif
12271# ifdef FEAT_MOUSE_GPM
12272 "mouse_gpm",
12273# endif
12274# ifdef FEAT_MOUSE_JSB
12275 "mouse_jsbterm",
12276# endif
12277# ifdef FEAT_MOUSE_NET
12278 "mouse_netterm",
12279# endif
12280# ifdef FEAT_MOUSE_PTERM
12281 "mouse_pterm",
12282# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012283# ifdef FEAT_MOUSE_SGR
12284 "mouse_sgr",
12285# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012286# ifdef FEAT_SYSMOUSE
12287 "mouse_sysmouse",
12288# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012289# ifdef FEAT_MOUSE_URXVT
12290 "mouse_urxvt",
12291# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292# ifdef FEAT_MOUSE_XTERM
12293 "mouse_xterm",
12294# endif
12295#endif
12296#ifdef FEAT_MBYTE
12297 "multi_byte",
12298#endif
12299#ifdef FEAT_MBYTE_IME
12300 "multi_byte_ime",
12301#endif
12302#ifdef FEAT_MULTI_LANG
12303 "multi_lang",
12304#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012305#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012306#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012307 "mzscheme",
12308#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310#ifdef FEAT_OLE
12311 "ole",
12312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313#ifdef FEAT_PATH_EXTRA
12314 "path_extra",
12315#endif
12316#ifdef FEAT_PERL
12317#ifndef DYNAMIC_PERL
12318 "perl",
12319#endif
12320#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012321#ifdef FEAT_PERSISTENT_UNDO
12322 "persistent_undo",
12323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324#ifdef FEAT_PYTHON
12325#ifndef DYNAMIC_PYTHON
12326 "python",
12327#endif
12328#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012329#ifdef FEAT_PYTHON3
12330#ifndef DYNAMIC_PYTHON3
12331 "python3",
12332#endif
12333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334#ifdef FEAT_POSTSCRIPT
12335 "postscript",
12336#endif
12337#ifdef FEAT_PRINTER
12338 "printer",
12339#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012340#ifdef FEAT_PROFILE
12341 "profile",
12342#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012343#ifdef FEAT_RELTIME
12344 "reltime",
12345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012346#ifdef FEAT_QUICKFIX
12347 "quickfix",
12348#endif
12349#ifdef FEAT_RIGHTLEFT
12350 "rightleft",
12351#endif
12352#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12353 "ruby",
12354#endif
12355#ifdef FEAT_SCROLLBIND
12356 "scrollbind",
12357#endif
12358#ifdef FEAT_CMDL_INFO
12359 "showcmd",
12360 "cmdline_info",
12361#endif
12362#ifdef FEAT_SIGNS
12363 "signs",
12364#endif
12365#ifdef FEAT_SMARTINDENT
12366 "smartindent",
12367#endif
12368#ifdef FEAT_SNIFF
12369 "sniff",
12370#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012371#ifdef STARTUPTIME
12372 "startuptime",
12373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374#ifdef FEAT_STL_OPT
12375 "statusline",
12376#endif
12377#ifdef FEAT_SUN_WORKSHOP
12378 "sun_workshop",
12379#endif
12380#ifdef FEAT_NETBEANS_INTG
12381 "netbeans_intg",
12382#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012383#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012384 "spell",
12385#endif
12386#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387 "syntax",
12388#endif
12389#if defined(USE_SYSTEM) || !defined(UNIX)
12390 "system",
12391#endif
12392#ifdef FEAT_TAG_BINS
12393 "tag_binary",
12394#endif
12395#ifdef FEAT_TAG_OLDSTATIC
12396 "tag_old_static",
12397#endif
12398#ifdef FEAT_TAG_ANYWHITE
12399 "tag_any_white",
12400#endif
12401#ifdef FEAT_TCL
12402# ifndef DYNAMIC_TCL
12403 "tcl",
12404# endif
12405#endif
12406#ifdef TERMINFO
12407 "terminfo",
12408#endif
12409#ifdef FEAT_TERMRESPONSE
12410 "termresponse",
12411#endif
12412#ifdef FEAT_TEXTOBJ
12413 "textobjects",
12414#endif
12415#ifdef HAVE_TGETENT
12416 "tgetent",
12417#endif
12418#ifdef FEAT_TITLE
12419 "title",
12420#endif
12421#ifdef FEAT_TOOLBAR
12422 "toolbar",
12423#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012424#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12425 "unnamedplus",
12426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427#ifdef FEAT_USR_CMDS
12428 "user-commands", /* was accidentally included in 5.4 */
12429 "user_commands",
12430#endif
12431#ifdef FEAT_VIMINFO
12432 "viminfo",
12433#endif
12434#ifdef FEAT_VERTSPLIT
12435 "vertsplit",
12436#endif
12437#ifdef FEAT_VIRTUALEDIT
12438 "virtualedit",
12439#endif
12440#ifdef FEAT_VISUAL
12441 "visual",
12442#endif
12443#ifdef FEAT_VISUALEXTRA
12444 "visualextra",
12445#endif
12446#ifdef FEAT_VREPLACE
12447 "vreplace",
12448#endif
12449#ifdef FEAT_WILDIGN
12450 "wildignore",
12451#endif
12452#ifdef FEAT_WILDMENU
12453 "wildmenu",
12454#endif
12455#ifdef FEAT_WINDOWS
12456 "windows",
12457#endif
12458#ifdef FEAT_WAK
12459 "winaltkeys",
12460#endif
12461#ifdef FEAT_WRITEBACKUP
12462 "writebackup",
12463#endif
12464#ifdef FEAT_XIM
12465 "xim",
12466#endif
12467#ifdef FEAT_XFONTSET
12468 "xfontset",
12469#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012470#ifdef FEAT_XPM_W32
12471 "xpm_w32",
12472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473#ifdef USE_XSMP
12474 "xsmp",
12475#endif
12476#ifdef USE_XSMP_INTERACT
12477 "xsmp_interact",
12478#endif
12479#ifdef FEAT_XCLIPBOARD
12480 "xterm_clipboard",
12481#endif
12482#ifdef FEAT_XTERM_SAVE
12483 "xterm_save",
12484#endif
12485#if defined(UNIX) && defined(FEAT_X11)
12486 "X11",
12487#endif
12488 NULL
12489 };
12490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012491 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492 for (i = 0; has_list[i] != NULL; ++i)
12493 if (STRICMP(name, has_list[i]) == 0)
12494 {
12495 n = TRUE;
12496 break;
12497 }
12498
12499 if (n == FALSE)
12500 {
12501 if (STRNICMP(name, "patch", 5) == 0)
12502 n = has_patch(atoi((char *)name + 5));
12503 else if (STRICMP(name, "vim_starting") == 0)
12504 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012505#ifdef FEAT_MBYTE
12506 else if (STRICMP(name, "multi_byte_encoding") == 0)
12507 n = has_mbyte;
12508#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012509#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12510 else if (STRICMP(name, "balloon_multiline") == 0)
12511 n = multiline_balloon_available();
12512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513#ifdef DYNAMIC_TCL
12514 else if (STRICMP(name, "tcl") == 0)
12515 n = tcl_enabled(FALSE);
12516#endif
12517#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12518 else if (STRICMP(name, "iconv") == 0)
12519 n = iconv_enabled(FALSE);
12520#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012521#ifdef DYNAMIC_LUA
12522 else if (STRICMP(name, "lua") == 0)
12523 n = lua_enabled(FALSE);
12524#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012525#ifdef DYNAMIC_MZSCHEME
12526 else if (STRICMP(name, "mzscheme") == 0)
12527 n = mzscheme_enabled(FALSE);
12528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529#ifdef DYNAMIC_RUBY
12530 else if (STRICMP(name, "ruby") == 0)
12531 n = ruby_enabled(FALSE);
12532#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012533#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534#ifdef DYNAMIC_PYTHON
12535 else if (STRICMP(name, "python") == 0)
12536 n = python_enabled(FALSE);
12537#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012538#endif
12539#ifdef FEAT_PYTHON3
12540#ifdef DYNAMIC_PYTHON3
12541 else if (STRICMP(name, "python3") == 0)
12542 n = python3_enabled(FALSE);
12543#endif
12544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545#ifdef DYNAMIC_PERL
12546 else if (STRICMP(name, "perl") == 0)
12547 n = perl_enabled(FALSE);
12548#endif
12549#ifdef FEAT_GUI
12550 else if (STRICMP(name, "gui_running") == 0)
12551 n = (gui.in_use || gui.starting);
12552# ifdef FEAT_GUI_W32
12553 else if (STRICMP(name, "gui_win32s") == 0)
12554 n = gui_is_win32s();
12555# endif
12556# ifdef FEAT_BROWSE
12557 else if (STRICMP(name, "browse") == 0)
12558 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12559# endif
12560#endif
12561#ifdef FEAT_SYN_HL
12562 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012563 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012564#endif
12565#if defined(WIN3264)
12566 else if (STRICMP(name, "win95") == 0)
12567 n = mch_windows95();
12568#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012569#ifdef FEAT_NETBEANS_INTG
12570 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012571 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012573 }
12574
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012575 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576}
12577
12578/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012579 * "has_key()" function
12580 */
12581 static void
12582f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012583 typval_T *argvars;
12584 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012585{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012586 if (argvars[0].v_type != VAR_DICT)
12587 {
12588 EMSG(_(e_dictreq));
12589 return;
12590 }
12591 if (argvars[0].vval.v_dict == NULL)
12592 return;
12593
12594 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012595 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012596}
12597
12598/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012599 * "haslocaldir()" function
12600 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012601 static void
12602f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012603 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012604 typval_T *rettv;
12605{
12606 rettv->vval.v_number = (curwin->w_localdir != NULL);
12607}
12608
12609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012610 * "hasmapto()" function
12611 */
12612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012613f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012614 typval_T *argvars;
12615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012616{
12617 char_u *name;
12618 char_u *mode;
12619 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012620 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012621
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012622 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012623 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624 mode = (char_u *)"nvo";
12625 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012626 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012627 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012628 if (argvars[2].v_type != VAR_UNKNOWN)
12629 abbr = get_tv_number(&argvars[2]);
12630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631
Bram Moolenaar2c932302006-03-18 21:42:09 +000012632 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012633 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012635 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012636}
12637
12638/*
12639 * "histadd()" function
12640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012642f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012643 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645{
12646#ifdef FEAT_CMDHIST
12647 int histype;
12648 char_u *str;
12649 char_u buf[NUMBUFLEN];
12650#endif
12651
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012652 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653 if (check_restricted() || check_secure())
12654 return;
12655#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012656 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12657 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658 if (histype >= 0)
12659 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012660 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661 if (*str != NUL)
12662 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012663 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012664 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012665 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012666 return;
12667 }
12668 }
12669#endif
12670}
12671
12672/*
12673 * "histdel()" function
12674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012676f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012677 typval_T *argvars UNUSED;
12678 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679{
12680#ifdef FEAT_CMDHIST
12681 int n;
12682 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012683 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012685 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12686 if (str == NULL)
12687 n = 0;
12688 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012690 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012691 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012693 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012694 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695 else
12696 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012697 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012698 get_tv_string_buf(&argvars[1], buf));
12699 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700#endif
12701}
12702
12703/*
12704 * "histget()" function
12705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012707f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012708 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710{
12711#ifdef FEAT_CMDHIST
12712 int type;
12713 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012714 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012716 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12717 if (str == NULL)
12718 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012720 {
12721 type = get_histtype(str);
12722 if (argvars[1].v_type == VAR_UNKNOWN)
12723 idx = get_history_idx(type);
12724 else
12725 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12726 /* -1 on type error */
12727 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012730 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012732 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733}
12734
12735/*
12736 * "histnr()" function
12737 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012739f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012740 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742{
12743 int i;
12744
12745#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012746 char_u *history = get_tv_string_chk(&argvars[0]);
12747
12748 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749 if (i >= HIST_CMD && i < HIST_COUNT)
12750 i = get_history_idx(i);
12751 else
12752#endif
12753 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012754 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755}
12756
12757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758 * "highlightID(name)" function
12759 */
12760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012761f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012762 typval_T *argvars;
12763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012765 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766}
12767
12768/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012769 * "highlight_exists()" function
12770 */
12771 static void
12772f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012773 typval_T *argvars;
12774 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012775{
12776 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12777}
12778
12779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780 * "hostname()" function
12781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012783f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012784 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786{
12787 char_u hostname[256];
12788
12789 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012790 rettv->v_type = VAR_STRING;
12791 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792}
12793
12794/*
12795 * iconv() function
12796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012798f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012799 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801{
12802#ifdef FEAT_MBYTE
12803 char_u buf1[NUMBUFLEN];
12804 char_u buf2[NUMBUFLEN];
12805 char_u *from, *to, *str;
12806 vimconv_T vimconv;
12807#endif
12808
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809 rettv->v_type = VAR_STRING;
12810 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811
12812#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012813 str = get_tv_string(&argvars[0]);
12814 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12815 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816 vimconv.vc_type = CONV_NONE;
12817 convert_setup(&vimconv, from, to);
12818
12819 /* If the encodings are equal, no conversion needed. */
12820 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012821 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824
12825 convert_setup(&vimconv, NULL, NULL);
12826 vim_free(from);
12827 vim_free(to);
12828#endif
12829}
12830
12831/*
12832 * "indent()" function
12833 */
12834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012835f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012836 typval_T *argvars;
12837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838{
12839 linenr_T lnum;
12840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012841 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012843 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012846}
12847
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012848/*
12849 * "index()" function
12850 */
12851 static void
12852f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012853 typval_T *argvars;
12854 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012855{
Bram Moolenaar33570922005-01-25 22:26:29 +000012856 list_T *l;
12857 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012858 long idx = 0;
12859 int ic = FALSE;
12860
12861 rettv->vval.v_number = -1;
12862 if (argvars[0].v_type != VAR_LIST)
12863 {
12864 EMSG(_(e_listreq));
12865 return;
12866 }
12867 l = argvars[0].vval.v_list;
12868 if (l != NULL)
12869 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012870 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012871 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012873 int error = FALSE;
12874
Bram Moolenaar758711c2005-02-02 23:11:38 +000012875 /* Start at specified item. Use the cached index that list_find()
12876 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012877 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012878 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012879 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012880 ic = get_tv_number_chk(&argvars[3], &error);
12881 if (error)
12882 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012883 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012884
Bram Moolenaar758711c2005-02-02 23:11:38 +000012885 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012886 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012887 {
12888 rettv->vval.v_number = idx;
12889 break;
12890 }
12891 }
12892}
12893
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894static int inputsecret_flag = 0;
12895
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012896static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12897
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012899 * This function is used by f_input() and f_inputdialog() functions. The third
12900 * argument to f_input() specifies the type of completion to use at the
12901 * prompt. The third argument to f_inputdialog() specifies the value to return
12902 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903 */
12904 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012905get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012906 typval_T *argvars;
12907 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012908 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012910 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911 char_u *p = NULL;
12912 int c;
12913 char_u buf[NUMBUFLEN];
12914 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012915 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012916 int xp_type = EXPAND_NOTHING;
12917 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012919 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012920 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921
12922#ifdef NO_CONSOLE_INPUT
12923 /* While starting up, there is no place to enter text. */
12924 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012926#endif
12927
12928 cmd_silent = FALSE; /* Want to see the prompt. */
12929 if (prompt != NULL)
12930 {
12931 /* Only the part of the message after the last NL is considered as
12932 * prompt for the command line */
12933 p = vim_strrchr(prompt, '\n');
12934 if (p == NULL)
12935 p = prompt;
12936 else
12937 {
12938 ++p;
12939 c = *p;
12940 *p = NUL;
12941 msg_start();
12942 msg_clr_eos();
12943 msg_puts_attr(prompt, echo_attr);
12944 msg_didout = FALSE;
12945 msg_starthere();
12946 *p = c;
12947 }
12948 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012950 if (argvars[1].v_type != VAR_UNKNOWN)
12951 {
12952 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12953 if (defstr != NULL)
12954 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012956 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012957 {
12958 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012959 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012960 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012961
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012962 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000012963 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012964
Bram Moolenaar4463f292005-09-25 22:20:24 +000012965 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12966 if (xp_name == NULL)
12967 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012968
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012969 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012970
Bram Moolenaar4463f292005-09-25 22:20:24 +000012971 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12972 &xp_arg) == FAIL)
12973 return;
12974 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012975 }
12976
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012977 if (defstr != NULL)
12978 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012979 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12980 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020012981 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012982 && argvars[1].v_type != VAR_UNKNOWN
12983 && argvars[2].v_type != VAR_UNKNOWN)
12984 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
12985 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012986
12987 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012989 /* since the user typed this, no need to wait for return */
12990 need_wait_return = FALSE;
12991 msg_didout = FALSE;
12992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993 cmd_silent = cmd_silent_save;
12994}
12995
12996/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012997 * "input()" function
12998 * Also handles inputsecret() when inputsecret is set.
12999 */
13000 static void
13001f_input(argvars, rettv)
13002 typval_T *argvars;
13003 typval_T *rettv;
13004{
13005 get_user_input(argvars, rettv, FALSE);
13006}
13007
13008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013009 * "inputdialog()" function
13010 */
13011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013012f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013013 typval_T *argvars;
13014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013015{
13016#if defined(FEAT_GUI_TEXTDIALOG)
13017 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13018 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13019 {
13020 char_u *message;
13021 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013022 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013024 message = get_tv_string_chk(&argvars[0]);
13025 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013026 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013027 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013028 else
13029 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013030 if (message != NULL && defstr != NULL
13031 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013032 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013033 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034 else
13035 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013036 if (message != NULL && defstr != NULL
13037 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013038 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013039 rettv->vval.v_string = vim_strsave(
13040 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013042 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013044 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045 }
13046 else
13047#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013048 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049}
13050
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013051/*
13052 * "inputlist()" function
13053 */
13054 static void
13055f_inputlist(argvars, rettv)
13056 typval_T *argvars;
13057 typval_T *rettv;
13058{
13059 listitem_T *li;
13060 int selected;
13061 int mouse_used;
13062
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013063#ifdef NO_CONSOLE_INPUT
13064 /* While starting up, there is no place to enter text. */
13065 if (no_console_input())
13066 return;
13067#endif
13068 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13069 {
13070 EMSG2(_(e_listarg), "inputlist()");
13071 return;
13072 }
13073
13074 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013075 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013076 lines_left = Rows; /* avoid more prompt */
13077 msg_scroll = TRUE;
13078 msg_clr_eos();
13079
13080 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13081 {
13082 msg_puts(get_tv_string(&li->li_tv));
13083 msg_putchar('\n');
13084 }
13085
13086 /* Ask for choice. */
13087 selected = prompt_for_number(&mouse_used);
13088 if (mouse_used)
13089 selected -= lines_left;
13090
13091 rettv->vval.v_number = selected;
13092}
13093
13094
Bram Moolenaar071d4272004-06-13 20:20:40 +000013095static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13096
13097/*
13098 * "inputrestore()" function
13099 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013101f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013102 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104{
13105 if (ga_userinput.ga_len > 0)
13106 {
13107 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13109 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013110 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111 }
13112 else if (p_verbose > 1)
13113 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013114 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013115 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116 }
13117}
13118
13119/*
13120 * "inputsave()" function
13121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013123f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013124 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013127 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128 if (ga_grow(&ga_userinput, 1) == OK)
13129 {
13130 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13131 + ga_userinput.ga_len);
13132 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013133 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134 }
13135 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013136 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137}
13138
13139/*
13140 * "inputsecret()" function
13141 */
13142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013143f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013144 typval_T *argvars;
13145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146{
13147 ++cmdline_star;
13148 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013149 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150 --cmdline_star;
13151 --inputsecret_flag;
13152}
13153
13154/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013155 * "insert()" function
13156 */
13157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013158f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013159 typval_T *argvars;
13160 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013161{
13162 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013163 listitem_T *item;
13164 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013165 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013166
13167 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013168 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013169 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013170 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013171 {
13172 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013173 before = get_tv_number_chk(&argvars[2], &error);
13174 if (error)
13175 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013176
Bram Moolenaar758711c2005-02-02 23:11:38 +000013177 if (before == l->lv_len)
13178 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013179 else
13180 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013181 item = list_find(l, before);
13182 if (item == NULL)
13183 {
13184 EMSGN(_(e_listidx), before);
13185 l = NULL;
13186 }
13187 }
13188 if (l != NULL)
13189 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013190 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013191 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013192 }
13193 }
13194}
13195
13196/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013197 * "invert(expr)" function
13198 */
13199 static void
13200f_invert(argvars, rettv)
13201 typval_T *argvars;
13202 typval_T *rettv;
13203{
13204 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13205}
13206
13207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208 * "isdirectory()" function
13209 */
13210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013211f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013212 typval_T *argvars;
13213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013215 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216}
13217
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013218/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013219 * "islocked()" function
13220 */
13221 static void
13222f_islocked(argvars, rettv)
13223 typval_T *argvars;
13224 typval_T *rettv;
13225{
13226 lval_T lv;
13227 char_u *end;
13228 dictitem_T *di;
13229
13230 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013231 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13232 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013233 if (end != NULL && lv.ll_name != NULL)
13234 {
13235 if (*end != NUL)
13236 EMSG(_(e_trailing));
13237 else
13238 {
13239 if (lv.ll_tv == NULL)
13240 {
13241 if (check_changedtick(lv.ll_name))
13242 rettv->vval.v_number = 1; /* always locked */
13243 else
13244 {
13245 di = find_var(lv.ll_name, NULL);
13246 if (di != NULL)
13247 {
13248 /* Consider a variable locked when:
13249 * 1. the variable itself is locked
13250 * 2. the value of the variable is locked.
13251 * 3. the List or Dict value is locked.
13252 */
13253 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13254 || tv_islocked(&di->di_tv));
13255 }
13256 }
13257 }
13258 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013259 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013260 else if (lv.ll_newkey != NULL)
13261 EMSG2(_(e_dictkey), lv.ll_newkey);
13262 else if (lv.ll_list != NULL)
13263 /* List item. */
13264 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13265 else
13266 /* Dictionary item. */
13267 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13268 }
13269 }
13270
13271 clear_lval(&lv);
13272}
13273
Bram Moolenaar33570922005-01-25 22:26:29 +000013274static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013275
13276/*
13277 * Turn a dict into a list:
13278 * "what" == 0: list of keys
13279 * "what" == 1: list of values
13280 * "what" == 2: list of items
13281 */
13282 static void
13283dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013284 typval_T *argvars;
13285 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013286 int what;
13287{
Bram Moolenaar33570922005-01-25 22:26:29 +000013288 list_T *l2;
13289 dictitem_T *di;
13290 hashitem_T *hi;
13291 listitem_T *li;
13292 listitem_T *li2;
13293 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013294 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013295
Bram Moolenaar8c711452005-01-14 21:53:12 +000013296 if (argvars[0].v_type != VAR_DICT)
13297 {
13298 EMSG(_(e_dictreq));
13299 return;
13300 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013301 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013302 return;
13303
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013304 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013305 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013306
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013307 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013308 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013309 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013310 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013311 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013312 --todo;
13313 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013314
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013315 li = listitem_alloc();
13316 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013317 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013318 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013319
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013320 if (what == 0)
13321 {
13322 /* keys() */
13323 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013324 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013325 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13326 }
13327 else if (what == 1)
13328 {
13329 /* values() */
13330 copy_tv(&di->di_tv, &li->li_tv);
13331 }
13332 else
13333 {
13334 /* items() */
13335 l2 = list_alloc();
13336 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013337 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013338 li->li_tv.vval.v_list = l2;
13339 if (l2 == NULL)
13340 break;
13341 ++l2->lv_refcount;
13342
13343 li2 = listitem_alloc();
13344 if (li2 == NULL)
13345 break;
13346 list_append(l2, li2);
13347 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013348 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013349 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13350
13351 li2 = listitem_alloc();
13352 if (li2 == NULL)
13353 break;
13354 list_append(l2, li2);
13355 copy_tv(&di->di_tv, &li2->li_tv);
13356 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013357 }
13358 }
13359}
13360
13361/*
13362 * "items(dict)" function
13363 */
13364 static void
13365f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013366 typval_T *argvars;
13367 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013368{
13369 dict_list(argvars, rettv, 2);
13370}
13371
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013373 * "join()" function
13374 */
13375 static void
13376f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013377 typval_T *argvars;
13378 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013379{
13380 garray_T ga;
13381 char_u *sep;
13382
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013383 if (argvars[0].v_type != VAR_LIST)
13384 {
13385 EMSG(_(e_listreq));
13386 return;
13387 }
13388 if (argvars[0].vval.v_list == NULL)
13389 return;
13390 if (argvars[1].v_type == VAR_UNKNOWN)
13391 sep = (char_u *)" ";
13392 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013393 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013394
13395 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013396
13397 if (sep != NULL)
13398 {
13399 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013400 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013401 ga_append(&ga, NUL);
13402 rettv->vval.v_string = (char_u *)ga.ga_data;
13403 }
13404 else
13405 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013406}
13407
13408/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013409 * "keys()" function
13410 */
13411 static void
13412f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013413 typval_T *argvars;
13414 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013415{
13416 dict_list(argvars, rettv, 0);
13417}
13418
13419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420 * "last_buffer_nr()" function.
13421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013423f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013424 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013425 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426{
13427 int n = 0;
13428 buf_T *buf;
13429
13430 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13431 if (n < buf->b_fnum)
13432 n = buf->b_fnum;
13433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013434 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013435}
13436
13437/*
13438 * "len()" function
13439 */
13440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013441f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013442 typval_T *argvars;
13443 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013444{
13445 switch (argvars[0].v_type)
13446 {
13447 case VAR_STRING:
13448 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013449 rettv->vval.v_number = (varnumber_T)STRLEN(
13450 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013451 break;
13452 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013453 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013454 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013455 case VAR_DICT:
13456 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13457 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013458 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013459 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013460 break;
13461 }
13462}
13463
Bram Moolenaar33570922005-01-25 22:26:29 +000013464static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013465
13466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013467libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013468 typval_T *argvars;
13469 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013470 int type;
13471{
13472#ifdef FEAT_LIBCALL
13473 char_u *string_in;
13474 char_u **string_result;
13475 int nr_result;
13476#endif
13477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013478 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013479 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013480 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013481
13482 if (check_restricted() || check_secure())
13483 return;
13484
13485#ifdef FEAT_LIBCALL
13486 /* The first two args must be strings, otherwise its meaningless */
13487 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13488 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013489 string_in = NULL;
13490 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013491 string_in = argvars[2].vval.v_string;
13492 if (type == VAR_NUMBER)
13493 string_result = NULL;
13494 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013495 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013496 if (mch_libcall(argvars[0].vval.v_string,
13497 argvars[1].vval.v_string,
13498 string_in,
13499 argvars[2].vval.v_number,
13500 string_result,
13501 &nr_result) == OK
13502 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013503 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013504 }
13505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506}
13507
13508/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013509 * "libcall()" function
13510 */
13511 static void
13512f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013513 typval_T *argvars;
13514 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013515{
13516 libcall_common(argvars, rettv, VAR_STRING);
13517}
13518
13519/*
13520 * "libcallnr()" function
13521 */
13522 static void
13523f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013524 typval_T *argvars;
13525 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013526{
13527 libcall_common(argvars, rettv, VAR_NUMBER);
13528}
13529
13530/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 * "line(string)" function
13532 */
13533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013534f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013535 typval_T *argvars;
13536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537{
13538 linenr_T lnum = 0;
13539 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013540 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013542 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543 if (fp != NULL)
13544 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013545 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546}
13547
13548/*
13549 * "line2byte(lnum)" function
13550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013552f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013553 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555{
13556#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013557 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558#else
13559 linenr_T lnum;
13560
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013561 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013563 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013565 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13566 if (rettv->vval.v_number >= 0)
13567 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568#endif
13569}
13570
13571/*
13572 * "lispindent(lnum)" function
13573 */
13574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013575f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013576 typval_T *argvars;
13577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578{
13579#ifdef FEAT_LISP
13580 pos_T pos;
13581 linenr_T lnum;
13582
13583 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013584 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13586 {
13587 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013588 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589 curwin->w_cursor = pos;
13590 }
13591 else
13592#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013593 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594}
13595
13596/*
13597 * "localtime()" function
13598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013600f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013601 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013603{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013604 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605}
13606
Bram Moolenaar33570922005-01-25 22:26:29 +000013607static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608
13609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013610get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013611 typval_T *argvars;
13612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613 int exact;
13614{
13615 char_u *keys;
13616 char_u *which;
13617 char_u buf[NUMBUFLEN];
13618 char_u *keys_buf = NULL;
13619 char_u *rhs;
13620 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013621 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013622 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013623 mapblock_T *mp;
13624 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625
13626 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013627 rettv->v_type = VAR_STRING;
13628 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013630 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631 if (*keys == NUL)
13632 return;
13633
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013634 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013635 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013636 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013637 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013638 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013639 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013640 if (argvars[3].v_type != VAR_UNKNOWN)
13641 get_dict = get_tv_number(&argvars[3]);
13642 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644 else
13645 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013646 if (which == NULL)
13647 return;
13648
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649 mode = get_map_mode(&which, 0);
13650
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013651 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013652 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013654
13655 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013657 /* Return a string. */
13658 if (rhs != NULL)
13659 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660
Bram Moolenaarbd743252010-10-20 21:23:33 +020013661 }
13662 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13663 {
13664 /* Return a dictionary. */
13665 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13666 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13667 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668
Bram Moolenaarbd743252010-10-20 21:23:33 +020013669 dict_add_nr_str(dict, "lhs", 0L, lhs);
13670 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13671 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13672 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13673 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13674 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13675 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13676 dict_add_nr_str(dict, "mode", 0L, mapmode);
13677
13678 vim_free(lhs);
13679 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680 }
13681}
13682
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013683#ifdef FEAT_FLOAT
13684/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013685 * "log()" function
13686 */
13687 static void
13688f_log(argvars, rettv)
13689 typval_T *argvars;
13690 typval_T *rettv;
13691{
13692 float_T f;
13693
13694 rettv->v_type = VAR_FLOAT;
13695 if (get_float_arg(argvars, &f) == OK)
13696 rettv->vval.v_float = log(f);
13697 else
13698 rettv->vval.v_float = 0.0;
13699}
13700
13701/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013702 * "log10()" function
13703 */
13704 static void
13705f_log10(argvars, rettv)
13706 typval_T *argvars;
13707 typval_T *rettv;
13708{
13709 float_T f;
13710
13711 rettv->v_type = VAR_FLOAT;
13712 if (get_float_arg(argvars, &f) == OK)
13713 rettv->vval.v_float = log10(f);
13714 else
13715 rettv->vval.v_float = 0.0;
13716}
13717#endif
13718
Bram Moolenaar1dced572012-04-05 16:54:08 +020013719#ifdef FEAT_LUA
13720/*
13721 * "luaeval()" function
13722 */
13723 static void
13724f_luaeval(argvars, rettv)
13725 typval_T *argvars;
13726 typval_T *rettv;
13727{
13728 char_u *str;
13729 char_u buf[NUMBUFLEN];
13730
13731 str = get_tv_string_buf(&argvars[0], buf);
13732 do_luaeval(str, argvars + 1, rettv);
13733}
13734#endif
13735
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013737 * "map()" function
13738 */
13739 static void
13740f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013741 typval_T *argvars;
13742 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013743{
13744 filter_map(argvars, rettv, TRUE);
13745}
13746
13747/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013748 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749 */
13750 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013751f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013752 typval_T *argvars;
13753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013755 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756}
13757
13758/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013759 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760 */
13761 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013762f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013763 typval_T *argvars;
13764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013766 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767}
13768
Bram Moolenaar33570922005-01-25 22:26:29 +000013769static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770
13771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013772find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013773 typval_T *argvars;
13774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775 int type;
13776{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013777 char_u *str = NULL;
13778 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779 char_u *pat;
13780 regmatch_T regmatch;
13781 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013782 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013783 char_u *save_cpo;
13784 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013785 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013786 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013787 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013788 list_T *l = NULL;
13789 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013790 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013791 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792
13793 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13794 save_cpo = p_cpo;
13795 p_cpo = (char_u *)"";
13796
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013797 rettv->vval.v_number = -1;
13798 if (type == 3)
13799 {
13800 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013801 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013802 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013803 }
13804 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013806 rettv->v_type = VAR_STRING;
13807 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013810 if (argvars[0].v_type == VAR_LIST)
13811 {
13812 if ((l = argvars[0].vval.v_list) == NULL)
13813 goto theend;
13814 li = l->lv_first;
13815 }
13816 else
13817 expr = str = get_tv_string(&argvars[0]);
13818
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013819 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13820 if (pat == NULL)
13821 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013822
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013823 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013825 int error = FALSE;
13826
13827 start = get_tv_number_chk(&argvars[2], &error);
13828 if (error)
13829 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013830 if (l != NULL)
13831 {
13832 li = list_find(l, start);
13833 if (li == NULL)
13834 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013835 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013836 }
13837 else
13838 {
13839 if (start < 0)
13840 start = 0;
13841 if (start > (long)STRLEN(str))
13842 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013843 /* When "count" argument is there ignore matches before "start",
13844 * otherwise skip part of the string. Differs when pattern is "^"
13845 * or "\<". */
13846 if (argvars[3].v_type != VAR_UNKNOWN)
13847 startcol = start;
13848 else
13849 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013850 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013851
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013852 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013853 nth = get_tv_number_chk(&argvars[3], &error);
13854 if (error)
13855 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856 }
13857
13858 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13859 if (regmatch.regprog != NULL)
13860 {
13861 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013862
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013863 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013864 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013865 if (l != NULL)
13866 {
13867 if (li == NULL)
13868 {
13869 match = FALSE;
13870 break;
13871 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013872 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013873 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013874 if (str == NULL)
13875 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013876 }
13877
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013878 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013879
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013880 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013881 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013882 if (l == NULL && !match)
13883 break;
13884
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013885 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013886 if (l != NULL)
13887 {
13888 li = li->li_next;
13889 ++idx;
13890 }
13891 else
13892 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013893#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013894 startcol = (colnr_T)(regmatch.startp[0]
13895 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013896#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013897 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013898#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013899 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013900 }
13901
13902 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013904 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013905 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013906 int i;
13907
13908 /* return list with matched string and submatches */
13909 for (i = 0; i < NSUBEXP; ++i)
13910 {
13911 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013912 {
13913 if (list_append_string(rettv->vval.v_list,
13914 (char_u *)"", 0) == FAIL)
13915 break;
13916 }
13917 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013918 regmatch.startp[i],
13919 (int)(regmatch.endp[i] - regmatch.startp[i]))
13920 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013921 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013922 }
13923 }
13924 else if (type == 2)
13925 {
13926 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013927 if (l != NULL)
13928 copy_tv(&li->li_tv, rettv);
13929 else
13930 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013932 }
13933 else if (l != NULL)
13934 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935 else
13936 {
13937 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013938 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939 (varnumber_T)(regmatch.startp[0] - str);
13940 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013941 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013943 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944 }
13945 }
13946 vim_free(regmatch.regprog);
13947 }
13948
13949theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013950 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 p_cpo = save_cpo;
13952}
13953
13954/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013955 * "match()" function
13956 */
13957 static void
13958f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013959 typval_T *argvars;
13960 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013961{
13962 find_some_match(argvars, rettv, 1);
13963}
13964
13965/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013966 * "matchadd()" function
13967 */
13968 static void
13969f_matchadd(argvars, rettv)
13970 typval_T *argvars;
13971 typval_T *rettv;
13972{
13973#ifdef FEAT_SEARCH_EXTRA
13974 char_u buf[NUMBUFLEN];
13975 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13976 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13977 int prio = 10; /* default priority */
13978 int id = -1;
13979 int error = FALSE;
13980
13981 rettv->vval.v_number = -1;
13982
13983 if (grp == NULL || pat == NULL)
13984 return;
13985 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013986 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013987 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013988 if (argvars[3].v_type != VAR_UNKNOWN)
13989 id = get_tv_number_chk(&argvars[3], &error);
13990 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013991 if (error == TRUE)
13992 return;
13993 if (id >= 1 && id <= 3)
13994 {
13995 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13996 return;
13997 }
13998
13999 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14000#endif
14001}
14002
14003/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014004 * "matcharg()" function
14005 */
14006 static void
14007f_matcharg(argvars, rettv)
14008 typval_T *argvars;
14009 typval_T *rettv;
14010{
14011 if (rettv_list_alloc(rettv) == OK)
14012 {
14013#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014014 int id = get_tv_number(&argvars[0]);
14015 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014016
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014017 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014018 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014019 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14020 {
14021 list_append_string(rettv->vval.v_list,
14022 syn_id2name(m->hlg_id), -1);
14023 list_append_string(rettv->vval.v_list, m->pattern, -1);
14024 }
14025 else
14026 {
14027 list_append_string(rettv->vval.v_list, NUL, -1);
14028 list_append_string(rettv->vval.v_list, NUL, -1);
14029 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014030 }
14031#endif
14032 }
14033}
14034
14035/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014036 * "matchdelete()" function
14037 */
14038 static void
14039f_matchdelete(argvars, rettv)
14040 typval_T *argvars;
14041 typval_T *rettv;
14042{
14043#ifdef FEAT_SEARCH_EXTRA
14044 rettv->vval.v_number = match_delete(curwin,
14045 (int)get_tv_number(&argvars[0]), TRUE);
14046#endif
14047}
14048
14049/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014050 * "matchend()" function
14051 */
14052 static void
14053f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014054 typval_T *argvars;
14055 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014056{
14057 find_some_match(argvars, rettv, 0);
14058}
14059
14060/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014061 * "matchlist()" function
14062 */
14063 static void
14064f_matchlist(argvars, rettv)
14065 typval_T *argvars;
14066 typval_T *rettv;
14067{
14068 find_some_match(argvars, rettv, 3);
14069}
14070
14071/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014072 * "matchstr()" function
14073 */
14074 static void
14075f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014076 typval_T *argvars;
14077 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014078{
14079 find_some_match(argvars, rettv, 2);
14080}
14081
Bram Moolenaar33570922005-01-25 22:26:29 +000014082static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014083
14084 static void
14085max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014086 typval_T *argvars;
14087 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014088 int domax;
14089{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014090 long n = 0;
14091 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014092 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014093
14094 if (argvars[0].v_type == VAR_LIST)
14095 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014096 list_T *l;
14097 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014098
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014099 l = argvars[0].vval.v_list;
14100 if (l != NULL)
14101 {
14102 li = l->lv_first;
14103 if (li != NULL)
14104 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014105 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014106 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014107 {
14108 li = li->li_next;
14109 if (li == NULL)
14110 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014111 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014112 if (domax ? i > n : i < n)
14113 n = i;
14114 }
14115 }
14116 }
14117 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014118 else if (argvars[0].v_type == VAR_DICT)
14119 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014120 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014121 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014122 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014123 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014124
14125 d = argvars[0].vval.v_dict;
14126 if (d != NULL)
14127 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014128 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014129 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014130 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014131 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014132 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014133 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014134 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014135 if (first)
14136 {
14137 n = i;
14138 first = FALSE;
14139 }
14140 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014141 n = i;
14142 }
14143 }
14144 }
14145 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014146 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014147 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014148 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014149}
14150
14151/*
14152 * "max()" function
14153 */
14154 static void
14155f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014156 typval_T *argvars;
14157 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014158{
14159 max_min(argvars, rettv, TRUE);
14160}
14161
14162/*
14163 * "min()" function
14164 */
14165 static void
14166f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014167 typval_T *argvars;
14168 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014169{
14170 max_min(argvars, rettv, FALSE);
14171}
14172
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014173static int mkdir_recurse __ARGS((char_u *dir, int prot));
14174
14175/*
14176 * Create the directory in which "dir" is located, and higher levels when
14177 * needed.
14178 */
14179 static int
14180mkdir_recurse(dir, prot)
14181 char_u *dir;
14182 int prot;
14183{
14184 char_u *p;
14185 char_u *updir;
14186 int r = FAIL;
14187
14188 /* Get end of directory name in "dir".
14189 * We're done when it's "/" or "c:/". */
14190 p = gettail_sep(dir);
14191 if (p <= get_past_head(dir))
14192 return OK;
14193
14194 /* If the directory exists we're done. Otherwise: create it.*/
14195 updir = vim_strnsave(dir, (int)(p - dir));
14196 if (updir == NULL)
14197 return FAIL;
14198 if (mch_isdir(updir))
14199 r = OK;
14200 else if (mkdir_recurse(updir, prot) == OK)
14201 r = vim_mkdir_emsg(updir, prot);
14202 vim_free(updir);
14203 return r;
14204}
14205
14206#ifdef vim_mkdir
14207/*
14208 * "mkdir()" function
14209 */
14210 static void
14211f_mkdir(argvars, rettv)
14212 typval_T *argvars;
14213 typval_T *rettv;
14214{
14215 char_u *dir;
14216 char_u buf[NUMBUFLEN];
14217 int prot = 0755;
14218
14219 rettv->vval.v_number = FAIL;
14220 if (check_restricted() || check_secure())
14221 return;
14222
14223 dir = get_tv_string_buf(&argvars[0], buf);
14224 if (argvars[1].v_type != VAR_UNKNOWN)
14225 {
14226 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014227 prot = get_tv_number_chk(&argvars[2], NULL);
14228 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014229 mkdir_recurse(dir, prot);
14230 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014231 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014232}
14233#endif
14234
Bram Moolenaar0d660222005-01-07 21:51:51 +000014235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236 * "mode()" function
14237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014239f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014240 typval_T *argvars;
14241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014243 char_u buf[3];
14244
14245 buf[1] = NUL;
14246 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247
14248#ifdef FEAT_VISUAL
14249 if (VIsual_active)
14250 {
14251 if (VIsual_select)
14252 buf[0] = VIsual_mode + 's' - 'v';
14253 else
14254 buf[0] = VIsual_mode;
14255 }
14256 else
14257#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014258 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14259 || State == CONFIRM)
14260 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014262 if (State == ASKMORE)
14263 buf[1] = 'm';
14264 else if (State == CONFIRM)
14265 buf[1] = '?';
14266 }
14267 else if (State == EXTERNCMD)
14268 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269 else if (State & INSERT)
14270 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014271#ifdef FEAT_VREPLACE
14272 if (State & VREPLACE_FLAG)
14273 {
14274 buf[0] = 'R';
14275 buf[1] = 'v';
14276 }
14277 else
14278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279 if (State & REPLACE_FLAG)
14280 buf[0] = 'R';
14281 else
14282 buf[0] = 'i';
14283 }
14284 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014285 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014287 if (exmode_active)
14288 buf[1] = 'v';
14289 }
14290 else if (exmode_active)
14291 {
14292 buf[0] = 'c';
14293 buf[1] = 'e';
14294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014296 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014298 if (finish_op)
14299 buf[1] = 'o';
14300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014302 /* Clear out the minor mode when the argument is not a non-zero number or
14303 * non-empty string. */
14304 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014305 buf[1] = NUL;
14306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014307 rettv->vval.v_string = vim_strsave(buf);
14308 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309}
14310
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014311#ifdef FEAT_MZSCHEME
14312/*
14313 * "mzeval()" function
14314 */
14315 static void
14316f_mzeval(argvars, rettv)
14317 typval_T *argvars;
14318 typval_T *rettv;
14319{
14320 char_u *str;
14321 char_u buf[NUMBUFLEN];
14322
14323 str = get_tv_string_buf(&argvars[0], buf);
14324 do_mzeval(str, rettv);
14325}
14326#endif
14327
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014329 * "nextnonblank()" function
14330 */
14331 static void
14332f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014333 typval_T *argvars;
14334 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014335{
14336 linenr_T lnum;
14337
14338 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014340 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014341 {
14342 lnum = 0;
14343 break;
14344 }
14345 if (*skipwhite(ml_get(lnum)) != NUL)
14346 break;
14347 }
14348 rettv->vval.v_number = lnum;
14349}
14350
14351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352 * "nr2char()" function
14353 */
14354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014355f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014356 typval_T *argvars;
14357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358{
14359 char_u buf[NUMBUFLEN];
14360
14361#ifdef FEAT_MBYTE
14362 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014363 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364 else
14365#endif
14366 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014367 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368 buf[1] = NUL;
14369 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014370 rettv->v_type = VAR_STRING;
14371 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014372}
14373
14374/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014375 * "or(expr, expr)" function
14376 */
14377 static void
14378f_or(argvars, rettv)
14379 typval_T *argvars;
14380 typval_T *rettv;
14381{
14382 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14383 | get_tv_number_chk(&argvars[1], NULL);
14384}
14385
14386/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014387 * "pathshorten()" function
14388 */
14389 static void
14390f_pathshorten(argvars, rettv)
14391 typval_T *argvars;
14392 typval_T *rettv;
14393{
14394 char_u *p;
14395
14396 rettv->v_type = VAR_STRING;
14397 p = get_tv_string_chk(&argvars[0]);
14398 if (p == NULL)
14399 rettv->vval.v_string = NULL;
14400 else
14401 {
14402 p = vim_strsave(p);
14403 rettv->vval.v_string = p;
14404 if (p != NULL)
14405 shorten_dir(p);
14406 }
14407}
14408
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014409#ifdef FEAT_FLOAT
14410/*
14411 * "pow()" function
14412 */
14413 static void
14414f_pow(argvars, rettv)
14415 typval_T *argvars;
14416 typval_T *rettv;
14417{
14418 float_T fx, fy;
14419
14420 rettv->v_type = VAR_FLOAT;
14421 if (get_float_arg(argvars, &fx) == OK
14422 && get_float_arg(&argvars[1], &fy) == OK)
14423 rettv->vval.v_float = pow(fx, fy);
14424 else
14425 rettv->vval.v_float = 0.0;
14426}
14427#endif
14428
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014429/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014430 * "prevnonblank()" function
14431 */
14432 static void
14433f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014434 typval_T *argvars;
14435 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014436{
14437 linenr_T lnum;
14438
14439 lnum = get_tv_lnum(argvars);
14440 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14441 lnum = 0;
14442 else
14443 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14444 --lnum;
14445 rettv->vval.v_number = lnum;
14446}
14447
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014448#ifdef HAVE_STDARG_H
14449/* This dummy va_list is here because:
14450 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14451 * - locally in the function results in a "used before set" warning
14452 * - using va_start() to initialize it gives "function with fixed args" error */
14453static va_list ap;
14454#endif
14455
Bram Moolenaar8c711452005-01-14 21:53:12 +000014456/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014457 * "printf()" function
14458 */
14459 static void
14460f_printf(argvars, rettv)
14461 typval_T *argvars;
14462 typval_T *rettv;
14463{
14464 rettv->v_type = VAR_STRING;
14465 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014466#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014467 {
14468 char_u buf[NUMBUFLEN];
14469 int len;
14470 char_u *s;
14471 int saved_did_emsg = did_emsg;
14472 char *fmt;
14473
14474 /* Get the required length, allocate the buffer and do it for real. */
14475 did_emsg = FALSE;
14476 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014477 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014478 if (!did_emsg)
14479 {
14480 s = alloc(len + 1);
14481 if (s != NULL)
14482 {
14483 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014484 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014485 }
14486 }
14487 did_emsg |= saved_did_emsg;
14488 }
14489#endif
14490}
14491
14492/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014493 * "pumvisible()" function
14494 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014495 static void
14496f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014497 typval_T *argvars UNUSED;
14498 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014499{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014500#ifdef FEAT_INS_EXPAND
14501 if (pum_visible())
14502 rettv->vval.v_number = 1;
14503#endif
14504}
14505
Bram Moolenaardb913952012-06-29 12:54:53 +020014506#ifdef FEAT_PYTHON3
14507/*
14508 * "py3eval()" function
14509 */
14510 static void
14511f_py3eval(argvars, rettv)
14512 typval_T *argvars;
14513 typval_T *rettv;
14514{
14515 char_u *str;
14516 char_u buf[NUMBUFLEN];
14517
14518 str = get_tv_string_buf(&argvars[0], buf);
14519 do_py3eval(str, rettv);
14520}
14521#endif
14522
14523#ifdef FEAT_PYTHON
14524/*
14525 * "pyeval()" function
14526 */
14527 static void
14528f_pyeval(argvars, rettv)
14529 typval_T *argvars;
14530 typval_T *rettv;
14531{
14532 char_u *str;
14533 char_u buf[NUMBUFLEN];
14534
14535 str = get_tv_string_buf(&argvars[0], buf);
14536 do_pyeval(str, rettv);
14537}
14538#endif
14539
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014540/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014541 * "range()" function
14542 */
14543 static void
14544f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014545 typval_T *argvars;
14546 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014547{
14548 long start;
14549 long end;
14550 long stride = 1;
14551 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014552 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014553
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014554 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014555 if (argvars[1].v_type == VAR_UNKNOWN)
14556 {
14557 end = start - 1;
14558 start = 0;
14559 }
14560 else
14561 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014562 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014563 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014564 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014565 }
14566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014567 if (error)
14568 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014569 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014570 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014571 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014572 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014573 else
14574 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014575 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014576 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014577 if (list_append_number(rettv->vval.v_list,
14578 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014579 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014580 }
14581}
14582
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014583/*
14584 * "readfile()" function
14585 */
14586 static void
14587f_readfile(argvars, rettv)
14588 typval_T *argvars;
14589 typval_T *rettv;
14590{
14591 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014592 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014593 char_u *fname;
14594 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014595 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14596 int io_size = sizeof(buf);
14597 int readlen; /* size of last fread() */
14598 char_u *prev = NULL; /* previously read bytes, if any */
14599 long prevlen = 0; /* length of data in prev */
14600 long prevsize = 0; /* size of prev buffer */
14601 long maxline = MAXLNUM;
14602 long cnt = 0;
14603 char_u *p; /* position in buf */
14604 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014605
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014606 if (argvars[1].v_type != VAR_UNKNOWN)
14607 {
14608 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14609 binary = TRUE;
14610 if (argvars[2].v_type != VAR_UNKNOWN)
14611 maxline = get_tv_number(&argvars[2]);
14612 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014613
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014614 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014615 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014616
14617 /* Always open the file in binary mode, library functions have a mind of
14618 * their own about CR-LF conversion. */
14619 fname = get_tv_string(&argvars[0]);
14620 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14621 {
14622 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14623 return;
14624 }
14625
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014626 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014627 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014628 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014629
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014630 /* This for loop processes what was read, but is also entered at end
14631 * of file so that either:
14632 * - an incomplete line gets written
14633 * - a "binary" file gets an empty line at the end if it ends in a
14634 * newline. */
14635 for (p = buf, start = buf;
14636 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14637 ++p)
14638 {
14639 if (*p == '\n' || readlen <= 0)
14640 {
14641 listitem_T *li;
14642 char_u *s = NULL;
14643 long_u len = p - start;
14644
14645 /* Finished a line. Remove CRs before NL. */
14646 if (readlen > 0 && !binary)
14647 {
14648 while (len > 0 && start[len - 1] == '\r')
14649 --len;
14650 /* removal may cross back to the "prev" string */
14651 if (len == 0)
14652 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14653 --prevlen;
14654 }
14655 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014656 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014657 else
14658 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014659 /* Change "prev" buffer to be the right size. This way
14660 * the bytes are only copied once, and very long lines are
14661 * allocated only once. */
14662 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014663 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014664 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014665 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014666 prev = NULL; /* the list will own the string */
14667 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014668 }
14669 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014670 if (s == NULL)
14671 {
14672 do_outofmem_msg((long_u) prevlen + len + 1);
14673 failed = TRUE;
14674 break;
14675 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014676
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014677 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014678 {
14679 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014680 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014681 break;
14682 }
14683 li->li_tv.v_type = VAR_STRING;
14684 li->li_tv.v_lock = 0;
14685 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014686 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014687
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014688 start = p + 1; /* step over newline */
14689 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014690 break;
14691 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014692 else if (*p == NUL)
14693 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014694#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014695 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14696 * when finding the BF and check the previous two bytes. */
14697 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014698 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014699 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14700 * + 1, these may be in the "prev" string. */
14701 char_u back1 = p >= buf + 1 ? p[-1]
14702 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14703 char_u back2 = p >= buf + 2 ? p[-2]
14704 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14705 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014706
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014707 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014708 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014709 char_u *dest = p - 2;
14710
14711 /* Usually a BOM is at the beginning of a file, and so at
14712 * the beginning of a line; then we can just step over it.
14713 */
14714 if (start == dest)
14715 start = p + 1;
14716 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014717 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014718 /* have to shuffle buf to close gap */
14719 int adjust_prevlen = 0;
14720
14721 if (dest < buf)
14722 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014723 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014724 dest = buf;
14725 }
14726 if (readlen > p - buf + 1)
14727 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14728 readlen -= 3 - adjust_prevlen;
14729 prevlen -= adjust_prevlen;
14730 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014731 }
14732 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014733 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014734#endif
14735 } /* for */
14736
14737 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14738 break;
14739 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014740 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014741 /* There's part of a line in buf, store it in "prev". */
14742 if (p - start + prevlen >= prevsize)
14743 {
14744 /* need bigger "prev" buffer */
14745 char_u *newprev;
14746
14747 /* A common use case is ordinary text files and "prev" gets a
14748 * fragment of a line, so the first allocation is made
14749 * small, to avoid repeatedly 'allocing' large and
14750 * 'reallocing' small. */
14751 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014752 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014753 else
14754 {
14755 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014756 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014757 prevsize = grow50pc > growmin ? grow50pc : growmin;
14758 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014759 newprev = prev == NULL ? alloc(prevsize)
14760 : vim_realloc(prev, prevsize);
14761 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014762 {
14763 do_outofmem_msg((long_u)prevsize);
14764 failed = TRUE;
14765 break;
14766 }
14767 prev = newprev;
14768 }
14769 /* Add the line part to end of "prev". */
14770 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014771 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014772 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014773 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014774
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014775 /*
14776 * For a negative line count use only the lines at the end of the file,
14777 * free the rest.
14778 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014779 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014780 while (cnt > -maxline)
14781 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014782 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014783 --cnt;
14784 }
14785
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014786 if (failed)
14787 {
14788 list_free(rettv->vval.v_list, TRUE);
14789 /* readfile doc says an empty list is returned on error */
14790 rettv->vval.v_list = list_alloc();
14791 }
14792
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014793 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014794 fclose(fd);
14795}
14796
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014797#if defined(FEAT_RELTIME)
14798static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14799
14800/*
14801 * Convert a List to proftime_T.
14802 * Return FAIL when there is something wrong.
14803 */
14804 static int
14805list2proftime(arg, tm)
14806 typval_T *arg;
14807 proftime_T *tm;
14808{
14809 long n1, n2;
14810 int error = FALSE;
14811
14812 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14813 || arg->vval.v_list->lv_len != 2)
14814 return FAIL;
14815 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14816 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14817# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014818 tm->HighPart = n1;
14819 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014820# else
14821 tm->tv_sec = n1;
14822 tm->tv_usec = n2;
14823# endif
14824 return error ? FAIL : OK;
14825}
14826#endif /* FEAT_RELTIME */
14827
14828/*
14829 * "reltime()" function
14830 */
14831 static void
14832f_reltime(argvars, rettv)
14833 typval_T *argvars;
14834 typval_T *rettv;
14835{
14836#ifdef FEAT_RELTIME
14837 proftime_T res;
14838 proftime_T start;
14839
14840 if (argvars[0].v_type == VAR_UNKNOWN)
14841 {
14842 /* No arguments: get current time. */
14843 profile_start(&res);
14844 }
14845 else if (argvars[1].v_type == VAR_UNKNOWN)
14846 {
14847 if (list2proftime(&argvars[0], &res) == FAIL)
14848 return;
14849 profile_end(&res);
14850 }
14851 else
14852 {
14853 /* Two arguments: compute the difference. */
14854 if (list2proftime(&argvars[0], &start) == FAIL
14855 || list2proftime(&argvars[1], &res) == FAIL)
14856 return;
14857 profile_sub(&res, &start);
14858 }
14859
14860 if (rettv_list_alloc(rettv) == OK)
14861 {
14862 long n1, n2;
14863
14864# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014865 n1 = res.HighPart;
14866 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014867# else
14868 n1 = res.tv_sec;
14869 n2 = res.tv_usec;
14870# endif
14871 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14872 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14873 }
14874#endif
14875}
14876
14877/*
14878 * "reltimestr()" function
14879 */
14880 static void
14881f_reltimestr(argvars, rettv)
14882 typval_T *argvars;
14883 typval_T *rettv;
14884{
14885#ifdef FEAT_RELTIME
14886 proftime_T tm;
14887#endif
14888
14889 rettv->v_type = VAR_STRING;
14890 rettv->vval.v_string = NULL;
14891#ifdef FEAT_RELTIME
14892 if (list2proftime(&argvars[0], &tm) == OK)
14893 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14894#endif
14895}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014896
Bram Moolenaar0d660222005-01-07 21:51:51 +000014897#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14898static void make_connection __ARGS((void));
14899static int check_connection __ARGS((void));
14900
14901 static void
14902make_connection()
14903{
14904 if (X_DISPLAY == NULL
14905# ifdef FEAT_GUI
14906 && !gui.in_use
14907# endif
14908 )
14909 {
14910 x_force_connect = TRUE;
14911 setup_term_clip();
14912 x_force_connect = FALSE;
14913 }
14914}
14915
14916 static int
14917check_connection()
14918{
14919 make_connection();
14920 if (X_DISPLAY == NULL)
14921 {
14922 EMSG(_("E240: No connection to Vim server"));
14923 return FAIL;
14924 }
14925 return OK;
14926}
14927#endif
14928
14929#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014930static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014931
14932 static void
14933remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014934 typval_T *argvars;
14935 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014936 int expr;
14937{
14938 char_u *server_name;
14939 char_u *keys;
14940 char_u *r = NULL;
14941 char_u buf[NUMBUFLEN];
14942# ifdef WIN32
14943 HWND w;
14944# else
14945 Window w;
14946# endif
14947
14948 if (check_restricted() || check_secure())
14949 return;
14950
14951# ifdef FEAT_X11
14952 if (check_connection() == FAIL)
14953 return;
14954# endif
14955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014956 server_name = get_tv_string_chk(&argvars[0]);
14957 if (server_name == NULL)
14958 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014959 keys = get_tv_string_buf(&argvars[1], buf);
14960# ifdef WIN32
14961 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14962# else
14963 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14964 < 0)
14965# endif
14966 {
14967 if (r != NULL)
14968 EMSG(r); /* sending worked but evaluation failed */
14969 else
14970 EMSG2(_("E241: Unable to send to %s"), server_name);
14971 return;
14972 }
14973
14974 rettv->vval.v_string = r;
14975
14976 if (argvars[2].v_type != VAR_UNKNOWN)
14977 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014978 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014979 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014980 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014981
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014982 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014983 v.di_tv.v_type = VAR_STRING;
14984 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014985 idvar = get_tv_string_chk(&argvars[2]);
14986 if (idvar != NULL)
14987 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014988 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014989 }
14990}
14991#endif
14992
14993/*
14994 * "remote_expr()" function
14995 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014996 static void
14997f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014998 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014999 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015000{
15001 rettv->v_type = VAR_STRING;
15002 rettv->vval.v_string = NULL;
15003#ifdef FEAT_CLIENTSERVER
15004 remote_common(argvars, rettv, TRUE);
15005#endif
15006}
15007
15008/*
15009 * "remote_foreground()" function
15010 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015011 static void
15012f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015013 typval_T *argvars UNUSED;
15014 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015015{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015016#ifdef FEAT_CLIENTSERVER
15017# ifdef WIN32
15018 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015019 {
15020 char_u *server_name = get_tv_string_chk(&argvars[0]);
15021
15022 if (server_name != NULL)
15023 serverForeground(server_name);
15024 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015025# else
15026 /* Send a foreground() expression to the server. */
15027 argvars[1].v_type = VAR_STRING;
15028 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15029 argvars[2].v_type = VAR_UNKNOWN;
15030 remote_common(argvars, rettv, TRUE);
15031 vim_free(argvars[1].vval.v_string);
15032# endif
15033#endif
15034}
15035
Bram Moolenaar0d660222005-01-07 21:51:51 +000015036 static void
15037f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015038 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015039 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015040{
15041#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015042 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015043 char_u *s = NULL;
15044# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015045 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015046# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015047 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015048
15049 if (check_restricted() || check_secure())
15050 {
15051 rettv->vval.v_number = -1;
15052 return;
15053 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015054 serverid = get_tv_string_chk(&argvars[0]);
15055 if (serverid == NULL)
15056 {
15057 rettv->vval.v_number = -1;
15058 return; /* type error; errmsg already given */
15059 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015060# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015061 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015062 if (n == 0)
15063 rettv->vval.v_number = -1;
15064 else
15065 {
15066 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15067 rettv->vval.v_number = (s != NULL);
15068 }
15069# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015070 if (check_connection() == FAIL)
15071 return;
15072
15073 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015074 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015075# endif
15076
15077 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15078 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015079 char_u *retvar;
15080
Bram Moolenaar33570922005-01-25 22:26:29 +000015081 v.di_tv.v_type = VAR_STRING;
15082 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015083 retvar = get_tv_string_chk(&argvars[1]);
15084 if (retvar != NULL)
15085 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015086 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015087 }
15088#else
15089 rettv->vval.v_number = -1;
15090#endif
15091}
15092
Bram Moolenaar0d660222005-01-07 21:51:51 +000015093 static void
15094f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015095 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015096 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015097{
15098 char_u *r = NULL;
15099
15100#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015101 char_u *serverid = get_tv_string_chk(&argvars[0]);
15102
15103 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015104 {
15105# ifdef WIN32
15106 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015107 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015108
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015109 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015110 if (n != 0)
15111 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15112 if (r == NULL)
15113# else
15114 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015115 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015116# endif
15117 EMSG(_("E277: Unable to read a server reply"));
15118 }
15119#endif
15120 rettv->v_type = VAR_STRING;
15121 rettv->vval.v_string = r;
15122}
15123
15124/*
15125 * "remote_send()" function
15126 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015127 static void
15128f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015129 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015130 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015131{
15132 rettv->v_type = VAR_STRING;
15133 rettv->vval.v_string = NULL;
15134#ifdef FEAT_CLIENTSERVER
15135 remote_common(argvars, rettv, FALSE);
15136#endif
15137}
15138
15139/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015140 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015141 */
15142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015143f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015144 typval_T *argvars;
15145 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015146{
Bram Moolenaar33570922005-01-25 22:26:29 +000015147 list_T *l;
15148 listitem_T *item, *item2;
15149 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015150 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015151 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015152 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015153 dict_T *d;
15154 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015155 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015156
Bram Moolenaar8c711452005-01-14 21:53:12 +000015157 if (argvars[0].v_type == VAR_DICT)
15158 {
15159 if (argvars[2].v_type != VAR_UNKNOWN)
15160 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015161 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015162 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015164 key = get_tv_string_chk(&argvars[1]);
15165 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015167 di = dict_find(d, key, -1);
15168 if (di == NULL)
15169 EMSG2(_(e_dictkey), key);
15170 else
15171 {
15172 *rettv = di->di_tv;
15173 init_tv(&di->di_tv);
15174 dictitem_remove(d, di);
15175 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015176 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015177 }
15178 }
15179 else if (argvars[0].v_type != VAR_LIST)
15180 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015181 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015182 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015184 int error = FALSE;
15185
15186 idx = get_tv_number_chk(&argvars[1], &error);
15187 if (error)
15188 ; /* type error: do nothing, errmsg already given */
15189 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015190 EMSGN(_(e_listidx), idx);
15191 else
15192 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015193 if (argvars[2].v_type == VAR_UNKNOWN)
15194 {
15195 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015196 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015197 *rettv = item->li_tv;
15198 vim_free(item);
15199 }
15200 else
15201 {
15202 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015203 end = get_tv_number_chk(&argvars[2], &error);
15204 if (error)
15205 ; /* type error: do nothing */
15206 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015207 EMSGN(_(e_listidx), end);
15208 else
15209 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015210 int cnt = 0;
15211
15212 for (li = item; li != NULL; li = li->li_next)
15213 {
15214 ++cnt;
15215 if (li == item2)
15216 break;
15217 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015218 if (li == NULL) /* didn't find "item2" after "item" */
15219 EMSG(_(e_invrange));
15220 else
15221 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015222 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015223 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015224 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015225 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015226 l->lv_first = item;
15227 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015228 item->li_prev = NULL;
15229 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015230 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015231 }
15232 }
15233 }
15234 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015235 }
15236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015237}
15238
15239/*
15240 * "rename({from}, {to})" function
15241 */
15242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015243f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015244 typval_T *argvars;
15245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015246{
15247 char_u buf[NUMBUFLEN];
15248
15249 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015250 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015251 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015252 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15253 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015254}
15255
15256/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015257 * "repeat()" function
15258 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015259 static void
15260f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015261 typval_T *argvars;
15262 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015263{
15264 char_u *p;
15265 int n;
15266 int slen;
15267 int len;
15268 char_u *r;
15269 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015270
15271 n = get_tv_number(&argvars[1]);
15272 if (argvars[0].v_type == VAR_LIST)
15273 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015274 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015275 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015276 if (list_extend(rettv->vval.v_list,
15277 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015278 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015279 }
15280 else
15281 {
15282 p = get_tv_string(&argvars[0]);
15283 rettv->v_type = VAR_STRING;
15284 rettv->vval.v_string = NULL;
15285
15286 slen = (int)STRLEN(p);
15287 len = slen * n;
15288 if (len <= 0)
15289 return;
15290
15291 r = alloc(len + 1);
15292 if (r != NULL)
15293 {
15294 for (i = 0; i < n; i++)
15295 mch_memmove(r + i * slen, p, (size_t)slen);
15296 r[len] = NUL;
15297 }
15298
15299 rettv->vval.v_string = r;
15300 }
15301}
15302
15303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015304 * "resolve()" function
15305 */
15306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015307f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015308 typval_T *argvars;
15309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015310{
15311 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015312#ifdef HAVE_READLINK
15313 char_u *buf = NULL;
15314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015316 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015317#ifdef FEAT_SHORTCUT
15318 {
15319 char_u *v = NULL;
15320
15321 v = mch_resolve_shortcut(p);
15322 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015323 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015324 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015325 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326 }
15327#else
15328# ifdef HAVE_READLINK
15329 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015330 char_u *cpy;
15331 int len;
15332 char_u *remain = NULL;
15333 char_u *q;
15334 int is_relative_to_current = FALSE;
15335 int has_trailing_pathsep = FALSE;
15336 int limit = 100;
15337
15338 p = vim_strsave(p);
15339
15340 if (p[0] == '.' && (vim_ispathsep(p[1])
15341 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15342 is_relative_to_current = TRUE;
15343
15344 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015345 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015346 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015348 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350
15351 q = getnextcomp(p);
15352 if (*q != NUL)
15353 {
15354 /* Separate the first path component in "p", and keep the
15355 * remainder (beginning with the path separator). */
15356 remain = vim_strsave(q - 1);
15357 q[-1] = NUL;
15358 }
15359
Bram Moolenaard9462e32011-04-11 21:35:11 +020015360 buf = alloc(MAXPATHL + 1);
15361 if (buf == NULL)
15362 goto fail;
15363
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364 for (;;)
15365 {
15366 for (;;)
15367 {
15368 len = readlink((char *)p, (char *)buf, MAXPATHL);
15369 if (len <= 0)
15370 break;
15371 buf[len] = NUL;
15372
15373 if (limit-- == 0)
15374 {
15375 vim_free(p);
15376 vim_free(remain);
15377 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015378 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379 goto fail;
15380 }
15381
15382 /* Ensure that the result will have a trailing path separator
15383 * if the argument has one. */
15384 if (remain == NULL && has_trailing_pathsep)
15385 add_pathsep(buf);
15386
15387 /* Separate the first path component in the link value and
15388 * concatenate the remainders. */
15389 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15390 if (*q != NUL)
15391 {
15392 if (remain == NULL)
15393 remain = vim_strsave(q - 1);
15394 else
15395 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015396 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397 if (cpy != NULL)
15398 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399 vim_free(remain);
15400 remain = cpy;
15401 }
15402 }
15403 q[-1] = NUL;
15404 }
15405
15406 q = gettail(p);
15407 if (q > p && *q == NUL)
15408 {
15409 /* Ignore trailing path separator. */
15410 q[-1] = NUL;
15411 q = gettail(p);
15412 }
15413 if (q > p && !mch_isFullName(buf))
15414 {
15415 /* symlink is relative to directory of argument */
15416 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15417 if (cpy != NULL)
15418 {
15419 STRCPY(cpy, p);
15420 STRCPY(gettail(cpy), buf);
15421 vim_free(p);
15422 p = cpy;
15423 }
15424 }
15425 else
15426 {
15427 vim_free(p);
15428 p = vim_strsave(buf);
15429 }
15430 }
15431
15432 if (remain == NULL)
15433 break;
15434
15435 /* Append the first path component of "remain" to "p". */
15436 q = getnextcomp(remain + 1);
15437 len = q - remain - (*q != NUL);
15438 cpy = vim_strnsave(p, STRLEN(p) + len);
15439 if (cpy != NULL)
15440 {
15441 STRNCAT(cpy, remain, len);
15442 vim_free(p);
15443 p = cpy;
15444 }
15445 /* Shorten "remain". */
15446 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015447 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448 else
15449 {
15450 vim_free(remain);
15451 remain = NULL;
15452 }
15453 }
15454
15455 /* If the result is a relative path name, make it explicitly relative to
15456 * the current directory if and only if the argument had this form. */
15457 if (!vim_ispathsep(*p))
15458 {
15459 if (is_relative_to_current
15460 && *p != NUL
15461 && !(p[0] == '.'
15462 && (p[1] == NUL
15463 || vim_ispathsep(p[1])
15464 || (p[1] == '.'
15465 && (p[2] == NUL
15466 || vim_ispathsep(p[2]))))))
15467 {
15468 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015469 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015470 if (cpy != NULL)
15471 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472 vim_free(p);
15473 p = cpy;
15474 }
15475 }
15476 else if (!is_relative_to_current)
15477 {
15478 /* Strip leading "./". */
15479 q = p;
15480 while (q[0] == '.' && vim_ispathsep(q[1]))
15481 q += 2;
15482 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015483 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484 }
15485 }
15486
15487 /* Ensure that the result will have no trailing path separator
15488 * if the argument had none. But keep "/" or "//". */
15489 if (!has_trailing_pathsep)
15490 {
15491 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015492 if (after_pathsep(p, q))
15493 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494 }
15495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015496 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015497 }
15498# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015499 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500# endif
15501#endif
15502
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015503 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504
15505#ifdef HAVE_READLINK
15506fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015507 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015509 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510}
15511
15512/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015513 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514 */
15515 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015516f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015517 typval_T *argvars;
15518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519{
Bram Moolenaar33570922005-01-25 22:26:29 +000015520 list_T *l;
15521 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015522
Bram Moolenaar0d660222005-01-07 21:51:51 +000015523 if (argvars[0].v_type != VAR_LIST)
15524 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015525 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015526 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015527 {
15528 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015529 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015530 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015531 while (li != NULL)
15532 {
15533 ni = li->li_prev;
15534 list_append(l, li);
15535 li = ni;
15536 }
15537 rettv->vval.v_list = l;
15538 rettv->v_type = VAR_LIST;
15539 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015540 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542}
15543
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015544#define SP_NOMOVE 0x01 /* don't move cursor */
15545#define SP_REPEAT 0x02 /* repeat to find outer pair */
15546#define SP_RETCOUNT 0x04 /* return matchcount */
15547#define SP_SETPCMARK 0x08 /* set previous context mark */
15548#define SP_START 0x10 /* accept match at start position */
15549#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15550#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015551
Bram Moolenaar33570922005-01-25 22:26:29 +000015552static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015553
15554/*
15555 * Get flags for a search function.
15556 * Possibly sets "p_ws".
15557 * Returns BACKWARD, FORWARD or zero (for an error).
15558 */
15559 static int
15560get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015561 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015562 int *flagsp;
15563{
15564 int dir = FORWARD;
15565 char_u *flags;
15566 char_u nbuf[NUMBUFLEN];
15567 int mask;
15568
15569 if (varp->v_type != VAR_UNKNOWN)
15570 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015571 flags = get_tv_string_buf_chk(varp, nbuf);
15572 if (flags == NULL)
15573 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015574 while (*flags != NUL)
15575 {
15576 switch (*flags)
15577 {
15578 case 'b': dir = BACKWARD; break;
15579 case 'w': p_ws = TRUE; break;
15580 case 'W': p_ws = FALSE; break;
15581 default: mask = 0;
15582 if (flagsp != NULL)
15583 switch (*flags)
15584 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015585 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015586 case 'e': mask = SP_END; break;
15587 case 'm': mask = SP_RETCOUNT; break;
15588 case 'n': mask = SP_NOMOVE; break;
15589 case 'p': mask = SP_SUBPAT; break;
15590 case 'r': mask = SP_REPEAT; break;
15591 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015592 }
15593 if (mask == 0)
15594 {
15595 EMSG2(_(e_invarg2), flags);
15596 dir = 0;
15597 }
15598 else
15599 *flagsp |= mask;
15600 }
15601 if (dir == 0)
15602 break;
15603 ++flags;
15604 }
15605 }
15606 return dir;
15607}
15608
Bram Moolenaar071d4272004-06-13 20:20:40 +000015609/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015610 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015612 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015613search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015614 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015615 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015616 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015618 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015619 char_u *pat;
15620 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015621 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622 int save_p_ws = p_ws;
15623 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015624 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015625 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015626 proftime_T tm;
15627#ifdef FEAT_RELTIME
15628 long time_limit = 0;
15629#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015630 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015631 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015633 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015634 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015635 if (dir == 0)
15636 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015637 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015638 if (flags & SP_START)
15639 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015640 if (flags & SP_END)
15641 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015642
Bram Moolenaar76929292008-01-06 19:07:36 +000015643 /* Optional arguments: line number to stop searching and timeout. */
15644 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015645 {
15646 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15647 if (lnum_stop < 0)
15648 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015649#ifdef FEAT_RELTIME
15650 if (argvars[3].v_type != VAR_UNKNOWN)
15651 {
15652 time_limit = get_tv_number_chk(&argvars[3], NULL);
15653 if (time_limit < 0)
15654 goto theend;
15655 }
15656#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015657 }
15658
Bram Moolenaar76929292008-01-06 19:07:36 +000015659#ifdef FEAT_RELTIME
15660 /* Set the time limit, if there is one. */
15661 profile_setlimit(time_limit, &tm);
15662#endif
15663
Bram Moolenaar231334e2005-07-25 20:46:57 +000015664 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015665 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015666 * Check to make sure only those flags are set.
15667 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15668 * flags cannot be set. Check for that condition also.
15669 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015670 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015671 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015672 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015673 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015674 goto theend;
15675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015677 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015678 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015679 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015680 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015682 if (flags & SP_SUBPAT)
15683 retval = subpatnum;
15684 else
15685 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015686 if (flags & SP_SETPCMARK)
15687 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015689 if (match_pos != NULL)
15690 {
15691 /* Store the match cursor position */
15692 match_pos->lnum = pos.lnum;
15693 match_pos->col = pos.col + 1;
15694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015695 /* "/$" will put the cursor after the end of the line, may need to
15696 * correct that here */
15697 check_cursor();
15698 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015699
15700 /* If 'n' flag is used: restore cursor position. */
15701 if (flags & SP_NOMOVE)
15702 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015703 else
15704 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015705theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015707
15708 return retval;
15709}
15710
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015711#ifdef FEAT_FLOAT
15712/*
15713 * "round({float})" function
15714 */
15715 static void
15716f_round(argvars, rettv)
15717 typval_T *argvars;
15718 typval_T *rettv;
15719{
15720 float_T f;
15721
15722 rettv->v_type = VAR_FLOAT;
15723 if (get_float_arg(argvars, &f) == OK)
15724 /* round() is not in C90, use ceil() or floor() instead. */
15725 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15726 else
15727 rettv->vval.v_float = 0.0;
15728}
15729#endif
15730
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015731/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015732 * "screencol()" function
15733 *
15734 * First column is 1 to be consistent with virtcol().
15735 */
15736 static void
15737f_screencol(argvars, rettv)
15738 typval_T *argvars UNUSED;
15739 typval_T *rettv;
15740{
15741 rettv->vval.v_number = screen_screencol() + 1;
15742}
15743
15744/*
15745 * "screenrow()" function
15746 */
15747 static void
15748f_screenrow(argvars, rettv)
15749 typval_T *argvars UNUSED;
15750 typval_T *rettv;
15751{
15752 rettv->vval.v_number = screen_screenrow() + 1;
15753}
15754
15755/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015756 * "search()" function
15757 */
15758 static void
15759f_search(argvars, rettv)
15760 typval_T *argvars;
15761 typval_T *rettv;
15762{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015763 int flags = 0;
15764
15765 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015766}
15767
Bram Moolenaar071d4272004-06-13 20:20:40 +000015768/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015769 * "searchdecl()" function
15770 */
15771 static void
15772f_searchdecl(argvars, rettv)
15773 typval_T *argvars;
15774 typval_T *rettv;
15775{
15776 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015777 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015778 int error = FALSE;
15779 char_u *name;
15780
15781 rettv->vval.v_number = 1; /* default: FAIL */
15782
15783 name = get_tv_string_chk(&argvars[0]);
15784 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015785 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015786 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015787 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15788 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15789 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015790 if (!error && name != NULL)
15791 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015792 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015793}
15794
15795/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015796 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015797 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015798 static int
15799searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015800 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015801 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802{
15803 char_u *spat, *mpat, *epat;
15804 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015806 int dir;
15807 int flags = 0;
15808 char_u nbuf1[NUMBUFLEN];
15809 char_u nbuf2[NUMBUFLEN];
15810 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015811 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015812 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015813 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814
Bram Moolenaar071d4272004-06-13 20:20:40 +000015815 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015816 spat = get_tv_string_chk(&argvars[0]);
15817 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15818 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15819 if (spat == NULL || mpat == NULL || epat == NULL)
15820 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822 /* Handle the optional fourth argument: flags */
15823 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015824 if (dir == 0)
15825 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015826
15827 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015828 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15829 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015830 if ((flags & (SP_END | SP_SUBPAT)) != 0
15831 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015832 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015833 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015834 goto theend;
15835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015836
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015837 /* Using 'r' implies 'W', otherwise it doesn't work. */
15838 if (flags & SP_REPEAT)
15839 p_ws = FALSE;
15840
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015841 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015842 if (argvars[3].v_type == VAR_UNKNOWN
15843 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015844 skip = (char_u *)"";
15845 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015847 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015848 if (argvars[5].v_type != VAR_UNKNOWN)
15849 {
15850 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15851 if (lnum_stop < 0)
15852 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015853#ifdef FEAT_RELTIME
15854 if (argvars[6].v_type != VAR_UNKNOWN)
15855 {
15856 time_limit = get_tv_number_chk(&argvars[6], NULL);
15857 if (time_limit < 0)
15858 goto theend;
15859 }
15860#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015861 }
15862 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015863 if (skip == NULL)
15864 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015866 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015867 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015868
15869theend:
15870 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015871
15872 return retval;
15873}
15874
15875/*
15876 * "searchpair()" function
15877 */
15878 static void
15879f_searchpair(argvars, rettv)
15880 typval_T *argvars;
15881 typval_T *rettv;
15882{
15883 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15884}
15885
15886/*
15887 * "searchpairpos()" function
15888 */
15889 static void
15890f_searchpairpos(argvars, rettv)
15891 typval_T *argvars;
15892 typval_T *rettv;
15893{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015894 pos_T match_pos;
15895 int lnum = 0;
15896 int col = 0;
15897
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015898 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015899 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015900
15901 if (searchpair_cmn(argvars, &match_pos) > 0)
15902 {
15903 lnum = match_pos.lnum;
15904 col = match_pos.col;
15905 }
15906
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015907 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15908 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015909}
15910
15911/*
15912 * Search for a start/middle/end thing.
15913 * Used by searchpair(), see its documentation for the details.
15914 * Returns 0 or -1 for no match,
15915 */
15916 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015917do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15918 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015919 char_u *spat; /* start pattern */
15920 char_u *mpat; /* middle pattern */
15921 char_u *epat; /* end pattern */
15922 int dir; /* BACKWARD or FORWARD */
15923 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015924 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015925 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015926 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015927 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015928{
15929 char_u *save_cpo;
15930 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15931 long retval = 0;
15932 pos_T pos;
15933 pos_T firstpos;
15934 pos_T foundpos;
15935 pos_T save_cursor;
15936 pos_T save_pos;
15937 int n;
15938 int r;
15939 int nest = 1;
15940 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015941 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015942 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015943
15944 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15945 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015946 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015947
Bram Moolenaar76929292008-01-06 19:07:36 +000015948#ifdef FEAT_RELTIME
15949 /* Set the time limit, if there is one. */
15950 profile_setlimit(time_limit, &tm);
15951#endif
15952
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015953 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15954 * start/middle/end (pat3, for the top pair). */
15955 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15956 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15957 if (pat2 == NULL || pat3 == NULL)
15958 goto theend;
15959 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15960 if (*mpat == NUL)
15961 STRCPY(pat3, pat2);
15962 else
15963 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15964 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015965 if (flags & SP_START)
15966 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015967
Bram Moolenaar071d4272004-06-13 20:20:40 +000015968 save_cursor = curwin->w_cursor;
15969 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015970 clearpos(&firstpos);
15971 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972 pat = pat3;
15973 for (;;)
15974 {
15975 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015976 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015977 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15978 /* didn't find it or found the first match again: FAIL */
15979 break;
15980
15981 if (firstpos.lnum == 0)
15982 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015983 if (equalpos(pos, foundpos))
15984 {
15985 /* Found the same position again. Can happen with a pattern that
15986 * has "\zs" at the end and searching backwards. Advance one
15987 * character and try again. */
15988 if (dir == BACKWARD)
15989 decl(&pos);
15990 else
15991 incl(&pos);
15992 }
15993 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015994
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015995 /* clear the start flag to avoid getting stuck here */
15996 options &= ~SEARCH_START;
15997
Bram Moolenaar071d4272004-06-13 20:20:40 +000015998 /* If the skip pattern matches, ignore this match. */
15999 if (*skip != NUL)
16000 {
16001 save_pos = curwin->w_cursor;
16002 curwin->w_cursor = pos;
16003 r = eval_to_bool(skip, &err, NULL, FALSE);
16004 curwin->w_cursor = save_pos;
16005 if (err)
16006 {
16007 /* Evaluating {skip} caused an error, break here. */
16008 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016009 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010 break;
16011 }
16012 if (r)
16013 continue;
16014 }
16015
16016 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16017 {
16018 /* Found end when searching backwards or start when searching
16019 * forward: nested pair. */
16020 ++nest;
16021 pat = pat2; /* nested, don't search for middle */
16022 }
16023 else
16024 {
16025 /* Found end when searching forward or start when searching
16026 * backward: end of (nested) pair; or found middle in outer pair. */
16027 if (--nest == 1)
16028 pat = pat3; /* outer level, search for middle */
16029 }
16030
16031 if (nest == 0)
16032 {
16033 /* Found the match: return matchcount or line number. */
16034 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016035 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016036 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016037 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016038 if (flags & SP_SETPCMARK)
16039 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040 curwin->w_cursor = pos;
16041 if (!(flags & SP_REPEAT))
16042 break;
16043 nest = 1; /* search for next unmatched */
16044 }
16045 }
16046
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016047 if (match_pos != NULL)
16048 {
16049 /* Store the match cursor position */
16050 match_pos->lnum = curwin->w_cursor.lnum;
16051 match_pos->col = curwin->w_cursor.col + 1;
16052 }
16053
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016055 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056 curwin->w_cursor = save_cursor;
16057
16058theend:
16059 vim_free(pat2);
16060 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016061 if (p_cpo == empty_option)
16062 p_cpo = save_cpo;
16063 else
16064 /* Darn, evaluating the {skip} expression changed the value. */
16065 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016066
16067 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016068}
16069
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016070/*
16071 * "searchpos()" function
16072 */
16073 static void
16074f_searchpos(argvars, rettv)
16075 typval_T *argvars;
16076 typval_T *rettv;
16077{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016078 pos_T match_pos;
16079 int lnum = 0;
16080 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016081 int n;
16082 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016083
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016084 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016085 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016086
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016087 n = search_cmn(argvars, &match_pos, &flags);
16088 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016089 {
16090 lnum = match_pos.lnum;
16091 col = match_pos.col;
16092 }
16093
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016094 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16095 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016096 if (flags & SP_SUBPAT)
16097 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016098}
16099
16100
Bram Moolenaar0d660222005-01-07 21:51:51 +000016101 static void
16102f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016103 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016104 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016105{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016106#ifdef FEAT_CLIENTSERVER
16107 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016108 char_u *server = get_tv_string_chk(&argvars[0]);
16109 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016110
Bram Moolenaar0d660222005-01-07 21:51:51 +000016111 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016112 if (server == NULL || reply == NULL)
16113 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016114 if (check_restricted() || check_secure())
16115 return;
16116# ifdef FEAT_X11
16117 if (check_connection() == FAIL)
16118 return;
16119# endif
16120
16121 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016123 EMSG(_("E258: Unable to send to client"));
16124 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016126 rettv->vval.v_number = 0;
16127#else
16128 rettv->vval.v_number = -1;
16129#endif
16130}
16131
Bram Moolenaar0d660222005-01-07 21:51:51 +000016132 static void
16133f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016134 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016135 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016136{
16137 char_u *r = NULL;
16138
16139#ifdef FEAT_CLIENTSERVER
16140# ifdef WIN32
16141 r = serverGetVimNames();
16142# else
16143 make_connection();
16144 if (X_DISPLAY != NULL)
16145 r = serverGetVimNames(X_DISPLAY);
16146# endif
16147#endif
16148 rettv->v_type = VAR_STRING;
16149 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016150}
16151
16152/*
16153 * "setbufvar()" function
16154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016156f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016157 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016158 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159{
16160 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016163 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164 char_u nbuf[NUMBUFLEN];
16165
16166 if (check_restricted() || check_secure())
16167 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016168 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16169 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016170 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171 varp = &argvars[2];
16172
16173 if (buf != NULL && varname != NULL && varp != NULL)
16174 {
16175 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016177
16178 if (*varname == '&')
16179 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016180 long numval;
16181 char_u *strval;
16182 int error = FALSE;
16183
Bram Moolenaar071d4272004-06-13 20:20:40 +000016184 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016185 numval = get_tv_number_chk(varp, &error);
16186 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016187 if (!error && strval != NULL)
16188 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016189 }
16190 else
16191 {
16192 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16193 if (bufvarname != NULL)
16194 {
16195 STRCPY(bufvarname, "b:");
16196 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016197 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016198 vim_free(bufvarname);
16199 }
16200 }
16201
16202 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205}
16206
16207/*
16208 * "setcmdpos()" function
16209 */
16210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016211f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016212 typval_T *argvars;
16213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016214{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016215 int pos = (int)get_tv_number(&argvars[0]) - 1;
16216
16217 if (pos >= 0)
16218 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016219}
16220
16221/*
16222 * "setline()" function
16223 */
16224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016225f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016226 typval_T *argvars;
16227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228{
16229 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016230 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016231 list_T *l = NULL;
16232 listitem_T *li = NULL;
16233 long added = 0;
16234 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016235
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016236 lnum = get_tv_lnum(&argvars[0]);
16237 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016239 l = argvars[1].vval.v_list;
16240 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016241 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016242 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016243 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016244
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016245 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016246 for (;;)
16247 {
16248 if (l != NULL)
16249 {
16250 /* list argument, get next string */
16251 if (li == NULL)
16252 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016253 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016254 li = li->li_next;
16255 }
16256
16257 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016258 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016259 break;
16260 if (lnum <= curbuf->b_ml.ml_line_count)
16261 {
16262 /* existing line, replace it */
16263 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16264 {
16265 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016266 if (lnum == curwin->w_cursor.lnum)
16267 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016268 rettv->vval.v_number = 0; /* OK */
16269 }
16270 }
16271 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16272 {
16273 /* lnum is one past the last line, append the line */
16274 ++added;
16275 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16276 rettv->vval.v_number = 0; /* OK */
16277 }
16278
16279 if (l == NULL) /* only one string argument */
16280 break;
16281 ++lnum;
16282 }
16283
16284 if (added > 0)
16285 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016286}
16287
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016288static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16289
Bram Moolenaar071d4272004-06-13 20:20:40 +000016290/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016291 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016292 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016293 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016294set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016295 win_T *wp UNUSED;
16296 typval_T *list_arg UNUSED;
16297 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016298 typval_T *rettv;
16299{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016300#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016301 char_u *act;
16302 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016303#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016304
Bram Moolenaar2641f772005-03-25 21:58:17 +000016305 rettv->vval.v_number = -1;
16306
16307#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016308 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016309 EMSG(_(e_listreq));
16310 else
16311 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016312 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016313
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016314 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016315 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016316 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016317 if (act == NULL)
16318 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016319 if (*act == 'a' || *act == 'r')
16320 action = *act;
16321 }
16322
Bram Moolenaar81484f42012-12-05 15:16:47 +010016323 if (l != NULL && set_errorlist(wp, l, action,
16324 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016325 rettv->vval.v_number = 0;
16326 }
16327#endif
16328}
16329
16330/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016331 * "setloclist()" function
16332 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016333 static void
16334f_setloclist(argvars, rettv)
16335 typval_T *argvars;
16336 typval_T *rettv;
16337{
16338 win_T *win;
16339
16340 rettv->vval.v_number = -1;
16341
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016342 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016343 if (win != NULL)
16344 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16345}
16346
16347/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016348 * "setmatches()" function
16349 */
16350 static void
16351f_setmatches(argvars, rettv)
16352 typval_T *argvars;
16353 typval_T *rettv;
16354{
16355#ifdef FEAT_SEARCH_EXTRA
16356 list_T *l;
16357 listitem_T *li;
16358 dict_T *d;
16359
16360 rettv->vval.v_number = -1;
16361 if (argvars[0].v_type != VAR_LIST)
16362 {
16363 EMSG(_(e_listreq));
16364 return;
16365 }
16366 if ((l = argvars[0].vval.v_list) != NULL)
16367 {
16368
16369 /* To some extent make sure that we are dealing with a list from
16370 * "getmatches()". */
16371 li = l->lv_first;
16372 while (li != NULL)
16373 {
16374 if (li->li_tv.v_type != VAR_DICT
16375 || (d = li->li_tv.vval.v_dict) == NULL)
16376 {
16377 EMSG(_(e_invarg));
16378 return;
16379 }
16380 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16381 && dict_find(d, (char_u *)"pattern", -1) != NULL
16382 && dict_find(d, (char_u *)"priority", -1) != NULL
16383 && dict_find(d, (char_u *)"id", -1) != NULL))
16384 {
16385 EMSG(_(e_invarg));
16386 return;
16387 }
16388 li = li->li_next;
16389 }
16390
16391 clear_matches(curwin);
16392 li = l->lv_first;
16393 while (li != NULL)
16394 {
16395 d = li->li_tv.vval.v_dict;
16396 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16397 get_dict_string(d, (char_u *)"pattern", FALSE),
16398 (int)get_dict_number(d, (char_u *)"priority"),
16399 (int)get_dict_number(d, (char_u *)"id"));
16400 li = li->li_next;
16401 }
16402 rettv->vval.v_number = 0;
16403 }
16404#endif
16405}
16406
16407/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016408 * "setpos()" function
16409 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016410 static void
16411f_setpos(argvars, rettv)
16412 typval_T *argvars;
16413 typval_T *rettv;
16414{
16415 pos_T pos;
16416 int fnum;
16417 char_u *name;
16418
Bram Moolenaar08250432008-02-13 11:42:46 +000016419 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016420 name = get_tv_string_chk(argvars);
16421 if (name != NULL)
16422 {
16423 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16424 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016425 if (--pos.col < 0)
16426 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016427 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016428 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016429 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016430 if (fnum == curbuf->b_fnum)
16431 {
16432 curwin->w_cursor = pos;
16433 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016434 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016435 }
16436 else
16437 EMSG(_(e_invarg));
16438 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016439 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16440 {
16441 /* set mark */
16442 if (setmark_pos(name[1], &pos, fnum) == OK)
16443 rettv->vval.v_number = 0;
16444 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016445 else
16446 EMSG(_(e_invarg));
16447 }
16448 }
16449}
16450
16451/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016452 * "setqflist()" function
16453 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016454 static void
16455f_setqflist(argvars, rettv)
16456 typval_T *argvars;
16457 typval_T *rettv;
16458{
16459 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16460}
16461
16462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463 * "setreg()" function
16464 */
16465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016466f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016467 typval_T *argvars;
16468 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469{
16470 int regname;
16471 char_u *strregname;
16472 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016473 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016474 int append;
16475 char_u yank_type;
16476 long block_len;
16477
16478 block_len = -1;
16479 yank_type = MAUTO;
16480 append = FALSE;
16481
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016482 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016483 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016484
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016485 if (strregname == NULL)
16486 return; /* type error; errmsg already given */
16487 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488 if (regname == 0 || regname == '@')
16489 regname = '"';
16490 else if (regname == '=')
16491 return;
16492
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016493 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016495 stropt = get_tv_string_chk(&argvars[2]);
16496 if (stropt == NULL)
16497 return; /* type error */
16498 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499 switch (*stropt)
16500 {
16501 case 'a': case 'A': /* append */
16502 append = TRUE;
16503 break;
16504 case 'v': case 'c': /* character-wise selection */
16505 yank_type = MCHAR;
16506 break;
16507 case 'V': case 'l': /* line-wise selection */
16508 yank_type = MLINE;
16509 break;
16510#ifdef FEAT_VISUAL
16511 case 'b': case Ctrl_V: /* block-wise selection */
16512 yank_type = MBLOCK;
16513 if (VIM_ISDIGIT(stropt[1]))
16514 {
16515 ++stropt;
16516 block_len = getdigits(&stropt) - 1;
16517 --stropt;
16518 }
16519 break;
16520#endif
16521 }
16522 }
16523
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016524 strval = get_tv_string_chk(&argvars[1]);
16525 if (strval != NULL)
16526 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016527 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016528 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529}
16530
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016531/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016532 * "settabvar()" function
16533 */
16534 static void
16535f_settabvar(argvars, rettv)
16536 typval_T *argvars;
16537 typval_T *rettv;
16538{
16539 tabpage_T *save_curtab;
16540 char_u *varname, *tabvarname;
16541 typval_T *varp;
16542 tabpage_T *tp;
16543
16544 rettv->vval.v_number = 0;
16545
16546 if (check_restricted() || check_secure())
16547 return;
16548
16549 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16550 varname = get_tv_string_chk(&argvars[1]);
16551 varp = &argvars[2];
16552
16553 if (tp != NULL && varname != NULL && varp != NULL)
16554 {
16555 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016556 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016557
16558 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16559 if (tabvarname != NULL)
16560 {
16561 STRCPY(tabvarname, "t:");
16562 STRCPY(tabvarname + 2, varname);
16563 set_var(tabvarname, varp, TRUE);
16564 vim_free(tabvarname);
16565 }
16566
16567 /* Restore current tabpage */
16568 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016569 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016570 }
16571}
16572
16573/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016574 * "settabwinvar()" function
16575 */
16576 static void
16577f_settabwinvar(argvars, rettv)
16578 typval_T *argvars;
16579 typval_T *rettv;
16580{
16581 setwinvar(argvars, rettv, 1);
16582}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016583
16584/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016585 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016588f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016589 typval_T *argvars;
16590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016592 setwinvar(argvars, rettv, 0);
16593}
16594
16595/*
16596 * "setwinvar()" and "settabwinvar()" functions
16597 */
16598 static void
16599setwinvar(argvars, rettv, off)
16600 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016601 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016602 int off;
16603{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604 win_T *win;
16605#ifdef FEAT_WINDOWS
16606 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016607 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016608#endif
16609 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016610 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016611 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016612 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016613
16614 if (check_restricted() || check_secure())
16615 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016616
16617#ifdef FEAT_WINDOWS
16618 if (off == 1)
16619 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16620 else
16621 tp = curtab;
16622#endif
16623 win = find_win_by_nr(&argvars[off], tp);
16624 varname = get_tv_string_chk(&argvars[off + 1]);
16625 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626
16627 if (win != NULL && varname != NULL && varp != NULL)
16628 {
16629#ifdef FEAT_WINDOWS
16630 /* set curwin to be our win, temporarily */
16631 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016632 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016633 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016634 if (!win_valid(win))
16635 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636 curwin = win;
16637 curbuf = curwin->w_buffer;
16638#endif
16639
16640 if (*varname == '&')
16641 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016642 long numval;
16643 char_u *strval;
16644 int error = FALSE;
16645
Bram Moolenaar071d4272004-06-13 20:20:40 +000016646 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016647 numval = get_tv_number_chk(varp, &error);
16648 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016649 if (!error && strval != NULL)
16650 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016651 }
16652 else
16653 {
16654 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16655 if (winvarname != NULL)
16656 {
16657 STRCPY(winvarname, "w:");
16658 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016659 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016660 vim_free(winvarname);
16661 }
16662 }
16663
16664#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016665 /* Restore current tabpage and window, if still valid (autocomands can
16666 * make them invalid). */
16667 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016668 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669 if (win_valid(save_curwin))
16670 {
16671 curwin = save_curwin;
16672 curbuf = curwin->w_buffer;
16673 }
16674#endif
16675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016676}
16677
16678/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016679 * "shellescape({string})" function
16680 */
16681 static void
16682f_shellescape(argvars, rettv)
16683 typval_T *argvars;
16684 typval_T *rettv;
16685{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016686 rettv->vval.v_string = vim_strsave_shellescape(
16687 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016688 rettv->v_type = VAR_STRING;
16689}
16690
16691/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016692 * shiftwidth() function
16693 */
16694 static void
16695f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016696 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016697 typval_T *rettv;
16698{
16699 rettv->vval.v_number = get_sw_value();
16700}
16701
16702/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016703 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704 */
16705 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016706f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016707 typval_T *argvars;
16708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016710 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711
Bram Moolenaar0d660222005-01-07 21:51:51 +000016712 p = get_tv_string(&argvars[0]);
16713 rettv->vval.v_string = vim_strsave(p);
16714 simplify_filename(rettv->vval.v_string); /* simplify in place */
16715 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716}
16717
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016718#ifdef FEAT_FLOAT
16719/*
16720 * "sin()" function
16721 */
16722 static void
16723f_sin(argvars, rettv)
16724 typval_T *argvars;
16725 typval_T *rettv;
16726{
16727 float_T f;
16728
16729 rettv->v_type = VAR_FLOAT;
16730 if (get_float_arg(argvars, &f) == OK)
16731 rettv->vval.v_float = sin(f);
16732 else
16733 rettv->vval.v_float = 0.0;
16734}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016735
16736/*
16737 * "sinh()" function
16738 */
16739 static void
16740f_sinh(argvars, rettv)
16741 typval_T *argvars;
16742 typval_T *rettv;
16743{
16744 float_T f;
16745
16746 rettv->v_type = VAR_FLOAT;
16747 if (get_float_arg(argvars, &f) == OK)
16748 rettv->vval.v_float = sinh(f);
16749 else
16750 rettv->vval.v_float = 0.0;
16751}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016752#endif
16753
Bram Moolenaar0d660222005-01-07 21:51:51 +000016754static int
16755#ifdef __BORLANDC__
16756 _RTLENTRYF
16757#endif
16758 item_compare __ARGS((const void *s1, const void *s2));
16759static int
16760#ifdef __BORLANDC__
16761 _RTLENTRYF
16762#endif
16763 item_compare2 __ARGS((const void *s1, const void *s2));
16764
16765static int item_compare_ic;
16766static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016767static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016768static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016769#define ITEM_COMPARE_FAIL 999
16770
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016772 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016774 static int
16775#ifdef __BORLANDC__
16776_RTLENTRYF
16777#endif
16778item_compare(s1, s2)
16779 const void *s1;
16780 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016782 char_u *p1, *p2;
16783 char_u *tofree1, *tofree2;
16784 int res;
16785 char_u numbuf1[NUMBUFLEN];
16786 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016788 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16789 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016790 if (p1 == NULL)
16791 p1 = (char_u *)"";
16792 if (p2 == NULL)
16793 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016794 if (item_compare_ic)
16795 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016796 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016797 res = STRCMP(p1, p2);
16798 vim_free(tofree1);
16799 vim_free(tofree2);
16800 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016801}
16802
16803 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016804#ifdef __BORLANDC__
16805_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016806#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016807item_compare2(s1, s2)
16808 const void *s1;
16809 const void *s2;
16810{
16811 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016812 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016813 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016814 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016816 /* shortcut after failure in previous call; compare all items equal */
16817 if (item_compare_func_err)
16818 return 0;
16819
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016820 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16821 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016822 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16823 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016824
16825 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016826 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016827 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16828 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016829 clear_tv(&argv[0]);
16830 clear_tv(&argv[1]);
16831
16832 if (res == FAIL)
16833 res = ITEM_COMPARE_FAIL;
16834 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016835 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16836 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016837 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016838 clear_tv(&rettv);
16839 return res;
16840}
16841
16842/*
16843 * "sort({list})" function
16844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016845 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016846f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016847 typval_T *argvars;
16848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849{
Bram Moolenaar33570922005-01-25 22:26:29 +000016850 list_T *l;
16851 listitem_T *li;
16852 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016853 long len;
16854 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855
Bram Moolenaar0d660222005-01-07 21:51:51 +000016856 if (argvars[0].v_type != VAR_LIST)
16857 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858 else
16859 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016860 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016861 if (l == NULL || tv_check_lock(l->lv_lock,
16862 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016863 return;
16864 rettv->vval.v_list = l;
16865 rettv->v_type = VAR_LIST;
16866 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867
Bram Moolenaar0d660222005-01-07 21:51:51 +000016868 len = list_len(l);
16869 if (len <= 1)
16870 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871
Bram Moolenaar0d660222005-01-07 21:51:51 +000016872 item_compare_ic = FALSE;
16873 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016874 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016875 if (argvars[1].v_type != VAR_UNKNOWN)
16876 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016877 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016878 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016879 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016880 else
16881 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016882 int error = FALSE;
16883
16884 i = get_tv_number_chk(&argvars[1], &error);
16885 if (error)
16886 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016887 if (i == 1)
16888 item_compare_ic = TRUE;
16889 else
16890 item_compare_func = get_tv_string(&argvars[1]);
16891 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016892
16893 if (argvars[2].v_type != VAR_UNKNOWN)
16894 {
16895 /* optional third argument: {dict} */
16896 if (argvars[2].v_type != VAR_DICT)
16897 {
16898 EMSG(_(e_dictreq));
16899 return;
16900 }
16901 item_compare_selfdict = argvars[2].vval.v_dict;
16902 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016904
Bram Moolenaar0d660222005-01-07 21:51:51 +000016905 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016906 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016907 if (ptrs == NULL)
16908 return;
16909 i = 0;
16910 for (li = l->lv_first; li != NULL; li = li->li_next)
16911 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016912
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016913 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016914 /* test the compare function */
16915 if (item_compare_func != NULL
16916 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16917 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016918 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016920 {
16921 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016922 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016923 item_compare_func == NULL ? item_compare : item_compare2);
16924
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016925 if (!item_compare_func_err)
16926 {
16927 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016928 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016929 l->lv_len = 0;
16930 for (i = 0; i < len; ++i)
16931 list_append(l, ptrs[i]);
16932 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016933 }
16934
16935 vim_free(ptrs);
16936 }
16937}
16938
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016939/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016940 * "soundfold({word})" function
16941 */
16942 static void
16943f_soundfold(argvars, rettv)
16944 typval_T *argvars;
16945 typval_T *rettv;
16946{
16947 char_u *s;
16948
16949 rettv->v_type = VAR_STRING;
16950 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016951#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016952 rettv->vval.v_string = eval_soundfold(s);
16953#else
16954 rettv->vval.v_string = vim_strsave(s);
16955#endif
16956}
16957
16958/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016959 * "spellbadword()" function
16960 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016961 static void
16962f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016963 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016964 typval_T *rettv;
16965{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016966 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016967 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016968 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016969
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016970 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016971 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016972
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016973#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016974 if (argvars[0].v_type == VAR_UNKNOWN)
16975 {
16976 /* Find the start and length of the badly spelled word. */
16977 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16978 if (len != 0)
16979 word = ml_get_cursor();
16980 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016981 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016982 {
16983 char_u *str = get_tv_string_chk(&argvars[0]);
16984 int capcol = -1;
16985
16986 if (str != NULL)
16987 {
16988 /* Check the argument for spelling. */
16989 while (*str != NUL)
16990 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016991 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016992 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016993 {
16994 word = str;
16995 break;
16996 }
16997 str += len;
16998 }
16999 }
17000 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017001#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017002
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017003 list_append_string(rettv->vval.v_list, word, len);
17004 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017005 attr == HLF_SPB ? "bad" :
17006 attr == HLF_SPR ? "rare" :
17007 attr == HLF_SPL ? "local" :
17008 attr == HLF_SPC ? "caps" :
17009 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017010}
17011
17012/*
17013 * "spellsuggest()" function
17014 */
17015 static void
17016f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017017 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017018 typval_T *rettv;
17019{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017020#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017021 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017022 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017023 int maxcount;
17024 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017025 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017026 listitem_T *li;
17027 int need_capital = FALSE;
17028#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017029
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017030 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017031 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017032
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017033#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017034 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017035 {
17036 str = get_tv_string(&argvars[0]);
17037 if (argvars[1].v_type != VAR_UNKNOWN)
17038 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017039 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017040 if (maxcount <= 0)
17041 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017042 if (argvars[2].v_type != VAR_UNKNOWN)
17043 {
17044 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17045 if (typeerr)
17046 return;
17047 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017048 }
17049 else
17050 maxcount = 25;
17051
Bram Moolenaar4770d092006-01-12 23:22:24 +000017052 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017053
17054 for (i = 0; i < ga.ga_len; ++i)
17055 {
17056 str = ((char_u **)ga.ga_data)[i];
17057
17058 li = listitem_alloc();
17059 if (li == NULL)
17060 vim_free(str);
17061 else
17062 {
17063 li->li_tv.v_type = VAR_STRING;
17064 li->li_tv.v_lock = 0;
17065 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017066 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017067 }
17068 }
17069 ga_clear(&ga);
17070 }
17071#endif
17072}
17073
Bram Moolenaar0d660222005-01-07 21:51:51 +000017074 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017075f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017076 typval_T *argvars;
17077 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017078{
17079 char_u *str;
17080 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017081 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017082 regmatch_T regmatch;
17083 char_u patbuf[NUMBUFLEN];
17084 char_u *save_cpo;
17085 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017086 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017087 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017088 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017089
17090 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17091 save_cpo = p_cpo;
17092 p_cpo = (char_u *)"";
17093
17094 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017095 if (argvars[1].v_type != VAR_UNKNOWN)
17096 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017097 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17098 if (pat == NULL)
17099 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017100 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017101 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017102 }
17103 if (pat == NULL || *pat == NUL)
17104 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017105
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017106 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017108 if (typeerr)
17109 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110
Bram Moolenaar0d660222005-01-07 21:51:51 +000017111 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17112 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017113 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017114 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017115 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017116 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017117 if (*str == NUL)
17118 match = FALSE; /* empty item at the end */
17119 else
17120 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017121 if (match)
17122 end = regmatch.startp[0];
17123 else
17124 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017125 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17126 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017127 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017128 if (list_append_string(rettv->vval.v_list, str,
17129 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017130 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017131 }
17132 if (!match)
17133 break;
17134 /* Advance to just after the match. */
17135 if (regmatch.endp[0] > str)
17136 col = 0;
17137 else
17138 {
17139 /* Don't get stuck at the same match. */
17140#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017141 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017142#else
17143 col = 1;
17144#endif
17145 }
17146 str = regmatch.endp[0];
17147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148
Bram Moolenaar0d660222005-01-07 21:51:51 +000017149 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017151
Bram Moolenaar0d660222005-01-07 21:51:51 +000017152 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153}
17154
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017155#ifdef FEAT_FLOAT
17156/*
17157 * "sqrt()" function
17158 */
17159 static void
17160f_sqrt(argvars, rettv)
17161 typval_T *argvars;
17162 typval_T *rettv;
17163{
17164 float_T f;
17165
17166 rettv->v_type = VAR_FLOAT;
17167 if (get_float_arg(argvars, &f) == OK)
17168 rettv->vval.v_float = sqrt(f);
17169 else
17170 rettv->vval.v_float = 0.0;
17171}
17172
17173/*
17174 * "str2float()" function
17175 */
17176 static void
17177f_str2float(argvars, rettv)
17178 typval_T *argvars;
17179 typval_T *rettv;
17180{
17181 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17182
17183 if (*p == '+')
17184 p = skipwhite(p + 1);
17185 (void)string2float(p, &rettv->vval.v_float);
17186 rettv->v_type = VAR_FLOAT;
17187}
17188#endif
17189
Bram Moolenaar2c932302006-03-18 21:42:09 +000017190/*
17191 * "str2nr()" function
17192 */
17193 static void
17194f_str2nr(argvars, rettv)
17195 typval_T *argvars;
17196 typval_T *rettv;
17197{
17198 int base = 10;
17199 char_u *p;
17200 long n;
17201
17202 if (argvars[1].v_type != VAR_UNKNOWN)
17203 {
17204 base = get_tv_number(&argvars[1]);
17205 if (base != 8 && base != 10 && base != 16)
17206 {
17207 EMSG(_(e_invarg));
17208 return;
17209 }
17210 }
17211
17212 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017213 if (*p == '+')
17214 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017215 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17216 rettv->vval.v_number = n;
17217}
17218
Bram Moolenaar071d4272004-06-13 20:20:40 +000017219#ifdef HAVE_STRFTIME
17220/*
17221 * "strftime({format}[, {time}])" function
17222 */
17223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017224f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017225 typval_T *argvars;
17226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017227{
17228 char_u result_buf[256];
17229 struct tm *curtime;
17230 time_t seconds;
17231 char_u *p;
17232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017233 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017234
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017235 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017236 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017237 seconds = time(NULL);
17238 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017239 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240 curtime = localtime(&seconds);
17241 /* MSVC returns NULL for an invalid value of seconds. */
17242 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017243 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244 else
17245 {
17246# ifdef FEAT_MBYTE
17247 vimconv_T conv;
17248 char_u *enc;
17249
17250 conv.vc_type = CONV_NONE;
17251 enc = enc_locale();
17252 convert_setup(&conv, p_enc, enc);
17253 if (conv.vc_type != CONV_NONE)
17254 p = string_convert(&conv, p, NULL);
17255# endif
17256 if (p != NULL)
17257 (void)strftime((char *)result_buf, sizeof(result_buf),
17258 (char *)p, curtime);
17259 else
17260 result_buf[0] = NUL;
17261
17262# ifdef FEAT_MBYTE
17263 if (conv.vc_type != CONV_NONE)
17264 vim_free(p);
17265 convert_setup(&conv, enc, p_enc);
17266 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017267 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 else
17269# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017270 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271
17272# ifdef FEAT_MBYTE
17273 /* Release conversion descriptors */
17274 convert_setup(&conv, NULL, NULL);
17275 vim_free(enc);
17276# endif
17277 }
17278}
17279#endif
17280
17281/*
17282 * "stridx()" function
17283 */
17284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017285f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017286 typval_T *argvars;
17287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288{
17289 char_u buf[NUMBUFLEN];
17290 char_u *needle;
17291 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017292 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017294 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017296 needle = get_tv_string_chk(&argvars[1]);
17297 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017298 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017299 if (needle == NULL || haystack == NULL)
17300 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301
Bram Moolenaar33570922005-01-25 22:26:29 +000017302 if (argvars[2].v_type != VAR_UNKNOWN)
17303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017304 int error = FALSE;
17305
17306 start_idx = get_tv_number_chk(&argvars[2], &error);
17307 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017308 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017309 if (start_idx >= 0)
17310 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017311 }
17312
17313 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17314 if (pos != NULL)
17315 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316}
17317
17318/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017319 * "string()" function
17320 */
17321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017322f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017323 typval_T *argvars;
17324 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017325{
17326 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017327 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017328
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017329 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017330 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017331 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017332 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017333 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334}
17335
17336/*
17337 * "strlen()" function
17338 */
17339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017340f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017341 typval_T *argvars;
17342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017343{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017344 rettv->vval.v_number = (varnumber_T)(STRLEN(
17345 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346}
17347
17348/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017349 * "strchars()" function
17350 */
17351 static void
17352f_strchars(argvars, rettv)
17353 typval_T *argvars;
17354 typval_T *rettv;
17355{
17356 char_u *s = get_tv_string(&argvars[0]);
17357#ifdef FEAT_MBYTE
17358 varnumber_T len = 0;
17359
17360 while (*s != NUL)
17361 {
17362 mb_cptr2char_adv(&s);
17363 ++len;
17364 }
17365 rettv->vval.v_number = len;
17366#else
17367 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17368#endif
17369}
17370
17371/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017372 * "strdisplaywidth()" function
17373 */
17374 static void
17375f_strdisplaywidth(argvars, rettv)
17376 typval_T *argvars;
17377 typval_T *rettv;
17378{
17379 char_u *s = get_tv_string(&argvars[0]);
17380 int col = 0;
17381
17382 if (argvars[1].v_type != VAR_UNKNOWN)
17383 col = get_tv_number(&argvars[1]);
17384
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017385 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017386}
17387
17388/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017389 * "strwidth()" function
17390 */
17391 static void
17392f_strwidth(argvars, rettv)
17393 typval_T *argvars;
17394 typval_T *rettv;
17395{
17396 char_u *s = get_tv_string(&argvars[0]);
17397
17398 rettv->vval.v_number = (varnumber_T)(
17399#ifdef FEAT_MBYTE
17400 mb_string2cells(s, -1)
17401#else
17402 STRLEN(s)
17403#endif
17404 );
17405}
17406
17407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408 * "strpart()" function
17409 */
17410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017411f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017412 typval_T *argvars;
17413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414{
17415 char_u *p;
17416 int n;
17417 int len;
17418 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017419 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017421 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422 slen = (int)STRLEN(p);
17423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017424 n = get_tv_number_chk(&argvars[1], &error);
17425 if (error)
17426 len = 0;
17427 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017428 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429 else
17430 len = slen - n; /* default len: all bytes that are available. */
17431
17432 /*
17433 * Only return the overlap between the specified part and the actual
17434 * string.
17435 */
17436 if (n < 0)
17437 {
17438 len += n;
17439 n = 0;
17440 }
17441 else if (n > slen)
17442 n = slen;
17443 if (len < 0)
17444 len = 0;
17445 else if (n + len > slen)
17446 len = slen - n;
17447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017448 rettv->v_type = VAR_STRING;
17449 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450}
17451
17452/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017453 * "strridx()" function
17454 */
17455 static void
17456f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017457 typval_T *argvars;
17458 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017459{
17460 char_u buf[NUMBUFLEN];
17461 char_u *needle;
17462 char_u *haystack;
17463 char_u *rest;
17464 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017465 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017467 needle = get_tv_string_chk(&argvars[1]);
17468 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017469
17470 rettv->vval.v_number = -1;
17471 if (needle == NULL || haystack == NULL)
17472 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017473
17474 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017475 if (argvars[2].v_type != VAR_UNKNOWN)
17476 {
17477 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017478 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017479 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017480 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017481 }
17482 else
17483 end_idx = haystack_len;
17484
Bram Moolenaar0d660222005-01-07 21:51:51 +000017485 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017486 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017487 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017488 lastmatch = haystack + end_idx;
17489 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017490 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017491 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017492 for (rest = haystack; *rest != '\0'; ++rest)
17493 {
17494 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017495 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017496 break;
17497 lastmatch = rest;
17498 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017499 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017500
17501 if (lastmatch == NULL)
17502 rettv->vval.v_number = -1;
17503 else
17504 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17505}
17506
17507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017508 * "strtrans()" function
17509 */
17510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017511f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017512 typval_T *argvars;
17513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017515 rettv->v_type = VAR_STRING;
17516 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517}
17518
17519/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017520 * "submatch()" function
17521 */
17522 static void
17523f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017524 typval_T *argvars;
17525 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017526{
17527 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017528 rettv->vval.v_string =
17529 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017530}
17531
17532/*
17533 * "substitute()" function
17534 */
17535 static void
17536f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017537 typval_T *argvars;
17538 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017539{
17540 char_u patbuf[NUMBUFLEN];
17541 char_u subbuf[NUMBUFLEN];
17542 char_u flagsbuf[NUMBUFLEN];
17543
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017544 char_u *str = get_tv_string_chk(&argvars[0]);
17545 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17546 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17547 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17548
Bram Moolenaar0d660222005-01-07 21:51:51 +000017549 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017550 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17551 rettv->vval.v_string = NULL;
17552 else
17553 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017554}
17555
17556/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017557 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017560f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017561 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563{
17564 int id = 0;
17565#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017566 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567 long col;
17568 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017569 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017571 lnum = get_tv_lnum(argvars); /* -1 on type error */
17572 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17573 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017575 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017576 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017577 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578#endif
17579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017580 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581}
17582
17583/*
17584 * "synIDattr(id, what [, mode])" function
17585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017587f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017588 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590{
17591 char_u *p = NULL;
17592#ifdef FEAT_SYN_HL
17593 int id;
17594 char_u *what;
17595 char_u *mode;
17596 char_u modebuf[NUMBUFLEN];
17597 int modec;
17598
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017599 id = get_tv_number(&argvars[0]);
17600 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017601 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017603 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017604 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017605 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606 modec = 0; /* replace invalid with current */
17607 }
17608 else
17609 {
17610#ifdef FEAT_GUI
17611 if (gui.in_use)
17612 modec = 'g';
17613 else
17614#endif
17615 if (t_colors > 1)
17616 modec = 'c';
17617 else
17618 modec = 't';
17619 }
17620
17621
17622 switch (TOLOWER_ASC(what[0]))
17623 {
17624 case 'b':
17625 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17626 p = highlight_color(id, what, modec);
17627 else /* bold */
17628 p = highlight_has_attr(id, HL_BOLD, modec);
17629 break;
17630
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017631 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632 p = highlight_color(id, what, modec);
17633 break;
17634
17635 case 'i':
17636 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17637 p = highlight_has_attr(id, HL_INVERSE, modec);
17638 else /* italic */
17639 p = highlight_has_attr(id, HL_ITALIC, modec);
17640 break;
17641
17642 case 'n': /* name */
17643 p = get_highlight_name(NULL, id - 1);
17644 break;
17645
17646 case 'r': /* reverse */
17647 p = highlight_has_attr(id, HL_INVERSE, modec);
17648 break;
17649
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017650 case 's':
17651 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17652 p = highlight_color(id, what, modec);
17653 else /* standout */
17654 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655 break;
17656
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017657 case 'u':
17658 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17659 /* underline */
17660 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17661 else
17662 /* undercurl */
17663 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664 break;
17665 }
17666
17667 if (p != NULL)
17668 p = vim_strsave(p);
17669#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017670 rettv->v_type = VAR_STRING;
17671 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672}
17673
17674/*
17675 * "synIDtrans(id)" function
17676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017678f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017679 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681{
17682 int id;
17683
17684#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017685 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686
17687 if (id > 0)
17688 id = syn_get_final_id(id);
17689 else
17690#endif
17691 id = 0;
17692
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017693 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017694}
17695
17696/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017697 * "synconcealed(lnum, col)" function
17698 */
17699 static void
17700f_synconcealed(argvars, rettv)
17701 typval_T *argvars UNUSED;
17702 typval_T *rettv;
17703{
17704#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17705 long lnum;
17706 long col;
17707 int syntax_flags = 0;
17708 int cchar;
17709 int matchid = 0;
17710 char_u str[NUMBUFLEN];
17711#endif
17712
17713 rettv->v_type = VAR_LIST;
17714 rettv->vval.v_list = NULL;
17715
17716#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17717 lnum = get_tv_lnum(argvars); /* -1 on type error */
17718 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17719
17720 vim_memset(str, NUL, sizeof(str));
17721
17722 if (rettv_list_alloc(rettv) != FAIL)
17723 {
17724 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17725 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17726 && curwin->w_p_cole > 0)
17727 {
17728 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17729 syntax_flags = get_syntax_info(&matchid);
17730
17731 /* get the conceal character */
17732 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17733 {
17734 cchar = syn_get_sub_char();
17735 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17736 cchar = lcs_conceal;
17737 if (cchar != NUL)
17738 {
17739# ifdef FEAT_MBYTE
17740 if (has_mbyte)
17741 (*mb_char2bytes)(cchar, str);
17742 else
17743# endif
17744 str[0] = cchar;
17745 }
17746 }
17747 }
17748
17749 list_append_number(rettv->vval.v_list,
17750 (syntax_flags & HL_CONCEAL) != 0);
17751 /* -1 to auto-determine strlen */
17752 list_append_string(rettv->vval.v_list, str, -1);
17753 list_append_number(rettv->vval.v_list, matchid);
17754 }
17755#endif
17756}
17757
17758/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017759 * "synstack(lnum, col)" function
17760 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017761 static void
17762f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017763 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017764 typval_T *rettv;
17765{
17766#ifdef FEAT_SYN_HL
17767 long lnum;
17768 long col;
17769 int i;
17770 int id;
17771#endif
17772
17773 rettv->v_type = VAR_LIST;
17774 rettv->vval.v_list = NULL;
17775
17776#ifdef FEAT_SYN_HL
17777 lnum = get_tv_lnum(argvars); /* -1 on type error */
17778 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17779
17780 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017781 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017782 && rettv_list_alloc(rettv) != FAIL)
17783 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017784 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017785 for (i = 0; ; ++i)
17786 {
17787 id = syn_get_stack_item(i);
17788 if (id < 0)
17789 break;
17790 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17791 break;
17792 }
17793 }
17794#endif
17795}
17796
17797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798 * "system()" function
17799 */
17800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017801f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017802 typval_T *argvars;
17803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017804{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017805 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017807 char_u *infile = NULL;
17808 char_u buf[NUMBUFLEN];
17809 int err = FALSE;
17810 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017812 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017813 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017814
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017815 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017816 {
17817 /*
17818 * Write the string to a temp file, to be used for input of the shell
17819 * command.
17820 */
17821 if ((infile = vim_tempname('i')) == NULL)
17822 {
17823 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017824 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017825 }
17826
17827 fd = mch_fopen((char *)infile, WRITEBIN);
17828 if (fd == NULL)
17829 {
17830 EMSG2(_(e_notopen), infile);
17831 goto done;
17832 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017833 p = get_tv_string_buf_chk(&argvars[1], buf);
17834 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017835 {
17836 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017837 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017838 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017839 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17840 err = TRUE;
17841 if (fclose(fd) != 0)
17842 err = TRUE;
17843 if (err)
17844 {
17845 EMSG(_("E677: Error writing temp file"));
17846 goto done;
17847 }
17848 }
17849
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017850 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17851 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017852
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853#ifdef USE_CR
17854 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017855 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856 {
17857 char_u *s;
17858
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017859 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860 {
17861 if (*s == CAR)
17862 *s = NL;
17863 }
17864 }
17865#else
17866# ifdef USE_CRNL
17867 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017868 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869 {
17870 char_u *s, *d;
17871
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017872 d = res;
17873 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874 {
17875 if (s[0] == CAR && s[1] == NL)
17876 ++s;
17877 *d++ = *s;
17878 }
17879 *d = NUL;
17880 }
17881# endif
17882#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017883
17884done:
17885 if (infile != NULL)
17886 {
17887 mch_remove(infile);
17888 vim_free(infile);
17889 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017890 rettv->v_type = VAR_STRING;
17891 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892}
17893
17894/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017895 * "tabpagebuflist()" function
17896 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017897 static void
17898f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017899 typval_T *argvars UNUSED;
17900 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017901{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017902#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017903 tabpage_T *tp;
17904 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017905
17906 if (argvars[0].v_type == VAR_UNKNOWN)
17907 wp = firstwin;
17908 else
17909 {
17910 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17911 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017912 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017913 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017914 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017915 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017916 for (; wp != NULL; wp = wp->w_next)
17917 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017918 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017919 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017920 }
17921#endif
17922}
17923
17924
17925/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017926 * "tabpagenr()" function
17927 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017928 static void
17929f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017930 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017931 typval_T *rettv;
17932{
17933 int nr = 1;
17934#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017935 char_u *arg;
17936
17937 if (argvars[0].v_type != VAR_UNKNOWN)
17938 {
17939 arg = get_tv_string_chk(&argvars[0]);
17940 nr = 0;
17941 if (arg != NULL)
17942 {
17943 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017944 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017945 else
17946 EMSG2(_(e_invexpr2), arg);
17947 }
17948 }
17949 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017950 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017951#endif
17952 rettv->vval.v_number = nr;
17953}
17954
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017955
17956#ifdef FEAT_WINDOWS
17957static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17958
17959/*
17960 * Common code for tabpagewinnr() and winnr().
17961 */
17962 static int
17963get_winnr(tp, argvar)
17964 tabpage_T *tp;
17965 typval_T *argvar;
17966{
17967 win_T *twin;
17968 int nr = 1;
17969 win_T *wp;
17970 char_u *arg;
17971
17972 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17973 if (argvar->v_type != VAR_UNKNOWN)
17974 {
17975 arg = get_tv_string_chk(argvar);
17976 if (arg == NULL)
17977 nr = 0; /* type error; errmsg already given */
17978 else if (STRCMP(arg, "$") == 0)
17979 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17980 else if (STRCMP(arg, "#") == 0)
17981 {
17982 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17983 if (twin == NULL)
17984 nr = 0;
17985 }
17986 else
17987 {
17988 EMSG2(_(e_invexpr2), arg);
17989 nr = 0;
17990 }
17991 }
17992
17993 if (nr > 0)
17994 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17995 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017996 {
17997 if (wp == NULL)
17998 {
17999 /* didn't find it in this tabpage */
18000 nr = 0;
18001 break;
18002 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018003 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018004 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018005 return nr;
18006}
18007#endif
18008
18009/*
18010 * "tabpagewinnr()" function
18011 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018012 static void
18013f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018014 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018015 typval_T *rettv;
18016{
18017 int nr = 1;
18018#ifdef FEAT_WINDOWS
18019 tabpage_T *tp;
18020
18021 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18022 if (tp == NULL)
18023 nr = 0;
18024 else
18025 nr = get_winnr(tp, &argvars[1]);
18026#endif
18027 rettv->vval.v_number = nr;
18028}
18029
18030
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018031/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018032 * "tagfiles()" function
18033 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018034 static void
18035f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018036 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018037 typval_T *rettv;
18038{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018039 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018040 tagname_T tn;
18041 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018042
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018043 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018044 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018045 fname = alloc(MAXPATHL);
18046 if (fname == NULL)
18047 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018048
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018049 for (first = TRUE; ; first = FALSE)
18050 if (get_tagfname(&tn, first, fname) == FAIL
18051 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018052 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018053 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018054 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018055}
18056
18057/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018058 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018059 */
18060 static void
18061f_taglist(argvars, rettv)
18062 typval_T *argvars;
18063 typval_T *rettv;
18064{
18065 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018066
18067 tag_pattern = get_tv_string(&argvars[0]);
18068
18069 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018070 if (*tag_pattern == NUL)
18071 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018072
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018073 if (rettv_list_alloc(rettv) == OK)
18074 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018075}
18076
18077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078 * "tempname()" function
18079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018081f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018082 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084{
18085 static int x = 'A';
18086
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018087 rettv->v_type = VAR_STRING;
18088 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089
18090 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18091 * names. Skip 'I' and 'O', they are used for shell redirection. */
18092 do
18093 {
18094 if (x == 'Z')
18095 x = '0';
18096 else if (x == '9')
18097 x = 'A';
18098 else
18099 {
18100#ifdef EBCDIC
18101 if (x == 'I')
18102 x = 'J';
18103 else if (x == 'R')
18104 x = 'S';
18105 else
18106#endif
18107 ++x;
18108 }
18109 } while (x == 'I' || x == 'O');
18110}
18111
18112/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018113 * "test(list)" function: Just checking the walls...
18114 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018115 static void
18116f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018117 typval_T *argvars UNUSED;
18118 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018119{
18120 /* Used for unit testing. Change the code below to your liking. */
18121#if 0
18122 listitem_T *li;
18123 list_T *l;
18124 char_u *bad, *good;
18125
18126 if (argvars[0].v_type != VAR_LIST)
18127 return;
18128 l = argvars[0].vval.v_list;
18129 if (l == NULL)
18130 return;
18131 li = l->lv_first;
18132 if (li == NULL)
18133 return;
18134 bad = get_tv_string(&li->li_tv);
18135 li = li->li_next;
18136 if (li == NULL)
18137 return;
18138 good = get_tv_string(&li->li_tv);
18139 rettv->vval.v_number = test_edit_score(bad, good);
18140#endif
18141}
18142
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018143#ifdef FEAT_FLOAT
18144/*
18145 * "tan()" function
18146 */
18147 static void
18148f_tan(argvars, rettv)
18149 typval_T *argvars;
18150 typval_T *rettv;
18151{
18152 float_T f;
18153
18154 rettv->v_type = VAR_FLOAT;
18155 if (get_float_arg(argvars, &f) == OK)
18156 rettv->vval.v_float = tan(f);
18157 else
18158 rettv->vval.v_float = 0.0;
18159}
18160
18161/*
18162 * "tanh()" function
18163 */
18164 static void
18165f_tanh(argvars, rettv)
18166 typval_T *argvars;
18167 typval_T *rettv;
18168{
18169 float_T f;
18170
18171 rettv->v_type = VAR_FLOAT;
18172 if (get_float_arg(argvars, &f) == OK)
18173 rettv->vval.v_float = tanh(f);
18174 else
18175 rettv->vval.v_float = 0.0;
18176}
18177#endif
18178
Bram Moolenaard52d9742005-08-21 22:20:28 +000018179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180 * "tolower(string)" function
18181 */
18182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018183f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018184 typval_T *argvars;
18185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018186{
18187 char_u *p;
18188
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018189 p = vim_strsave(get_tv_string(&argvars[0]));
18190 rettv->v_type = VAR_STRING;
18191 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018192
18193 if (p != NULL)
18194 while (*p != NUL)
18195 {
18196#ifdef FEAT_MBYTE
18197 int l;
18198
18199 if (enc_utf8)
18200 {
18201 int c, lc;
18202
18203 c = utf_ptr2char(p);
18204 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018205 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206 /* TODO: reallocate string when byte count changes. */
18207 if (utf_char2len(lc) == l)
18208 utf_char2bytes(lc, p);
18209 p += l;
18210 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018211 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212 p += l; /* skip multi-byte character */
18213 else
18214#endif
18215 {
18216 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18217 ++p;
18218 }
18219 }
18220}
18221
18222/*
18223 * "toupper(string)" function
18224 */
18225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018226f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018227 typval_T *argvars;
18228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018230 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018231 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232}
18233
18234/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018235 * "tr(string, fromstr, tostr)" function
18236 */
18237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018238f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018239 typval_T *argvars;
18240 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018241{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018242 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018243 char_u *fromstr;
18244 char_u *tostr;
18245 char_u *p;
18246#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018247 int inlen;
18248 int fromlen;
18249 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018250 int idx;
18251 char_u *cpstr;
18252 int cplen;
18253 int first = TRUE;
18254#endif
18255 char_u buf[NUMBUFLEN];
18256 char_u buf2[NUMBUFLEN];
18257 garray_T ga;
18258
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018259 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018260 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18261 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018262
18263 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018264 rettv->v_type = VAR_STRING;
18265 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018266 if (fromstr == NULL || tostr == NULL)
18267 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018268 ga_init2(&ga, (int)sizeof(char), 80);
18269
18270#ifdef FEAT_MBYTE
18271 if (!has_mbyte)
18272#endif
18273 /* not multi-byte: fromstr and tostr must be the same length */
18274 if (STRLEN(fromstr) != STRLEN(tostr))
18275 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018276#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018277error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018278#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018279 EMSG2(_(e_invarg2), fromstr);
18280 ga_clear(&ga);
18281 return;
18282 }
18283
18284 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018285 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018286 {
18287#ifdef FEAT_MBYTE
18288 if (has_mbyte)
18289 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018290 inlen = (*mb_ptr2len)(in_str);
18291 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018292 cplen = inlen;
18293 idx = 0;
18294 for (p = fromstr; *p != NUL; p += fromlen)
18295 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018296 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018297 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018298 {
18299 for (p = tostr; *p != NUL; p += tolen)
18300 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018301 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018302 if (idx-- == 0)
18303 {
18304 cplen = tolen;
18305 cpstr = p;
18306 break;
18307 }
18308 }
18309 if (*p == NUL) /* tostr is shorter than fromstr */
18310 goto error;
18311 break;
18312 }
18313 ++idx;
18314 }
18315
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018316 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018317 {
18318 /* Check that fromstr and tostr have the same number of
18319 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018320 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018321 first = FALSE;
18322 for (p = tostr; *p != NUL; p += tolen)
18323 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018324 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018325 --idx;
18326 }
18327 if (idx != 0)
18328 goto error;
18329 }
18330
18331 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018332 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018333 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018334
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018335 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018336 }
18337 else
18338#endif
18339 {
18340 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018341 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018342 if (p != NULL)
18343 ga_append(&ga, tostr[p - fromstr]);
18344 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018345 ga_append(&ga, *in_str);
18346 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018347 }
18348 }
18349
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018350 /* add a terminating NUL */
18351 ga_grow(&ga, 1);
18352 ga_append(&ga, NUL);
18353
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018354 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018355}
18356
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018357#ifdef FEAT_FLOAT
18358/*
18359 * "trunc({float})" function
18360 */
18361 static void
18362f_trunc(argvars, rettv)
18363 typval_T *argvars;
18364 typval_T *rettv;
18365{
18366 float_T f;
18367
18368 rettv->v_type = VAR_FLOAT;
18369 if (get_float_arg(argvars, &f) == OK)
18370 /* trunc() is not in C90, use floor() or ceil() instead. */
18371 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18372 else
18373 rettv->vval.v_float = 0.0;
18374}
18375#endif
18376
Bram Moolenaar8299df92004-07-10 09:47:34 +000018377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018378 * "type(expr)" function
18379 */
18380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018381f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018382 typval_T *argvars;
18383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018385 int n;
18386
18387 switch (argvars[0].v_type)
18388 {
18389 case VAR_NUMBER: n = 0; break;
18390 case VAR_STRING: n = 1; break;
18391 case VAR_FUNC: n = 2; break;
18392 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018393 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018394#ifdef FEAT_FLOAT
18395 case VAR_FLOAT: n = 5; break;
18396#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018397 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18398 }
18399 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400}
18401
18402/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018403 * "undofile(name)" function
18404 */
18405 static void
18406f_undofile(argvars, rettv)
18407 typval_T *argvars;
18408 typval_T *rettv;
18409{
18410 rettv->v_type = VAR_STRING;
18411#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018412 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018413 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018414
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018415 if (*fname == NUL)
18416 {
18417 /* If there is no file name there will be no undo file. */
18418 rettv->vval.v_string = NULL;
18419 }
18420 else
18421 {
18422 char_u *ffname = FullName_save(fname, FALSE);
18423
18424 if (ffname != NULL)
18425 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18426 vim_free(ffname);
18427 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018428 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018429#else
18430 rettv->vval.v_string = NULL;
18431#endif
18432}
18433
18434/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018435 * "undotree()" function
18436 */
18437 static void
18438f_undotree(argvars, rettv)
18439 typval_T *argvars UNUSED;
18440 typval_T *rettv;
18441{
18442 if (rettv_dict_alloc(rettv) == OK)
18443 {
18444 dict_T *dict = rettv->vval.v_dict;
18445 list_T *list;
18446
Bram Moolenaar730cde92010-06-27 05:18:54 +020018447 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018448 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018449 dict_add_nr_str(dict, "save_last",
18450 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018451 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18452 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018453 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018454
18455 list = list_alloc();
18456 if (list != NULL)
18457 {
18458 u_eval_tree(curbuf->b_u_oldhead, list);
18459 dict_add_list(dict, "entries", list);
18460 }
18461 }
18462}
18463
18464/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018465 * "values(dict)" function
18466 */
18467 static void
18468f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018469 typval_T *argvars;
18470 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018471{
18472 dict_list(argvars, rettv, 1);
18473}
18474
18475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476 * "virtcol(string)" function
18477 */
18478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018479f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018480 typval_T *argvars;
18481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018482{
18483 colnr_T vcol = 0;
18484 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018485 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018487 fp = var2fpos(&argvars[0], FALSE, &fnum);
18488 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18489 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490 {
18491 getvvcol(curwin, fp, NULL, NULL, &vcol);
18492 ++vcol;
18493 }
18494
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018495 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496}
18497
18498/*
18499 * "visualmode()" function
18500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018502f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018503 typval_T *argvars UNUSED;
18504 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018505{
18506#ifdef FEAT_VISUAL
18507 char_u str[2];
18508
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018509 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018510 str[0] = curbuf->b_visual_mode_eval;
18511 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018512 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018513
18514 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018515 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018517#endif
18518}
18519
18520/*
18521 * "winbufnr(nr)" function
18522 */
18523 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018524f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018525 typval_T *argvars;
18526 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527{
18528 win_T *wp;
18529
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018530 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018532 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018534 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535}
18536
18537/*
18538 * "wincol()" function
18539 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018541f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018542 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544{
18545 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018546 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547}
18548
18549/*
18550 * "winheight(nr)" function
18551 */
18552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018553f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018554 typval_T *argvars;
18555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018556{
18557 win_T *wp;
18558
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018559 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018561 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018563 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564}
18565
18566/*
18567 * "winline()" function
18568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018570f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018571 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573{
18574 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018575 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576}
18577
18578/*
18579 * "winnr()" function
18580 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018582f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018583 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018584 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585{
18586 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018587
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018589 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018591 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592}
18593
18594/*
18595 * "winrestcmd()" function
18596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018598f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018599 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601{
18602#ifdef FEAT_WINDOWS
18603 win_T *wp;
18604 int winnr = 1;
18605 garray_T ga;
18606 char_u buf[50];
18607
18608 ga_init2(&ga, (int)sizeof(char), 70);
18609 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18610 {
18611 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18612 ga_concat(&ga, buf);
18613# ifdef FEAT_VERTSPLIT
18614 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18615 ga_concat(&ga, buf);
18616# endif
18617 ++winnr;
18618 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018619 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018621 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018623 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018625 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626}
18627
18628/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018629 * "winrestview()" function
18630 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018631 static void
18632f_winrestview(argvars, rettv)
18633 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018634 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018635{
18636 dict_T *dict;
18637
18638 if (argvars[0].v_type != VAR_DICT
18639 || (dict = argvars[0].vval.v_dict) == NULL)
18640 EMSG(_(e_invarg));
18641 else
18642 {
18643 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18644 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18645#ifdef FEAT_VIRTUALEDIT
18646 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18647#endif
18648 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018649 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018650
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018651 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018652#ifdef FEAT_DIFF
18653 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18654#endif
18655 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18656 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18657
18658 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018659 win_new_height(curwin, curwin->w_height);
18660# ifdef FEAT_VERTSPLIT
18661 win_new_width(curwin, W_WIDTH(curwin));
18662# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018663 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018664
18665 if (curwin->w_topline == 0)
18666 curwin->w_topline = 1;
18667 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18668 curwin->w_topline = curbuf->b_ml.ml_line_count;
18669#ifdef FEAT_DIFF
18670 check_topfill(curwin, TRUE);
18671#endif
18672 }
18673}
18674
18675/*
18676 * "winsaveview()" function
18677 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018678 static void
18679f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018680 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018681 typval_T *rettv;
18682{
18683 dict_T *dict;
18684
Bram Moolenaara800b422010-06-27 01:15:55 +020018685 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018686 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018687 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018688
18689 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18690 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18691#ifdef FEAT_VIRTUALEDIT
18692 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18693#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018694 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018695 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18696
18697 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18698#ifdef FEAT_DIFF
18699 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18700#endif
18701 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18702 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18703}
18704
18705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018706 * "winwidth(nr)" function
18707 */
18708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018709f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018710 typval_T *argvars;
18711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018712{
18713 win_T *wp;
18714
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018715 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018716 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018717 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718 else
18719#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018720 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018722 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723#endif
18724}
18725
Bram Moolenaar071d4272004-06-13 20:20:40 +000018726/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018727 * "writefile()" function
18728 */
18729 static void
18730f_writefile(argvars, rettv)
18731 typval_T *argvars;
18732 typval_T *rettv;
18733{
18734 int binary = FALSE;
18735 char_u *fname;
18736 FILE *fd;
18737 listitem_T *li;
18738 char_u *s;
18739 int ret = 0;
18740 int c;
18741
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018742 if (check_restricted() || check_secure())
18743 return;
18744
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018745 if (argvars[0].v_type != VAR_LIST)
18746 {
18747 EMSG2(_(e_listarg), "writefile()");
18748 return;
18749 }
18750 if (argvars[0].vval.v_list == NULL)
18751 return;
18752
18753 if (argvars[2].v_type != VAR_UNKNOWN
18754 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18755 binary = TRUE;
18756
18757 /* Always open the file in binary mode, library functions have a mind of
18758 * their own about CR-LF conversion. */
18759 fname = get_tv_string(&argvars[1]);
18760 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18761 {
18762 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18763 ret = -1;
18764 }
18765 else
18766 {
18767 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18768 li = li->li_next)
18769 {
18770 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18771 {
18772 if (*s == '\n')
18773 c = putc(NUL, fd);
18774 else
18775 c = putc(*s, fd);
18776 if (c == EOF)
18777 {
18778 ret = -1;
18779 break;
18780 }
18781 }
18782 if (!binary || li->li_next != NULL)
18783 if (putc('\n', fd) == EOF)
18784 {
18785 ret = -1;
18786 break;
18787 }
18788 if (ret < 0)
18789 {
18790 EMSG(_(e_write));
18791 break;
18792 }
18793 }
18794 fclose(fd);
18795 }
18796
18797 rettv->vval.v_number = ret;
18798}
18799
18800/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018801 * "xor(expr, expr)" function
18802 */
18803 static void
18804f_xor(argvars, rettv)
18805 typval_T *argvars;
18806 typval_T *rettv;
18807{
18808 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18809 ^ get_tv_number_chk(&argvars[1], NULL);
18810}
18811
18812
18813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018815 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018816 */
18817 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018818var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018819 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018820 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018821 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018822{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018823 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018825 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826
Bram Moolenaara5525202006-03-02 22:52:09 +000018827 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018828 if (varp->v_type == VAR_LIST)
18829 {
18830 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018831 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018832 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018833 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018834
18835 l = varp->vval.v_list;
18836 if (l == NULL)
18837 return NULL;
18838
18839 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018840 pos.lnum = list_find_nr(l, 0L, &error);
18841 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018842 return NULL; /* invalid line number */
18843
18844 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018845 pos.col = list_find_nr(l, 1L, &error);
18846 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018847 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018848 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018849
18850 /* We accept "$" for the column number: last column. */
18851 li = list_find(l, 1L);
18852 if (li != NULL && li->li_tv.v_type == VAR_STRING
18853 && li->li_tv.vval.v_string != NULL
18854 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18855 pos.col = len + 1;
18856
Bram Moolenaara5525202006-03-02 22:52:09 +000018857 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018858 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018859 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018860 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018861
Bram Moolenaara5525202006-03-02 22:52:09 +000018862#ifdef FEAT_VIRTUALEDIT
18863 /* Get the virtual offset. Defaults to zero. */
18864 pos.coladd = list_find_nr(l, 2L, &error);
18865 if (error)
18866 pos.coladd = 0;
18867#endif
18868
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018869 return &pos;
18870 }
18871
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018872 name = get_tv_string_chk(varp);
18873 if (name == NULL)
18874 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018875 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018876 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018877#ifdef FEAT_VISUAL
18878 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18879 {
18880 if (VIsual_active)
18881 return &VIsual;
18882 return &curwin->w_cursor;
18883 }
18884#endif
18885 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018886 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018887 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18889 return NULL;
18890 return pp;
18891 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018892
18893#ifdef FEAT_VIRTUALEDIT
18894 pos.coladd = 0;
18895#endif
18896
Bram Moolenaar477933c2007-07-17 14:32:23 +000018897 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018898 {
18899 pos.col = 0;
18900 if (name[1] == '0') /* "w0": first visible line */
18901 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018902 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018903 pos.lnum = curwin->w_topline;
18904 return &pos;
18905 }
18906 else if (name[1] == '$') /* "w$": last visible line */
18907 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018908 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018909 pos.lnum = curwin->w_botline - 1;
18910 return &pos;
18911 }
18912 }
18913 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018914 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018915 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916 {
18917 pos.lnum = curbuf->b_ml.ml_line_count;
18918 pos.col = 0;
18919 }
18920 else
18921 {
18922 pos.lnum = curwin->w_cursor.lnum;
18923 pos.col = (colnr_T)STRLEN(ml_get_curline());
18924 }
18925 return &pos;
18926 }
18927 return NULL;
18928}
18929
18930/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018931 * Convert list in "arg" into a position and optional file number.
18932 * When "fnump" is NULL there is no file number, only 3 items.
18933 * Note that the column is passed on as-is, the caller may want to decrement
18934 * it to use 1 for the first column.
18935 * Return FAIL when conversion is not possible, doesn't check the position for
18936 * validity.
18937 */
18938 static int
18939list2fpos(arg, posp, fnump)
18940 typval_T *arg;
18941 pos_T *posp;
18942 int *fnump;
18943{
18944 list_T *l = arg->vval.v_list;
18945 long i = 0;
18946 long n;
18947
Bram Moolenaarbde35262006-07-23 20:12:24 +000018948 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18949 * when "fnump" isn't NULL and "coladd" is optional. */
18950 if (arg->v_type != VAR_LIST
18951 || l == NULL
18952 || l->lv_len < (fnump == NULL ? 2 : 3)
18953 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018954 return FAIL;
18955
18956 if (fnump != NULL)
18957 {
18958 n = list_find_nr(l, i++, NULL); /* fnum */
18959 if (n < 0)
18960 return FAIL;
18961 if (n == 0)
18962 n = curbuf->b_fnum; /* current buffer */
18963 *fnump = n;
18964 }
18965
18966 n = list_find_nr(l, i++, NULL); /* lnum */
18967 if (n < 0)
18968 return FAIL;
18969 posp->lnum = n;
18970
18971 n = list_find_nr(l, i++, NULL); /* col */
18972 if (n < 0)
18973 return FAIL;
18974 posp->col = n;
18975
18976#ifdef FEAT_VIRTUALEDIT
18977 n = list_find_nr(l, i, NULL);
18978 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018979 posp->coladd = 0;
18980 else
18981 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018982#endif
18983
18984 return OK;
18985}
18986
18987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988 * Get the length of an environment variable name.
18989 * Advance "arg" to the first character after the name.
18990 * Return 0 for error.
18991 */
18992 static int
18993get_env_len(arg)
18994 char_u **arg;
18995{
18996 char_u *p;
18997 int len;
18998
18999 for (p = *arg; vim_isIDc(*p); ++p)
19000 ;
19001 if (p == *arg) /* no name found */
19002 return 0;
19003
19004 len = (int)(p - *arg);
19005 *arg = p;
19006 return len;
19007}
19008
19009/*
19010 * Get the length of the name of a function or internal variable.
19011 * "arg" is advanced to the first non-white character after the name.
19012 * Return 0 if something is wrong.
19013 */
19014 static int
19015get_id_len(arg)
19016 char_u **arg;
19017{
19018 char_u *p;
19019 int len;
19020
19021 /* Find the end of the name. */
19022 for (p = *arg; eval_isnamec(*p); ++p)
19023 ;
19024 if (p == *arg) /* no name found */
19025 return 0;
19026
19027 len = (int)(p - *arg);
19028 *arg = skipwhite(p);
19029
19030 return len;
19031}
19032
19033/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019034 * Get the length of the name of a variable or function.
19035 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019036 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019037 * Return -1 if curly braces expansion failed.
19038 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039 * If the name contains 'magic' {}'s, expand them and return the
19040 * expanded name in an allocated string via 'alias' - caller must free.
19041 */
19042 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019043get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044 char_u **arg;
19045 char_u **alias;
19046 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019047 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048{
19049 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050 char_u *p;
19051 char_u *expr_start;
19052 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053
19054 *alias = NULL; /* default to no alias */
19055
19056 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19057 && (*arg)[2] == (int)KE_SNR)
19058 {
19059 /* hard coded <SNR>, already translated */
19060 *arg += 3;
19061 return get_id_len(arg) + 3;
19062 }
19063 len = eval_fname_script(*arg);
19064 if (len > 0)
19065 {
19066 /* literal "<SID>", "s:" or "<SNR>" */
19067 *arg += len;
19068 }
19069
Bram Moolenaar071d4272004-06-13 20:20:40 +000019070 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019071 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019073 p = find_name_end(*arg, &expr_start, &expr_end,
19074 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019075 if (expr_start != NULL)
19076 {
19077 char_u *temp_string;
19078
19079 if (!evaluate)
19080 {
19081 len += (int)(p - *arg);
19082 *arg = skipwhite(p);
19083 return len;
19084 }
19085
19086 /*
19087 * Include any <SID> etc in the expanded string:
19088 * Thus the -len here.
19089 */
19090 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19091 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019092 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093 *alias = temp_string;
19094 *arg = skipwhite(p);
19095 return (int)STRLEN(temp_string);
19096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097
19098 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019099 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019100 EMSG2(_(e_invexpr2), *arg);
19101
19102 return len;
19103}
19104
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019105/*
19106 * Find the end of a variable or function name, taking care of magic braces.
19107 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19108 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019109 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019110 * Return a pointer to just after the name. Equal to "arg" if there is no
19111 * valid name.
19112 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019114find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115 char_u *arg;
19116 char_u **expr_start;
19117 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019118 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019119{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019120 int mb_nest = 0;
19121 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 char_u *p;
19123
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019124 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019126 *expr_start = NULL;
19127 *expr_end = NULL;
19128 }
19129
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019130 /* Quick check for valid starting character. */
19131 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19132 return arg;
19133
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019134 for (p = arg; *p != NUL
19135 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019136 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019137 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019138 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019139 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019140 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019141 if (*p == '\'')
19142 {
19143 /* skip over 'string' to avoid counting [ and ] inside it. */
19144 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19145 ;
19146 if (*p == NUL)
19147 break;
19148 }
19149 else if (*p == '"')
19150 {
19151 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19152 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19153 if (*p == '\\' && p[1] != NUL)
19154 ++p;
19155 if (*p == NUL)
19156 break;
19157 }
19158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019159 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019161 if (*p == '[')
19162 ++br_nest;
19163 else if (*p == ']')
19164 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019167 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019169 if (*p == '{')
19170 {
19171 mb_nest++;
19172 if (expr_start != NULL && *expr_start == NULL)
19173 *expr_start = p;
19174 }
19175 else if (*p == '}')
19176 {
19177 mb_nest--;
19178 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19179 *expr_end = p;
19180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182 }
19183
19184 return p;
19185}
19186
19187/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019188 * Expands out the 'magic' {}'s in a variable/function name.
19189 * Note that this can call itself recursively, to deal with
19190 * constructs like foo{bar}{baz}{bam}
19191 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19192 * "in_start" ^
19193 * "expr_start" ^
19194 * "expr_end" ^
19195 * "in_end" ^
19196 *
19197 * Returns a new allocated string, which the caller must free.
19198 * Returns NULL for failure.
19199 */
19200 static char_u *
19201make_expanded_name(in_start, expr_start, expr_end, in_end)
19202 char_u *in_start;
19203 char_u *expr_start;
19204 char_u *expr_end;
19205 char_u *in_end;
19206{
19207 char_u c1;
19208 char_u *retval = NULL;
19209 char_u *temp_result;
19210 char_u *nextcmd = NULL;
19211
19212 if (expr_end == NULL || in_end == NULL)
19213 return NULL;
19214 *expr_start = NUL;
19215 *expr_end = NUL;
19216 c1 = *in_end;
19217 *in_end = NUL;
19218
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019219 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019220 if (temp_result != NULL && nextcmd == NULL)
19221 {
19222 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19223 + (in_end - expr_end) + 1));
19224 if (retval != NULL)
19225 {
19226 STRCPY(retval, in_start);
19227 STRCAT(retval, temp_result);
19228 STRCAT(retval, expr_end + 1);
19229 }
19230 }
19231 vim_free(temp_result);
19232
19233 *in_end = c1; /* put char back for error messages */
19234 *expr_start = '{';
19235 *expr_end = '}';
19236
19237 if (retval != NULL)
19238 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019239 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019240 if (expr_start != NULL)
19241 {
19242 /* Further expansion! */
19243 temp_result = make_expanded_name(retval, expr_start,
19244 expr_end, temp_result);
19245 vim_free(retval);
19246 retval = temp_result;
19247 }
19248 }
19249
19250 return retval;
19251}
19252
19253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019255 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256 */
19257 static int
19258eval_isnamec(c)
19259 int c;
19260{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019261 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19262}
19263
19264/*
19265 * Return TRUE if character "c" can be used as the first character in a
19266 * variable or function name (excluding '{' and '}').
19267 */
19268 static int
19269eval_isnamec1(c)
19270 int c;
19271{
19272 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273}
19274
19275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019276 * Set number v: variable to "val".
19277 */
19278 void
19279set_vim_var_nr(idx, val)
19280 int idx;
19281 long val;
19282{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019283 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019284}
19285
19286/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019287 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019288 */
19289 long
19290get_vim_var_nr(idx)
19291 int idx;
19292{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019293 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019294}
19295
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019296/*
19297 * Get string v: variable value. Uses a static buffer, can only be used once.
19298 */
19299 char_u *
19300get_vim_var_str(idx)
19301 int idx;
19302{
19303 return get_tv_string(&vimvars[idx].vv_tv);
19304}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019305
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019307 * Get List v: variable value. Caller must take care of reference count when
19308 * needed.
19309 */
19310 list_T *
19311get_vim_var_list(idx)
19312 int idx;
19313{
19314 return vimvars[idx].vv_list;
19315}
19316
19317/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019318 * Set v:char to character "c".
19319 */
19320 void
19321set_vim_var_char(c)
19322 int c;
19323{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019324 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019325
19326#ifdef FEAT_MBYTE
19327 if (has_mbyte)
19328 buf[(*mb_char2bytes)(c, buf)] = NUL;
19329 else
19330#endif
19331 {
19332 buf[0] = c;
19333 buf[1] = NUL;
19334 }
19335 set_vim_var_string(VV_CHAR, buf, -1);
19336}
19337
19338/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019339 * Set v:count to "count" and v:count1 to "count1".
19340 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341 */
19342 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019343set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019344 long count;
19345 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019346 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019347{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019348 if (set_prevcount)
19349 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019350 vimvars[VV_COUNT].vv_nr = count;
19351 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019352}
19353
19354/*
19355 * Set string v: variable to a copy of "val".
19356 */
19357 void
19358set_vim_var_string(idx, val, len)
19359 int idx;
19360 char_u *val;
19361 int len; /* length of "val" to use or -1 (whole string) */
19362{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019363 /* Need to do this (at least) once, since we can't initialize a union.
19364 * Will always be invoked when "v:progname" is set. */
19365 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19366
Bram Moolenaare9a41262005-01-15 22:18:47 +000019367 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019369 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019370 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019371 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019372 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019373 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019374}
19375
19376/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019377 * Set List v: variable to "val".
19378 */
19379 void
19380set_vim_var_list(idx, val)
19381 int idx;
19382 list_T *val;
19383{
19384 list_unref(vimvars[idx].vv_list);
19385 vimvars[idx].vv_list = val;
19386 if (val != NULL)
19387 ++val->lv_refcount;
19388}
19389
19390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391 * Set v:register if needed.
19392 */
19393 void
19394set_reg_var(c)
19395 int c;
19396{
19397 char_u regname;
19398
19399 if (c == 0 || c == ' ')
19400 regname = '"';
19401 else
19402 regname = c;
19403 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019404 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405 set_vim_var_string(VV_REG, &regname, 1);
19406}
19407
19408/*
19409 * Get or set v:exception. If "oldval" == NULL, return the current value.
19410 * Otherwise, restore the value to "oldval" and return NULL.
19411 * Must always be called in pairs to save and restore v:exception! Does not
19412 * take care of memory allocations.
19413 */
19414 char_u *
19415v_exception(oldval)
19416 char_u *oldval;
19417{
19418 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019419 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420
Bram Moolenaare9a41262005-01-15 22:18:47 +000019421 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 return NULL;
19423}
19424
19425/*
19426 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19427 * Otherwise, restore the value to "oldval" and return NULL.
19428 * Must always be called in pairs to save and restore v:throwpoint! Does not
19429 * take care of memory allocations.
19430 */
19431 char_u *
19432v_throwpoint(oldval)
19433 char_u *oldval;
19434{
19435 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019436 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019437
Bram Moolenaare9a41262005-01-15 22:18:47 +000019438 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439 return NULL;
19440}
19441
19442#if defined(FEAT_AUTOCMD) || defined(PROTO)
19443/*
19444 * Set v:cmdarg.
19445 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19446 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19447 * Must always be called in pairs!
19448 */
19449 char_u *
19450set_cmdarg(eap, oldarg)
19451 exarg_T *eap;
19452 char_u *oldarg;
19453{
19454 char_u *oldval;
19455 char_u *newval;
19456 unsigned len;
19457
Bram Moolenaare9a41262005-01-15 22:18:47 +000019458 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019459 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019460 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019461 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019462 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019463 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019464 }
19465
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019466 if (eap->force_bin == FORCE_BIN)
19467 len = 6;
19468 else if (eap->force_bin == FORCE_NOBIN)
19469 len = 8;
19470 else
19471 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019472
19473 if (eap->read_edit)
19474 len += 7;
19475
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019476 if (eap->force_ff != 0)
19477 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19478# ifdef FEAT_MBYTE
19479 if (eap->force_enc != 0)
19480 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019481 if (eap->bad_char != 0)
19482 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019483# endif
19484
19485 newval = alloc(len + 1);
19486 if (newval == NULL)
19487 return NULL;
19488
19489 if (eap->force_bin == FORCE_BIN)
19490 sprintf((char *)newval, " ++bin");
19491 else if (eap->force_bin == FORCE_NOBIN)
19492 sprintf((char *)newval, " ++nobin");
19493 else
19494 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019495
19496 if (eap->read_edit)
19497 STRCAT(newval, " ++edit");
19498
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019499 if (eap->force_ff != 0)
19500 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19501 eap->cmd + eap->force_ff);
19502# ifdef FEAT_MBYTE
19503 if (eap->force_enc != 0)
19504 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19505 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019506 if (eap->bad_char == BAD_KEEP)
19507 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19508 else if (eap->bad_char == BAD_DROP)
19509 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19510 else if (eap->bad_char != 0)
19511 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019512# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019513 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019514 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515}
19516#endif
19517
19518/*
19519 * Get the value of internal variable "name".
19520 * Return OK or FAIL.
19521 */
19522 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019523get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524 char_u *name;
19525 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019526 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019527 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528{
19529 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019530 typval_T *tv = NULL;
19531 typval_T atv;
19532 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019533 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534
19535 /* truncate the name, so that we can use strcmp() */
19536 cc = name[len];
19537 name[len] = NUL;
19538
19539 /*
19540 * Check for "b:changedtick".
19541 */
19542 if (STRCMP(name, "b:changedtick") == 0)
19543 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019544 atv.v_type = VAR_NUMBER;
19545 atv.vval.v_number = curbuf->b_changedtick;
19546 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547 }
19548
19549 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550 * Check for user-defined variables.
19551 */
19552 else
19553 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019554 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019556 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557 }
19558
Bram Moolenaare9a41262005-01-15 22:18:47 +000019559 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019561 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019562 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563 ret = FAIL;
19564 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019565 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019566 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019567
19568 name[len] = cc;
19569
19570 return ret;
19571}
19572
19573/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019574 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19575 * Also handle function call with Funcref variable: func(expr)
19576 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19577 */
19578 static int
19579handle_subscript(arg, rettv, evaluate, verbose)
19580 char_u **arg;
19581 typval_T *rettv;
19582 int evaluate; /* do more than finding the end */
19583 int verbose; /* give error messages */
19584{
19585 int ret = OK;
19586 dict_T *selfdict = NULL;
19587 char_u *s;
19588 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019589 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019590
19591 while (ret == OK
19592 && (**arg == '['
19593 || (**arg == '.' && rettv->v_type == VAR_DICT)
19594 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19595 && !vim_iswhite(*(*arg - 1)))
19596 {
19597 if (**arg == '(')
19598 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019599 /* need to copy the funcref so that we can clear rettv */
19600 functv = *rettv;
19601 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019602
19603 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019604 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019605 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019606 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19607 &len, evaluate, selfdict);
19608
19609 /* Clear the funcref afterwards, so that deleting it while
19610 * evaluating the arguments is possible (see test55). */
19611 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019612
19613 /* Stop the expression evaluation when immediately aborting on
19614 * error, or when an interrupt occurred or an exception was thrown
19615 * but not caught. */
19616 if (aborting())
19617 {
19618 if (ret == OK)
19619 clear_tv(rettv);
19620 ret = FAIL;
19621 }
19622 dict_unref(selfdict);
19623 selfdict = NULL;
19624 }
19625 else /* **arg == '[' || **arg == '.' */
19626 {
19627 dict_unref(selfdict);
19628 if (rettv->v_type == VAR_DICT)
19629 {
19630 selfdict = rettv->vval.v_dict;
19631 if (selfdict != NULL)
19632 ++selfdict->dv_refcount;
19633 }
19634 else
19635 selfdict = NULL;
19636 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19637 {
19638 clear_tv(rettv);
19639 ret = FAIL;
19640 }
19641 }
19642 }
19643 dict_unref(selfdict);
19644 return ret;
19645}
19646
19647/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019648 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019649 * value).
19650 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019651 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019652alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019653{
Bram Moolenaar33570922005-01-25 22:26:29 +000019654 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019655}
19656
19657/*
19658 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019659 * The string "s" must have been allocated, it is consumed.
19660 * Return NULL for out of memory, the variable otherwise.
19661 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019662 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019663alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019664 char_u *s;
19665{
Bram Moolenaar33570922005-01-25 22:26:29 +000019666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019668 rettv = alloc_tv();
19669 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019671 rettv->v_type = VAR_STRING;
19672 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673 }
19674 else
19675 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019676 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677}
19678
19679/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019680 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019682 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019683free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019684 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685{
19686 if (varp != NULL)
19687 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019688 switch (varp->v_type)
19689 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019690 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019691 func_unref(varp->vval.v_string);
19692 /*FALLTHROUGH*/
19693 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019694 vim_free(varp->vval.v_string);
19695 break;
19696 case VAR_LIST:
19697 list_unref(varp->vval.v_list);
19698 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019699 case VAR_DICT:
19700 dict_unref(varp->vval.v_dict);
19701 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019702 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019703#ifdef FEAT_FLOAT
19704 case VAR_FLOAT:
19705#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019706 case VAR_UNKNOWN:
19707 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019708 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019709 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019710 break;
19711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019712 vim_free(varp);
19713 }
19714}
19715
19716/*
19717 * Free the memory for a variable value and set the value to NULL or 0.
19718 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019719 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019720clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019721 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722{
19723 if (varp != NULL)
19724 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019725 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019726 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019727 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019728 func_unref(varp->vval.v_string);
19729 /*FALLTHROUGH*/
19730 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019731 vim_free(varp->vval.v_string);
19732 varp->vval.v_string = NULL;
19733 break;
19734 case VAR_LIST:
19735 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019736 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019737 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019738 case VAR_DICT:
19739 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019740 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019741 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019742 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019743 varp->vval.v_number = 0;
19744 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019745#ifdef FEAT_FLOAT
19746 case VAR_FLOAT:
19747 varp->vval.v_float = 0.0;
19748 break;
19749#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019750 case VAR_UNKNOWN:
19751 break;
19752 default:
19753 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019755 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756 }
19757}
19758
19759/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019760 * Set the value of a variable to NULL without freeing items.
19761 */
19762 static void
19763init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019764 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019765{
19766 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019767 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019768}
19769
19770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771 * Get the number value of a variable.
19772 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019773 * For incompatible types, return 0.
19774 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19775 * caller of incompatible types: it sets *denote to TRUE if "denote"
19776 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777 */
19778 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019779get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019780 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019782 int error = FALSE;
19783
19784 return get_tv_number_chk(varp, &error); /* return 0L on error */
19785}
19786
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019787 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019788get_tv_number_chk(varp, denote)
19789 typval_T *varp;
19790 int *denote;
19791{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019792 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019794 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019796 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019797 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019798#ifdef FEAT_FLOAT
19799 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019800 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019801 break;
19802#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019803 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019804 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019805 break;
19806 case VAR_STRING:
19807 if (varp->vval.v_string != NULL)
19808 vim_str2nr(varp->vval.v_string, NULL, NULL,
19809 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019810 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019811 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019812 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019813 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019814 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019815 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019816 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019817 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019818 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019819 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019821 if (denote == NULL) /* useful for values that must be unsigned */
19822 n = -1;
19823 else
19824 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019825 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826}
19827
19828/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019829 * Get the lnum from the first argument.
19830 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019831 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832 */
19833 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019834get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019835 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836{
Bram Moolenaar33570922005-01-25 22:26:29 +000019837 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838 linenr_T lnum;
19839
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019840 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841 if (lnum == 0) /* no valid number, try using line() */
19842 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019843 rettv.v_type = VAR_NUMBER;
19844 f_line(argvars, &rettv);
19845 lnum = rettv.vval.v_number;
19846 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847 }
19848 return lnum;
19849}
19850
19851/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019852 * Get the lnum from the first argument.
19853 * Also accepts "$", then "buf" is used.
19854 * Returns 0 on error.
19855 */
19856 static linenr_T
19857get_tv_lnum_buf(argvars, buf)
19858 typval_T *argvars;
19859 buf_T *buf;
19860{
19861 if (argvars[0].v_type == VAR_STRING
19862 && argvars[0].vval.v_string != NULL
19863 && argvars[0].vval.v_string[0] == '$'
19864 && buf != NULL)
19865 return buf->b_ml.ml_line_count;
19866 return get_tv_number_chk(&argvars[0], NULL);
19867}
19868
19869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019870 * Get the string value of a variable.
19871 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019872 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19873 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874 * If the String variable has never been set, return an empty string.
19875 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019876 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19877 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878 */
19879 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019880get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019881 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019882{
19883 static char_u mybuf[NUMBUFLEN];
19884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019885 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019886}
19887
19888 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019889get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019890 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019891 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019893 char_u *res = get_tv_string_buf_chk(varp, buf);
19894
19895 return res != NULL ? res : (char_u *)"";
19896}
19897
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019898 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019899get_tv_string_chk(varp)
19900 typval_T *varp;
19901{
19902 static char_u mybuf[NUMBUFLEN];
19903
19904 return get_tv_string_buf_chk(varp, mybuf);
19905}
19906
19907 static char_u *
19908get_tv_string_buf_chk(varp, buf)
19909 typval_T *varp;
19910 char_u *buf;
19911{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019912 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019913 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019914 case VAR_NUMBER:
19915 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19916 return buf;
19917 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019918 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019919 break;
19920 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019921 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019922 break;
19923 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019924 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019925 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019926#ifdef FEAT_FLOAT
19927 case VAR_FLOAT:
19928 EMSG(_("E806: using Float as a String"));
19929 break;
19930#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019931 case VAR_STRING:
19932 if (varp->vval.v_string != NULL)
19933 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019934 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019935 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019936 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019937 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019939 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940}
19941
19942/*
19943 * Find variable "name" in the list of variables.
19944 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019945 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019946 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019947 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019949 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019950find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019952 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019954 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019955 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019956
Bram Moolenaara7043832005-01-21 11:56:39 +000019957 ht = find_var_ht(name, &varname);
19958 if (htp != NULL)
19959 *htp = ht;
19960 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019961 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019962 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963}
19964
19965/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019966 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019967 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019969 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019970find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019971 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019972 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019973 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019974{
Bram Moolenaar33570922005-01-25 22:26:29 +000019975 hashitem_T *hi;
19976
19977 if (*varname == NUL)
19978 {
19979 /* Must be something like "s:", otherwise "ht" would be NULL. */
19980 switch (varname[-2])
19981 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019982 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019983 case 'g': return &globvars_var;
19984 case 'v': return &vimvars_var;
19985 case 'b': return &curbuf->b_bufvar;
19986 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019987#ifdef FEAT_WINDOWS
19988 case 't': return &curtab->tp_winvar;
19989#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019990 case 'l': return current_funccal == NULL
19991 ? NULL : &current_funccal->l_vars_var;
19992 case 'a': return current_funccal == NULL
19993 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019994 }
19995 return NULL;
19996 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019997
19998 hi = hash_find(ht, varname);
19999 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020000 {
20001 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020002 * worked find the variable again. Don't auto-load a script if it was
20003 * loaded already, otherwise it would be loaded every time when
20004 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020005 if (ht == &globvarht && !writing)
20006 {
20007 /* Note: script_autoload() may make "hi" invalid. It must either
20008 * be obtained again or not used. */
20009 if (!script_autoload(varname, FALSE) || aborting())
20010 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020011 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020012 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020013 if (HASHITEM_EMPTY(hi))
20014 return NULL;
20015 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020016 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020017}
20018
20019/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020020 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020021 * Set "varname" to the start of name without ':'.
20022 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020023 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020024find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025 char_u *name;
20026 char_u **varname;
20027{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020028 hashitem_T *hi;
20029
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030 if (name[1] != ':')
20031 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020032 /* The name must not start with a colon or #. */
20033 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034 return NULL;
20035 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020036
20037 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020038 hi = hash_find(&compat_hashtab, name);
20039 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020040 return &compat_hashtab;
20041
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020043 return &globvarht; /* global variable */
20044 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045 }
20046 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020047 if (*name == 'g') /* global variable */
20048 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020049 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20050 */
20051 if (vim_strchr(name + 2, ':') != NULL
20052 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020053 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020054 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000020055 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000020057 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020058#ifdef FEAT_WINDOWS
20059 if (*name == 't') /* tab page variable */
20060 return &curtab->tp_vars.dv_hashtab;
20061#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020062 if (*name == 'v') /* v: variable */
20063 return &vimvarht;
20064 if (*name == 'a' && current_funccal != NULL) /* function argument */
20065 return &current_funccal->l_avars.dv_hashtab;
20066 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20067 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020068 if (*name == 's' /* script variable */
20069 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20070 return &SCRIPT_VARS(current_SID);
20071 return NULL;
20072}
20073
20074/*
20075 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020076 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077 * Returns NULL when it doesn't exist.
20078 */
20079 char_u *
20080get_var_value(name)
20081 char_u *name;
20082{
Bram Moolenaar33570922005-01-25 22:26:29 +000020083 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084
Bram Moolenaara7043832005-01-21 11:56:39 +000020085 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020086 if (v == NULL)
20087 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020088 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089}
20090
20091/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020092 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093 * sourcing this script and when executing functions defined in the script.
20094 */
20095 void
20096new_script_vars(id)
20097 scid_T id;
20098{
Bram Moolenaara7043832005-01-21 11:56:39 +000020099 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020100 hashtab_T *ht;
20101 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020102
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20104 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020105 /* Re-allocating ga_data means that an ht_array pointing to
20106 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020107 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020108 for (i = 1; i <= ga_scripts.ga_len; ++i)
20109 {
20110 ht = &SCRIPT_VARS(i);
20111 if (ht->ht_mask == HT_INIT_SIZE - 1)
20112 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020113 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020114 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020115 }
20116
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117 while (ga_scripts.ga_len < id)
20118 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020119 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020120 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020121 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020123 }
20124 }
20125}
20126
20127/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020128 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20129 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 */
20131 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020132init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020133 dict_T *dict;
20134 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020135 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136{
Bram Moolenaar33570922005-01-25 22:26:29 +000020137 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020138 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020139 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020140 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020141 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020142 dict_var->di_tv.vval.v_dict = dict;
20143 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020144 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020145 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20146 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020147}
20148
20149/*
20150 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020151 * Frees all allocated variables and the value they contain.
20152 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153 */
20154 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020155vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020156 hashtab_T *ht;
20157{
20158 vars_clear_ext(ht, TRUE);
20159}
20160
20161/*
20162 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20163 */
20164 static void
20165vars_clear_ext(ht, free_val)
20166 hashtab_T *ht;
20167 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168{
Bram Moolenaara7043832005-01-21 11:56:39 +000020169 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020170 hashitem_T *hi;
20171 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020172
Bram Moolenaar33570922005-01-25 22:26:29 +000020173 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020174 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020175 for (hi = ht->ht_array; todo > 0; ++hi)
20176 {
20177 if (!HASHITEM_EMPTY(hi))
20178 {
20179 --todo;
20180
Bram Moolenaar33570922005-01-25 22:26:29 +000020181 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020182 * ht_array might change then. hash_clear() takes care of it
20183 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020184 v = HI2DI(hi);
20185 if (free_val)
20186 clear_tv(&v->di_tv);
20187 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20188 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020189 }
20190 }
20191 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020192 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020193}
20194
Bram Moolenaara7043832005-01-21 11:56:39 +000020195/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020196 * Delete a variable from hashtab "ht" at item "hi".
20197 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020200delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020201 hashtab_T *ht;
20202 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203{
Bram Moolenaar33570922005-01-25 22:26:29 +000020204 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020205
20206 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020207 clear_tv(&di->di_tv);
20208 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020209}
20210
20211/*
20212 * List the value of one internal variable.
20213 */
20214 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020215list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020216 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020218 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020220 char_u *tofree;
20221 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020222 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020223
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020224 current_copyID += COPYID_INC;
20225 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020226 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020227 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020228 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229}
20230
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020232list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020233 char_u *prefix;
20234 char_u *name;
20235 int type;
20236 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020237 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238{
Bram Moolenaar31859182007-08-14 20:41:13 +000020239 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20240 msg_start();
20241 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 if (name != NULL) /* "a:" vars don't have a name stored */
20243 msg_puts(name);
20244 msg_putchar(' ');
20245 msg_advance(22);
20246 if (type == VAR_NUMBER)
20247 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020248 else if (type == VAR_FUNC)
20249 msg_putchar('*');
20250 else if (type == VAR_LIST)
20251 {
20252 msg_putchar('[');
20253 if (*string == '[')
20254 ++string;
20255 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020256 else if (type == VAR_DICT)
20257 {
20258 msg_putchar('{');
20259 if (*string == '{')
20260 ++string;
20261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262 else
20263 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020264
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020266
20267 if (type == VAR_FUNC)
20268 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020269 if (*first)
20270 {
20271 msg_clr_eos();
20272 *first = FALSE;
20273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274}
20275
20276/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020277 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278 * If the variable already exists, the value is updated.
20279 * Otherwise the variable is created.
20280 */
20281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020282set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020284 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020285 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286{
Bram Moolenaar33570922005-01-25 22:26:29 +000020287 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020289 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020291 ht = find_var_ht(name, &varname);
20292 if (ht == NULL || *varname == NUL)
20293 {
20294 EMSG2(_(e_illvar), name);
20295 return;
20296 }
20297 v = find_var_in_ht(ht, varname, TRUE);
20298
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020299 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20300 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020301
Bram Moolenaar33570922005-01-25 22:26:29 +000020302 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020304 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020305 if (var_check_ro(v->di_flags, name)
20306 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020307 return;
20308 if (v->di_tv.v_type != tv->v_type
20309 && !((v->di_tv.v_type == VAR_STRING
20310 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020311 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020312 || tv->v_type == VAR_NUMBER))
20313#ifdef FEAT_FLOAT
20314 && !((v->di_tv.v_type == VAR_NUMBER
20315 || v->di_tv.v_type == VAR_FLOAT)
20316 && (tv->v_type == VAR_NUMBER
20317 || tv->v_type == VAR_FLOAT))
20318#endif
20319 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020320 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020321 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020322 return;
20323 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020324
20325 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020326 * Handle setting internal v: variables separately: we don't change
20327 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020328 */
20329 if (ht == &vimvarht)
20330 {
20331 if (v->di_tv.v_type == VAR_STRING)
20332 {
20333 vim_free(v->di_tv.vval.v_string);
20334 if (copy || tv->v_type != VAR_STRING)
20335 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20336 else
20337 {
20338 /* Take over the string to avoid an extra alloc/free. */
20339 v->di_tv.vval.v_string = tv->vval.v_string;
20340 tv->vval.v_string = NULL;
20341 }
20342 }
20343 else if (v->di_tv.v_type != VAR_NUMBER)
20344 EMSG2(_(e_intern2), "set_var()");
20345 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020346 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020347 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020348 if (STRCMP(varname, "searchforward") == 0)
20349 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20350 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020351 return;
20352 }
20353
20354 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355 }
20356 else /* add a new variable */
20357 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020358 /* Can't add "v:" variable. */
20359 if (ht == &vimvarht)
20360 {
20361 EMSG2(_(e_illvar), name);
20362 return;
20363 }
20364
Bram Moolenaar92124a32005-06-17 22:03:40 +000020365 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020366 if (!valid_varname(varname))
20367 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020368
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020369 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20370 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020371 if (v == NULL)
20372 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020373 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020374 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020376 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 return;
20378 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020379 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020381
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020382 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020383 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020384 else
20385 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020386 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020387 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020388 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390}
20391
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020392/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020393 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020394 * Also give an error message.
20395 */
20396 static int
20397var_check_ro(flags, name)
20398 int flags;
20399 char_u *name;
20400{
20401 if (flags & DI_FLAGS_RO)
20402 {
20403 EMSG2(_(e_readonlyvar), name);
20404 return TRUE;
20405 }
20406 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20407 {
20408 EMSG2(_(e_readonlysbx), name);
20409 return TRUE;
20410 }
20411 return FALSE;
20412}
20413
20414/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020415 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20416 * Also give an error message.
20417 */
20418 static int
20419var_check_fixed(flags, name)
20420 int flags;
20421 char_u *name;
20422{
20423 if (flags & DI_FLAGS_FIX)
20424 {
20425 EMSG2(_("E795: Cannot delete variable %s"), name);
20426 return TRUE;
20427 }
20428 return FALSE;
20429}
20430
20431/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020432 * Check if a funcref is assigned to a valid variable name.
20433 * Return TRUE and give an error if not.
20434 */
20435 static int
20436var_check_func_name(name, new_var)
20437 char_u *name; /* points to start of variable name */
20438 int new_var; /* TRUE when creating the variable */
20439{
20440 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20441 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20442 ? name[2] : name[0]))
20443 {
20444 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20445 name);
20446 return TRUE;
20447 }
20448 /* Don't allow hiding a function. When "v" is not NULL we might be
20449 * assigning another function to the same var, the type is checked
20450 * below. */
20451 if (new_var && function_exists(name))
20452 {
20453 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20454 name);
20455 return TRUE;
20456 }
20457 return FALSE;
20458}
20459
20460/*
20461 * Check if a variable name is valid.
20462 * Return FALSE and give an error if not.
20463 */
20464 static int
20465valid_varname(varname)
20466 char_u *varname;
20467{
20468 char_u *p;
20469
20470 for (p = varname; *p != NUL; ++p)
20471 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20472 && *p != AUTOLOAD_CHAR)
20473 {
20474 EMSG2(_(e_illvar), varname);
20475 return FALSE;
20476 }
20477 return TRUE;
20478}
20479
20480/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020481 * Return TRUE if typeval "tv" is set to be locked (immutable).
20482 * Also give an error message, using "name".
20483 */
20484 static int
20485tv_check_lock(lock, name)
20486 int lock;
20487 char_u *name;
20488{
20489 if (lock & VAR_LOCKED)
20490 {
20491 EMSG2(_("E741: Value is locked: %s"),
20492 name == NULL ? (char_u *)_("Unknown") : name);
20493 return TRUE;
20494 }
20495 if (lock & VAR_FIXED)
20496 {
20497 EMSG2(_("E742: Cannot change value of %s"),
20498 name == NULL ? (char_u *)_("Unknown") : name);
20499 return TRUE;
20500 }
20501 return FALSE;
20502}
20503
20504/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020505 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020506 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020507 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020508 * It is OK for "from" and "to" to point to the same item. This is used to
20509 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020510 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020511 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020512copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020513 typval_T *from;
20514 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020515{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020516 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020517 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020518 switch (from->v_type)
20519 {
20520 case VAR_NUMBER:
20521 to->vval.v_number = from->vval.v_number;
20522 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020523#ifdef FEAT_FLOAT
20524 case VAR_FLOAT:
20525 to->vval.v_float = from->vval.v_float;
20526 break;
20527#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020528 case VAR_STRING:
20529 case VAR_FUNC:
20530 if (from->vval.v_string == NULL)
20531 to->vval.v_string = NULL;
20532 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020533 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020534 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020535 if (from->v_type == VAR_FUNC)
20536 func_ref(to->vval.v_string);
20537 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020538 break;
20539 case VAR_LIST:
20540 if (from->vval.v_list == NULL)
20541 to->vval.v_list = NULL;
20542 else
20543 {
20544 to->vval.v_list = from->vval.v_list;
20545 ++to->vval.v_list->lv_refcount;
20546 }
20547 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020548 case VAR_DICT:
20549 if (from->vval.v_dict == NULL)
20550 to->vval.v_dict = NULL;
20551 else
20552 {
20553 to->vval.v_dict = from->vval.v_dict;
20554 ++to->vval.v_dict->dv_refcount;
20555 }
20556 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020557 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020558 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020559 break;
20560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020561}
20562
20563/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020564 * Make a copy of an item.
20565 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020566 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20567 * reference to an already copied list/dict can be used.
20568 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020569 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020570 static int
20571item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020572 typval_T *from;
20573 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020574 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020575 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020576{
20577 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020578 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020579
Bram Moolenaar33570922005-01-25 22:26:29 +000020580 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020581 {
20582 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020583 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020584 }
20585 ++recurse;
20586
20587 switch (from->v_type)
20588 {
20589 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020590#ifdef FEAT_FLOAT
20591 case VAR_FLOAT:
20592#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020593 case VAR_STRING:
20594 case VAR_FUNC:
20595 copy_tv(from, to);
20596 break;
20597 case VAR_LIST:
20598 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020599 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020600 if (from->vval.v_list == NULL)
20601 to->vval.v_list = NULL;
20602 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20603 {
20604 /* use the copy made earlier */
20605 to->vval.v_list = from->vval.v_list->lv_copylist;
20606 ++to->vval.v_list->lv_refcount;
20607 }
20608 else
20609 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20610 if (to->vval.v_list == NULL)
20611 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020612 break;
20613 case VAR_DICT:
20614 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020615 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020616 if (from->vval.v_dict == NULL)
20617 to->vval.v_dict = NULL;
20618 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20619 {
20620 /* use the copy made earlier */
20621 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20622 ++to->vval.v_dict->dv_refcount;
20623 }
20624 else
20625 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20626 if (to->vval.v_dict == NULL)
20627 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020628 break;
20629 default:
20630 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020631 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020632 }
20633 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020634 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020635}
20636
20637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638 * ":echo expr1 ..." print each argument separated with a space, add a
20639 * newline at the end.
20640 * ":echon expr1 ..." print each argument plain.
20641 */
20642 void
20643ex_echo(eap)
20644 exarg_T *eap;
20645{
20646 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020647 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020648 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020649 char_u *p;
20650 int needclr = TRUE;
20651 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020652 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020653
20654 if (eap->skip)
20655 ++emsg_skip;
20656 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20657 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020658 /* If eval1() causes an error message the text from the command may
20659 * still need to be cleared. E.g., "echo 22,44". */
20660 need_clr_eos = needclr;
20661
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020663 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664 {
20665 /*
20666 * Report the invalid expression unless the expression evaluation
20667 * has been cancelled due to an aborting error, an interrupt, or an
20668 * exception.
20669 */
20670 if (!aborting())
20671 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020672 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020673 break;
20674 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020675 need_clr_eos = FALSE;
20676
Bram Moolenaar071d4272004-06-13 20:20:40 +000020677 if (!eap->skip)
20678 {
20679 if (atstart)
20680 {
20681 atstart = FALSE;
20682 /* Call msg_start() after eval1(), evaluating the expression
20683 * may cause a message to appear. */
20684 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020685 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020686 /* Mark the saved text as finishing the line, so that what
20687 * follows is displayed on a new line when scrolling back
20688 * at the more prompt. */
20689 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020690 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020692 }
20693 else if (eap->cmdidx == CMD_echo)
20694 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020695 current_copyID += COPYID_INC;
20696 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020697 if (p != NULL)
20698 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020699 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020700 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020701 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020702 if (*p != TAB && needclr)
20703 {
20704 /* remove any text still there from the command */
20705 msg_clr_eos();
20706 needclr = FALSE;
20707 }
20708 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020709 }
20710 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020711 {
20712#ifdef FEAT_MBYTE
20713 if (has_mbyte)
20714 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020715 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020716
20717 (void)msg_outtrans_len_attr(p, i, echo_attr);
20718 p += i - 1;
20719 }
20720 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020721#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020722 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020724 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020725 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020727 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020728 arg = skipwhite(arg);
20729 }
20730 eap->nextcmd = check_nextcmd(arg);
20731
20732 if (eap->skip)
20733 --emsg_skip;
20734 else
20735 {
20736 /* remove text that may still be there from the command */
20737 if (needclr)
20738 msg_clr_eos();
20739 if (eap->cmdidx == CMD_echo)
20740 msg_end();
20741 }
20742}
20743
20744/*
20745 * ":echohl {name}".
20746 */
20747 void
20748ex_echohl(eap)
20749 exarg_T *eap;
20750{
20751 int id;
20752
20753 id = syn_name2id(eap->arg);
20754 if (id == 0)
20755 echo_attr = 0;
20756 else
20757 echo_attr = syn_id2attr(id);
20758}
20759
20760/*
20761 * ":execute expr1 ..." execute the result of an expression.
20762 * ":echomsg expr1 ..." Print a message
20763 * ":echoerr expr1 ..." Print an error
20764 * Each gets spaces around each argument and a newline at the end for
20765 * echo commands
20766 */
20767 void
20768ex_execute(eap)
20769 exarg_T *eap;
20770{
20771 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020772 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773 int ret = OK;
20774 char_u *p;
20775 garray_T ga;
20776 int len;
20777 int save_did_emsg;
20778
20779 ga_init2(&ga, 1, 80);
20780
20781 if (eap->skip)
20782 ++emsg_skip;
20783 while (*arg != NUL && *arg != '|' && *arg != '\n')
20784 {
20785 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020786 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787 {
20788 /*
20789 * Report the invalid expression unless the expression evaluation
20790 * has been cancelled due to an aborting error, an interrupt, or an
20791 * exception.
20792 */
20793 if (!aborting())
20794 EMSG2(_(e_invexpr2), p);
20795 ret = FAIL;
20796 break;
20797 }
20798
20799 if (!eap->skip)
20800 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020801 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020802 len = (int)STRLEN(p);
20803 if (ga_grow(&ga, len + 2) == FAIL)
20804 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020805 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020806 ret = FAIL;
20807 break;
20808 }
20809 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020812 ga.ga_len += len;
20813 }
20814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020815 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020816 arg = skipwhite(arg);
20817 }
20818
20819 if (ret != FAIL && ga.ga_data != NULL)
20820 {
20821 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020822 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020824 out_flush();
20825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826 else if (eap->cmdidx == CMD_echoerr)
20827 {
20828 /* We don't want to abort following commands, restore did_emsg. */
20829 save_did_emsg = did_emsg;
20830 EMSG((char_u *)ga.ga_data);
20831 if (!force_abort)
20832 did_emsg = save_did_emsg;
20833 }
20834 else if (eap->cmdidx == CMD_execute)
20835 do_cmdline((char_u *)ga.ga_data,
20836 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20837 }
20838
20839 ga_clear(&ga);
20840
20841 if (eap->skip)
20842 --emsg_skip;
20843
20844 eap->nextcmd = check_nextcmd(arg);
20845}
20846
20847/*
20848 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20849 * "arg" points to the "&" or '+' when called, to "option" when returning.
20850 * Returns NULL when no option name found. Otherwise pointer to the char
20851 * after the option name.
20852 */
20853 static char_u *
20854find_option_end(arg, opt_flags)
20855 char_u **arg;
20856 int *opt_flags;
20857{
20858 char_u *p = *arg;
20859
20860 ++p;
20861 if (*p == 'g' && p[1] == ':')
20862 {
20863 *opt_flags = OPT_GLOBAL;
20864 p += 2;
20865 }
20866 else if (*p == 'l' && p[1] == ':')
20867 {
20868 *opt_flags = OPT_LOCAL;
20869 p += 2;
20870 }
20871 else
20872 *opt_flags = 0;
20873
20874 if (!ASCII_ISALPHA(*p))
20875 return NULL;
20876 *arg = p;
20877
20878 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20879 p += 4; /* termcap option */
20880 else
20881 while (ASCII_ISALPHA(*p))
20882 ++p;
20883 return p;
20884}
20885
20886/*
20887 * ":function"
20888 */
20889 void
20890ex_function(eap)
20891 exarg_T *eap;
20892{
20893 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020894 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020895 int j;
20896 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020897 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020898 char_u *name = NULL;
20899 char_u *p;
20900 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020901 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902 garray_T newargs;
20903 garray_T newlines;
20904 int varargs = FALSE;
20905 int mustend = FALSE;
20906 int flags = 0;
20907 ufunc_T *fp;
20908 int indent;
20909 int nesting;
20910 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020911 dictitem_T *v;
20912 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020913 static int func_nr = 0; /* number for nameless function */
20914 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020915 hashtab_T *ht;
20916 int todo;
20917 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020918 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919
20920 /*
20921 * ":function" without argument: list functions.
20922 */
20923 if (ends_excmd(*eap->arg))
20924 {
20925 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020926 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020927 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020928 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020929 {
20930 if (!HASHITEM_EMPTY(hi))
20931 {
20932 --todo;
20933 fp = HI2UF(hi);
20934 if (!isdigit(*fp->uf_name))
20935 list_func_head(fp, FALSE);
20936 }
20937 }
20938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939 eap->nextcmd = check_nextcmd(eap->arg);
20940 return;
20941 }
20942
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020943 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020944 * ":function /pat": list functions matching pattern.
20945 */
20946 if (*eap->arg == '/')
20947 {
20948 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20949 if (!eap->skip)
20950 {
20951 regmatch_T regmatch;
20952
20953 c = *p;
20954 *p = NUL;
20955 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20956 *p = c;
20957 if (regmatch.regprog != NULL)
20958 {
20959 regmatch.rm_ic = p_ic;
20960
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020961 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020962 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20963 {
20964 if (!HASHITEM_EMPTY(hi))
20965 {
20966 --todo;
20967 fp = HI2UF(hi);
20968 if (!isdigit(*fp->uf_name)
20969 && vim_regexec(&regmatch, fp->uf_name, 0))
20970 list_func_head(fp, FALSE);
20971 }
20972 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020973 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020974 }
20975 }
20976 if (*p == '/')
20977 ++p;
20978 eap->nextcmd = check_nextcmd(p);
20979 return;
20980 }
20981
20982 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020983 * Get the function name. There are these situations:
20984 * func normal function name
20985 * "name" == func, "fudi.fd_dict" == NULL
20986 * dict.func new dictionary entry
20987 * "name" == NULL, "fudi.fd_dict" set,
20988 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20989 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020990 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020991 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20992 * dict.func existing dict entry that's not a Funcref
20993 * "name" == NULL, "fudi.fd_dict" set,
20994 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020997 name = trans_function_name(&p, eap->skip, 0, &fudi);
20998 paren = (vim_strchr(p, '(') != NULL);
20999 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021000 {
21001 /*
21002 * Return on an invalid expression in braces, unless the expression
21003 * evaluation has been cancelled due to an aborting error, an
21004 * interrupt, or an exception.
21005 */
21006 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021007 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021008 if (!eap->skip && fudi.fd_newkey != NULL)
21009 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021010 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021011 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021013 else
21014 eap->skip = TRUE;
21015 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021016
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 /* An error in a function call during evaluation of an expression in magic
21018 * braces should not cause the function not to be defined. */
21019 saved_did_emsg = did_emsg;
21020 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021
21022 /*
21023 * ":function func" with only function name: list function.
21024 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021025 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026 {
21027 if (!ends_excmd(*skipwhite(p)))
21028 {
21029 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021030 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031 }
21032 eap->nextcmd = check_nextcmd(p);
21033 if (eap->nextcmd != NULL)
21034 *p = NUL;
21035 if (!eap->skip && !got_int)
21036 {
21037 fp = find_func(name);
21038 if (fp != NULL)
21039 {
21040 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021041 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021043 if (FUNCLINE(fp, j) == NULL)
21044 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021045 msg_putchar('\n');
21046 msg_outnum((long)(j + 1));
21047 if (j < 9)
21048 msg_putchar(' ');
21049 if (j < 99)
21050 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021051 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052 out_flush(); /* show a line at a time */
21053 ui_breakcheck();
21054 }
21055 if (!got_int)
21056 {
21057 msg_putchar('\n');
21058 msg_puts((char_u *)" endfunction");
21059 }
21060 }
21061 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021062 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021064 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021065 }
21066
21067 /*
21068 * ":function name(arg1, arg2)" Define function.
21069 */
21070 p = skipwhite(p);
21071 if (*p != '(')
21072 {
21073 if (!eap->skip)
21074 {
21075 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021076 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077 }
21078 /* attempt to continue by skipping some text */
21079 if (vim_strchr(p, '(') != NULL)
21080 p = vim_strchr(p, '(');
21081 }
21082 p = skipwhite(p + 1);
21083
21084 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21085 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21086
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021087 if (!eap->skip)
21088 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021089 /* Check the name of the function. Unless it's a dictionary function
21090 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021091 if (name != NULL)
21092 arg = name;
21093 else
21094 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021095 if (arg != NULL && (fudi.fd_di == NULL
21096 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021097 {
21098 if (*arg == K_SPECIAL)
21099 j = 3;
21100 else
21101 j = 0;
21102 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21103 : eval_isnamec(arg[j])))
21104 ++j;
21105 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021106 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021107 }
21108 }
21109
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110 /*
21111 * Isolate the arguments: "arg1, arg2, ...)"
21112 */
21113 while (*p != ')')
21114 {
21115 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21116 {
21117 varargs = TRUE;
21118 p += 3;
21119 mustend = TRUE;
21120 }
21121 else
21122 {
21123 arg = p;
21124 while (ASCII_ISALNUM(*p) || *p == '_')
21125 ++p;
21126 if (arg == p || isdigit(*arg)
21127 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21128 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21129 {
21130 if (!eap->skip)
21131 EMSG2(_("E125: Illegal argument: %s"), arg);
21132 break;
21133 }
21134 if (ga_grow(&newargs, 1) == FAIL)
21135 goto erret;
21136 c = *p;
21137 *p = NUL;
21138 arg = vim_strsave(arg);
21139 if (arg == NULL)
21140 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021141
21142 /* Check for duplicate argument name. */
21143 for (i = 0; i < newargs.ga_len; ++i)
21144 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21145 {
21146 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21147 goto erret;
21148 }
21149
Bram Moolenaar071d4272004-06-13 20:20:40 +000021150 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21151 *p = c;
21152 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153 if (*p == ',')
21154 ++p;
21155 else
21156 mustend = TRUE;
21157 }
21158 p = skipwhite(p);
21159 if (mustend && *p != ')')
21160 {
21161 if (!eap->skip)
21162 EMSG2(_(e_invarg2), eap->arg);
21163 break;
21164 }
21165 }
21166 ++p; /* skip the ')' */
21167
Bram Moolenaare9a41262005-01-15 22:18:47 +000021168 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021169 for (;;)
21170 {
21171 p = skipwhite(p);
21172 if (STRNCMP(p, "range", 5) == 0)
21173 {
21174 flags |= FC_RANGE;
21175 p += 5;
21176 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021177 else if (STRNCMP(p, "dict", 4) == 0)
21178 {
21179 flags |= FC_DICT;
21180 p += 4;
21181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021182 else if (STRNCMP(p, "abort", 5) == 0)
21183 {
21184 flags |= FC_ABORT;
21185 p += 5;
21186 }
21187 else
21188 break;
21189 }
21190
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021191 /* When there is a line break use what follows for the function body.
21192 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21193 if (*p == '\n')
21194 line_arg = p + 1;
21195 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196 EMSG(_(e_trailing));
21197
21198 /*
21199 * Read the body of the function, until ":endfunction" is found.
21200 */
21201 if (KeyTyped)
21202 {
21203 /* Check if the function already exists, don't let the user type the
21204 * whole function before telling him it doesn't work! For a script we
21205 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021206 if (!eap->skip && !eap->forceit)
21207 {
21208 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21209 EMSG(_(e_funcdict));
21210 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021211 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021214 if (!eap->skip && did_emsg)
21215 goto erret;
21216
Bram Moolenaar071d4272004-06-13 20:20:40 +000021217 msg_putchar('\n'); /* don't overwrite the function name */
21218 cmdline_row = msg_row;
21219 }
21220
21221 indent = 2;
21222 nesting = 0;
21223 for (;;)
21224 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021225 if (KeyTyped)
21226 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021228 sourcing_lnum_off = sourcing_lnum;
21229
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021230 if (line_arg != NULL)
21231 {
21232 /* Use eap->arg, split up in parts by line breaks. */
21233 theline = line_arg;
21234 p = vim_strchr(theline, '\n');
21235 if (p == NULL)
21236 line_arg += STRLEN(line_arg);
21237 else
21238 {
21239 *p = NUL;
21240 line_arg = p + 1;
21241 }
21242 }
21243 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244 theline = getcmdline(':', 0L, indent);
21245 else
21246 theline = eap->getline(':', eap->cookie, indent);
21247 if (KeyTyped)
21248 lines_left = Rows - 1;
21249 if (theline == NULL)
21250 {
21251 EMSG(_("E126: Missing :endfunction"));
21252 goto erret;
21253 }
21254
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021255 /* Detect line continuation: sourcing_lnum increased more than one. */
21256 if (sourcing_lnum > sourcing_lnum_off + 1)
21257 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21258 else
21259 sourcing_lnum_off = 0;
21260
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261 if (skip_until != NULL)
21262 {
21263 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21264 * don't check for ":endfunc". */
21265 if (STRCMP(theline, skip_until) == 0)
21266 {
21267 vim_free(skip_until);
21268 skip_until = NULL;
21269 }
21270 }
21271 else
21272 {
21273 /* skip ':' and blanks*/
21274 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21275 ;
21276
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021277 /* Check for "endfunction". */
21278 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021280 if (line_arg == NULL)
21281 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282 break;
21283 }
21284
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021285 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021286 * at "end". */
21287 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21288 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021289 else if (STRNCMP(p, "if", 2) == 0
21290 || STRNCMP(p, "wh", 2) == 0
21291 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021292 || STRNCMP(p, "try", 3) == 0)
21293 indent += 2;
21294
21295 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021296 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021298 if (*p == '!')
21299 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021300 p += eval_fname_script(p);
21301 if (ASCII_ISALPHA(*p))
21302 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021303 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304 if (*skipwhite(p) == '(')
21305 {
21306 ++nesting;
21307 indent += 2;
21308 }
21309 }
21310 }
21311
21312 /* Check for ":append" or ":insert". */
21313 p = skip_range(p, NULL);
21314 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21315 || (p[0] == 'i'
21316 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21317 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21318 skip_until = vim_strsave((char_u *)".");
21319
21320 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21321 arg = skipwhite(skiptowhite(p));
21322 if (arg[0] == '<' && arg[1] =='<'
21323 && ((p[0] == 'p' && p[1] == 'y'
21324 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21325 || (p[0] == 'p' && p[1] == 'e'
21326 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21327 || (p[0] == 't' && p[1] == 'c'
21328 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021329 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21330 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21332 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021333 || (p[0] == 'm' && p[1] == 'z'
21334 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021335 ))
21336 {
21337 /* ":python <<" continues until a dot, like ":append" */
21338 p = skipwhite(arg + 2);
21339 if (*p == NUL)
21340 skip_until = vim_strsave((char_u *)".");
21341 else
21342 skip_until = vim_strsave(p);
21343 }
21344 }
21345
21346 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021347 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021348 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021349 if (line_arg == NULL)
21350 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021351 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021352 }
21353
21354 /* Copy the line to newly allocated memory. get_one_sourceline()
21355 * allocates 250 bytes per line, this saves 80% on average. The cost
21356 * is an extra alloc/free. */
21357 p = vim_strsave(theline);
21358 if (p != NULL)
21359 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021360 if (line_arg == NULL)
21361 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021362 theline = p;
21363 }
21364
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021365 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21366
21367 /* Add NULL lines for continuation lines, so that the line count is
21368 * equal to the index in the growarray. */
21369 while (sourcing_lnum_off-- > 0)
21370 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021371
21372 /* Check for end of eap->arg. */
21373 if (line_arg != NULL && *line_arg == NUL)
21374 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021375 }
21376
21377 /* Don't define the function when skipping commands or when an error was
21378 * detected. */
21379 if (eap->skip || did_emsg)
21380 goto erret;
21381
21382 /*
21383 * If there are no errors, add the function
21384 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021385 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021386 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021387 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021388 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021389 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021390 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021391 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021392 goto erret;
21393 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021394
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021395 fp = find_func(name);
21396 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021398 if (!eap->forceit)
21399 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021400 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021401 goto erret;
21402 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021403 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021404 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021405 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021406 name);
21407 goto erret;
21408 }
21409 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021410 ga_clear_strings(&(fp->uf_args));
21411 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021412 vim_free(name);
21413 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415 }
21416 else
21417 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021418 char numbuf[20];
21419
21420 fp = NULL;
21421 if (fudi.fd_newkey == NULL && !eap->forceit)
21422 {
21423 EMSG(_(e_funcdict));
21424 goto erret;
21425 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021426 if (fudi.fd_di == NULL)
21427 {
21428 /* Can't add a function to a locked dictionary */
21429 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21430 goto erret;
21431 }
21432 /* Can't change an existing function if it is locked */
21433 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21434 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021435
21436 /* Give the function a sequential number. Can only be used with a
21437 * Funcref! */
21438 vim_free(name);
21439 sprintf(numbuf, "%d", ++func_nr);
21440 name = vim_strsave((char_u *)numbuf);
21441 if (name == NULL)
21442 goto erret;
21443 }
21444
21445 if (fp == NULL)
21446 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021447 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021448 {
21449 int slen, plen;
21450 char_u *scriptname;
21451
21452 /* Check that the autoload name matches the script name. */
21453 j = FAIL;
21454 if (sourcing_name != NULL)
21455 {
21456 scriptname = autoload_name(name);
21457 if (scriptname != NULL)
21458 {
21459 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021460 plen = (int)STRLEN(p);
21461 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021462 if (slen > plen && fnamecmp(p,
21463 sourcing_name + slen - plen) == 0)
21464 j = OK;
21465 vim_free(scriptname);
21466 }
21467 }
21468 if (j == FAIL)
21469 {
21470 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21471 goto erret;
21472 }
21473 }
21474
21475 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476 if (fp == NULL)
21477 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021478
21479 if (fudi.fd_dict != NULL)
21480 {
21481 if (fudi.fd_di == NULL)
21482 {
21483 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021484 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021485 if (fudi.fd_di == NULL)
21486 {
21487 vim_free(fp);
21488 goto erret;
21489 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021490 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21491 {
21492 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021493 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021494 goto erret;
21495 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021496 }
21497 else
21498 /* overwrite existing dict entry */
21499 clear_tv(&fudi.fd_di->di_tv);
21500 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021501 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021502 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021503 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021504
21505 /* behave like "dict" was used */
21506 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021507 }
21508
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021510 STRCPY(fp->uf_name, name);
21511 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021512 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021513 fp->uf_args = newargs;
21514 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021515#ifdef FEAT_PROFILE
21516 fp->uf_tml_count = NULL;
21517 fp->uf_tml_total = NULL;
21518 fp->uf_tml_self = NULL;
21519 fp->uf_profiling = FALSE;
21520 if (prof_def_func())
21521 func_do_profile(fp);
21522#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021523 fp->uf_varargs = varargs;
21524 fp->uf_flags = flags;
21525 fp->uf_calls = 0;
21526 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021527 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021528
21529erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021530 ga_clear_strings(&newargs);
21531 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021532ret_free:
21533 vim_free(skip_until);
21534 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537}
21538
21539/*
21540 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021541 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021542 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021543 * flags:
21544 * TFN_INT: internal function name OK
21545 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021546 * Advances "pp" to just after the function name (if no error).
21547 */
21548 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021549trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021550 char_u **pp;
21551 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021552 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021553 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021554{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021555 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556 char_u *start;
21557 char_u *end;
21558 int lead;
21559 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021560 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021561 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021562
21563 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021564 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021565 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021566
21567 /* Check for hard coded <SNR>: already translated function ID (from a user
21568 * command). */
21569 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21570 && (*pp)[2] == (int)KE_SNR)
21571 {
21572 *pp += 3;
21573 len = get_id_len(pp) + 3;
21574 return vim_strnsave(start, len);
21575 }
21576
21577 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21578 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021579 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021580 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021582
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021583 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21584 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021585 if (end == start)
21586 {
21587 if (!skip)
21588 EMSG(_("E129: Function name required"));
21589 goto theend;
21590 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021591 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021592 {
21593 /*
21594 * Report an invalid expression in braces, unless the expression
21595 * evaluation has been cancelled due to an aborting error, an
21596 * interrupt, or an exception.
21597 */
21598 if (!aborting())
21599 {
21600 if (end != NULL)
21601 EMSG2(_(e_invarg2), start);
21602 }
21603 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021604 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021605 goto theend;
21606 }
21607
21608 if (lv.ll_tv != NULL)
21609 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021610 if (fdp != NULL)
21611 {
21612 fdp->fd_dict = lv.ll_dict;
21613 fdp->fd_newkey = lv.ll_newkey;
21614 lv.ll_newkey = NULL;
21615 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021616 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021617 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21618 {
21619 name = vim_strsave(lv.ll_tv->vval.v_string);
21620 *pp = end;
21621 }
21622 else
21623 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021624 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21625 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021626 EMSG(_(e_funcref));
21627 else
21628 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021629 name = NULL;
21630 }
21631 goto theend;
21632 }
21633
21634 if (lv.ll_name == NULL)
21635 {
21636 /* Error found, but continue after the function name. */
21637 *pp = end;
21638 goto theend;
21639 }
21640
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021641 /* Check if the name is a Funcref. If so, use the value. */
21642 if (lv.ll_exp_name != NULL)
21643 {
21644 len = (int)STRLEN(lv.ll_exp_name);
21645 name = deref_func_name(lv.ll_exp_name, &len);
21646 if (name == lv.ll_exp_name)
21647 name = NULL;
21648 }
21649 else
21650 {
21651 len = (int)(end - *pp);
21652 name = deref_func_name(*pp, &len);
21653 if (name == *pp)
21654 name = NULL;
21655 }
21656 if (name != NULL)
21657 {
21658 name = vim_strsave(name);
21659 *pp = end;
21660 goto theend;
21661 }
21662
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021663 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021664 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021665 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021666 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21667 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21668 {
21669 /* When there was "s:" already or the name expanded to get a
21670 * leading "s:" then remove it. */
21671 lv.ll_name += 2;
21672 len -= 2;
21673 lead = 2;
21674 }
21675 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021676 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021677 {
21678 if (lead == 2) /* skip over "s:" */
21679 lv.ll_name += 2;
21680 len = (int)(end - lv.ll_name);
21681 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021682
21683 /*
21684 * Copy the function name to allocated memory.
21685 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21686 * Accept <SNR>123_name() outside a script.
21687 */
21688 if (skip)
21689 lead = 0; /* do nothing */
21690 else if (lead > 0)
21691 {
21692 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021693 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21694 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021695 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021696 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021697 if (current_SID <= 0)
21698 {
21699 EMSG(_(e_usingsid));
21700 goto theend;
21701 }
21702 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21703 lead += (int)STRLEN(sid_buf);
21704 }
21705 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021706 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021707 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021708 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021709 goto theend;
21710 }
21711 name = alloc((unsigned)(len + lead + 1));
21712 if (name != NULL)
21713 {
21714 if (lead > 0)
21715 {
21716 name[0] = K_SPECIAL;
21717 name[1] = KS_EXTRA;
21718 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021719 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021720 STRCPY(name + 3, sid_buf);
21721 }
21722 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21723 name[len + lead] = NUL;
21724 }
21725 *pp = end;
21726
21727theend:
21728 clear_lval(&lv);
21729 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730}
21731
21732/*
21733 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21734 * Return 2 if "p" starts with "s:".
21735 * Return 0 otherwise.
21736 */
21737 static int
21738eval_fname_script(p)
21739 char_u *p;
21740{
21741 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21742 || STRNICMP(p + 1, "SNR>", 4) == 0))
21743 return 5;
21744 if (p[0] == 's' && p[1] == ':')
21745 return 2;
21746 return 0;
21747}
21748
21749/*
21750 * Return TRUE if "p" starts with "<SID>" or "s:".
21751 * Only works if eval_fname_script() returned non-zero for "p"!
21752 */
21753 static int
21754eval_fname_sid(p)
21755 char_u *p;
21756{
21757 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21758}
21759
21760/*
21761 * List the head of the function: "name(arg1, arg2)".
21762 */
21763 static void
21764list_func_head(fp, indent)
21765 ufunc_T *fp;
21766 int indent;
21767{
21768 int j;
21769
21770 msg_start();
21771 if (indent)
21772 MSG_PUTS(" ");
21773 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021774 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775 {
21776 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021777 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021778 }
21779 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021780 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021782 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021783 {
21784 if (j)
21785 MSG_PUTS(", ");
21786 msg_puts(FUNCARG(fp, j));
21787 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021788 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789 {
21790 if (j)
21791 MSG_PUTS(", ");
21792 MSG_PUTS("...");
21793 }
21794 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021795 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021796 if (p_verbose > 0)
21797 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798}
21799
21800/*
21801 * Find a function by name, return pointer to it in ufuncs.
21802 * Return NULL for unknown function.
21803 */
21804 static ufunc_T *
21805find_func(name)
21806 char_u *name;
21807{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021808 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021809
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021810 hi = hash_find(&func_hashtab, name);
21811 if (!HASHITEM_EMPTY(hi))
21812 return HI2UF(hi);
21813 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814}
21815
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021816#if defined(EXITFREE) || defined(PROTO)
21817 void
21818free_all_functions()
21819{
21820 hashitem_T *hi;
21821
21822 /* Need to start all over every time, because func_free() may change the
21823 * hash table. */
21824 while (func_hashtab.ht_used > 0)
21825 for (hi = func_hashtab.ht_array; ; ++hi)
21826 if (!HASHITEM_EMPTY(hi))
21827 {
21828 func_free(HI2UF(hi));
21829 break;
21830 }
21831}
21832#endif
21833
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021834/*
21835 * Return TRUE if a function "name" exists.
21836 */
21837 static int
21838function_exists(name)
21839 char_u *name;
21840{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021841 char_u *nm = name;
21842 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021843 int n = FALSE;
21844
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021845 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021846 nm = skipwhite(nm);
21847
21848 /* Only accept "funcname", "funcname ", "funcname (..." and
21849 * "funcname(...", not "funcname!...". */
21850 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021851 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021852 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021853 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021854 else
21855 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021856 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021857 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021858 return n;
21859}
21860
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021861/*
21862 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021863 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021864 */
21865 static int
21866builtin_function(name)
21867 char_u *name;
21868{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021869 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21870 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021871}
21872
Bram Moolenaar05159a02005-02-26 23:04:13 +000021873#if defined(FEAT_PROFILE) || defined(PROTO)
21874/*
21875 * Start profiling function "fp".
21876 */
21877 static void
21878func_do_profile(fp)
21879 ufunc_T *fp;
21880{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021881 int len = fp->uf_lines.ga_len;
21882
21883 if (len == 0)
21884 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021885 fp->uf_tm_count = 0;
21886 profile_zero(&fp->uf_tm_self);
21887 profile_zero(&fp->uf_tm_total);
21888 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021889 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021890 if (fp->uf_tml_total == NULL)
21891 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021892 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021893 if (fp->uf_tml_self == NULL)
21894 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021895 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021896 fp->uf_tml_idx = -1;
21897 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21898 || fp->uf_tml_self == NULL)
21899 return; /* out of memory */
21900
21901 fp->uf_profiling = TRUE;
21902}
21903
21904/*
21905 * Dump the profiling results for all functions in file "fd".
21906 */
21907 void
21908func_dump_profile(fd)
21909 FILE *fd;
21910{
21911 hashitem_T *hi;
21912 int todo;
21913 ufunc_T *fp;
21914 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021915 ufunc_T **sorttab;
21916 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021917
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021918 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021919 if (todo == 0)
21920 return; /* nothing to dump */
21921
Bram Moolenaar73830342005-02-28 22:48:19 +000021922 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21923
Bram Moolenaar05159a02005-02-26 23:04:13 +000021924 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21925 {
21926 if (!HASHITEM_EMPTY(hi))
21927 {
21928 --todo;
21929 fp = HI2UF(hi);
21930 if (fp->uf_profiling)
21931 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021932 if (sorttab != NULL)
21933 sorttab[st_len++] = fp;
21934
Bram Moolenaar05159a02005-02-26 23:04:13 +000021935 if (fp->uf_name[0] == K_SPECIAL)
21936 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21937 else
21938 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21939 if (fp->uf_tm_count == 1)
21940 fprintf(fd, "Called 1 time\n");
21941 else
21942 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21943 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21944 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21945 fprintf(fd, "\n");
21946 fprintf(fd, "count total (s) self (s)\n");
21947
21948 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21949 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021950 if (FUNCLINE(fp, i) == NULL)
21951 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021952 prof_func_line(fd, fp->uf_tml_count[i],
21953 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021954 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21955 }
21956 fprintf(fd, "\n");
21957 }
21958 }
21959 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021960
21961 if (sorttab != NULL && st_len > 0)
21962 {
21963 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21964 prof_total_cmp);
21965 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21966 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21967 prof_self_cmp);
21968 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21969 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021970
21971 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021972}
Bram Moolenaar73830342005-02-28 22:48:19 +000021973
21974 static void
21975prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21976 FILE *fd;
21977 ufunc_T **sorttab;
21978 int st_len;
21979 char *title;
21980 int prefer_self; /* when equal print only self time */
21981{
21982 int i;
21983 ufunc_T *fp;
21984
21985 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21986 fprintf(fd, "count total (s) self (s) function\n");
21987 for (i = 0; i < 20 && i < st_len; ++i)
21988 {
21989 fp = sorttab[i];
21990 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21991 prefer_self);
21992 if (fp->uf_name[0] == K_SPECIAL)
21993 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21994 else
21995 fprintf(fd, " %s()\n", fp->uf_name);
21996 }
21997 fprintf(fd, "\n");
21998}
21999
22000/*
22001 * Print the count and times for one function or function line.
22002 */
22003 static void
22004prof_func_line(fd, count, total, self, prefer_self)
22005 FILE *fd;
22006 int count;
22007 proftime_T *total;
22008 proftime_T *self;
22009 int prefer_self; /* when equal print only self time */
22010{
22011 if (count > 0)
22012 {
22013 fprintf(fd, "%5d ", count);
22014 if (prefer_self && profile_equal(total, self))
22015 fprintf(fd, " ");
22016 else
22017 fprintf(fd, "%s ", profile_msg(total));
22018 if (!prefer_self && profile_equal(total, self))
22019 fprintf(fd, " ");
22020 else
22021 fprintf(fd, "%s ", profile_msg(self));
22022 }
22023 else
22024 fprintf(fd, " ");
22025}
22026
22027/*
22028 * Compare function for total time sorting.
22029 */
22030 static int
22031#ifdef __BORLANDC__
22032_RTLENTRYF
22033#endif
22034prof_total_cmp(s1, s2)
22035 const void *s1;
22036 const void *s2;
22037{
22038 ufunc_T *p1, *p2;
22039
22040 p1 = *(ufunc_T **)s1;
22041 p2 = *(ufunc_T **)s2;
22042 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22043}
22044
22045/*
22046 * Compare function for self time sorting.
22047 */
22048 static int
22049#ifdef __BORLANDC__
22050_RTLENTRYF
22051#endif
22052prof_self_cmp(s1, s2)
22053 const void *s1;
22054 const void *s2;
22055{
22056 ufunc_T *p1, *p2;
22057
22058 p1 = *(ufunc_T **)s1;
22059 p2 = *(ufunc_T **)s2;
22060 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22061}
22062
Bram Moolenaar05159a02005-02-26 23:04:13 +000022063#endif
22064
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022065/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022066 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022067 * Return TRUE if a package was loaded.
22068 */
22069 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022070script_autoload(name, reload)
22071 char_u *name;
22072 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022073{
22074 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022075 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022076 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022077 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022078
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022079 /* Return quickly when autoload disabled. */
22080 if (no_autoload)
22081 return FALSE;
22082
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022083 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022084 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022085 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022086 return FALSE;
22087
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022088 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022089
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022090 /* Find the name in the list of previously loaded package names. Skip
22091 * "autoload/", it's always the same. */
22092 for (i = 0; i < ga_loaded.ga_len; ++i)
22093 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22094 break;
22095 if (!reload && i < ga_loaded.ga_len)
22096 ret = FALSE; /* was loaded already */
22097 else
22098 {
22099 /* Remember the name if it wasn't loaded already. */
22100 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22101 {
22102 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22103 tofree = NULL;
22104 }
22105
22106 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022107 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022108 ret = TRUE;
22109 }
22110
22111 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022112 return ret;
22113}
22114
22115/*
22116 * Return the autoload script name for a function or variable name.
22117 * Returns NULL when out of memory.
22118 */
22119 static char_u *
22120autoload_name(name)
22121 char_u *name;
22122{
22123 char_u *p;
22124 char_u *scriptname;
22125
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022126 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022127 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22128 if (scriptname == NULL)
22129 return FALSE;
22130 STRCPY(scriptname, "autoload/");
22131 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022132 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022133 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022134 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022135 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022136 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022137}
22138
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22140
22141/*
22142 * Function given to ExpandGeneric() to obtain the list of user defined
22143 * function names.
22144 */
22145 char_u *
22146get_user_func_name(xp, idx)
22147 expand_T *xp;
22148 int idx;
22149{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022150 static long_u done;
22151 static hashitem_T *hi;
22152 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153
22154 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022156 done = 0;
22157 hi = func_hashtab.ht_array;
22158 }
22159 if (done < func_hashtab.ht_used)
22160 {
22161 if (done++ > 0)
22162 ++hi;
22163 while (HASHITEM_EMPTY(hi))
22164 ++hi;
22165 fp = HI2UF(hi);
22166
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022167 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022168 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022169
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022170 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22171 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172
22173 cat_func_name(IObuff, fp);
22174 if (xp->xp_context != EXPAND_USER_FUNC)
22175 {
22176 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022177 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178 STRCAT(IObuff, ")");
22179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022180 return IObuff;
22181 }
22182 return NULL;
22183}
22184
22185#endif /* FEAT_CMDL_COMPL */
22186
22187/*
22188 * Copy the function name of "fp" to buffer "buf".
22189 * "buf" must be able to hold the function name plus three bytes.
22190 * Takes care of script-local function names.
22191 */
22192 static void
22193cat_func_name(buf, fp)
22194 char_u *buf;
22195 ufunc_T *fp;
22196{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022197 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198 {
22199 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022200 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022201 }
22202 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022203 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022204}
22205
22206/*
22207 * ":delfunction {name}"
22208 */
22209 void
22210ex_delfunction(eap)
22211 exarg_T *eap;
22212{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022213 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022214 char_u *p;
22215 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022216 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217
22218 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022219 name = trans_function_name(&p, eap->skip, 0, &fudi);
22220 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022222 {
22223 if (fudi.fd_dict != NULL && !eap->skip)
22224 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022225 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022227 if (!ends_excmd(*skipwhite(p)))
22228 {
22229 vim_free(name);
22230 EMSG(_(e_trailing));
22231 return;
22232 }
22233 eap->nextcmd = check_nextcmd(p);
22234 if (eap->nextcmd != NULL)
22235 *p = NUL;
22236
22237 if (!eap->skip)
22238 fp = find_func(name);
22239 vim_free(name);
22240
22241 if (!eap->skip)
22242 {
22243 if (fp == NULL)
22244 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022245 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022246 return;
22247 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022248 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022249 {
22250 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22251 return;
22252 }
22253
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022254 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022256 /* Delete the dict item that refers to the function, it will
22257 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022258 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022260 else
22261 func_free(fp);
22262 }
22263}
22264
22265/*
22266 * Free a function and remove it from the list of functions.
22267 */
22268 static void
22269func_free(fp)
22270 ufunc_T *fp;
22271{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022272 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022273
22274 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022275 ga_clear_strings(&(fp->uf_args));
22276 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022277#ifdef FEAT_PROFILE
22278 vim_free(fp->uf_tml_count);
22279 vim_free(fp->uf_tml_total);
22280 vim_free(fp->uf_tml_self);
22281#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022282
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022283 /* remove the function from the function hashtable */
22284 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22285 if (HASHITEM_EMPTY(hi))
22286 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022287 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022288 hash_remove(&func_hashtab, hi);
22289
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022290 vim_free(fp);
22291}
22292
22293/*
22294 * Unreference a Function: decrement the reference count and free it when it
22295 * becomes zero. Only for numbered functions.
22296 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022297 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022298func_unref(name)
22299 char_u *name;
22300{
22301 ufunc_T *fp;
22302
22303 if (name != NULL && isdigit(*name))
22304 {
22305 fp = find_func(name);
22306 if (fp == NULL)
22307 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022308 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022309 {
22310 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022311 * when "uf_calls" becomes zero. */
22312 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022313 func_free(fp);
22314 }
22315 }
22316}
22317
22318/*
22319 * Count a reference to a Function.
22320 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022321 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022322func_ref(name)
22323 char_u *name;
22324{
22325 ufunc_T *fp;
22326
22327 if (name != NULL && isdigit(*name))
22328 {
22329 fp = find_func(name);
22330 if (fp == NULL)
22331 EMSG2(_(e_intern2), "func_ref()");
22332 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022333 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022334 }
22335}
22336
22337/*
22338 * Call a user function.
22339 */
22340 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022341call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022342 ufunc_T *fp; /* pointer to function */
22343 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022344 typval_T *argvars; /* arguments */
22345 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022346 linenr_T firstline; /* first line of range */
22347 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022348 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349{
Bram Moolenaar33570922005-01-25 22:26:29 +000022350 char_u *save_sourcing_name;
22351 linenr_T save_sourcing_lnum;
22352 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022353 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022354 int save_did_emsg;
22355 static int depth = 0;
22356 dictitem_T *v;
22357 int fixvar_idx = 0; /* index in fixvar[] */
22358 int i;
22359 int ai;
22360 char_u numbuf[NUMBUFLEN];
22361 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022362#ifdef FEAT_PROFILE
22363 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022364 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366
22367 /* If depth of calling is getting too high, don't execute the function */
22368 if (depth >= p_mfd)
22369 {
22370 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022371 rettv->v_type = VAR_NUMBER;
22372 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022373 return;
22374 }
22375 ++depth;
22376
22377 line_breakcheck(); /* check for CTRL-C hit */
22378
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022379 fc = (funccall_T *)alloc(sizeof(funccall_T));
22380 fc->caller = current_funccal;
22381 current_funccal = fc;
22382 fc->func = fp;
22383 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022384 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022385 fc->linenr = 0;
22386 fc->returned = FALSE;
22387 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022389 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22390 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022391
Bram Moolenaar33570922005-01-25 22:26:29 +000022392 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022393 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022394 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22395 * each argument variable and saves a lot of time.
22396 */
22397 /*
22398 * Init l: variables.
22399 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022400 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022401 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022402 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022403 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22404 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022405 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022406 name = v->di_key;
22407 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022408 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022409 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022410 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022411 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022412 v->di_tv.vval.v_dict = selfdict;
22413 ++selfdict->dv_refcount;
22414 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022415
Bram Moolenaar33570922005-01-25 22:26:29 +000022416 /*
22417 * Init a: variables.
22418 * Set a:0 to "argcount".
22419 * Set a:000 to a list with room for the "..." arguments.
22420 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022421 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022422 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022423 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022424 /* Use "name" to avoid a warning from some compiler that checks the
22425 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022426 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022427 name = v->di_key;
22428 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022429 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022430 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022431 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022432 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022433 v->di_tv.vval.v_list = &fc->l_varlist;
22434 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22435 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22436 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022437
22438 /*
22439 * Set a:firstline to "firstline" and a:lastline to "lastline".
22440 * Set a:name to named arguments.
22441 * Set a:N to the "..." arguments.
22442 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022443 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022444 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022445 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022446 (varnumber_T)lastline);
22447 for (i = 0; i < argcount; ++i)
22448 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022449 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022450 if (ai < 0)
22451 /* named argument a:name */
22452 name = FUNCARG(fp, i);
22453 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022454 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022455 /* "..." argument a:1, a:2, etc. */
22456 sprintf((char *)numbuf, "%d", ai + 1);
22457 name = numbuf;
22458 }
22459 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22460 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022461 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022462 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22463 }
22464 else
22465 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022466 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22467 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022468 if (v == NULL)
22469 break;
22470 v->di_flags = DI_FLAGS_RO;
22471 }
22472 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022473 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022474
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022475 /* Note: the values are copied directly to avoid alloc/free.
22476 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022477 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022478 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022479
22480 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22481 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022482 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22483 fc->l_listitems[ai].li_tv = argvars[i];
22484 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022485 }
22486 }
22487
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488 /* Don't redraw while executing the function. */
22489 ++RedrawingDisabled;
22490 save_sourcing_name = sourcing_name;
22491 save_sourcing_lnum = sourcing_lnum;
22492 sourcing_lnum = 1;
22493 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022494 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022495 if (sourcing_name != NULL)
22496 {
22497 if (save_sourcing_name != NULL
22498 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22499 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22500 else
22501 STRCPY(sourcing_name, "function ");
22502 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22503
22504 if (p_verbose >= 12)
22505 {
22506 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022507 verbose_enter_scroll();
22508
Bram Moolenaar555b2802005-05-19 21:08:39 +000022509 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510 if (p_verbose >= 14)
22511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022513 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022514 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022515 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022516
22517 msg_puts((char_u *)"(");
22518 for (i = 0; i < argcount; ++i)
22519 {
22520 if (i > 0)
22521 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022522 if (argvars[i].v_type == VAR_NUMBER)
22523 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022524 else
22525 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022526 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22527 if (s != NULL)
22528 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022529 if (vim_strsize(s) > MSG_BUF_CLEN)
22530 {
22531 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22532 s = buf;
22533 }
22534 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022535 vim_free(tofree);
22536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537 }
22538 }
22539 msg_puts((char_u *)")");
22540 }
22541 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022542
22543 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544 --no_wait_return;
22545 }
22546 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022547#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022548 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022549 {
22550 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22551 func_do_profile(fp);
22552 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022553 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022554 {
22555 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022556 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022557 profile_zero(&fp->uf_tm_children);
22558 }
22559 script_prof_save(&wait_start);
22560 }
22561#endif
22562
Bram Moolenaar071d4272004-06-13 20:20:40 +000022563 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022564 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022565 save_did_emsg = did_emsg;
22566 did_emsg = FALSE;
22567
22568 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022569 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22571
22572 --RedrawingDisabled;
22573
22574 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022575 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022577 clear_tv(rettv);
22578 rettv->v_type = VAR_NUMBER;
22579 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580 }
22581
Bram Moolenaar05159a02005-02-26 23:04:13 +000022582#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022583 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022584 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022585 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022586 profile_end(&call_start);
22587 profile_sub_wait(&wait_start, &call_start);
22588 profile_add(&fp->uf_tm_total, &call_start);
22589 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022590 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022591 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022592 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22593 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022594 }
22595 }
22596#endif
22597
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598 /* when being verbose, mention the return value */
22599 if (p_verbose >= 12)
22600 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022602 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022603
Bram Moolenaar071d4272004-06-13 20:20:40 +000022604 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022605 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022606 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022607 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022608 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022609 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022611 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022612 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022613 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022614 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022615
Bram Moolenaar555b2802005-05-19 21:08:39 +000022616 /* The value may be very long. Skip the middle part, so that we
22617 * have some idea how it starts and ends. smsg() would always
22618 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022619 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022620 if (s != NULL)
22621 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022622 if (vim_strsize(s) > MSG_BUF_CLEN)
22623 {
22624 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22625 s = buf;
22626 }
22627 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022628 vim_free(tofree);
22629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 }
22631 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022632
22633 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634 --no_wait_return;
22635 }
22636
22637 vim_free(sourcing_name);
22638 sourcing_name = save_sourcing_name;
22639 sourcing_lnum = save_sourcing_lnum;
22640 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022641#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022642 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022643 script_prof_restore(&wait_start);
22644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645
22646 if (p_verbose >= 12 && sourcing_name != NULL)
22647 {
22648 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022649 verbose_enter_scroll();
22650
Bram Moolenaar555b2802005-05-19 21:08:39 +000022651 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022652 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022653
22654 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022655 --no_wait_return;
22656 }
22657
22658 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022659 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022661
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022662 /* If the a:000 list and the l: and a: dicts are not referenced we can
22663 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022664 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22665 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22666 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22667 {
22668 free_funccal(fc, FALSE);
22669 }
22670 else
22671 {
22672 hashitem_T *hi;
22673 listitem_T *li;
22674 int todo;
22675
22676 /* "fc" is still in use. This can happen when returning "a:000" or
22677 * assigning "l:" to a global variable.
22678 * Link "fc" in the list for garbage collection later. */
22679 fc->caller = previous_funccal;
22680 previous_funccal = fc;
22681
22682 /* Make a copy of the a: variables, since we didn't do that above. */
22683 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22684 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22685 {
22686 if (!HASHITEM_EMPTY(hi))
22687 {
22688 --todo;
22689 v = HI2DI(hi);
22690 copy_tv(&v->di_tv, &v->di_tv);
22691 }
22692 }
22693
22694 /* Make a copy of the a:000 items, since we didn't do that above. */
22695 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22696 copy_tv(&li->li_tv, &li->li_tv);
22697 }
22698}
22699
22700/*
22701 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022702 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022703 */
22704 static int
22705can_free_funccal(fc, copyID)
22706 funccall_T *fc;
22707 int copyID;
22708{
22709 return (fc->l_varlist.lv_copyID != copyID
22710 && fc->l_vars.dv_copyID != copyID
22711 && fc->l_avars.dv_copyID != copyID);
22712}
22713
22714/*
22715 * Free "fc" and what it contains.
22716 */
22717 static void
22718free_funccal(fc, free_val)
22719 funccall_T *fc;
22720 int free_val; /* a: vars were allocated */
22721{
22722 listitem_T *li;
22723
22724 /* The a: variables typevals may not have been allocated, only free the
22725 * allocated variables. */
22726 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22727
22728 /* free all l: variables */
22729 vars_clear(&fc->l_vars.dv_hashtab);
22730
22731 /* Free the a:000 variables if they were allocated. */
22732 if (free_val)
22733 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22734 clear_tv(&li->li_tv);
22735
22736 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022737}
22738
22739/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022740 * Add a number variable "name" to dict "dp" with value "nr".
22741 */
22742 static void
22743add_nr_var(dp, v, name, nr)
22744 dict_T *dp;
22745 dictitem_T *v;
22746 char *name;
22747 varnumber_T nr;
22748{
22749 STRCPY(v->di_key, name);
22750 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22751 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22752 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022753 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022754 v->di_tv.vval.v_number = nr;
22755}
22756
22757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022758 * ":return [expr]"
22759 */
22760 void
22761ex_return(eap)
22762 exarg_T *eap;
22763{
22764 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022765 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022766 int returning = FALSE;
22767
22768 if (current_funccal == NULL)
22769 {
22770 EMSG(_("E133: :return not inside a function"));
22771 return;
22772 }
22773
22774 if (eap->skip)
22775 ++emsg_skip;
22776
22777 eap->nextcmd = NULL;
22778 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022779 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022780 {
22781 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022782 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022783 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022784 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022785 }
22786 /* It's safer to return also on error. */
22787 else if (!eap->skip)
22788 {
22789 /*
22790 * Return unless the expression evaluation has been cancelled due to an
22791 * aborting error, an interrupt, or an exception.
22792 */
22793 if (!aborting())
22794 returning = do_return(eap, FALSE, TRUE, NULL);
22795 }
22796
22797 /* When skipping or the return gets pending, advance to the next command
22798 * in this line (!returning). Otherwise, ignore the rest of the line.
22799 * Following lines will be ignored by get_func_line(). */
22800 if (returning)
22801 eap->nextcmd = NULL;
22802 else if (eap->nextcmd == NULL) /* no argument */
22803 eap->nextcmd = check_nextcmd(arg);
22804
22805 if (eap->skip)
22806 --emsg_skip;
22807}
22808
22809/*
22810 * Return from a function. Possibly makes the return pending. Also called
22811 * for a pending return at the ":endtry" or after returning from an extra
22812 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022813 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022814 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022815 * FALSE when the return gets pending.
22816 */
22817 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022818do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022819 exarg_T *eap;
22820 int reanimate;
22821 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022822 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022823{
22824 int idx;
22825 struct condstack *cstack = eap->cstack;
22826
22827 if (reanimate)
22828 /* Undo the return. */
22829 current_funccal->returned = FALSE;
22830
22831 /*
22832 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22833 * not in its finally clause (which then is to be executed next) is found.
22834 * In this case, make the ":return" pending for execution at the ":endtry".
22835 * Otherwise, return normally.
22836 */
22837 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22838 if (idx >= 0)
22839 {
22840 cstack->cs_pending[idx] = CSTP_RETURN;
22841
22842 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022843 /* A pending return again gets pending. "rettv" points to an
22844 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022845 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022846 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847 else
22848 {
22849 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022850 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022852 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022854 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855 {
22856 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022857 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022858 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859 else
22860 EMSG(_(e_outofmem));
22861 }
22862 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022863 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022864
22865 if (reanimate)
22866 {
22867 /* The pending return value could be overwritten by a ":return"
22868 * without argument in a finally clause; reset the default
22869 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022870 current_funccal->rettv->v_type = VAR_NUMBER;
22871 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 }
22873 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022874 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 }
22876 else
22877 {
22878 current_funccal->returned = TRUE;
22879
22880 /* If the return is carried out now, store the return value. For
22881 * a return immediately after reanimation, the value is already
22882 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022883 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022885 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022886 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022888 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022889 }
22890 }
22891
22892 return idx < 0;
22893}
22894
22895/*
22896 * Free the variable with a pending return value.
22897 */
22898 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022899discard_pending_return(rettv)
22900 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901{
Bram Moolenaar33570922005-01-25 22:26:29 +000022902 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903}
22904
22905/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022906 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907 * is an allocated string. Used by report_pending() for verbose messages.
22908 */
22909 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022910get_return_cmd(rettv)
22911 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022913 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022914 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022915 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022916
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022917 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022918 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022919 if (s == NULL)
22920 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022921
22922 STRCPY(IObuff, ":return ");
22923 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22924 if (STRLEN(s) + 8 >= IOSIZE)
22925 STRCPY(IObuff + IOSIZE - 4, "...");
22926 vim_free(tofree);
22927 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928}
22929
22930/*
22931 * Get next function line.
22932 * Called by do_cmdline() to get the next line.
22933 * Returns allocated string, or NULL for end of function.
22934 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935 char_u *
22936get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022937 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022939 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022940{
Bram Moolenaar33570922005-01-25 22:26:29 +000022941 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022942 ufunc_T *fp = fcp->func;
22943 char_u *retval;
22944 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022945
22946 /* If breakpoints have been added/deleted need to check for it. */
22947 if (fcp->dbg_tick != debug_tick)
22948 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022949 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 sourcing_lnum);
22951 fcp->dbg_tick = debug_tick;
22952 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022953#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022954 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022955 func_line_end(cookie);
22956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957
Bram Moolenaar05159a02005-02-26 23:04:13 +000022958 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022959 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22960 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 retval = NULL;
22962 else
22963 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022964 /* Skip NULL lines (continuation lines). */
22965 while (fcp->linenr < gap->ga_len
22966 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22967 ++fcp->linenr;
22968 if (fcp->linenr >= gap->ga_len)
22969 retval = NULL;
22970 else
22971 {
22972 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22973 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022974#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022975 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022976 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022977#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022979 }
22980
22981 /* Did we encounter a breakpoint? */
22982 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22983 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022984 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022986 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 sourcing_lnum);
22988 fcp->dbg_tick = debug_tick;
22989 }
22990
22991 return retval;
22992}
22993
Bram Moolenaar05159a02005-02-26 23:04:13 +000022994#if defined(FEAT_PROFILE) || defined(PROTO)
22995/*
22996 * Called when starting to read a function line.
22997 * "sourcing_lnum" must be correct!
22998 * When skipping lines it may not actually be executed, but we won't find out
22999 * until later and we need to store the time now.
23000 */
23001 void
23002func_line_start(cookie)
23003 void *cookie;
23004{
23005 funccall_T *fcp = (funccall_T *)cookie;
23006 ufunc_T *fp = fcp->func;
23007
23008 if (fp->uf_profiling && sourcing_lnum >= 1
23009 && sourcing_lnum <= fp->uf_lines.ga_len)
23010 {
23011 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023012 /* Skip continuation lines. */
23013 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23014 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023015 fp->uf_tml_execed = FALSE;
23016 profile_start(&fp->uf_tml_start);
23017 profile_zero(&fp->uf_tml_children);
23018 profile_get_wait(&fp->uf_tml_wait);
23019 }
23020}
23021
23022/*
23023 * Called when actually executing a function line.
23024 */
23025 void
23026func_line_exec(cookie)
23027 void *cookie;
23028{
23029 funccall_T *fcp = (funccall_T *)cookie;
23030 ufunc_T *fp = fcp->func;
23031
23032 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23033 fp->uf_tml_execed = TRUE;
23034}
23035
23036/*
23037 * Called when done with a function line.
23038 */
23039 void
23040func_line_end(cookie)
23041 void *cookie;
23042{
23043 funccall_T *fcp = (funccall_T *)cookie;
23044 ufunc_T *fp = fcp->func;
23045
23046 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23047 {
23048 if (fp->uf_tml_execed)
23049 {
23050 ++fp->uf_tml_count[fp->uf_tml_idx];
23051 profile_end(&fp->uf_tml_start);
23052 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023053 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023054 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23055 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023056 }
23057 fp->uf_tml_idx = -1;
23058 }
23059}
23060#endif
23061
Bram Moolenaar071d4272004-06-13 20:20:40 +000023062/*
23063 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023064 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065 */
23066 int
23067func_has_ended(cookie)
23068 void *cookie;
23069{
Bram Moolenaar33570922005-01-25 22:26:29 +000023070 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023071
23072 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23073 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023074 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023075 || fcp->returned);
23076}
23077
23078/*
23079 * return TRUE if cookie indicates a function which "abort"s on errors.
23080 */
23081 int
23082func_has_abort(cookie)
23083 void *cookie;
23084{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023085 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023086}
23087
23088#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23089typedef enum
23090{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023091 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23092 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23093 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023094} var_flavour_T;
23095
23096static var_flavour_T var_flavour __ARGS((char_u *varname));
23097
23098 static var_flavour_T
23099var_flavour(varname)
23100 char_u *varname;
23101{
23102 char_u *p = varname;
23103
23104 if (ASCII_ISUPPER(*p))
23105 {
23106 while (*(++p))
23107 if (ASCII_ISLOWER(*p))
23108 return VAR_FLAVOUR_SESSION;
23109 return VAR_FLAVOUR_VIMINFO;
23110 }
23111 else
23112 return VAR_FLAVOUR_DEFAULT;
23113}
23114#endif
23115
23116#if defined(FEAT_VIMINFO) || defined(PROTO)
23117/*
23118 * Restore global vars that start with a capital from the viminfo file
23119 */
23120 int
23121read_viminfo_varlist(virp, writing)
23122 vir_T *virp;
23123 int writing;
23124{
23125 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023126 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023127 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128
23129 if (!writing && (find_viminfo_parameter('!') != NULL))
23130 {
23131 tab = vim_strchr(virp->vir_line + 1, '\t');
23132 if (tab != NULL)
23133 {
23134 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023135 switch (*tab)
23136 {
23137 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023138#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023139 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023140#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023141 case 'D': type = VAR_DICT; break;
23142 case 'L': type = VAR_LIST; break;
23143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023144
23145 tab = vim_strchr(tab, '\t');
23146 if (tab != NULL)
23147 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023148 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023149 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023150 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023152#ifdef FEAT_FLOAT
23153 else if (type == VAR_FLOAT)
23154 (void)string2float(tab + 1, &tv.vval.v_float);
23155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023156 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023157 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023158 if (type == VAR_DICT || type == VAR_LIST)
23159 {
23160 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23161
23162 if (etv == NULL)
23163 /* Failed to parse back the dict or list, use it as a
23164 * string. */
23165 tv.v_type = VAR_STRING;
23166 else
23167 {
23168 vim_free(tv.vval.v_string);
23169 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023170 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023171 }
23172 }
23173
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023174 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023175
23176 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023177 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023178 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23179 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180 }
23181 }
23182 }
23183
23184 return viminfo_readline(virp);
23185}
23186
23187/*
23188 * Write global vars that start with a capital to the viminfo file
23189 */
23190 void
23191write_viminfo_varlist(fp)
23192 FILE *fp;
23193{
Bram Moolenaar33570922005-01-25 22:26:29 +000023194 hashitem_T *hi;
23195 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023196 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023197 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023198 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023199 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023200 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201
23202 if (find_viminfo_parameter('!') == NULL)
23203 return;
23204
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023205 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023206
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023207 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023208 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023210 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023211 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023212 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023213 this_var = HI2DI(hi);
23214 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023215 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023216 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023217 {
23218 case VAR_STRING: s = "STR"; break;
23219 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023220#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023221 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023222#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023223 case VAR_DICT: s = "DIC"; break;
23224 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023225 default: continue;
23226 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023227 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023228 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023229 if (p != NULL)
23230 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023231 vim_free(tofree);
23232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233 }
23234 }
23235}
23236#endif
23237
23238#if defined(FEAT_SESSION) || defined(PROTO)
23239 int
23240store_session_globals(fd)
23241 FILE *fd;
23242{
Bram Moolenaar33570922005-01-25 22:26:29 +000023243 hashitem_T *hi;
23244 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023245 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023246 char_u *p, *t;
23247
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023248 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023249 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023250 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023251 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023252 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023253 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023254 this_var = HI2DI(hi);
23255 if ((this_var->di_tv.v_type == VAR_NUMBER
23256 || this_var->di_tv.v_type == VAR_STRING)
23257 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023258 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023259 /* Escape special characters with a backslash. Turn a LF and
23260 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023261 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023262 (char_u *)"\\\"\n\r");
23263 if (p == NULL) /* out of memory */
23264 break;
23265 for (t = p; *t != NUL; ++t)
23266 if (*t == '\n')
23267 *t = 'n';
23268 else if (*t == '\r')
23269 *t = 'r';
23270 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023271 this_var->di_key,
23272 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23273 : ' ',
23274 p,
23275 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23276 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023277 || put_eol(fd) == FAIL)
23278 {
23279 vim_free(p);
23280 return FAIL;
23281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023282 vim_free(p);
23283 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023284#ifdef FEAT_FLOAT
23285 else if (this_var->di_tv.v_type == VAR_FLOAT
23286 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23287 {
23288 float_T f = this_var->di_tv.vval.v_float;
23289 int sign = ' ';
23290
23291 if (f < 0)
23292 {
23293 f = -f;
23294 sign = '-';
23295 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023296 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023297 this_var->di_key, sign, f) < 0)
23298 || put_eol(fd) == FAIL)
23299 return FAIL;
23300 }
23301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 }
23303 }
23304 return OK;
23305}
23306#endif
23307
Bram Moolenaar661b1822005-07-28 22:36:45 +000023308/*
23309 * Display script name where an item was last set.
23310 * Should only be invoked when 'verbose' is non-zero.
23311 */
23312 void
23313last_set_msg(scriptID)
23314 scid_T scriptID;
23315{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023316 char_u *p;
23317
Bram Moolenaar661b1822005-07-28 22:36:45 +000023318 if (scriptID != 0)
23319 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023320 p = home_replace_save(NULL, get_scriptname(scriptID));
23321 if (p != NULL)
23322 {
23323 verbose_enter();
23324 MSG_PUTS(_("\n\tLast set from "));
23325 MSG_PUTS(p);
23326 vim_free(p);
23327 verbose_leave();
23328 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023329 }
23330}
23331
Bram Moolenaard812df62008-11-09 12:46:09 +000023332/*
23333 * List v:oldfiles in a nice way.
23334 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023335 void
23336ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023337 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023338{
23339 list_T *l = vimvars[VV_OLDFILES].vv_list;
23340 listitem_T *li;
23341 int nr = 0;
23342
23343 if (l == NULL)
23344 msg((char_u *)_("No old files"));
23345 else
23346 {
23347 msg_start();
23348 msg_scroll = TRUE;
23349 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23350 {
23351 msg_outnum((long)++nr);
23352 MSG_PUTS(": ");
23353 msg_outtrans(get_tv_string(&li->li_tv));
23354 msg_putchar('\n');
23355 out_flush(); /* output one line at a time */
23356 ui_breakcheck();
23357 }
23358 /* Assume "got_int" was set to truncate the listing. */
23359 got_int = FALSE;
23360
23361#ifdef FEAT_BROWSE_CMD
23362 if (cmdmod.browse)
23363 {
23364 quit_more = FALSE;
23365 nr = prompt_for_number(FALSE);
23366 msg_starthere();
23367 if (nr > 0)
23368 {
23369 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23370 (long)nr);
23371
23372 if (p != NULL)
23373 {
23374 p = expand_env_save(p);
23375 eap->arg = p;
23376 eap->cmdidx = CMD_edit;
23377 cmdmod.browse = FALSE;
23378 do_exedit(eap, NULL);
23379 vim_free(p);
23380 }
23381 }
23382 }
23383#endif
23384 }
23385}
23386
Bram Moolenaar071d4272004-06-13 20:20:40 +000023387#endif /* FEAT_EVAL */
23388
Bram Moolenaar071d4272004-06-13 20:20:40 +000023389
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023390#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023391
23392#ifdef WIN3264
23393/*
23394 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23395 */
23396static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23397static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23398static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23399
23400/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023401 * Get the short path (8.3) for the filename in "fnamep".
23402 * Only works for a valid file name.
23403 * When the path gets longer "fnamep" is changed and the allocated buffer
23404 * is put in "bufp".
23405 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23406 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023407 */
23408 static int
23409get_short_pathname(fnamep, bufp, fnamelen)
23410 char_u **fnamep;
23411 char_u **bufp;
23412 int *fnamelen;
23413{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023414 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023415 char_u *newbuf;
23416
23417 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023418 l = GetShortPathName(*fnamep, *fnamep, len);
23419 if (l > len - 1)
23420 {
23421 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023422 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023423 newbuf = vim_strnsave(*fnamep, l);
23424 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023425 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426
23427 vim_free(*bufp);
23428 *fnamep = *bufp = newbuf;
23429
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023430 /* Really should always succeed, as the buffer is big enough. */
23431 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023432 }
23433
23434 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023435 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023436}
23437
23438/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023439 * Get the short path (8.3) for the filename in "fname". The converted
23440 * path is returned in "bufp".
23441 *
23442 * Some of the directories specified in "fname" may not exist. This function
23443 * will shorten the existing directories at the beginning of the path and then
23444 * append the remaining non-existing path.
23445 *
23446 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023447 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023448 * bufp - Pointer to an allocated buffer for the filename.
23449 * fnamelen - Length of the filename pointed to by fname
23450 *
23451 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023452 */
23453 static int
23454shortpath_for_invalid_fname(fname, bufp, fnamelen)
23455 char_u **fname;
23456 char_u **bufp;
23457 int *fnamelen;
23458{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023459 char_u *short_fname, *save_fname, *pbuf_unused;
23460 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023461 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023462 int old_len, len;
23463 int new_len, sfx_len;
23464 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023465
23466 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023467 old_len = *fnamelen;
23468 save_fname = vim_strnsave(*fname, old_len);
23469 pbuf_unused = NULL;
23470 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023471
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023472 endp = save_fname + old_len - 1; /* Find the end of the copy */
23473 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023474
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023475 /*
23476 * Try shortening the supplied path till it succeeds by removing one
23477 * directory at a time from the tail of the path.
23478 */
23479 len = 0;
23480 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023481 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023482 /* go back one path-separator */
23483 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23484 --endp;
23485 if (endp <= save_fname)
23486 break; /* processed the complete path */
23487
23488 /*
23489 * Replace the path separator with a NUL and try to shorten the
23490 * resulting path.
23491 */
23492 ch = *endp;
23493 *endp = 0;
23494 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023495 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023496 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23497 {
23498 retval = FAIL;
23499 goto theend;
23500 }
23501 *endp = ch; /* preserve the string */
23502
23503 if (len > 0)
23504 break; /* successfully shortened the path */
23505
23506 /* failed to shorten the path. Skip the path separator */
23507 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023508 }
23509
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023510 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023511 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023512 /*
23513 * Succeeded in shortening the path. Now concatenate the shortened
23514 * path with the remaining path at the tail.
23515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023516
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023517 /* Compute the length of the new path. */
23518 sfx_len = (int)(save_endp - endp) + 1;
23519 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023520
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023521 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023522 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023523 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023524 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023525 /* There is not enough space in the currently allocated string,
23526 * copy it to a buffer big enough. */
23527 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023528 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023529 {
23530 retval = FAIL;
23531 goto theend;
23532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023533 }
23534 else
23535 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023536 /* Transfer short_fname to the main buffer (it's big enough),
23537 * unless get_short_pathname() did its work in-place. */
23538 *fname = *bufp = save_fname;
23539 if (short_fname != save_fname)
23540 vim_strncpy(save_fname, short_fname, len);
23541 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023542 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023543
23544 /* concat the not-shortened part of the path */
23545 vim_strncpy(*fname + len, endp, sfx_len);
23546 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023547 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023548
23549theend:
23550 vim_free(pbuf_unused);
23551 vim_free(save_fname);
23552
23553 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554}
23555
23556/*
23557 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023558 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023559 */
23560 static int
23561shortpath_for_partial(fnamep, bufp, fnamelen)
23562 char_u **fnamep;
23563 char_u **bufp;
23564 int *fnamelen;
23565{
23566 int sepcount, len, tflen;
23567 char_u *p;
23568 char_u *pbuf, *tfname;
23569 int hasTilde;
23570
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023571 /* Count up the path separators from the RHS.. so we know which part
23572 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023573 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023574 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575 if (vim_ispathsep(*p))
23576 ++sepcount;
23577
23578 /* Need full path first (use expand_env() to remove a "~/") */
23579 hasTilde = (**fnamep == '~');
23580 if (hasTilde)
23581 pbuf = tfname = expand_env_save(*fnamep);
23582 else
23583 pbuf = tfname = FullName_save(*fnamep, FALSE);
23584
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023585 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023586
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023587 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23588 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023589
23590 if (len == 0)
23591 {
23592 /* Don't have a valid filename, so shorten the rest of the
23593 * path if we can. This CAN give us invalid 8.3 filenames, but
23594 * there's not a lot of point in guessing what it might be.
23595 */
23596 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023597 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23598 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599 }
23600
23601 /* Count the paths backward to find the beginning of the desired string. */
23602 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023603 {
23604#ifdef FEAT_MBYTE
23605 if (has_mbyte)
23606 p -= mb_head_off(tfname, p);
23607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608 if (vim_ispathsep(*p))
23609 {
23610 if (sepcount == 0 || (hasTilde && sepcount == 1))
23611 break;
23612 else
23613 sepcount --;
23614 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023616 if (hasTilde)
23617 {
23618 --p;
23619 if (p >= tfname)
23620 *p = '~';
23621 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023622 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023623 }
23624 else
23625 ++p;
23626
23627 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23628 vim_free(*bufp);
23629 *fnamelen = (int)STRLEN(p);
23630 *bufp = pbuf;
23631 *fnamep = p;
23632
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023633 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634}
23635#endif /* WIN3264 */
23636
23637/*
23638 * Adjust a filename, according to a string of modifiers.
23639 * *fnamep must be NUL terminated when called. When returning, the length is
23640 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023641 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023642 * When there is an error, *fnamep is set to NULL.
23643 */
23644 int
23645modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23646 char_u *src; /* string with modifiers */
23647 int *usedlen; /* characters after src that are used */
23648 char_u **fnamep; /* file name so far */
23649 char_u **bufp; /* buffer for allocated file name or NULL */
23650 int *fnamelen; /* length of fnamep */
23651{
23652 int valid = 0;
23653 char_u *tail;
23654 char_u *s, *p, *pbuf;
23655 char_u dirname[MAXPATHL];
23656 int c;
23657 int has_fullname = 0;
23658#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023659 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023660 int has_shortname = 0;
23661#endif
23662
23663repeat:
23664 /* ":p" - full path/file_name */
23665 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23666 {
23667 has_fullname = 1;
23668
23669 valid |= VALID_PATH;
23670 *usedlen += 2;
23671
23672 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23673 if ((*fnamep)[0] == '~'
23674#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23675 && ((*fnamep)[1] == '/'
23676# ifdef BACKSLASH_IN_FILENAME
23677 || (*fnamep)[1] == '\\'
23678# endif
23679 || (*fnamep)[1] == NUL)
23680
23681#endif
23682 )
23683 {
23684 *fnamep = expand_env_save(*fnamep);
23685 vim_free(*bufp); /* free any allocated file name */
23686 *bufp = *fnamep;
23687 if (*fnamep == NULL)
23688 return -1;
23689 }
23690
23691 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023692 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023693 {
23694 if (vim_ispathsep(*p)
23695 && p[1] == '.'
23696 && (p[2] == NUL
23697 || vim_ispathsep(p[2])
23698 || (p[2] == '.'
23699 && (p[3] == NUL || vim_ispathsep(p[3])))))
23700 break;
23701 }
23702
23703 /* FullName_save() is slow, don't use it when not needed. */
23704 if (*p != NUL || !vim_isAbsName(*fnamep))
23705 {
23706 *fnamep = FullName_save(*fnamep, *p != NUL);
23707 vim_free(*bufp); /* free any allocated file name */
23708 *bufp = *fnamep;
23709 if (*fnamep == NULL)
23710 return -1;
23711 }
23712
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023713#ifdef WIN3264
23714# if _WIN32_WINNT >= 0x0500
23715 if (vim_strchr(*fnamep, '~') != NULL)
23716 {
23717 /* Expand 8.3 filename to full path. Needed to make sure the same
23718 * file does not have two different names.
23719 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23720 p = alloc(_MAX_PATH + 1);
23721 if (p != NULL)
23722 {
23723 if (GetLongPathName(*fnamep, p, MAXPATHL))
23724 {
23725 vim_free(*bufp);
23726 *bufp = *fnamep = p;
23727 }
23728 else
23729 vim_free(p);
23730 }
23731 }
23732# endif
23733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023734 /* Append a path separator to a directory. */
23735 if (mch_isdir(*fnamep))
23736 {
23737 /* Make room for one or two extra characters. */
23738 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23739 vim_free(*bufp); /* free any allocated file name */
23740 *bufp = *fnamep;
23741 if (*fnamep == NULL)
23742 return -1;
23743 add_pathsep(*fnamep);
23744 }
23745 }
23746
23747 /* ":." - path relative to the current directory */
23748 /* ":~" - path relative to the home directory */
23749 /* ":8" - shortname path - postponed till after */
23750 while (src[*usedlen] == ':'
23751 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23752 {
23753 *usedlen += 2;
23754 if (c == '8')
23755 {
23756#ifdef WIN3264
23757 has_shortname = 1; /* Postpone this. */
23758#endif
23759 continue;
23760 }
23761 pbuf = NULL;
23762 /* Need full path first (use expand_env() to remove a "~/") */
23763 if (!has_fullname)
23764 {
23765 if (c == '.' && **fnamep == '~')
23766 p = pbuf = expand_env_save(*fnamep);
23767 else
23768 p = pbuf = FullName_save(*fnamep, FALSE);
23769 }
23770 else
23771 p = *fnamep;
23772
23773 has_fullname = 0;
23774
23775 if (p != NULL)
23776 {
23777 if (c == '.')
23778 {
23779 mch_dirname(dirname, MAXPATHL);
23780 s = shorten_fname(p, dirname);
23781 if (s != NULL)
23782 {
23783 *fnamep = s;
23784 if (pbuf != NULL)
23785 {
23786 vim_free(*bufp); /* free any allocated file name */
23787 *bufp = pbuf;
23788 pbuf = NULL;
23789 }
23790 }
23791 }
23792 else
23793 {
23794 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23795 /* Only replace it when it starts with '~' */
23796 if (*dirname == '~')
23797 {
23798 s = vim_strsave(dirname);
23799 if (s != NULL)
23800 {
23801 *fnamep = s;
23802 vim_free(*bufp);
23803 *bufp = s;
23804 }
23805 }
23806 }
23807 vim_free(pbuf);
23808 }
23809 }
23810
23811 tail = gettail(*fnamep);
23812 *fnamelen = (int)STRLEN(*fnamep);
23813
23814 /* ":h" - head, remove "/file_name", can be repeated */
23815 /* Don't remove the first "/" or "c:\" */
23816 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23817 {
23818 valid |= VALID_HEAD;
23819 *usedlen += 2;
23820 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023821 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023822 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023823 *fnamelen = (int)(tail - *fnamep);
23824#ifdef VMS
23825 if (*fnamelen > 0)
23826 *fnamelen += 1; /* the path separator is part of the path */
23827#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023828 if (*fnamelen == 0)
23829 {
23830 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23831 p = vim_strsave((char_u *)".");
23832 if (p == NULL)
23833 return -1;
23834 vim_free(*bufp);
23835 *bufp = *fnamep = tail = p;
23836 *fnamelen = 1;
23837 }
23838 else
23839 {
23840 while (tail > s && !after_pathsep(s, tail))
23841 mb_ptr_back(*fnamep, tail);
23842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023843 }
23844
23845 /* ":8" - shortname */
23846 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23847 {
23848 *usedlen += 2;
23849#ifdef WIN3264
23850 has_shortname = 1;
23851#endif
23852 }
23853
23854#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023855 /*
23856 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023857 */
23858 if (has_shortname)
23859 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023860 /* Copy the string if it is shortened by :h and when it wasn't copied
23861 * yet, because we are going to change it in place. Avoids changing
23862 * the buffer name for "%:8". */
23863 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023864 {
23865 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023866 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023867 return -1;
23868 vim_free(*bufp);
23869 *bufp = *fnamep = p;
23870 }
23871
23872 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023873 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874 if (!has_fullname && !vim_isAbsName(*fnamep))
23875 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023876 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023877 return -1;
23878 }
23879 else
23880 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023881 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023882
Bram Moolenaardc935552011-08-17 15:23:23 +020023883 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023884 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023885 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 return -1;
23887
23888 if (l == 0)
23889 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023890 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023891 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023892 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023893 return -1;
23894 }
23895 *fnamelen = l;
23896 }
23897 }
23898#endif /* WIN3264 */
23899
23900 /* ":t" - tail, just the basename */
23901 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23902 {
23903 *usedlen += 2;
23904 *fnamelen -= (int)(tail - *fnamep);
23905 *fnamep = tail;
23906 }
23907
23908 /* ":e" - extension, can be repeated */
23909 /* ":r" - root, without extension, can be repeated */
23910 while (src[*usedlen] == ':'
23911 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23912 {
23913 /* find a '.' in the tail:
23914 * - for second :e: before the current fname
23915 * - otherwise: The last '.'
23916 */
23917 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23918 s = *fnamep - 2;
23919 else
23920 s = *fnamep + *fnamelen - 1;
23921 for ( ; s > tail; --s)
23922 if (s[0] == '.')
23923 break;
23924 if (src[*usedlen + 1] == 'e') /* :e */
23925 {
23926 if (s > tail)
23927 {
23928 *fnamelen += (int)(*fnamep - (s + 1));
23929 *fnamep = s + 1;
23930#ifdef VMS
23931 /* cut version from the extension */
23932 s = *fnamep + *fnamelen - 1;
23933 for ( ; s > *fnamep; --s)
23934 if (s[0] == ';')
23935 break;
23936 if (s > *fnamep)
23937 *fnamelen = s - *fnamep;
23938#endif
23939 }
23940 else if (*fnamep <= tail)
23941 *fnamelen = 0;
23942 }
23943 else /* :r */
23944 {
23945 if (s > tail) /* remove one extension */
23946 *fnamelen = (int)(s - *fnamep);
23947 }
23948 *usedlen += 2;
23949 }
23950
23951 /* ":s?pat?foo?" - substitute */
23952 /* ":gs?pat?foo?" - global substitute */
23953 if (src[*usedlen] == ':'
23954 && (src[*usedlen + 1] == 's'
23955 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23956 {
23957 char_u *str;
23958 char_u *pat;
23959 char_u *sub;
23960 int sep;
23961 char_u *flags;
23962 int didit = FALSE;
23963
23964 flags = (char_u *)"";
23965 s = src + *usedlen + 2;
23966 if (src[*usedlen + 1] == 'g')
23967 {
23968 flags = (char_u *)"g";
23969 ++s;
23970 }
23971
23972 sep = *s++;
23973 if (sep)
23974 {
23975 /* find end of pattern */
23976 p = vim_strchr(s, sep);
23977 if (p != NULL)
23978 {
23979 pat = vim_strnsave(s, (int)(p - s));
23980 if (pat != NULL)
23981 {
23982 s = p + 1;
23983 /* find end of substitution */
23984 p = vim_strchr(s, sep);
23985 if (p != NULL)
23986 {
23987 sub = vim_strnsave(s, (int)(p - s));
23988 str = vim_strnsave(*fnamep, *fnamelen);
23989 if (sub != NULL && str != NULL)
23990 {
23991 *usedlen = (int)(p + 1 - src);
23992 s = do_string_sub(str, pat, sub, flags);
23993 if (s != NULL)
23994 {
23995 *fnamep = s;
23996 *fnamelen = (int)STRLEN(s);
23997 vim_free(*bufp);
23998 *bufp = s;
23999 didit = TRUE;
24000 }
24001 }
24002 vim_free(sub);
24003 vim_free(str);
24004 }
24005 vim_free(pat);
24006 }
24007 }
24008 /* after using ":s", repeat all the modifiers */
24009 if (didit)
24010 goto repeat;
24011 }
24012 }
24013
24014 return valid;
24015}
24016
24017/*
24018 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24019 * "flags" can be "g" to do a global substitute.
24020 * Returns an allocated string, NULL for error.
24021 */
24022 char_u *
24023do_string_sub(str, pat, sub, flags)
24024 char_u *str;
24025 char_u *pat;
24026 char_u *sub;
24027 char_u *flags;
24028{
24029 int sublen;
24030 regmatch_T regmatch;
24031 int i;
24032 int do_all;
24033 char_u *tail;
24034 garray_T ga;
24035 char_u *ret;
24036 char_u *save_cpo;
24037
24038 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24039 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024040 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024041
24042 ga_init2(&ga, 1, 200);
24043
24044 do_all = (flags[0] == 'g');
24045
24046 regmatch.rm_ic = p_ic;
24047 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24048 if (regmatch.regprog != NULL)
24049 {
24050 tail = str;
24051 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24052 {
24053 /*
24054 * Get some space for a temporary buffer to do the substitution
24055 * into. It will contain:
24056 * - The text up to where the match is.
24057 * - The substituted text.
24058 * - The text after the match.
24059 */
24060 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24061 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24062 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24063 {
24064 ga_clear(&ga);
24065 break;
24066 }
24067
24068 /* copy the text up to where the match is */
24069 i = (int)(regmatch.startp[0] - tail);
24070 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24071 /* add the substituted text */
24072 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24073 + ga.ga_len + i, TRUE, TRUE, FALSE);
24074 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024075 /* avoid getting stuck on a match with an empty string */
24076 if (tail == regmatch.endp[0])
24077 {
24078 if (*tail == NUL)
24079 break;
24080 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24081 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024082 }
24083 else
24084 {
24085 tail = regmatch.endp[0];
24086 if (*tail == NUL)
24087 break;
24088 }
24089 if (!do_all)
24090 break;
24091 }
24092
24093 if (ga.ga_data != NULL)
24094 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24095
24096 vim_free(regmatch.regprog);
24097 }
24098
24099 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24100 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024101 if (p_cpo == empty_option)
24102 p_cpo = save_cpo;
24103 else
24104 /* Darn, evaluating {sub} expression changed the value. */
24105 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024106
24107 return ret;
24108}
24109
24110#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */