blob: dc7c2409f1bd9135a284f6aa177ca962be2f4534 [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 Moolenaard35d7842013-01-23 17:17:10 +01007857 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 {"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},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008006 {"nr2char", 1, 2, 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 Moolenaard35d7842013-01-23 17:17:10 +01009306 {
9307 int utf8 = 0;
9308
9309 if (argvars[1].v_type != VAR_UNKNOWN)
9310 utf8 = get_tv_number_chk(&argvars[1], NULL);
9311
9312 if (utf8)
9313 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9314 else
9315 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 else
9318#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009319 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320}
9321
9322/*
9323 * "cindent(lnum)" function
9324 */
9325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009326f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009327 typval_T *argvars;
9328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329{
9330#ifdef FEAT_CINDENT
9331 pos_T pos;
9332 linenr_T lnum;
9333
9334 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009335 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9337 {
9338 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009339 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 curwin->w_cursor = pos;
9341 }
9342 else
9343#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009344 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345}
9346
9347/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009348 * "clearmatches()" function
9349 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009350 static void
9351f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009352 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009353 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009354{
9355#ifdef FEAT_SEARCH_EXTRA
9356 clear_matches(curwin);
9357#endif
9358}
9359
9360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 * "col(string)" function
9362 */
9363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009364f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009365 typval_T *argvars;
9366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367{
9368 colnr_T col = 0;
9369 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009370 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009372 fp = var2fpos(&argvars[0], FALSE, &fnum);
9373 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 {
9375 if (fp->col == MAXCOL)
9376 {
9377 /* '> can be MAXCOL, get the length of the line then */
9378 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009379 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 else
9381 col = MAXCOL;
9382 }
9383 else
9384 {
9385 col = fp->col + 1;
9386#ifdef FEAT_VIRTUALEDIT
9387 /* col(".") when the cursor is on the NUL at the end of the line
9388 * because of "coladd" can be seen as an extra column. */
9389 if (virtual_active() && fp == &curwin->w_cursor)
9390 {
9391 char_u *p = ml_get_cursor();
9392
9393 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9394 curwin->w_virtcol - curwin->w_cursor.coladd))
9395 {
9396# ifdef FEAT_MBYTE
9397 int l;
9398
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009399 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 col += l;
9401# else
9402 if (*p != NUL && p[1] == NUL)
9403 ++col;
9404# endif
9405 }
9406 }
9407#endif
9408 }
9409 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009410 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411}
9412
Bram Moolenaar572cb562005-08-05 21:35:02 +00009413#if defined(FEAT_INS_EXPAND)
9414/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009415 * "complete()" function
9416 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009417 static void
9418f_complete(argvars, rettv)
9419 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009420 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009421{
9422 int startcol;
9423
9424 if ((State & INSERT) == 0)
9425 {
9426 EMSG(_("E785: complete() can only be used in Insert mode"));
9427 return;
9428 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009429
9430 /* Check for undo allowed here, because if something was already inserted
9431 * the line was already saved for undo and this check isn't done. */
9432 if (!undo_allowed())
9433 return;
9434
Bram Moolenaarade00832006-03-10 21:46:58 +00009435 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9436 {
9437 EMSG(_(e_invarg));
9438 return;
9439 }
9440
9441 startcol = get_tv_number_chk(&argvars[0], NULL);
9442 if (startcol <= 0)
9443 return;
9444
9445 set_completion(startcol - 1, argvars[1].vval.v_list);
9446}
9447
9448/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009449 * "complete_add()" function
9450 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009451 static void
9452f_complete_add(argvars, rettv)
9453 typval_T *argvars;
9454 typval_T *rettv;
9455{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009456 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009457}
9458
9459/*
9460 * "complete_check()" function
9461 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009462 static void
9463f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009464 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009465 typval_T *rettv;
9466{
9467 int saved = RedrawingDisabled;
9468
9469 RedrawingDisabled = 0;
9470 ins_compl_check_keys(0);
9471 rettv->vval.v_number = compl_interrupted;
9472 RedrawingDisabled = saved;
9473}
9474#endif
9475
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476/*
9477 * "confirm(message, buttons[, default [, type]])" function
9478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009480f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009481 typval_T *argvars UNUSED;
9482 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483{
9484#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9485 char_u *message;
9486 char_u *buttons = NULL;
9487 char_u buf[NUMBUFLEN];
9488 char_u buf2[NUMBUFLEN];
9489 int def = 1;
9490 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009491 char_u *typestr;
9492 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009494 message = get_tv_string_chk(&argvars[0]);
9495 if (message == NULL)
9496 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009497 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009499 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9500 if (buttons == NULL)
9501 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009502 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009504 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009505 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009507 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9508 if (typestr == NULL)
9509 error = TRUE;
9510 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009512 switch (TOUPPER_ASC(*typestr))
9513 {
9514 case 'E': type = VIM_ERROR; break;
9515 case 'Q': type = VIM_QUESTION; break;
9516 case 'I': type = VIM_INFO; break;
9517 case 'W': type = VIM_WARNING; break;
9518 case 'G': type = VIM_GENERIC; break;
9519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 }
9521 }
9522 }
9523 }
9524
9525 if (buttons == NULL || *buttons == NUL)
9526 buttons = (char_u *)_("&Ok");
9527
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009528 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009529 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009530 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531#endif
9532}
9533
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009534/*
9535 * "copy()" function
9536 */
9537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009538f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009539 typval_T *argvars;
9540 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009541{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009542 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009543}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009545#ifdef FEAT_FLOAT
9546/*
9547 * "cos()" function
9548 */
9549 static void
9550f_cos(argvars, rettv)
9551 typval_T *argvars;
9552 typval_T *rettv;
9553{
9554 float_T f;
9555
9556 rettv->v_type = VAR_FLOAT;
9557 if (get_float_arg(argvars, &f) == OK)
9558 rettv->vval.v_float = cos(f);
9559 else
9560 rettv->vval.v_float = 0.0;
9561}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009562
9563/*
9564 * "cosh()" function
9565 */
9566 static void
9567f_cosh(argvars, rettv)
9568 typval_T *argvars;
9569 typval_T *rettv;
9570{
9571 float_T f;
9572
9573 rettv->v_type = VAR_FLOAT;
9574 if (get_float_arg(argvars, &f) == OK)
9575 rettv->vval.v_float = cosh(f);
9576 else
9577 rettv->vval.v_float = 0.0;
9578}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009579#endif
9580
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009582 * "count()" function
9583 */
9584 static void
9585f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009586 typval_T *argvars;
9587 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009588{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009589 long n = 0;
9590 int ic = FALSE;
9591
Bram Moolenaare9a41262005-01-15 22:18:47 +00009592 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009593 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009594 listitem_T *li;
9595 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009596 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009597
Bram Moolenaare9a41262005-01-15 22:18:47 +00009598 if ((l = argvars[0].vval.v_list) != NULL)
9599 {
9600 li = l->lv_first;
9601 if (argvars[2].v_type != VAR_UNKNOWN)
9602 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009603 int error = FALSE;
9604
9605 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009606 if (argvars[3].v_type != VAR_UNKNOWN)
9607 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009608 idx = get_tv_number_chk(&argvars[3], &error);
9609 if (!error)
9610 {
9611 li = list_find(l, idx);
9612 if (li == NULL)
9613 EMSGN(_(e_listidx), idx);
9614 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009615 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009616 if (error)
9617 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009618 }
9619
9620 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009621 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009622 ++n;
9623 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009624 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009625 else if (argvars[0].v_type == VAR_DICT)
9626 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009627 int todo;
9628 dict_T *d;
9629 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009630
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009631 if ((d = argvars[0].vval.v_dict) != NULL)
9632 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009633 int error = FALSE;
9634
Bram Moolenaare9a41262005-01-15 22:18:47 +00009635 if (argvars[2].v_type != VAR_UNKNOWN)
9636 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009637 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009638 if (argvars[3].v_type != VAR_UNKNOWN)
9639 EMSG(_(e_invarg));
9640 }
9641
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009642 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009643 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009644 {
9645 if (!HASHITEM_EMPTY(hi))
9646 {
9647 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009648 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009649 ++n;
9650 }
9651 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009652 }
9653 }
9654 else
9655 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009656 rettv->vval.v_number = n;
9657}
9658
9659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9661 *
9662 * Checks the existence of a cscope connection.
9663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009665f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009666 typval_T *argvars UNUSED;
9667 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668{
9669#ifdef FEAT_CSCOPE
9670 int num = 0;
9671 char_u *dbpath = NULL;
9672 char_u *prepend = NULL;
9673 char_u buf[NUMBUFLEN];
9674
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009675 if (argvars[0].v_type != VAR_UNKNOWN
9676 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009678 num = (int)get_tv_number(&argvars[0]);
9679 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009680 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009681 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 }
9683
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009684 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685#endif
9686}
9687
9688/*
9689 * "cursor(lnum, col)" function
9690 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009691 * Moves the cursor to the specified line and column.
9692 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009695f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009696 typval_T *argvars;
9697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698{
9699 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009700#ifdef FEAT_VIRTUALEDIT
9701 long coladd = 0;
9702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009704 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009705 if (argvars[1].v_type == VAR_UNKNOWN)
9706 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009707 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009708
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009709 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009710 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009711 line = pos.lnum;
9712 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009713#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009714 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009715#endif
9716 }
9717 else
9718 {
9719 line = get_tv_lnum(argvars);
9720 col = get_tv_number_chk(&argvars[1], NULL);
9721#ifdef FEAT_VIRTUALEDIT
9722 if (argvars[2].v_type != VAR_UNKNOWN)
9723 coladd = get_tv_number_chk(&argvars[2], NULL);
9724#endif
9725 }
9726 if (line < 0 || col < 0
9727#ifdef FEAT_VIRTUALEDIT
9728 || coladd < 0
9729#endif
9730 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009731 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 if (line > 0)
9733 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 if (col > 0)
9735 curwin->w_cursor.col = col - 1;
9736#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009737 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738#endif
9739
9740 /* Make sure the cursor is in a valid position. */
9741 check_cursor();
9742#ifdef FEAT_MBYTE
9743 /* Correct cursor for multi-byte character. */
9744 if (has_mbyte)
9745 mb_adjust_cursor();
9746#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009747
9748 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009749 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750}
9751
9752/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009753 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 */
9755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009757 typval_T *argvars;
9758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009760 int noref = 0;
9761
9762 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009763 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009764 if (noref < 0 || noref > 1)
9765 EMSG(_(e_invarg));
9766 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009767 {
9768 current_copyID += COPYID_INC;
9769 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771}
9772
9773/*
9774 * "delete()" function
9775 */
9776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009777f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009778 typval_T *argvars;
9779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780{
9781 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009782 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009784 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785}
9786
9787/*
9788 * "did_filetype()" function
9789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009791f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009792 typval_T *argvars UNUSED;
9793 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794{
9795#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009796 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797#endif
9798}
9799
9800/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009801 * "diff_filler()" function
9802 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009804f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009805 typval_T *argvars UNUSED;
9806 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009807{
9808#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009810#endif
9811}
9812
9813/*
9814 * "diff_hlID()" function
9815 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009817f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009818 typval_T *argvars UNUSED;
9819 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009820{
9821#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009822 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009823 static linenr_T prev_lnum = 0;
9824 static int changedtick = 0;
9825 static int fnum = 0;
9826 static int change_start = 0;
9827 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009828 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009829 int filler_lines;
9830 int col;
9831
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009832 if (lnum < 0) /* ignore type error in {lnum} arg */
9833 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009834 if (lnum != prev_lnum
9835 || changedtick != curbuf->b_changedtick
9836 || fnum != curbuf->b_fnum)
9837 {
9838 /* New line, buffer, change: need to get the values. */
9839 filler_lines = diff_check(curwin, lnum);
9840 if (filler_lines < 0)
9841 {
9842 if (filler_lines == -1)
9843 {
9844 change_start = MAXCOL;
9845 change_end = -1;
9846 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9847 hlID = HLF_ADD; /* added line */
9848 else
9849 hlID = HLF_CHD; /* changed line */
9850 }
9851 else
9852 hlID = HLF_ADD; /* added line */
9853 }
9854 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009855 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009856 prev_lnum = lnum;
9857 changedtick = curbuf->b_changedtick;
9858 fnum = curbuf->b_fnum;
9859 }
9860
9861 if (hlID == HLF_CHD || hlID == HLF_TXD)
9862 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009863 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009864 if (col >= change_start && col <= change_end)
9865 hlID = HLF_TXD; /* changed text */
9866 else
9867 hlID = HLF_CHD; /* changed line */
9868 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009869 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009870#endif
9871}
9872
9873/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009874 * "empty({expr})" function
9875 */
9876 static void
9877f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009878 typval_T *argvars;
9879 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009880{
9881 int n;
9882
9883 switch (argvars[0].v_type)
9884 {
9885 case VAR_STRING:
9886 case VAR_FUNC:
9887 n = argvars[0].vval.v_string == NULL
9888 || *argvars[0].vval.v_string == NUL;
9889 break;
9890 case VAR_NUMBER:
9891 n = argvars[0].vval.v_number == 0;
9892 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009893#ifdef FEAT_FLOAT
9894 case VAR_FLOAT:
9895 n = argvars[0].vval.v_float == 0.0;
9896 break;
9897#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009898 case VAR_LIST:
9899 n = argvars[0].vval.v_list == NULL
9900 || argvars[0].vval.v_list->lv_first == NULL;
9901 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009902 case VAR_DICT:
9903 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009904 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009905 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009906 default:
9907 EMSG2(_(e_intern2), "f_empty()");
9908 n = 0;
9909 }
9910
9911 rettv->vval.v_number = n;
9912}
9913
9914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 * "escape({string}, {chars})" function
9916 */
9917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009918f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009919 typval_T *argvars;
9920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921{
9922 char_u buf[NUMBUFLEN];
9923
Bram Moolenaar758711c2005-02-02 23:11:38 +00009924 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9925 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009926 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927}
9928
9929/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009930 * "eval()" function
9931 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009932 static void
9933f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009934 typval_T *argvars;
9935 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009936{
9937 char_u *s;
9938
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009939 s = get_tv_string_chk(&argvars[0]);
9940 if (s != NULL)
9941 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009942
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009943 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9944 {
9945 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009946 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009947 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009948 else if (*s != NUL)
9949 EMSG(_(e_trailing));
9950}
9951
9952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 * "eventhandler()" function
9954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009956f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009957 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009960 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961}
9962
9963/*
9964 * "executable()" function
9965 */
9966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009967f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009968 typval_T *argvars;
9969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009971 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972}
9973
9974/*
9975 * "exists()" function
9976 */
9977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009978f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009979 typval_T *argvars;
9980 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981{
9982 char_u *p;
9983 char_u *name;
9984 int n = FALSE;
9985 int len = 0;
9986
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009987 no_autoload = TRUE;
9988
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009989 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 if (*p == '$') /* environment variable */
9991 {
9992 /* first try "normal" environment variables (fast) */
9993 if (mch_getenv(p + 1) != NULL)
9994 n = TRUE;
9995 else
9996 {
9997 /* try expanding things like $VIM and ${HOME} */
9998 p = expand_env_save(p);
9999 if (p != NULL && *p != '$')
10000 n = TRUE;
10001 vim_free(p);
10002 }
10003 }
10004 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010006 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010007 if (*skipwhite(p) != NUL)
10008 n = FALSE; /* trailing garbage */
10009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 else if (*p == '*') /* internal or user defined function */
10011 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010012 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 }
10014 else if (*p == ':')
10015 {
10016 n = cmd_exists(p + 1);
10017 }
10018 else if (*p == '#')
10019 {
10020#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010021 if (p[1] == '#')
10022 n = autocmd_supported(p + 2);
10023 else
10024 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025#endif
10026 }
10027 else /* internal variable */
10028 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010029 char_u *tofree;
10030 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010032 /* get_name_len() takes care of expanding curly braces */
10033 name = p;
10034 len = get_name_len(&p, &tofree, TRUE, FALSE);
10035 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010037 if (tofree != NULL)
10038 name = tofree;
10039 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10040 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010042 /* handle d.key, l[idx], f(expr) */
10043 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10044 if (n)
10045 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 }
10047 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010048 if (*p != NUL)
10049 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010051 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 }
10053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010054 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010055
10056 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057}
10058
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010059#ifdef FEAT_FLOAT
10060/*
10061 * "exp()" function
10062 */
10063 static void
10064f_exp(argvars, rettv)
10065 typval_T *argvars;
10066 typval_T *rettv;
10067{
10068 float_T f;
10069
10070 rettv->v_type = VAR_FLOAT;
10071 if (get_float_arg(argvars, &f) == OK)
10072 rettv->vval.v_float = exp(f);
10073 else
10074 rettv->vval.v_float = 0.0;
10075}
10076#endif
10077
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078/*
10079 * "expand()" function
10080 */
10081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010082f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010083 typval_T *argvars;
10084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085{
10086 char_u *s;
10087 int len;
10088 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010089 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010091 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010092 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010095 if (argvars[1].v_type != VAR_UNKNOWN
10096 && argvars[2].v_type != VAR_UNKNOWN
10097 && get_tv_number_chk(&argvars[2], &error)
10098 && !error)
10099 {
10100 rettv->v_type = VAR_LIST;
10101 rettv->vval.v_list = NULL;
10102 }
10103
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010104 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 if (*s == '%' || *s == '#' || *s == '<')
10106 {
10107 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010108 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010110 if (rettv->v_type == VAR_LIST)
10111 {
10112 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10113 list_append_string(rettv->vval.v_list, result, -1);
10114 else
10115 vim_free(result);
10116 }
10117 else
10118 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 }
10120 else
10121 {
10122 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010123 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010124 if (argvars[1].v_type != VAR_UNKNOWN
10125 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010126 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010127 if (!error)
10128 {
10129 ExpandInit(&xpc);
10130 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010131 if (p_wic)
10132 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010133 if (rettv->v_type == VAR_STRING)
10134 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10135 options, WILD_ALL);
10136 else if (rettv_list_alloc(rettv) != FAIL)
10137 {
10138 int i;
10139
10140 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10141 for (i = 0; i < xpc.xp_numfiles; i++)
10142 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10143 ExpandCleanup(&xpc);
10144 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010145 }
10146 else
10147 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 }
10149}
10150
10151/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010152 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010153 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010154 */
10155 static void
10156f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010157 typval_T *argvars;
10158 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010159{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010160 char *arg_errmsg = N_("extend() argument");
10161
Bram Moolenaare9a41262005-01-15 22:18:47 +000010162 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010163 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010164 list_T *l1, *l2;
10165 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010166 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010167 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010168
Bram Moolenaare9a41262005-01-15 22:18:47 +000010169 l1 = argvars[0].vval.v_list;
10170 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010171 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010172 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010173 {
10174 if (argvars[2].v_type != VAR_UNKNOWN)
10175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010176 before = get_tv_number_chk(&argvars[2], &error);
10177 if (error)
10178 return; /* type error; errmsg already given */
10179
Bram Moolenaar758711c2005-02-02 23:11:38 +000010180 if (before == l1->lv_len)
10181 item = NULL;
10182 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010183 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010184 item = list_find(l1, before);
10185 if (item == NULL)
10186 {
10187 EMSGN(_(e_listidx), before);
10188 return;
10189 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010190 }
10191 }
10192 else
10193 item = NULL;
10194 list_extend(l1, l2, item);
10195
Bram Moolenaare9a41262005-01-15 22:18:47 +000010196 copy_tv(&argvars[0], rettv);
10197 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010198 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010199 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10200 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010201 dict_T *d1, *d2;
10202 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010203 char_u *action;
10204 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010205 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010206 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010207
10208 d1 = argvars[0].vval.v_dict;
10209 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010210 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010211 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010212 {
10213 /* Check the third argument. */
10214 if (argvars[2].v_type != VAR_UNKNOWN)
10215 {
10216 static char *(av[]) = {"keep", "force", "error"};
10217
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010218 action = get_tv_string_chk(&argvars[2]);
10219 if (action == NULL)
10220 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010221 for (i = 0; i < 3; ++i)
10222 if (STRCMP(action, av[i]) == 0)
10223 break;
10224 if (i == 3)
10225 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010226 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010227 return;
10228 }
10229 }
10230 else
10231 action = (char_u *)"force";
10232
10233 /* Go over all entries in the second dict and add them to the
10234 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010235 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010236 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010237 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010238 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010239 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010240 --todo;
10241 di1 = dict_find(d1, hi2->hi_key, -1);
Bram Moolenaarbdb62052012-07-16 17:31:53 +020010242 if (d1->dv_scope != 0)
10243 {
10244 /* Disallow replacing a builtin function in l: and g:.
10245 * Check the key to be valid when adding to any
10246 * scope. */
10247 if (d1->dv_scope == VAR_DEF_SCOPE
10248 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10249 && var_check_func_name(hi2->hi_key,
10250 di1 == NULL))
10251 break;
10252 if (!valid_varname(hi2->hi_key))
10253 break;
10254 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010255 if (di1 == NULL)
10256 {
10257 di1 = dictitem_copy(HI2DI(hi2));
10258 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10259 dictitem_free(di1);
10260 }
10261 else if (*action == 'e')
10262 {
10263 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10264 break;
10265 }
Bram Moolenaar2fc88022012-05-18 12:07:05 +020010266 else if (*action == 'f' && HI2DI(hi2) != di1)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010267 {
10268 clear_tv(&di1->di_tv);
10269 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10270 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010271 }
10272 }
10273
Bram Moolenaare9a41262005-01-15 22:18:47 +000010274 copy_tv(&argvars[0], rettv);
10275 }
10276 }
10277 else
10278 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010279}
10280
10281/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010282 * "feedkeys()" function
10283 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010284 static void
10285f_feedkeys(argvars, rettv)
10286 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010287 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010288{
10289 int remap = TRUE;
10290 char_u *keys, *flags;
10291 char_u nbuf[NUMBUFLEN];
10292 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010293 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010294
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010295 /* This is not allowed in the sandbox. If the commands would still be
10296 * executed in the sandbox it would be OK, but it probably happens later,
10297 * when "sandbox" is no longer set. */
10298 if (check_secure())
10299 return;
10300
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010301 keys = get_tv_string(&argvars[0]);
10302 if (*keys != NUL)
10303 {
10304 if (argvars[1].v_type != VAR_UNKNOWN)
10305 {
10306 flags = get_tv_string_buf(&argvars[1], nbuf);
10307 for ( ; *flags != NUL; ++flags)
10308 {
10309 switch (*flags)
10310 {
10311 case 'n': remap = FALSE; break;
10312 case 'm': remap = TRUE; break;
10313 case 't': typed = TRUE; break;
10314 }
10315 }
10316 }
10317
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010318 /* Need to escape K_SPECIAL and CSI before putting the string in the
10319 * typeahead buffer. */
10320 keys_esc = vim_strsave_escape_csi(keys);
10321 if (keys_esc != NULL)
10322 {
10323 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010324 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010325 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010326 if (vgetc_busy)
10327 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010328 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010329 }
10330}
10331
10332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 * "filereadable()" function
10334 */
10335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010336f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010337 typval_T *argvars;
10338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010340 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 char_u *p;
10342 int n;
10343
Bram Moolenaarc236c162008-07-13 17:41:49 +000010344#ifndef O_NONBLOCK
10345# define O_NONBLOCK 0
10346#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010347 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010348 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10349 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 {
10351 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010352 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 }
10354 else
10355 n = FALSE;
10356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010357 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358}
10359
10360/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010361 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 * rights to write into.
10363 */
10364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010365f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010366 typval_T *argvars;
10367 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010369 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370}
10371
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010372static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010373
10374 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010375findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010376 typval_T *argvars;
10377 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010378 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010379{
10380#ifdef FEAT_SEARCHPATH
10381 char_u *fname;
10382 char_u *fresult = NULL;
10383 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10384 char_u *p;
10385 char_u pathbuf[NUMBUFLEN];
10386 int count = 1;
10387 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010388 int error = FALSE;
10389#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010390
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010391 rettv->vval.v_string = NULL;
10392 rettv->v_type = VAR_STRING;
10393
10394#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010395 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010396
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010397 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010398 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010399 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10400 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010401 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010402 else
10403 {
10404 if (*p != NUL)
10405 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010406
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010407 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010408 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010409 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010410 }
10411
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010412 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10413 error = TRUE;
10414
10415 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010416 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010417 do
10418 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010419 if (rettv->v_type == VAR_STRING)
10420 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010421 fresult = find_file_in_path_option(first ? fname : NULL,
10422 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010423 0, first, path,
10424 find_what,
10425 curbuf->b_ffname,
10426 find_what == FINDFILE_DIR
10427 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010428 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010429
10430 if (fresult != NULL && rettv->v_type == VAR_LIST)
10431 list_append_string(rettv->vval.v_list, fresult, -1);
10432
10433 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010434 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010435
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010436 if (rettv->v_type == VAR_STRING)
10437 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010438#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010439}
10440
Bram Moolenaar33570922005-01-25 22:26:29 +000010441static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10442static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010443
10444/*
10445 * Implementation of map() and filter().
10446 */
10447 static void
10448filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010449 typval_T *argvars;
10450 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010451 int map;
10452{
10453 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010454 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010455 listitem_T *li, *nli;
10456 list_T *l = NULL;
10457 dictitem_T *di;
10458 hashtab_T *ht;
10459 hashitem_T *hi;
10460 dict_T *d = NULL;
10461 typval_T save_val;
10462 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010463 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010464 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010465 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10466 char *arg_errmsg = (map ? N_("map() argument")
10467 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010468 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010469 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010470
Bram Moolenaare9a41262005-01-15 22:18:47 +000010471 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010472 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010473 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010474 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010475 return;
10476 }
10477 else if (argvars[0].v_type == VAR_DICT)
10478 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010479 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010480 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010481 return;
10482 }
10483 else
10484 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010485 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010486 return;
10487 }
10488
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010489 expr = get_tv_string_buf_chk(&argvars[1], buf);
10490 /* On type errors, the preceding call has already displayed an error
10491 * message. Avoid a misleading error message for an empty string that
10492 * was not passed as argument. */
10493 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010495 prepare_vimvar(VV_VAL, &save_val);
10496 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010497
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010498 /* We reset "did_emsg" to be able to detect whether an error
10499 * occurred during evaluation of the expression. */
10500 save_did_emsg = did_emsg;
10501 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010502
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010503 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010504 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010506 vimvars[VV_KEY].vv_type = VAR_STRING;
10507
10508 ht = &d->dv_hashtab;
10509 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010510 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010511 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010512 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010513 if (!HASHITEM_EMPTY(hi))
10514 {
10515 --todo;
10516 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010517 if (tv_check_lock(di->di_tv.v_lock,
10518 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010519 break;
10520 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010521 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010522 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010523 break;
10524 if (!map && rem)
10525 dictitem_remove(d, di);
10526 clear_tv(&vimvars[VV_KEY].vv_tv);
10527 }
10528 }
10529 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010530 }
10531 else
10532 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010533 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10534
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010535 for (li = l->lv_first; li != NULL; li = nli)
10536 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010537 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010538 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010539 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010540 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010541 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010542 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010543 break;
10544 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010545 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010546 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010547 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010548 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010549
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010550 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010551 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010552
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010553 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010554 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010555
10556 copy_tv(&argvars[0], rettv);
10557}
10558
10559 static int
10560filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010561 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010562 char_u *expr;
10563 int map;
10564 int *remp;
10565{
Bram Moolenaar33570922005-01-25 22:26:29 +000010566 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010567 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010568 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010569
Bram Moolenaar33570922005-01-25 22:26:29 +000010570 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010571 s = expr;
10572 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010573 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010574 if (*s != NUL) /* check for trailing chars after expr */
10575 {
10576 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010577 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010578 }
10579 if (map)
10580 {
10581 /* map(): replace the list item value */
10582 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010583 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010584 *tv = rettv;
10585 }
10586 else
10587 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010588 int error = FALSE;
10589
Bram Moolenaare9a41262005-01-15 22:18:47 +000010590 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010591 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010592 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010593 /* On type error, nothing has been removed; return FAIL to stop the
10594 * loop. The error message was given by get_tv_number_chk(). */
10595 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010596 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010597 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010598 retval = OK;
10599theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010600 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010601 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010602}
10603
10604/*
10605 * "filter()" function
10606 */
10607 static void
10608f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010609 typval_T *argvars;
10610 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010611{
10612 filter_map(argvars, rettv, FALSE);
10613}
10614
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010615/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010616 * "finddir({fname}[, {path}[, {count}]])" function
10617 */
10618 static void
10619f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010620 typval_T *argvars;
10621 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010622{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010623 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010624}
10625
10626/*
10627 * "findfile({fname}[, {path}[, {count}]])" function
10628 */
10629 static void
10630f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010631 typval_T *argvars;
10632 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010633{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010634 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010635}
10636
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010637#ifdef FEAT_FLOAT
10638/*
10639 * "float2nr({float})" function
10640 */
10641 static void
10642f_float2nr(argvars, rettv)
10643 typval_T *argvars;
10644 typval_T *rettv;
10645{
10646 float_T f;
10647
10648 if (get_float_arg(argvars, &f) == OK)
10649 {
10650 if (f < -0x7fffffff)
10651 rettv->vval.v_number = -0x7fffffff;
10652 else if (f > 0x7fffffff)
10653 rettv->vval.v_number = 0x7fffffff;
10654 else
10655 rettv->vval.v_number = (varnumber_T)f;
10656 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010657}
10658
10659/*
10660 * "floor({float})" function
10661 */
10662 static void
10663f_floor(argvars, rettv)
10664 typval_T *argvars;
10665 typval_T *rettv;
10666{
10667 float_T f;
10668
10669 rettv->v_type = VAR_FLOAT;
10670 if (get_float_arg(argvars, &f) == OK)
10671 rettv->vval.v_float = floor(f);
10672 else
10673 rettv->vval.v_float = 0.0;
10674}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010675
10676/*
10677 * "fmod()" function
10678 */
10679 static void
10680f_fmod(argvars, rettv)
10681 typval_T *argvars;
10682 typval_T *rettv;
10683{
10684 float_T fx, fy;
10685
10686 rettv->v_type = VAR_FLOAT;
10687 if (get_float_arg(argvars, &fx) == OK
10688 && get_float_arg(&argvars[1], &fy) == OK)
10689 rettv->vval.v_float = fmod(fx, fy);
10690 else
10691 rettv->vval.v_float = 0.0;
10692}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010693#endif
10694
Bram Moolenaar0d660222005-01-07 21:51:51 +000010695/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010696 * "fnameescape({string})" function
10697 */
10698 static void
10699f_fnameescape(argvars, rettv)
10700 typval_T *argvars;
10701 typval_T *rettv;
10702{
10703 rettv->vval.v_string = vim_strsave_fnameescape(
10704 get_tv_string(&argvars[0]), FALSE);
10705 rettv->v_type = VAR_STRING;
10706}
10707
10708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 * "fnamemodify({fname}, {mods})" function
10710 */
10711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010712f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010713 typval_T *argvars;
10714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715{
10716 char_u *fname;
10717 char_u *mods;
10718 int usedlen = 0;
10719 int len;
10720 char_u *fbuf = NULL;
10721 char_u buf[NUMBUFLEN];
10722
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010723 fname = get_tv_string_chk(&argvars[0]);
10724 mods = get_tv_string_buf_chk(&argvars[1], buf);
10725 if (fname == NULL || mods == NULL)
10726 fname = NULL;
10727 else
10728 {
10729 len = (int)STRLEN(fname);
10730 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010733 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010735 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010737 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 vim_free(fbuf);
10739}
10740
Bram Moolenaar33570922005-01-25 22:26:29 +000010741static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742
10743/*
10744 * "foldclosed()" function
10745 */
10746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010747foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010748 typval_T *argvars;
10749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 int end;
10751{
10752#ifdef FEAT_FOLDING
10753 linenr_T lnum;
10754 linenr_T first, last;
10755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010756 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10758 {
10759 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10760 {
10761 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010762 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010764 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 return;
10766 }
10767 }
10768#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010769 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770}
10771
10772/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010773 * "foldclosed()" function
10774 */
10775 static void
10776f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010777 typval_T *argvars;
10778 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010779{
10780 foldclosed_both(argvars, rettv, FALSE);
10781}
10782
10783/*
10784 * "foldclosedend()" function
10785 */
10786 static void
10787f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010788 typval_T *argvars;
10789 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010790{
10791 foldclosed_both(argvars, rettv, TRUE);
10792}
10793
10794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 * "foldlevel()" function
10796 */
10797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010798f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010799 typval_T *argvars;
10800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801{
10802#ifdef FEAT_FOLDING
10803 linenr_T lnum;
10804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010805 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010807 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809}
10810
10811/*
10812 * "foldtext()" function
10813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010815f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010816 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818{
10819#ifdef FEAT_FOLDING
10820 linenr_T lnum;
10821 char_u *s;
10822 char_u *r;
10823 int len;
10824 char *txt;
10825#endif
10826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010827 rettv->v_type = VAR_STRING;
10828 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010830 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10831 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10832 <= curbuf->b_ml.ml_line_count
10833 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 {
10835 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010836 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10837 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 {
10839 if (!linewhite(lnum))
10840 break;
10841 ++lnum;
10842 }
10843
10844 /* Find interesting text in this line. */
10845 s = skipwhite(ml_get(lnum));
10846 /* skip C comment-start */
10847 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010848 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010850 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010851 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010852 {
10853 s = skipwhite(ml_get(lnum + 1));
10854 if (*s == '*')
10855 s = skipwhite(s + 1);
10856 }
10857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 txt = _("+-%s%3ld lines: ");
10859 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010860 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861 + 20 /* for %3ld */
10862 + STRLEN(s))); /* concatenated */
10863 if (r != NULL)
10864 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010865 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10866 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10867 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 len = (int)STRLEN(r);
10869 STRCAT(r, s);
10870 /* remove 'foldmarker' and 'commentstring' */
10871 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010872 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 }
10874 }
10875#endif
10876}
10877
10878/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010879 * "foldtextresult(lnum)" function
10880 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010882f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010883 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010884 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010885{
10886#ifdef FEAT_FOLDING
10887 linenr_T lnum;
10888 char_u *text;
10889 char_u buf[51];
10890 foldinfo_T foldinfo;
10891 int fold_count;
10892#endif
10893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010894 rettv->v_type = VAR_STRING;
10895 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010896#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010897 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010898 /* treat illegal types and illegal string values for {lnum} the same */
10899 if (lnum < 0)
10900 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010901 fold_count = foldedCount(curwin, lnum, &foldinfo);
10902 if (fold_count > 0)
10903 {
10904 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10905 &foldinfo, buf);
10906 if (text == buf)
10907 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010908 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010909 }
10910#endif
10911}
10912
10913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 * "foreground()" function
10915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010917f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010918 typval_T *argvars UNUSED;
10919 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921#ifdef FEAT_GUI
10922 if (gui.in_use)
10923 gui_mch_set_foreground();
10924#else
10925# ifdef WIN32
10926 win32_set_foreground();
10927# endif
10928#endif
10929}
10930
10931/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010932 * "function()" function
10933 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010935f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010936 typval_T *argvars;
10937 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010938{
10939 char_u *s;
10940
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010941 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010942 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010943 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010944 /* Don't check an autoload name for existence here. */
10945 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010946 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010947 else
10948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010949 rettv->vval.v_string = vim_strsave(s);
10950 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010951 }
10952}
10953
10954/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010955 * "garbagecollect()" function
10956 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010957 static void
10958f_garbagecollect(argvars, rettv)
10959 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010960 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010961{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010962 /* This is postponed until we are back at the toplevel, because we may be
10963 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10964 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010965
10966 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10967 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010968}
10969
10970/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010971 * "get()" function
10972 */
10973 static void
10974f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010975 typval_T *argvars;
10976 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010977{
Bram Moolenaar33570922005-01-25 22:26:29 +000010978 listitem_T *li;
10979 list_T *l;
10980 dictitem_T *di;
10981 dict_T *d;
10982 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010983
Bram Moolenaare9a41262005-01-15 22:18:47 +000010984 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010985 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010986 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010987 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010988 int error = FALSE;
10989
10990 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10991 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010992 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010993 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010994 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010995 else if (argvars[0].v_type == VAR_DICT)
10996 {
10997 if ((d = argvars[0].vval.v_dict) != NULL)
10998 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010999 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011000 if (di != NULL)
11001 tv = &di->di_tv;
11002 }
11003 }
11004 else
11005 EMSG2(_(e_listdictarg), "get()");
11006
11007 if (tv == NULL)
11008 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011009 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011010 copy_tv(&argvars[2], rettv);
11011 }
11012 else
11013 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011014}
11015
Bram Moolenaar342337a2005-07-21 21:11:17 +000011016static 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 +000011017
11018/*
11019 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011020 * Return a range (from start to end) of lines in rettv from the specified
11021 * buffer.
11022 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011023 */
11024 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011025get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011026 buf_T *buf;
11027 linenr_T start;
11028 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011029 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011030 typval_T *rettv;
11031{
11032 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011033
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011034 if (retlist && rettv_list_alloc(rettv) == FAIL)
11035 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011036
11037 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11038 return;
11039
11040 if (!retlist)
11041 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011042 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11043 p = ml_get_buf(buf, start, FALSE);
11044 else
11045 p = (char_u *)"";
11046
11047 rettv->v_type = VAR_STRING;
11048 rettv->vval.v_string = vim_strsave(p);
11049 }
11050 else
11051 {
11052 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011053 return;
11054
11055 if (start < 1)
11056 start = 1;
11057 if (end > buf->b_ml.ml_line_count)
11058 end = buf->b_ml.ml_line_count;
11059 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011060 if (list_append_string(rettv->vval.v_list,
11061 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011062 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011063 }
11064}
11065
11066/*
11067 * "getbufline()" function
11068 */
11069 static void
11070f_getbufline(argvars, rettv)
11071 typval_T *argvars;
11072 typval_T *rettv;
11073{
11074 linenr_T lnum;
11075 linenr_T end;
11076 buf_T *buf;
11077
11078 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11079 ++emsg_off;
11080 buf = get_buf_tv(&argvars[0]);
11081 --emsg_off;
11082
Bram Moolenaar661b1822005-07-28 22:36:45 +000011083 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011084 if (argvars[2].v_type == VAR_UNKNOWN)
11085 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011086 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011087 end = get_tv_lnum_buf(&argvars[2], buf);
11088
Bram Moolenaar342337a2005-07-21 21:11:17 +000011089 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011090}
11091
Bram Moolenaar0d660222005-01-07 21:51:51 +000011092/*
11093 * "getbufvar()" function
11094 */
11095 static void
11096f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011097 typval_T *argvars;
11098 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011099{
11100 buf_T *buf;
11101 buf_T *save_curbuf;
11102 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011103 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011105 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11106 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011107 ++emsg_off;
11108 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011109
11110 rettv->v_type = VAR_STRING;
11111 rettv->vval.v_string = NULL;
11112
11113 if (buf != NULL && varname != NULL)
11114 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011115 /* set curbuf to be our buf, temporarily */
11116 save_curbuf = curbuf;
11117 curbuf = buf;
11118
Bram Moolenaar0d660222005-01-07 21:51:51 +000011119 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011120 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010011121 else if (STRCMP(varname, "changedtick") == 0)
11122 {
11123 rettv->v_type = VAR_NUMBER;
11124 rettv->vval.v_number = curbuf->b_changedtick;
11125 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011126 else
11127 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011128 if (*varname == NUL)
11129 /* let getbufvar({nr}, "") return the "b:" dictionary. The
11130 * scope prefix before the NUL byte is required by
11131 * find_var_in_ht(). */
11132 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011133 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000011134 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011135 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011136 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011137 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011138
11139 /* restore previous notion of curbuf */
11140 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011141 }
11142
11143 --emsg_off;
11144}
11145
11146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147 * "getchar()" function
11148 */
11149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011150f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011151 typval_T *argvars;
11152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153{
11154 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011155 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011157 /* Position the cursor. Needed after a message that ends in a space. */
11158 windgoto(msg_row, msg_col);
11159
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160 ++no_mapping;
11161 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011162 for (;;)
11163 {
11164 if (argvars[0].v_type == VAR_UNKNOWN)
11165 /* getchar(): blocking wait. */
11166 n = safe_vgetc();
11167 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11168 /* getchar(1): only check if char avail */
11169 n = vpeekc();
11170 else if (error || vpeekc() == NUL)
11171 /* illegal argument or getchar(0) and no char avail: return zero */
11172 n = 0;
11173 else
11174 /* getchar(0) and char avail: return char */
11175 n = safe_vgetc();
11176 if (n == K_IGNORE)
11177 continue;
11178 break;
11179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180 --no_mapping;
11181 --allow_keys;
11182
Bram Moolenaar219b8702006-11-01 14:32:36 +000011183 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11184 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11185 vimvars[VV_MOUSE_COL].vv_nr = 0;
11186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011187 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 if (IS_SPECIAL(n) || mod_mask != 0)
11189 {
11190 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11191 int i = 0;
11192
11193 /* Turn a special key into three bytes, plus modifier. */
11194 if (mod_mask != 0)
11195 {
11196 temp[i++] = K_SPECIAL;
11197 temp[i++] = KS_MODIFIER;
11198 temp[i++] = mod_mask;
11199 }
11200 if (IS_SPECIAL(n))
11201 {
11202 temp[i++] = K_SPECIAL;
11203 temp[i++] = K_SECOND(n);
11204 temp[i++] = K_THIRD(n);
11205 }
11206#ifdef FEAT_MBYTE
11207 else if (has_mbyte)
11208 i += (*mb_char2bytes)(n, temp + i);
11209#endif
11210 else
11211 temp[i++] = n;
11212 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011213 rettv->v_type = VAR_STRING;
11214 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011215
11216#ifdef FEAT_MOUSE
11217 if (n == K_LEFTMOUSE
11218 || n == K_LEFTMOUSE_NM
11219 || n == K_LEFTDRAG
11220 || n == K_LEFTRELEASE
11221 || n == K_LEFTRELEASE_NM
11222 || n == K_MIDDLEMOUSE
11223 || n == K_MIDDLEDRAG
11224 || n == K_MIDDLERELEASE
11225 || n == K_RIGHTMOUSE
11226 || n == K_RIGHTDRAG
11227 || n == K_RIGHTRELEASE
11228 || n == K_X1MOUSE
11229 || n == K_X1DRAG
11230 || n == K_X1RELEASE
11231 || n == K_X2MOUSE
11232 || n == K_X2DRAG
11233 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011234 || n == K_MOUSELEFT
11235 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011236 || n == K_MOUSEDOWN
11237 || n == K_MOUSEUP)
11238 {
11239 int row = mouse_row;
11240 int col = mouse_col;
11241 win_T *win;
11242 linenr_T lnum;
11243# ifdef FEAT_WINDOWS
11244 win_T *wp;
11245# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011246 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011247
11248 if (row >= 0 && col >= 0)
11249 {
11250 /* Find the window at the mouse coordinates and compute the
11251 * text position. */
11252 win = mouse_find_win(&row, &col);
11253 (void)mouse_comp_pos(win, &row, &col, &lnum);
11254# ifdef FEAT_WINDOWS
11255 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011256 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011257# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011258 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011259 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11260 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11261 }
11262 }
11263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264 }
11265}
11266
11267/*
11268 * "getcharmod()" function
11269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011271f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011272 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276}
11277
11278/*
11279 * "getcmdline()" function
11280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011282f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011283 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286 rettv->v_type = VAR_STRING;
11287 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288}
11289
11290/*
11291 * "getcmdpos()" function
11292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011295 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299}
11300
11301/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011302 * "getcmdtype()" function
11303 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011304 static void
11305f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011306 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011307 typval_T *rettv;
11308{
11309 rettv->v_type = VAR_STRING;
11310 rettv->vval.v_string = alloc(2);
11311 if (rettv->vval.v_string != NULL)
11312 {
11313 rettv->vval.v_string[0] = get_cmdline_type();
11314 rettv->vval.v_string[1] = NUL;
11315 }
11316}
11317
11318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 * "getcwd()" function
11320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011322f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011323 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011324 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011326 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011328 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011329 rettv->vval.v_string = NULL;
11330 cwd = alloc(MAXPATHL);
11331 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011333 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11334 {
11335 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011337 if (rettv->vval.v_string != NULL)
11338 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011340 }
11341 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 }
11343}
11344
11345/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011346 * "getfontname()" function
11347 */
11348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011349f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011350 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011351 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011352{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011353 rettv->v_type = VAR_STRING;
11354 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011355#ifdef FEAT_GUI
11356 if (gui.in_use)
11357 {
11358 GuiFont font;
11359 char_u *name = NULL;
11360
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011361 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011362 {
11363 /* Get the "Normal" font. Either the name saved by
11364 * hl_set_font_name() or from the font ID. */
11365 font = gui.norm_font;
11366 name = hl_get_font_name();
11367 }
11368 else
11369 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011370 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011371 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11372 return;
11373 font = gui_mch_get_font(name, FALSE);
11374 if (font == NOFONT)
11375 return; /* Invalid font name, return empty string. */
11376 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011377 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011378 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011379 gui_mch_free_font(font);
11380 }
11381#endif
11382}
11383
11384/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011385 * "getfperm({fname})" function
11386 */
11387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011389 typval_T *argvars;
11390 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011391{
11392 char_u *fname;
11393 struct stat st;
11394 char_u *perm = NULL;
11395 char_u flags[] = "rwx";
11396 int i;
11397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011398 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011400 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011401 if (mch_stat((char *)fname, &st) >= 0)
11402 {
11403 perm = vim_strsave((char_u *)"---------");
11404 if (perm != NULL)
11405 {
11406 for (i = 0; i < 9; i++)
11407 {
11408 if (st.st_mode & (1 << (8 - i)))
11409 perm[i] = flags[i % 3];
11410 }
11411 }
11412 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011413 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011414}
11415
11416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 * "getfsize({fname})" function
11418 */
11419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011420f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011421 typval_T *argvars;
11422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423{
11424 char_u *fname;
11425 struct stat st;
11426
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011427 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011429 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430
11431 if (mch_stat((char *)fname, &st) >= 0)
11432 {
11433 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011434 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011436 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011437 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011438
11439 /* non-perfect check for overflow */
11440 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11441 rettv->vval.v_number = -2;
11442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443 }
11444 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446}
11447
11448/*
11449 * "getftime({fname})" function
11450 */
11451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011452f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011453 typval_T *argvars;
11454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455{
11456 char_u *fname;
11457 struct stat st;
11458
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011459 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460
11461 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011462 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465}
11466
11467/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011468 * "getftype({fname})" function
11469 */
11470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011472 typval_T *argvars;
11473 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011474{
11475 char_u *fname;
11476 struct stat st;
11477 char_u *type = NULL;
11478 char *t;
11479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011480 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011481
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011482 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011483 if (mch_lstat((char *)fname, &st) >= 0)
11484 {
11485#ifdef S_ISREG
11486 if (S_ISREG(st.st_mode))
11487 t = "file";
11488 else if (S_ISDIR(st.st_mode))
11489 t = "dir";
11490# ifdef S_ISLNK
11491 else if (S_ISLNK(st.st_mode))
11492 t = "link";
11493# endif
11494# ifdef S_ISBLK
11495 else if (S_ISBLK(st.st_mode))
11496 t = "bdev";
11497# endif
11498# ifdef S_ISCHR
11499 else if (S_ISCHR(st.st_mode))
11500 t = "cdev";
11501# endif
11502# ifdef S_ISFIFO
11503 else if (S_ISFIFO(st.st_mode))
11504 t = "fifo";
11505# endif
11506# ifdef S_ISSOCK
11507 else if (S_ISSOCK(st.st_mode))
11508 t = "fifo";
11509# endif
11510 else
11511 t = "other";
11512#else
11513# ifdef S_IFMT
11514 switch (st.st_mode & S_IFMT)
11515 {
11516 case S_IFREG: t = "file"; break;
11517 case S_IFDIR: t = "dir"; break;
11518# ifdef S_IFLNK
11519 case S_IFLNK: t = "link"; break;
11520# endif
11521# ifdef S_IFBLK
11522 case S_IFBLK: t = "bdev"; break;
11523# endif
11524# ifdef S_IFCHR
11525 case S_IFCHR: t = "cdev"; break;
11526# endif
11527# ifdef S_IFIFO
11528 case S_IFIFO: t = "fifo"; break;
11529# endif
11530# ifdef S_IFSOCK
11531 case S_IFSOCK: t = "socket"; break;
11532# endif
11533 default: t = "other";
11534 }
11535# else
11536 if (mch_isdir(fname))
11537 t = "dir";
11538 else
11539 t = "file";
11540# endif
11541#endif
11542 type = vim_strsave((char_u *)t);
11543 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011544 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011545}
11546
11547/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011548 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011549 */
11550 static void
11551f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011552 typval_T *argvars;
11553 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011554{
11555 linenr_T lnum;
11556 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011557 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011558
11559 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011560 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011561 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011562 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011563 retlist = FALSE;
11564 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011565 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011566 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011567 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011568 retlist = TRUE;
11569 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011570
Bram Moolenaar342337a2005-07-21 21:11:17 +000011571 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011572}
11573
11574/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011575 * "getmatches()" function
11576 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011577 static void
11578f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011579 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011580 typval_T *rettv;
11581{
11582#ifdef FEAT_SEARCH_EXTRA
11583 dict_T *dict;
11584 matchitem_T *cur = curwin->w_match_head;
11585
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011586 if (rettv_list_alloc(rettv) == OK)
11587 {
11588 while (cur != NULL)
11589 {
11590 dict = dict_alloc();
11591 if (dict == NULL)
11592 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011593 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11594 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11595 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11596 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11597 list_append_dict(rettv->vval.v_list, dict);
11598 cur = cur->next;
11599 }
11600 }
11601#endif
11602}
11603
11604/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011605 * "getpid()" function
11606 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011607 static void
11608f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011609 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011610 typval_T *rettv;
11611{
11612 rettv->vval.v_number = mch_get_pid();
11613}
11614
11615/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011616 * "getpos(string)" function
11617 */
11618 static void
11619f_getpos(argvars, rettv)
11620 typval_T *argvars;
11621 typval_T *rettv;
11622{
11623 pos_T *fp;
11624 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011625 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011626
11627 if (rettv_list_alloc(rettv) == OK)
11628 {
11629 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011630 fp = var2fpos(&argvars[0], TRUE, &fnum);
11631 if (fnum != -1)
11632 list_append_number(l, (varnumber_T)fnum);
11633 else
11634 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011635 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11636 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011637 list_append_number(l, (fp != NULL)
11638 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011639 : (varnumber_T)0);
11640 list_append_number(l,
11641#ifdef FEAT_VIRTUALEDIT
11642 (fp != NULL) ? (varnumber_T)fp->coladd :
11643#endif
11644 (varnumber_T)0);
11645 }
11646 else
11647 rettv->vval.v_number = FALSE;
11648}
11649
11650/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011651 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011652 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011653 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011654f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011655 typval_T *argvars UNUSED;
11656 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011657{
11658#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011659 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011660#endif
11661
Bram Moolenaar2641f772005-03-25 21:58:17 +000011662#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011663 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011664 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011665 wp = NULL;
11666 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11667 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011668 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011669 if (wp == NULL)
11670 return;
11671 }
11672
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011673 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011674 }
11675#endif
11676}
11677
11678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679 * "getreg()" function
11680 */
11681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011682f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011683 typval_T *argvars;
11684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685{
11686 char_u *strregname;
11687 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011688 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011689 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011691 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011693 strregname = get_tv_string_chk(&argvars[0]);
11694 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011695 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011696 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011699 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700 regname = (strregname == NULL ? '"' : *strregname);
11701 if (regname == 0)
11702 regname = '"';
11703
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011704 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011705 rettv->vval.v_string = error ? NULL :
11706 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707}
11708
11709/*
11710 * "getregtype()" function
11711 */
11712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011713f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011714 typval_T *argvars;
11715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716{
11717 char_u *strregname;
11718 int regname;
11719 char_u buf[NUMBUFLEN + 2];
11720 long reglen = 0;
11721
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011722 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011723 {
11724 strregname = get_tv_string_chk(&argvars[0]);
11725 if (strregname == NULL) /* type error; errmsg already given */
11726 {
11727 rettv->v_type = VAR_STRING;
11728 rettv->vval.v_string = NULL;
11729 return;
11730 }
11731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732 else
11733 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011734 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735
11736 regname = (strregname == NULL ? '"' : *strregname);
11737 if (regname == 0)
11738 regname = '"';
11739
11740 buf[0] = NUL;
11741 buf[1] = NUL;
11742 switch (get_reg_type(regname, &reglen))
11743 {
11744 case MLINE: buf[0] = 'V'; break;
11745 case MCHAR: buf[0] = 'v'; break;
11746#ifdef FEAT_VISUAL
11747 case MBLOCK:
11748 buf[0] = Ctrl_V;
11749 sprintf((char *)buf + 1, "%ld", reglen + 1);
11750 break;
11751#endif
11752 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753 rettv->v_type = VAR_STRING;
11754 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755}
11756
11757/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011758 * "gettabvar()" function
11759 */
11760 static void
11761f_gettabvar(argvars, rettv)
11762 typval_T *argvars;
11763 typval_T *rettv;
11764{
11765 tabpage_T *tp;
11766 dictitem_T *v;
11767 char_u *varname;
11768
11769 rettv->v_type = VAR_STRING;
11770 rettv->vval.v_string = NULL;
11771
11772 varname = get_tv_string_chk(&argvars[1]);
11773 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11774 if (tp != NULL && varname != NULL)
11775 {
11776 /* look up the variable */
11777 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11778 if (v != NULL)
11779 copy_tv(&v->di_tv, rettv);
11780 }
11781}
11782
11783/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011784 * "gettabwinvar()" function
11785 */
11786 static void
11787f_gettabwinvar(argvars, rettv)
11788 typval_T *argvars;
11789 typval_T *rettv;
11790{
11791 getwinvar(argvars, rettv, 1);
11792}
11793
11794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795 * "getwinposx()" function
11796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011798f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011799 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803#ifdef FEAT_GUI
11804 if (gui.in_use)
11805 {
11806 int x, y;
11807
11808 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011809 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 }
11811#endif
11812}
11813
11814/*
11815 * "getwinposy()" function
11816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011818f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011819 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011822 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823#ifdef FEAT_GUI
11824 if (gui.in_use)
11825 {
11826 int x, y;
11827
11828 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011829 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830 }
11831#endif
11832}
11833
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011834/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011835 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011836 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011837 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011838find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011839 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011840 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011841{
11842#ifdef FEAT_WINDOWS
11843 win_T *wp;
11844#endif
11845 int nr;
11846
11847 nr = get_tv_number_chk(vp, NULL);
11848
11849#ifdef FEAT_WINDOWS
11850 if (nr < 0)
11851 return NULL;
11852 if (nr == 0)
11853 return curwin;
11854
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011855 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11856 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011857 if (--nr <= 0)
11858 break;
11859 return wp;
11860#else
11861 if (nr == 0 || nr == 1)
11862 return curwin;
11863 return NULL;
11864#endif
11865}
11866
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867/*
11868 * "getwinvar()" function
11869 */
11870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011871f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011872 typval_T *argvars;
11873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011875 getwinvar(argvars, rettv, 0);
11876}
11877
11878/*
11879 * getwinvar() and gettabwinvar()
11880 */
11881 static void
11882getwinvar(argvars, rettv, off)
11883 typval_T *argvars;
11884 typval_T *rettv;
11885 int off; /* 1 for gettabwinvar() */
11886{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887 win_T *win, *oldcurwin;
11888 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011889 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011890 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011892#ifdef FEAT_WINDOWS
11893 if (off == 1)
11894 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11895 else
11896 tp = curtab;
11897#endif
11898 win = find_win_by_nr(&argvars[off], tp);
11899 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011900 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011902 rettv->v_type = VAR_STRING;
11903 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904
11905 if (win != NULL && varname != NULL)
11906 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011907 /* Set curwin to be our win, temporarily. Also set curbuf, so
11908 * that we can get buffer-local options. */
11909 oldcurwin = curwin;
11910 curwin = win;
11911 curbuf = win->w_buffer;
11912
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011914 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915 else
11916 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011917 if (*varname == NUL)
11918 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11919 * scope prefix before the NUL byte is required by
11920 * find_var_in_ht(). */
11921 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011923 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011925 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011927
11928 /* restore previous notion of curwin */
11929 curwin = oldcurwin;
11930 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931 }
11932
11933 --emsg_off;
11934}
11935
11936/*
11937 * "glob()" function
11938 */
11939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011940f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011941 typval_T *argvars;
11942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011944 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011946 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011948 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011949 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011950 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011951 if (argvars[1].v_type != VAR_UNKNOWN)
11952 {
11953 if (get_tv_number_chk(&argvars[1], &error))
11954 options |= WILD_KEEP_ALL;
11955 if (argvars[2].v_type != VAR_UNKNOWN
11956 && get_tv_number_chk(&argvars[2], &error))
11957 {
11958 rettv->v_type = VAR_LIST;
11959 rettv->vval.v_list = NULL;
11960 }
11961 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011962 if (!error)
11963 {
11964 ExpandInit(&xpc);
11965 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011966 if (p_wic)
11967 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011968 if (rettv->v_type == VAR_STRING)
11969 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011970 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011971 else if (rettv_list_alloc(rettv) != FAIL)
11972 {
11973 int i;
11974
11975 ExpandOne(&xpc, get_tv_string(&argvars[0]),
11976 NULL, options, WILD_ALL_KEEP);
11977 for (i = 0; i < xpc.xp_numfiles; i++)
11978 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11979
11980 ExpandCleanup(&xpc);
11981 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011982 }
11983 else
11984 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985}
11986
11987/*
11988 * "globpath()" function
11989 */
11990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011992 typval_T *argvars;
11993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011995 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011997 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011998 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012000 /* When the optional second argument is non-zero, don't remove matches
12001 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12002 if (argvars[2].v_type != VAR_UNKNOWN
12003 && get_tv_number_chk(&argvars[2], &error))
12004 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012005 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012006 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012007 rettv->vval.v_string = NULL;
12008 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012009 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12010 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011}
12012
12013/*
12014 * "has()" function
12015 */
12016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012017f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012018 typval_T *argvars;
12019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020{
12021 int i;
12022 char_u *name;
12023 int n = FALSE;
12024 static char *(has_list[]) =
12025 {
12026#ifdef AMIGA
12027 "amiga",
12028# ifdef FEAT_ARP
12029 "arp",
12030# endif
12031#endif
12032#ifdef __BEOS__
12033 "beos",
12034#endif
12035#ifdef MSDOS
12036# ifdef DJGPP
12037 "dos32",
12038# else
12039 "dos16",
12040# endif
12041#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012042#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 "mac",
12044#endif
12045#if defined(MACOS_X_UNIX)
12046 "macunix",
12047#endif
12048#ifdef OS2
12049 "os2",
12050#endif
12051#ifdef __QNX__
12052 "qnx",
12053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054#ifdef UNIX
12055 "unix",
12056#endif
12057#ifdef VMS
12058 "vms",
12059#endif
12060#ifdef WIN16
12061 "win16",
12062#endif
12063#ifdef WIN32
12064 "win32",
12065#endif
12066#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12067 "win32unix",
12068#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012069#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070 "win64",
12071#endif
12072#ifdef EBCDIC
12073 "ebcdic",
12074#endif
12075#ifndef CASE_INSENSITIVE_FILENAME
12076 "fname_case",
12077#endif
12078#ifdef FEAT_ARABIC
12079 "arabic",
12080#endif
12081#ifdef FEAT_AUTOCMD
12082 "autocmd",
12083#endif
12084#ifdef FEAT_BEVAL
12085 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012086# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12087 "balloon_multiline",
12088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089#endif
12090#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12091 "builtin_terms",
12092# ifdef ALL_BUILTIN_TCAPS
12093 "all_builtin_terms",
12094# endif
12095#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012096#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12097 || defined(FEAT_GUI_W32) \
12098 || defined(FEAT_GUI_MOTIF))
12099 "browsefilter",
12100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101#ifdef FEAT_BYTEOFF
12102 "byte_offset",
12103#endif
12104#ifdef FEAT_CINDENT
12105 "cindent",
12106#endif
12107#ifdef FEAT_CLIENTSERVER
12108 "clientserver",
12109#endif
12110#ifdef FEAT_CLIPBOARD
12111 "clipboard",
12112#endif
12113#ifdef FEAT_CMDL_COMPL
12114 "cmdline_compl",
12115#endif
12116#ifdef FEAT_CMDHIST
12117 "cmdline_hist",
12118#endif
12119#ifdef FEAT_COMMENTS
12120 "comments",
12121#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012122#ifdef FEAT_CONCEAL
12123 "conceal",
12124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125#ifdef FEAT_CRYPT
12126 "cryptv",
12127#endif
12128#ifdef FEAT_CSCOPE
12129 "cscope",
12130#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012131#ifdef FEAT_CURSORBIND
12132 "cursorbind",
12133#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012134#ifdef CURSOR_SHAPE
12135 "cursorshape",
12136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137#ifdef DEBUG
12138 "debug",
12139#endif
12140#ifdef FEAT_CON_DIALOG
12141 "dialog_con",
12142#endif
12143#ifdef FEAT_GUI_DIALOG
12144 "dialog_gui",
12145#endif
12146#ifdef FEAT_DIFF
12147 "diff",
12148#endif
12149#ifdef FEAT_DIGRAPHS
12150 "digraphs",
12151#endif
12152#ifdef FEAT_DND
12153 "dnd",
12154#endif
12155#ifdef FEAT_EMACS_TAGS
12156 "emacs_tags",
12157#endif
12158 "eval", /* always present, of course! */
12159#ifdef FEAT_EX_EXTRA
12160 "ex_extra",
12161#endif
12162#ifdef FEAT_SEARCH_EXTRA
12163 "extra_search",
12164#endif
12165#ifdef FEAT_FKMAP
12166 "farsi",
12167#endif
12168#ifdef FEAT_SEARCHPATH
12169 "file_in_path",
12170#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012171#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012172 "filterpipe",
12173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174#ifdef FEAT_FIND_ID
12175 "find_in_path",
12176#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012177#ifdef FEAT_FLOAT
12178 "float",
12179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180#ifdef FEAT_FOLDING
12181 "folding",
12182#endif
12183#ifdef FEAT_FOOTER
12184 "footer",
12185#endif
12186#if !defined(USE_SYSTEM) && defined(UNIX)
12187 "fork",
12188#endif
12189#ifdef FEAT_GETTEXT
12190 "gettext",
12191#endif
12192#ifdef FEAT_GUI
12193 "gui",
12194#endif
12195#ifdef FEAT_GUI_ATHENA
12196# ifdef FEAT_GUI_NEXTAW
12197 "gui_neXtaw",
12198# else
12199 "gui_athena",
12200# endif
12201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202#ifdef FEAT_GUI_GTK
12203 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012206#ifdef FEAT_GUI_GNOME
12207 "gui_gnome",
12208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209#ifdef FEAT_GUI_MAC
12210 "gui_mac",
12211#endif
12212#ifdef FEAT_GUI_MOTIF
12213 "gui_motif",
12214#endif
12215#ifdef FEAT_GUI_PHOTON
12216 "gui_photon",
12217#endif
12218#ifdef FEAT_GUI_W16
12219 "gui_win16",
12220#endif
12221#ifdef FEAT_GUI_W32
12222 "gui_win32",
12223#endif
12224#ifdef FEAT_HANGULIN
12225 "hangul_input",
12226#endif
12227#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12228 "iconv",
12229#endif
12230#ifdef FEAT_INS_EXPAND
12231 "insert_expand",
12232#endif
12233#ifdef FEAT_JUMPLIST
12234 "jumplist",
12235#endif
12236#ifdef FEAT_KEYMAP
12237 "keymap",
12238#endif
12239#ifdef FEAT_LANGMAP
12240 "langmap",
12241#endif
12242#ifdef FEAT_LIBCALL
12243 "libcall",
12244#endif
12245#ifdef FEAT_LINEBREAK
12246 "linebreak",
12247#endif
12248#ifdef FEAT_LISP
12249 "lispindent",
12250#endif
12251#ifdef FEAT_LISTCMDS
12252 "listcmds",
12253#endif
12254#ifdef FEAT_LOCALMAP
12255 "localmap",
12256#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012257#ifdef FEAT_LUA
12258# ifndef DYNAMIC_LUA
12259 "lua",
12260# endif
12261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262#ifdef FEAT_MENU
12263 "menu",
12264#endif
12265#ifdef FEAT_SESSION
12266 "mksession",
12267#endif
12268#ifdef FEAT_MODIFY_FNAME
12269 "modify_fname",
12270#endif
12271#ifdef FEAT_MOUSE
12272 "mouse",
12273#endif
12274#ifdef FEAT_MOUSESHAPE
12275 "mouseshape",
12276#endif
12277#if defined(UNIX) || defined(VMS)
12278# ifdef FEAT_MOUSE_DEC
12279 "mouse_dec",
12280# endif
12281# ifdef FEAT_MOUSE_GPM
12282 "mouse_gpm",
12283# endif
12284# ifdef FEAT_MOUSE_JSB
12285 "mouse_jsbterm",
12286# endif
12287# ifdef FEAT_MOUSE_NET
12288 "mouse_netterm",
12289# endif
12290# ifdef FEAT_MOUSE_PTERM
12291 "mouse_pterm",
12292# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012293# ifdef FEAT_MOUSE_SGR
12294 "mouse_sgr",
12295# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012296# ifdef FEAT_SYSMOUSE
12297 "mouse_sysmouse",
12298# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012299# ifdef FEAT_MOUSE_URXVT
12300 "mouse_urxvt",
12301# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302# ifdef FEAT_MOUSE_XTERM
12303 "mouse_xterm",
12304# endif
12305#endif
12306#ifdef FEAT_MBYTE
12307 "multi_byte",
12308#endif
12309#ifdef FEAT_MBYTE_IME
12310 "multi_byte_ime",
12311#endif
12312#ifdef FEAT_MULTI_LANG
12313 "multi_lang",
12314#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012315#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012316#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012317 "mzscheme",
12318#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320#ifdef FEAT_OLE
12321 "ole",
12322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323#ifdef FEAT_PATH_EXTRA
12324 "path_extra",
12325#endif
12326#ifdef FEAT_PERL
12327#ifndef DYNAMIC_PERL
12328 "perl",
12329#endif
12330#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012331#ifdef FEAT_PERSISTENT_UNDO
12332 "persistent_undo",
12333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334#ifdef FEAT_PYTHON
12335#ifndef DYNAMIC_PYTHON
12336 "python",
12337#endif
12338#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012339#ifdef FEAT_PYTHON3
12340#ifndef DYNAMIC_PYTHON3
12341 "python3",
12342#endif
12343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344#ifdef FEAT_POSTSCRIPT
12345 "postscript",
12346#endif
12347#ifdef FEAT_PRINTER
12348 "printer",
12349#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012350#ifdef FEAT_PROFILE
12351 "profile",
12352#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012353#ifdef FEAT_RELTIME
12354 "reltime",
12355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356#ifdef FEAT_QUICKFIX
12357 "quickfix",
12358#endif
12359#ifdef FEAT_RIGHTLEFT
12360 "rightleft",
12361#endif
12362#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12363 "ruby",
12364#endif
12365#ifdef FEAT_SCROLLBIND
12366 "scrollbind",
12367#endif
12368#ifdef FEAT_CMDL_INFO
12369 "showcmd",
12370 "cmdline_info",
12371#endif
12372#ifdef FEAT_SIGNS
12373 "signs",
12374#endif
12375#ifdef FEAT_SMARTINDENT
12376 "smartindent",
12377#endif
12378#ifdef FEAT_SNIFF
12379 "sniff",
12380#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012381#ifdef STARTUPTIME
12382 "startuptime",
12383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384#ifdef FEAT_STL_OPT
12385 "statusline",
12386#endif
12387#ifdef FEAT_SUN_WORKSHOP
12388 "sun_workshop",
12389#endif
12390#ifdef FEAT_NETBEANS_INTG
12391 "netbeans_intg",
12392#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012393#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012394 "spell",
12395#endif
12396#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397 "syntax",
12398#endif
12399#if defined(USE_SYSTEM) || !defined(UNIX)
12400 "system",
12401#endif
12402#ifdef FEAT_TAG_BINS
12403 "tag_binary",
12404#endif
12405#ifdef FEAT_TAG_OLDSTATIC
12406 "tag_old_static",
12407#endif
12408#ifdef FEAT_TAG_ANYWHITE
12409 "tag_any_white",
12410#endif
12411#ifdef FEAT_TCL
12412# ifndef DYNAMIC_TCL
12413 "tcl",
12414# endif
12415#endif
12416#ifdef TERMINFO
12417 "terminfo",
12418#endif
12419#ifdef FEAT_TERMRESPONSE
12420 "termresponse",
12421#endif
12422#ifdef FEAT_TEXTOBJ
12423 "textobjects",
12424#endif
12425#ifdef HAVE_TGETENT
12426 "tgetent",
12427#endif
12428#ifdef FEAT_TITLE
12429 "title",
12430#endif
12431#ifdef FEAT_TOOLBAR
12432 "toolbar",
12433#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012434#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12435 "unnamedplus",
12436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437#ifdef FEAT_USR_CMDS
12438 "user-commands", /* was accidentally included in 5.4 */
12439 "user_commands",
12440#endif
12441#ifdef FEAT_VIMINFO
12442 "viminfo",
12443#endif
12444#ifdef FEAT_VERTSPLIT
12445 "vertsplit",
12446#endif
12447#ifdef FEAT_VIRTUALEDIT
12448 "virtualedit",
12449#endif
12450#ifdef FEAT_VISUAL
12451 "visual",
12452#endif
12453#ifdef FEAT_VISUALEXTRA
12454 "visualextra",
12455#endif
12456#ifdef FEAT_VREPLACE
12457 "vreplace",
12458#endif
12459#ifdef FEAT_WILDIGN
12460 "wildignore",
12461#endif
12462#ifdef FEAT_WILDMENU
12463 "wildmenu",
12464#endif
12465#ifdef FEAT_WINDOWS
12466 "windows",
12467#endif
12468#ifdef FEAT_WAK
12469 "winaltkeys",
12470#endif
12471#ifdef FEAT_WRITEBACKUP
12472 "writebackup",
12473#endif
12474#ifdef FEAT_XIM
12475 "xim",
12476#endif
12477#ifdef FEAT_XFONTSET
12478 "xfontset",
12479#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012480#ifdef FEAT_XPM_W32
12481 "xpm_w32",
12482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483#ifdef USE_XSMP
12484 "xsmp",
12485#endif
12486#ifdef USE_XSMP_INTERACT
12487 "xsmp_interact",
12488#endif
12489#ifdef FEAT_XCLIPBOARD
12490 "xterm_clipboard",
12491#endif
12492#ifdef FEAT_XTERM_SAVE
12493 "xterm_save",
12494#endif
12495#if defined(UNIX) && defined(FEAT_X11)
12496 "X11",
12497#endif
12498 NULL
12499 };
12500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012501 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502 for (i = 0; has_list[i] != NULL; ++i)
12503 if (STRICMP(name, has_list[i]) == 0)
12504 {
12505 n = TRUE;
12506 break;
12507 }
12508
12509 if (n == FALSE)
12510 {
12511 if (STRNICMP(name, "patch", 5) == 0)
12512 n = has_patch(atoi((char *)name + 5));
12513 else if (STRICMP(name, "vim_starting") == 0)
12514 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012515#ifdef FEAT_MBYTE
12516 else if (STRICMP(name, "multi_byte_encoding") == 0)
12517 n = has_mbyte;
12518#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012519#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12520 else if (STRICMP(name, "balloon_multiline") == 0)
12521 n = multiline_balloon_available();
12522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523#ifdef DYNAMIC_TCL
12524 else if (STRICMP(name, "tcl") == 0)
12525 n = tcl_enabled(FALSE);
12526#endif
12527#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12528 else if (STRICMP(name, "iconv") == 0)
12529 n = iconv_enabled(FALSE);
12530#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012531#ifdef DYNAMIC_LUA
12532 else if (STRICMP(name, "lua") == 0)
12533 n = lua_enabled(FALSE);
12534#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012535#ifdef DYNAMIC_MZSCHEME
12536 else if (STRICMP(name, "mzscheme") == 0)
12537 n = mzscheme_enabled(FALSE);
12538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012539#ifdef DYNAMIC_RUBY
12540 else if (STRICMP(name, "ruby") == 0)
12541 n = ruby_enabled(FALSE);
12542#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012543#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012544#ifdef DYNAMIC_PYTHON
12545 else if (STRICMP(name, "python") == 0)
12546 n = python_enabled(FALSE);
12547#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012548#endif
12549#ifdef FEAT_PYTHON3
12550#ifdef DYNAMIC_PYTHON3
12551 else if (STRICMP(name, "python3") == 0)
12552 n = python3_enabled(FALSE);
12553#endif
12554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555#ifdef DYNAMIC_PERL
12556 else if (STRICMP(name, "perl") == 0)
12557 n = perl_enabled(FALSE);
12558#endif
12559#ifdef FEAT_GUI
12560 else if (STRICMP(name, "gui_running") == 0)
12561 n = (gui.in_use || gui.starting);
12562# ifdef FEAT_GUI_W32
12563 else if (STRICMP(name, "gui_win32s") == 0)
12564 n = gui_is_win32s();
12565# endif
12566# ifdef FEAT_BROWSE
12567 else if (STRICMP(name, "browse") == 0)
12568 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12569# endif
12570#endif
12571#ifdef FEAT_SYN_HL
12572 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012573 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574#endif
12575#if defined(WIN3264)
12576 else if (STRICMP(name, "win95") == 0)
12577 n = mch_windows95();
12578#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012579#ifdef FEAT_NETBEANS_INTG
12580 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012581 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583 }
12584
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012585 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012586}
12587
12588/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012589 * "has_key()" function
12590 */
12591 static void
12592f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012593 typval_T *argvars;
12594 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012595{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012596 if (argvars[0].v_type != VAR_DICT)
12597 {
12598 EMSG(_(e_dictreq));
12599 return;
12600 }
12601 if (argvars[0].vval.v_dict == NULL)
12602 return;
12603
12604 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012605 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012606}
12607
12608/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012609 * "haslocaldir()" function
12610 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012611 static void
12612f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012613 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012614 typval_T *rettv;
12615{
12616 rettv->vval.v_number = (curwin->w_localdir != NULL);
12617}
12618
12619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620 * "hasmapto()" function
12621 */
12622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012623f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012624 typval_T *argvars;
12625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626{
12627 char_u *name;
12628 char_u *mode;
12629 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012630 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012632 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012633 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634 mode = (char_u *)"nvo";
12635 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012637 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012638 if (argvars[2].v_type != VAR_UNKNOWN)
12639 abbr = get_tv_number(&argvars[2]);
12640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641
Bram Moolenaar2c932302006-03-18 21:42:09 +000012642 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012643 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012645 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646}
12647
12648/*
12649 * "histadd()" function
12650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012652f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012653 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655{
12656#ifdef FEAT_CMDHIST
12657 int histype;
12658 char_u *str;
12659 char_u buf[NUMBUFLEN];
12660#endif
12661
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012662 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663 if (check_restricted() || check_secure())
12664 return;
12665#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012666 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12667 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668 if (histype >= 0)
12669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012670 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671 if (*str != NUL)
12672 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012673 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012675 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676 return;
12677 }
12678 }
12679#endif
12680}
12681
12682/*
12683 * "histdel()" function
12684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012686f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012687 typval_T *argvars UNUSED;
12688 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689{
12690#ifdef FEAT_CMDHIST
12691 int n;
12692 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012693 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012695 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12696 if (str == NULL)
12697 n = 0;
12698 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012700 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012701 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012703 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012704 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705 else
12706 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012707 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012708 get_tv_string_buf(&argvars[1], buf));
12709 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710#endif
12711}
12712
12713/*
12714 * "histget()" function
12715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012717f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012718 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720{
12721#ifdef FEAT_CMDHIST
12722 int type;
12723 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012724 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012726 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12727 if (str == NULL)
12728 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012730 {
12731 type = get_histtype(str);
12732 if (argvars[1].v_type == VAR_UNKNOWN)
12733 idx = get_history_idx(type);
12734 else
12735 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12736 /* -1 on type error */
12737 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012740 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012742 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743}
12744
12745/*
12746 * "histnr()" function
12747 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012749f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012750 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752{
12753 int i;
12754
12755#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012756 char_u *history = get_tv_string_chk(&argvars[0]);
12757
12758 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759 if (i >= HIST_CMD && i < HIST_COUNT)
12760 i = get_history_idx(i);
12761 else
12762#endif
12763 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012764 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765}
12766
12767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 * "highlightID(name)" function
12769 */
12770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012771f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012772 typval_T *argvars;
12773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012775 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776}
12777
12778/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012779 * "highlight_exists()" function
12780 */
12781 static void
12782f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012783 typval_T *argvars;
12784 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012785{
12786 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12787}
12788
12789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790 * "hostname()" function
12791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012793f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012794 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796{
12797 char_u hostname[256];
12798
12799 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012800 rettv->v_type = VAR_STRING;
12801 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802}
12803
12804/*
12805 * iconv() function
12806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012808f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012809 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811{
12812#ifdef FEAT_MBYTE
12813 char_u buf1[NUMBUFLEN];
12814 char_u buf2[NUMBUFLEN];
12815 char_u *from, *to, *str;
12816 vimconv_T vimconv;
12817#endif
12818
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012819 rettv->v_type = VAR_STRING;
12820 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821
12822#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823 str = get_tv_string(&argvars[0]);
12824 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12825 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826 vimconv.vc_type = CONV_NONE;
12827 convert_setup(&vimconv, from, to);
12828
12829 /* If the encodings are equal, no conversion needed. */
12830 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012831 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012832 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012833 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834
12835 convert_setup(&vimconv, NULL, NULL);
12836 vim_free(from);
12837 vim_free(to);
12838#endif
12839}
12840
12841/*
12842 * "indent()" function
12843 */
12844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012846 typval_T *argvars;
12847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848{
12849 linenr_T lnum;
12850
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012851 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012853 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012855 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856}
12857
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012858/*
12859 * "index()" function
12860 */
12861 static void
12862f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012863 typval_T *argvars;
12864 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012865{
Bram Moolenaar33570922005-01-25 22:26:29 +000012866 list_T *l;
12867 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012868 long idx = 0;
12869 int ic = FALSE;
12870
12871 rettv->vval.v_number = -1;
12872 if (argvars[0].v_type != VAR_LIST)
12873 {
12874 EMSG(_(e_listreq));
12875 return;
12876 }
12877 l = argvars[0].vval.v_list;
12878 if (l != NULL)
12879 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012880 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012881 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012883 int error = FALSE;
12884
Bram Moolenaar758711c2005-02-02 23:11:38 +000012885 /* Start at specified item. Use the cached index that list_find()
12886 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012887 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012888 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012889 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012890 ic = get_tv_number_chk(&argvars[3], &error);
12891 if (error)
12892 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012893 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012894
Bram Moolenaar758711c2005-02-02 23:11:38 +000012895 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012896 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012897 {
12898 rettv->vval.v_number = idx;
12899 break;
12900 }
12901 }
12902}
12903
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904static int inputsecret_flag = 0;
12905
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012906static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12907
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012909 * This function is used by f_input() and f_inputdialog() functions. The third
12910 * argument to f_input() specifies the type of completion to use at the
12911 * prompt. The third argument to f_inputdialog() specifies the value to return
12912 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 */
12914 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012915get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012916 typval_T *argvars;
12917 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012918 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012920 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921 char_u *p = NULL;
12922 int c;
12923 char_u buf[NUMBUFLEN];
12924 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012925 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012926 int xp_type = EXPAND_NOTHING;
12927 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012929 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012930 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931
12932#ifdef NO_CONSOLE_INPUT
12933 /* While starting up, there is no place to enter text. */
12934 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936#endif
12937
12938 cmd_silent = FALSE; /* Want to see the prompt. */
12939 if (prompt != NULL)
12940 {
12941 /* Only the part of the message after the last NL is considered as
12942 * prompt for the command line */
12943 p = vim_strrchr(prompt, '\n');
12944 if (p == NULL)
12945 p = prompt;
12946 else
12947 {
12948 ++p;
12949 c = *p;
12950 *p = NUL;
12951 msg_start();
12952 msg_clr_eos();
12953 msg_puts_attr(prompt, echo_attr);
12954 msg_didout = FALSE;
12955 msg_starthere();
12956 *p = c;
12957 }
12958 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012960 if (argvars[1].v_type != VAR_UNKNOWN)
12961 {
12962 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12963 if (defstr != NULL)
12964 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012966 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012967 {
12968 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012969 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012970 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012971
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012972 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000012973 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012974
Bram Moolenaar4463f292005-09-25 22:20:24 +000012975 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12976 if (xp_name == NULL)
12977 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012978
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012979 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012980
Bram Moolenaar4463f292005-09-25 22:20:24 +000012981 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12982 &xp_arg) == FAIL)
12983 return;
12984 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012985 }
12986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012987 if (defstr != NULL)
12988 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012989 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12990 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020012991 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012992 && argvars[1].v_type != VAR_UNKNOWN
12993 && argvars[2].v_type != VAR_UNKNOWN)
12994 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
12995 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012996
12997 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012998
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012999 /* since the user typed this, no need to wait for return */
13000 need_wait_return = FALSE;
13001 msg_didout = FALSE;
13002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013003 cmd_silent = cmd_silent_save;
13004}
13005
13006/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013007 * "input()" function
13008 * Also handles inputsecret() when inputsecret is set.
13009 */
13010 static void
13011f_input(argvars, rettv)
13012 typval_T *argvars;
13013 typval_T *rettv;
13014{
13015 get_user_input(argvars, rettv, FALSE);
13016}
13017
13018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019 * "inputdialog()" function
13020 */
13021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013022f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013023 typval_T *argvars;
13024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025{
13026#if defined(FEAT_GUI_TEXTDIALOG)
13027 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13028 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13029 {
13030 char_u *message;
13031 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013032 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013034 message = get_tv_string_chk(&argvars[0]);
13035 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013036 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013037 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038 else
13039 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013040 if (message != NULL && defstr != NULL
13041 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013042 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013043 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044 else
13045 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013046 if (message != NULL && defstr != NULL
13047 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013048 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013049 rettv->vval.v_string = vim_strsave(
13050 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013052 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013054 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055 }
13056 else
13057#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013058 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059}
13060
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013061/*
13062 * "inputlist()" function
13063 */
13064 static void
13065f_inputlist(argvars, rettv)
13066 typval_T *argvars;
13067 typval_T *rettv;
13068{
13069 listitem_T *li;
13070 int selected;
13071 int mouse_used;
13072
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013073#ifdef NO_CONSOLE_INPUT
13074 /* While starting up, there is no place to enter text. */
13075 if (no_console_input())
13076 return;
13077#endif
13078 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13079 {
13080 EMSG2(_(e_listarg), "inputlist()");
13081 return;
13082 }
13083
13084 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013085 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013086 lines_left = Rows; /* avoid more prompt */
13087 msg_scroll = TRUE;
13088 msg_clr_eos();
13089
13090 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13091 {
13092 msg_puts(get_tv_string(&li->li_tv));
13093 msg_putchar('\n');
13094 }
13095
13096 /* Ask for choice. */
13097 selected = prompt_for_number(&mouse_used);
13098 if (mouse_used)
13099 selected -= lines_left;
13100
13101 rettv->vval.v_number = selected;
13102}
13103
13104
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13106
13107/*
13108 * "inputrestore()" function
13109 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013111f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013112 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013114{
13115 if (ga_userinput.ga_len > 0)
13116 {
13117 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13119 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013120 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013121 }
13122 else if (p_verbose > 1)
13123 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013124 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013125 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126 }
13127}
13128
13129/*
13130 * "inputsave()" function
13131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013133f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013134 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013137 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138 if (ga_grow(&ga_userinput, 1) == OK)
13139 {
13140 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13141 + ga_userinput.ga_len);
13142 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013143 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144 }
13145 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013146 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147}
13148
13149/*
13150 * "inputsecret()" function
13151 */
13152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013153f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013154 typval_T *argvars;
13155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156{
13157 ++cmdline_star;
13158 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013159 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160 --cmdline_star;
13161 --inputsecret_flag;
13162}
13163
13164/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013165 * "insert()" function
13166 */
13167 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013168f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013169 typval_T *argvars;
13170 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013171{
13172 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013173 listitem_T *item;
13174 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013175 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013176
13177 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013178 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013179 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013180 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013181 {
13182 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013183 before = get_tv_number_chk(&argvars[2], &error);
13184 if (error)
13185 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013186
Bram Moolenaar758711c2005-02-02 23:11:38 +000013187 if (before == l->lv_len)
13188 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013189 else
13190 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013191 item = list_find(l, before);
13192 if (item == NULL)
13193 {
13194 EMSGN(_(e_listidx), before);
13195 l = NULL;
13196 }
13197 }
13198 if (l != NULL)
13199 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013200 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013201 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013202 }
13203 }
13204}
13205
13206/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013207 * "invert(expr)" function
13208 */
13209 static void
13210f_invert(argvars, rettv)
13211 typval_T *argvars;
13212 typval_T *rettv;
13213{
13214 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13215}
13216
13217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013218 * "isdirectory()" function
13219 */
13220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013221f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013222 typval_T *argvars;
13223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013225 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226}
13227
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013228/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013229 * "islocked()" function
13230 */
13231 static void
13232f_islocked(argvars, rettv)
13233 typval_T *argvars;
13234 typval_T *rettv;
13235{
13236 lval_T lv;
13237 char_u *end;
13238 dictitem_T *di;
13239
13240 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013241 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13242 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013243 if (end != NULL && lv.ll_name != NULL)
13244 {
13245 if (*end != NUL)
13246 EMSG(_(e_trailing));
13247 else
13248 {
13249 if (lv.ll_tv == NULL)
13250 {
13251 if (check_changedtick(lv.ll_name))
13252 rettv->vval.v_number = 1; /* always locked */
13253 else
13254 {
13255 di = find_var(lv.ll_name, NULL);
13256 if (di != NULL)
13257 {
13258 /* Consider a variable locked when:
13259 * 1. the variable itself is locked
13260 * 2. the value of the variable is locked.
13261 * 3. the List or Dict value is locked.
13262 */
13263 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13264 || tv_islocked(&di->di_tv));
13265 }
13266 }
13267 }
13268 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013269 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013270 else if (lv.ll_newkey != NULL)
13271 EMSG2(_(e_dictkey), lv.ll_newkey);
13272 else if (lv.ll_list != NULL)
13273 /* List item. */
13274 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13275 else
13276 /* Dictionary item. */
13277 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13278 }
13279 }
13280
13281 clear_lval(&lv);
13282}
13283
Bram Moolenaar33570922005-01-25 22:26:29 +000013284static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013285
13286/*
13287 * Turn a dict into a list:
13288 * "what" == 0: list of keys
13289 * "what" == 1: list of values
13290 * "what" == 2: list of items
13291 */
13292 static void
13293dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013294 typval_T *argvars;
13295 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013296 int what;
13297{
Bram Moolenaar33570922005-01-25 22:26:29 +000013298 list_T *l2;
13299 dictitem_T *di;
13300 hashitem_T *hi;
13301 listitem_T *li;
13302 listitem_T *li2;
13303 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013304 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013305
Bram Moolenaar8c711452005-01-14 21:53:12 +000013306 if (argvars[0].v_type != VAR_DICT)
13307 {
13308 EMSG(_(e_dictreq));
13309 return;
13310 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013311 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013312 return;
13313
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013314 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013315 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013316
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013317 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013318 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013319 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013320 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013321 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013322 --todo;
13323 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013324
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013325 li = listitem_alloc();
13326 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013327 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013328 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013329
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013330 if (what == 0)
13331 {
13332 /* keys() */
13333 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013334 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013335 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13336 }
13337 else if (what == 1)
13338 {
13339 /* values() */
13340 copy_tv(&di->di_tv, &li->li_tv);
13341 }
13342 else
13343 {
13344 /* items() */
13345 l2 = list_alloc();
13346 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013347 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013348 li->li_tv.vval.v_list = l2;
13349 if (l2 == NULL)
13350 break;
13351 ++l2->lv_refcount;
13352
13353 li2 = listitem_alloc();
13354 if (li2 == NULL)
13355 break;
13356 list_append(l2, li2);
13357 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013358 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013359 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13360
13361 li2 = listitem_alloc();
13362 if (li2 == NULL)
13363 break;
13364 list_append(l2, li2);
13365 copy_tv(&di->di_tv, &li2->li_tv);
13366 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013367 }
13368 }
13369}
13370
13371/*
13372 * "items(dict)" function
13373 */
13374 static void
13375f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013376 typval_T *argvars;
13377 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013378{
13379 dict_list(argvars, rettv, 2);
13380}
13381
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013383 * "join()" function
13384 */
13385 static void
13386f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013387 typval_T *argvars;
13388 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013389{
13390 garray_T ga;
13391 char_u *sep;
13392
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013393 if (argvars[0].v_type != VAR_LIST)
13394 {
13395 EMSG(_(e_listreq));
13396 return;
13397 }
13398 if (argvars[0].vval.v_list == NULL)
13399 return;
13400 if (argvars[1].v_type == VAR_UNKNOWN)
13401 sep = (char_u *)" ";
13402 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013403 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013404
13405 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013406
13407 if (sep != NULL)
13408 {
13409 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013410 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013411 ga_append(&ga, NUL);
13412 rettv->vval.v_string = (char_u *)ga.ga_data;
13413 }
13414 else
13415 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013416}
13417
13418/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013419 * "keys()" function
13420 */
13421 static void
13422f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013423 typval_T *argvars;
13424 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013425{
13426 dict_list(argvars, rettv, 0);
13427}
13428
13429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 * "last_buffer_nr()" function.
13431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013433f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013434 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013435 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436{
13437 int n = 0;
13438 buf_T *buf;
13439
13440 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13441 if (n < buf->b_fnum)
13442 n = buf->b_fnum;
13443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013444 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013445}
13446
13447/*
13448 * "len()" function
13449 */
13450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013451f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013452 typval_T *argvars;
13453 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013454{
13455 switch (argvars[0].v_type)
13456 {
13457 case VAR_STRING:
13458 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013459 rettv->vval.v_number = (varnumber_T)STRLEN(
13460 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013461 break;
13462 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013463 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013464 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013465 case VAR_DICT:
13466 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13467 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013468 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013469 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013470 break;
13471 }
13472}
13473
Bram Moolenaar33570922005-01-25 22:26:29 +000013474static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013475
13476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013477libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013478 typval_T *argvars;
13479 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013480 int type;
13481{
13482#ifdef FEAT_LIBCALL
13483 char_u *string_in;
13484 char_u **string_result;
13485 int nr_result;
13486#endif
13487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013488 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013489 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013490 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013491
13492 if (check_restricted() || check_secure())
13493 return;
13494
13495#ifdef FEAT_LIBCALL
13496 /* The first two args must be strings, otherwise its meaningless */
13497 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13498 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013499 string_in = NULL;
13500 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013501 string_in = argvars[2].vval.v_string;
13502 if (type == VAR_NUMBER)
13503 string_result = NULL;
13504 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013505 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013506 if (mch_libcall(argvars[0].vval.v_string,
13507 argvars[1].vval.v_string,
13508 string_in,
13509 argvars[2].vval.v_number,
13510 string_result,
13511 &nr_result) == OK
13512 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013513 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013514 }
13515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516}
13517
13518/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013519 * "libcall()" function
13520 */
13521 static void
13522f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013523 typval_T *argvars;
13524 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013525{
13526 libcall_common(argvars, rettv, VAR_STRING);
13527}
13528
13529/*
13530 * "libcallnr()" function
13531 */
13532 static void
13533f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013534 typval_T *argvars;
13535 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013536{
13537 libcall_common(argvars, rettv, VAR_NUMBER);
13538}
13539
13540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541 * "line(string)" function
13542 */
13543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013544f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013545 typval_T *argvars;
13546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547{
13548 linenr_T lnum = 0;
13549 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013550 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013552 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553 if (fp != NULL)
13554 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013555 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556}
13557
13558/*
13559 * "line2byte(lnum)" function
13560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013562f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013563 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565{
13566#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013567 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568#else
13569 linenr_T lnum;
13570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013571 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013573 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013575 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13576 if (rettv->vval.v_number >= 0)
13577 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578#endif
13579}
13580
13581/*
13582 * "lispindent(lnum)" function
13583 */
13584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013585f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013586 typval_T *argvars;
13587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588{
13589#ifdef FEAT_LISP
13590 pos_T pos;
13591 linenr_T lnum;
13592
13593 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013594 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13596 {
13597 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013598 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599 curwin->w_cursor = pos;
13600 }
13601 else
13602#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013603 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604}
13605
13606/*
13607 * "localtime()" function
13608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013610f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013611 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013614 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615}
13616
Bram Moolenaar33570922005-01-25 22:26:29 +000013617static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618
13619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013620get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013621 typval_T *argvars;
13622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623 int exact;
13624{
13625 char_u *keys;
13626 char_u *which;
13627 char_u buf[NUMBUFLEN];
13628 char_u *keys_buf = NULL;
13629 char_u *rhs;
13630 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013631 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013632 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013633 mapblock_T *mp;
13634 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635
13636 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637 rettv->v_type = VAR_STRING;
13638 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 if (*keys == NUL)
13642 return;
13643
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013644 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013645 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013646 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013647 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013648 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013649 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013650 if (argvars[3].v_type != VAR_UNKNOWN)
13651 get_dict = get_tv_number(&argvars[3]);
13652 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654 else
13655 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013656 if (which == NULL)
13657 return;
13658
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659 mode = get_map_mode(&which, 0);
13660
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013661 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013662 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013664
13665 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013667 /* Return a string. */
13668 if (rhs != NULL)
13669 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670
Bram Moolenaarbd743252010-10-20 21:23:33 +020013671 }
13672 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13673 {
13674 /* Return a dictionary. */
13675 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13676 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13677 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678
Bram Moolenaarbd743252010-10-20 21:23:33 +020013679 dict_add_nr_str(dict, "lhs", 0L, lhs);
13680 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13681 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13682 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13683 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13684 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13685 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13686 dict_add_nr_str(dict, "mode", 0L, mapmode);
13687
13688 vim_free(lhs);
13689 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690 }
13691}
13692
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013693#ifdef FEAT_FLOAT
13694/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013695 * "log()" function
13696 */
13697 static void
13698f_log(argvars, rettv)
13699 typval_T *argvars;
13700 typval_T *rettv;
13701{
13702 float_T f;
13703
13704 rettv->v_type = VAR_FLOAT;
13705 if (get_float_arg(argvars, &f) == OK)
13706 rettv->vval.v_float = log(f);
13707 else
13708 rettv->vval.v_float = 0.0;
13709}
13710
13711/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013712 * "log10()" function
13713 */
13714 static void
13715f_log10(argvars, rettv)
13716 typval_T *argvars;
13717 typval_T *rettv;
13718{
13719 float_T f;
13720
13721 rettv->v_type = VAR_FLOAT;
13722 if (get_float_arg(argvars, &f) == OK)
13723 rettv->vval.v_float = log10(f);
13724 else
13725 rettv->vval.v_float = 0.0;
13726}
13727#endif
13728
Bram Moolenaar1dced572012-04-05 16:54:08 +020013729#ifdef FEAT_LUA
13730/*
13731 * "luaeval()" function
13732 */
13733 static void
13734f_luaeval(argvars, rettv)
13735 typval_T *argvars;
13736 typval_T *rettv;
13737{
13738 char_u *str;
13739 char_u buf[NUMBUFLEN];
13740
13741 str = get_tv_string_buf(&argvars[0], buf);
13742 do_luaeval(str, argvars + 1, rettv);
13743}
13744#endif
13745
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013747 * "map()" function
13748 */
13749 static void
13750f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013751 typval_T *argvars;
13752 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013753{
13754 filter_map(argvars, rettv, TRUE);
13755}
13756
13757/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013758 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759 */
13760 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013761f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013762 typval_T *argvars;
13763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013765 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766}
13767
13768/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013769 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770 */
13771 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013772f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013773 typval_T *argvars;
13774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013776 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777}
13778
Bram Moolenaar33570922005-01-25 22:26:29 +000013779static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780
13781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013782find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013783 typval_T *argvars;
13784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785 int type;
13786{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013787 char_u *str = NULL;
13788 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789 char_u *pat;
13790 regmatch_T regmatch;
13791 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013792 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013793 char_u *save_cpo;
13794 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013795 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013796 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013797 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013798 list_T *l = NULL;
13799 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013800 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013801 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802
13803 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13804 save_cpo = p_cpo;
13805 p_cpo = (char_u *)"";
13806
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013807 rettv->vval.v_number = -1;
13808 if (type == 3)
13809 {
13810 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013811 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013812 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013813 }
13814 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013816 rettv->v_type = VAR_STRING;
13817 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013820 if (argvars[0].v_type == VAR_LIST)
13821 {
13822 if ((l = argvars[0].vval.v_list) == NULL)
13823 goto theend;
13824 li = l->lv_first;
13825 }
13826 else
13827 expr = str = get_tv_string(&argvars[0]);
13828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013829 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13830 if (pat == NULL)
13831 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013832
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013833 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013835 int error = FALSE;
13836
13837 start = get_tv_number_chk(&argvars[2], &error);
13838 if (error)
13839 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013840 if (l != NULL)
13841 {
13842 li = list_find(l, start);
13843 if (li == NULL)
13844 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013845 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013846 }
13847 else
13848 {
13849 if (start < 0)
13850 start = 0;
13851 if (start > (long)STRLEN(str))
13852 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013853 /* When "count" argument is there ignore matches before "start",
13854 * otherwise skip part of the string. Differs when pattern is "^"
13855 * or "\<". */
13856 if (argvars[3].v_type != VAR_UNKNOWN)
13857 startcol = start;
13858 else
13859 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013860 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013861
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013862 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013863 nth = get_tv_number_chk(&argvars[3], &error);
13864 if (error)
13865 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866 }
13867
13868 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13869 if (regmatch.regprog != NULL)
13870 {
13871 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013872
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013873 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013874 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013875 if (l != NULL)
13876 {
13877 if (li == NULL)
13878 {
13879 match = FALSE;
13880 break;
13881 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013882 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013883 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013884 if (str == NULL)
13885 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013886 }
13887
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013888 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013889
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013890 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013891 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013892 if (l == NULL && !match)
13893 break;
13894
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013895 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013896 if (l != NULL)
13897 {
13898 li = li->li_next;
13899 ++idx;
13900 }
13901 else
13902 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013903#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013904 startcol = (colnr_T)(regmatch.startp[0]
13905 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013906#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013907 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013908#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013909 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013910 }
13911
13912 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013914 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013915 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013916 int i;
13917
13918 /* return list with matched string and submatches */
13919 for (i = 0; i < NSUBEXP; ++i)
13920 {
13921 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013922 {
13923 if (list_append_string(rettv->vval.v_list,
13924 (char_u *)"", 0) == FAIL)
13925 break;
13926 }
13927 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013928 regmatch.startp[i],
13929 (int)(regmatch.endp[i] - regmatch.startp[i]))
13930 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013931 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013932 }
13933 }
13934 else if (type == 2)
13935 {
13936 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013937 if (l != NULL)
13938 copy_tv(&li->li_tv, rettv);
13939 else
13940 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013942 }
13943 else if (l != NULL)
13944 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945 else
13946 {
13947 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013948 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949 (varnumber_T)(regmatch.startp[0] - str);
13950 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013951 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013953 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954 }
13955 }
13956 vim_free(regmatch.regprog);
13957 }
13958
13959theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013960 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961 p_cpo = save_cpo;
13962}
13963
13964/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013965 * "match()" function
13966 */
13967 static void
13968f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013969 typval_T *argvars;
13970 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013971{
13972 find_some_match(argvars, rettv, 1);
13973}
13974
13975/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013976 * "matchadd()" function
13977 */
13978 static void
13979f_matchadd(argvars, rettv)
13980 typval_T *argvars;
13981 typval_T *rettv;
13982{
13983#ifdef FEAT_SEARCH_EXTRA
13984 char_u buf[NUMBUFLEN];
13985 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13986 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13987 int prio = 10; /* default priority */
13988 int id = -1;
13989 int error = FALSE;
13990
13991 rettv->vval.v_number = -1;
13992
13993 if (grp == NULL || pat == NULL)
13994 return;
13995 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013996 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013997 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013998 if (argvars[3].v_type != VAR_UNKNOWN)
13999 id = get_tv_number_chk(&argvars[3], &error);
14000 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014001 if (error == TRUE)
14002 return;
14003 if (id >= 1 && id <= 3)
14004 {
14005 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14006 return;
14007 }
14008
14009 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14010#endif
14011}
14012
14013/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014014 * "matcharg()" function
14015 */
14016 static void
14017f_matcharg(argvars, rettv)
14018 typval_T *argvars;
14019 typval_T *rettv;
14020{
14021 if (rettv_list_alloc(rettv) == OK)
14022 {
14023#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014024 int id = get_tv_number(&argvars[0]);
14025 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014026
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014027 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014028 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014029 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14030 {
14031 list_append_string(rettv->vval.v_list,
14032 syn_id2name(m->hlg_id), -1);
14033 list_append_string(rettv->vval.v_list, m->pattern, -1);
14034 }
14035 else
14036 {
14037 list_append_string(rettv->vval.v_list, NUL, -1);
14038 list_append_string(rettv->vval.v_list, NUL, -1);
14039 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014040 }
14041#endif
14042 }
14043}
14044
14045/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014046 * "matchdelete()" function
14047 */
14048 static void
14049f_matchdelete(argvars, rettv)
14050 typval_T *argvars;
14051 typval_T *rettv;
14052{
14053#ifdef FEAT_SEARCH_EXTRA
14054 rettv->vval.v_number = match_delete(curwin,
14055 (int)get_tv_number(&argvars[0]), TRUE);
14056#endif
14057}
14058
14059/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014060 * "matchend()" function
14061 */
14062 static void
14063f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014064 typval_T *argvars;
14065 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014066{
14067 find_some_match(argvars, rettv, 0);
14068}
14069
14070/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014071 * "matchlist()" function
14072 */
14073 static void
14074f_matchlist(argvars, rettv)
14075 typval_T *argvars;
14076 typval_T *rettv;
14077{
14078 find_some_match(argvars, rettv, 3);
14079}
14080
14081/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014082 * "matchstr()" function
14083 */
14084 static void
14085f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014086 typval_T *argvars;
14087 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014088{
14089 find_some_match(argvars, rettv, 2);
14090}
14091
Bram Moolenaar33570922005-01-25 22:26:29 +000014092static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014093
14094 static void
14095max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014096 typval_T *argvars;
14097 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014098 int domax;
14099{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014100 long n = 0;
14101 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014102 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014103
14104 if (argvars[0].v_type == VAR_LIST)
14105 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014106 list_T *l;
14107 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014108
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014109 l = argvars[0].vval.v_list;
14110 if (l != NULL)
14111 {
14112 li = l->lv_first;
14113 if (li != NULL)
14114 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014115 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014116 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014117 {
14118 li = li->li_next;
14119 if (li == NULL)
14120 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014121 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014122 if (domax ? i > n : i < n)
14123 n = i;
14124 }
14125 }
14126 }
14127 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014128 else if (argvars[0].v_type == VAR_DICT)
14129 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014130 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014131 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014132 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014133 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014134
14135 d = argvars[0].vval.v_dict;
14136 if (d != NULL)
14137 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014138 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014139 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014140 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014141 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014142 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014143 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014144 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014145 if (first)
14146 {
14147 n = i;
14148 first = FALSE;
14149 }
14150 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014151 n = i;
14152 }
14153 }
14154 }
14155 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014156 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014157 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014158 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014159}
14160
14161/*
14162 * "max()" function
14163 */
14164 static void
14165f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014166 typval_T *argvars;
14167 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014168{
14169 max_min(argvars, rettv, TRUE);
14170}
14171
14172/*
14173 * "min()" function
14174 */
14175 static void
14176f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014177 typval_T *argvars;
14178 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014179{
14180 max_min(argvars, rettv, FALSE);
14181}
14182
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014183static int mkdir_recurse __ARGS((char_u *dir, int prot));
14184
14185/*
14186 * Create the directory in which "dir" is located, and higher levels when
14187 * needed.
14188 */
14189 static int
14190mkdir_recurse(dir, prot)
14191 char_u *dir;
14192 int prot;
14193{
14194 char_u *p;
14195 char_u *updir;
14196 int r = FAIL;
14197
14198 /* Get end of directory name in "dir".
14199 * We're done when it's "/" or "c:/". */
14200 p = gettail_sep(dir);
14201 if (p <= get_past_head(dir))
14202 return OK;
14203
14204 /* If the directory exists we're done. Otherwise: create it.*/
14205 updir = vim_strnsave(dir, (int)(p - dir));
14206 if (updir == NULL)
14207 return FAIL;
14208 if (mch_isdir(updir))
14209 r = OK;
14210 else if (mkdir_recurse(updir, prot) == OK)
14211 r = vim_mkdir_emsg(updir, prot);
14212 vim_free(updir);
14213 return r;
14214}
14215
14216#ifdef vim_mkdir
14217/*
14218 * "mkdir()" function
14219 */
14220 static void
14221f_mkdir(argvars, rettv)
14222 typval_T *argvars;
14223 typval_T *rettv;
14224{
14225 char_u *dir;
14226 char_u buf[NUMBUFLEN];
14227 int prot = 0755;
14228
14229 rettv->vval.v_number = FAIL;
14230 if (check_restricted() || check_secure())
14231 return;
14232
14233 dir = get_tv_string_buf(&argvars[0], buf);
14234 if (argvars[1].v_type != VAR_UNKNOWN)
14235 {
14236 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014237 prot = get_tv_number_chk(&argvars[2], NULL);
14238 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014239 mkdir_recurse(dir, prot);
14240 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014241 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014242}
14243#endif
14244
Bram Moolenaar0d660222005-01-07 21:51:51 +000014245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246 * "mode()" function
14247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014249f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014250 typval_T *argvars;
14251 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014253 char_u buf[3];
14254
14255 buf[1] = NUL;
14256 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257
14258#ifdef FEAT_VISUAL
14259 if (VIsual_active)
14260 {
14261 if (VIsual_select)
14262 buf[0] = VIsual_mode + 's' - 'v';
14263 else
14264 buf[0] = VIsual_mode;
14265 }
14266 else
14267#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014268 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14269 || State == CONFIRM)
14270 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014271 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014272 if (State == ASKMORE)
14273 buf[1] = 'm';
14274 else if (State == CONFIRM)
14275 buf[1] = '?';
14276 }
14277 else if (State == EXTERNCMD)
14278 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279 else if (State & INSERT)
14280 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014281#ifdef FEAT_VREPLACE
14282 if (State & VREPLACE_FLAG)
14283 {
14284 buf[0] = 'R';
14285 buf[1] = 'v';
14286 }
14287 else
14288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289 if (State & REPLACE_FLAG)
14290 buf[0] = 'R';
14291 else
14292 buf[0] = 'i';
14293 }
14294 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014296 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014297 if (exmode_active)
14298 buf[1] = 'v';
14299 }
14300 else if (exmode_active)
14301 {
14302 buf[0] = 'c';
14303 buf[1] = 'e';
14304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014306 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014308 if (finish_op)
14309 buf[1] = 'o';
14310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014312 /* Clear out the minor mode when the argument is not a non-zero number or
14313 * non-empty string. */
14314 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014315 buf[1] = NUL;
14316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014317 rettv->vval.v_string = vim_strsave(buf);
14318 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319}
14320
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014321#ifdef FEAT_MZSCHEME
14322/*
14323 * "mzeval()" function
14324 */
14325 static void
14326f_mzeval(argvars, rettv)
14327 typval_T *argvars;
14328 typval_T *rettv;
14329{
14330 char_u *str;
14331 char_u buf[NUMBUFLEN];
14332
14333 str = get_tv_string_buf(&argvars[0], buf);
14334 do_mzeval(str, rettv);
14335}
Bram Moolenaar75676462013-01-30 14:55:42 +010014336
14337 void
14338mzscheme_call_vim(name, args, rettv)
14339 char_u *name;
14340 typval_T *args;
14341 typval_T *rettv;
14342{
14343 typval_T argvars[3];
14344
14345 argvars[0].v_type = VAR_STRING;
14346 argvars[0].vval.v_string = name;
14347 copy_tv(args, &argvars[1]);
14348 argvars[2].v_type = VAR_UNKNOWN;
14349 f_call(argvars, rettv);
14350 clear_tv(&argvars[1]);
14351}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014352#endif
14353
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014355 * "nextnonblank()" function
14356 */
14357 static void
14358f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014359 typval_T *argvars;
14360 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014361{
14362 linenr_T lnum;
14363
14364 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14365 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014366 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014367 {
14368 lnum = 0;
14369 break;
14370 }
14371 if (*skipwhite(ml_get(lnum)) != NUL)
14372 break;
14373 }
14374 rettv->vval.v_number = lnum;
14375}
14376
14377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378 * "nr2char()" function
14379 */
14380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014381f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014382 typval_T *argvars;
14383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384{
14385 char_u buf[NUMBUFLEN];
14386
14387#ifdef FEAT_MBYTE
14388 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014389 {
14390 int utf8 = 0;
14391
14392 if (argvars[1].v_type != VAR_UNKNOWN)
14393 utf8 = get_tv_number_chk(&argvars[1], NULL);
14394 if (utf8)
14395 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14396 else
14397 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399 else
14400#endif
14401 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014402 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403 buf[1] = NUL;
14404 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014405 rettv->v_type = VAR_STRING;
14406 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014407}
14408
14409/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014410 * "or(expr, expr)" function
14411 */
14412 static void
14413f_or(argvars, rettv)
14414 typval_T *argvars;
14415 typval_T *rettv;
14416{
14417 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14418 | get_tv_number_chk(&argvars[1], NULL);
14419}
14420
14421/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014422 * "pathshorten()" function
14423 */
14424 static void
14425f_pathshorten(argvars, rettv)
14426 typval_T *argvars;
14427 typval_T *rettv;
14428{
14429 char_u *p;
14430
14431 rettv->v_type = VAR_STRING;
14432 p = get_tv_string_chk(&argvars[0]);
14433 if (p == NULL)
14434 rettv->vval.v_string = NULL;
14435 else
14436 {
14437 p = vim_strsave(p);
14438 rettv->vval.v_string = p;
14439 if (p != NULL)
14440 shorten_dir(p);
14441 }
14442}
14443
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014444#ifdef FEAT_FLOAT
14445/*
14446 * "pow()" function
14447 */
14448 static void
14449f_pow(argvars, rettv)
14450 typval_T *argvars;
14451 typval_T *rettv;
14452{
14453 float_T fx, fy;
14454
14455 rettv->v_type = VAR_FLOAT;
14456 if (get_float_arg(argvars, &fx) == OK
14457 && get_float_arg(&argvars[1], &fy) == OK)
14458 rettv->vval.v_float = pow(fx, fy);
14459 else
14460 rettv->vval.v_float = 0.0;
14461}
14462#endif
14463
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014464/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014465 * "prevnonblank()" function
14466 */
14467 static void
14468f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014469 typval_T *argvars;
14470 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014471{
14472 linenr_T lnum;
14473
14474 lnum = get_tv_lnum(argvars);
14475 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14476 lnum = 0;
14477 else
14478 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14479 --lnum;
14480 rettv->vval.v_number = lnum;
14481}
14482
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014483#ifdef HAVE_STDARG_H
14484/* This dummy va_list is here because:
14485 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14486 * - locally in the function results in a "used before set" warning
14487 * - using va_start() to initialize it gives "function with fixed args" error */
14488static va_list ap;
14489#endif
14490
Bram Moolenaar8c711452005-01-14 21:53:12 +000014491/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014492 * "printf()" function
14493 */
14494 static void
14495f_printf(argvars, rettv)
14496 typval_T *argvars;
14497 typval_T *rettv;
14498{
14499 rettv->v_type = VAR_STRING;
14500 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014501#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014502 {
14503 char_u buf[NUMBUFLEN];
14504 int len;
14505 char_u *s;
14506 int saved_did_emsg = did_emsg;
14507 char *fmt;
14508
14509 /* Get the required length, allocate the buffer and do it for real. */
14510 did_emsg = FALSE;
14511 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014512 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014513 if (!did_emsg)
14514 {
14515 s = alloc(len + 1);
14516 if (s != NULL)
14517 {
14518 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014519 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014520 }
14521 }
14522 did_emsg |= saved_did_emsg;
14523 }
14524#endif
14525}
14526
14527/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014528 * "pumvisible()" function
14529 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014530 static void
14531f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014532 typval_T *argvars UNUSED;
14533 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014534{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014535#ifdef FEAT_INS_EXPAND
14536 if (pum_visible())
14537 rettv->vval.v_number = 1;
14538#endif
14539}
14540
Bram Moolenaardb913952012-06-29 12:54:53 +020014541#ifdef FEAT_PYTHON3
14542/*
14543 * "py3eval()" function
14544 */
14545 static void
14546f_py3eval(argvars, rettv)
14547 typval_T *argvars;
14548 typval_T *rettv;
14549{
14550 char_u *str;
14551 char_u buf[NUMBUFLEN];
14552
14553 str = get_tv_string_buf(&argvars[0], buf);
14554 do_py3eval(str, rettv);
14555}
14556#endif
14557
14558#ifdef FEAT_PYTHON
14559/*
14560 * "pyeval()" function
14561 */
14562 static void
14563f_pyeval(argvars, rettv)
14564 typval_T *argvars;
14565 typval_T *rettv;
14566{
14567 char_u *str;
14568 char_u buf[NUMBUFLEN];
14569
14570 str = get_tv_string_buf(&argvars[0], buf);
14571 do_pyeval(str, rettv);
14572}
14573#endif
14574
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014575/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014576 * "range()" function
14577 */
14578 static void
14579f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014580 typval_T *argvars;
14581 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014582{
14583 long start;
14584 long end;
14585 long stride = 1;
14586 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014587 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014588
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014589 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014590 if (argvars[1].v_type == VAR_UNKNOWN)
14591 {
14592 end = start - 1;
14593 start = 0;
14594 }
14595 else
14596 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014597 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014598 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014599 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014600 }
14601
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014602 if (error)
14603 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014604 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014605 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014606 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014607 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014608 else
14609 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014610 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014611 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014612 if (list_append_number(rettv->vval.v_list,
14613 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014614 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014615 }
14616}
14617
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014618/*
14619 * "readfile()" function
14620 */
14621 static void
14622f_readfile(argvars, rettv)
14623 typval_T *argvars;
14624 typval_T *rettv;
14625{
14626 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014627 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014628 char_u *fname;
14629 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014630 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14631 int io_size = sizeof(buf);
14632 int readlen; /* size of last fread() */
14633 char_u *prev = NULL; /* previously read bytes, if any */
14634 long prevlen = 0; /* length of data in prev */
14635 long prevsize = 0; /* size of prev buffer */
14636 long maxline = MAXLNUM;
14637 long cnt = 0;
14638 char_u *p; /* position in buf */
14639 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014640
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014641 if (argvars[1].v_type != VAR_UNKNOWN)
14642 {
14643 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14644 binary = TRUE;
14645 if (argvars[2].v_type != VAR_UNKNOWN)
14646 maxline = get_tv_number(&argvars[2]);
14647 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014648
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014649 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014650 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014651
14652 /* Always open the file in binary mode, library functions have a mind of
14653 * their own about CR-LF conversion. */
14654 fname = get_tv_string(&argvars[0]);
14655 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14656 {
14657 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14658 return;
14659 }
14660
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014661 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014662 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014663 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014664
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014665 /* This for loop processes what was read, but is also entered at end
14666 * of file so that either:
14667 * - an incomplete line gets written
14668 * - a "binary" file gets an empty line at the end if it ends in a
14669 * newline. */
14670 for (p = buf, start = buf;
14671 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14672 ++p)
14673 {
14674 if (*p == '\n' || readlen <= 0)
14675 {
14676 listitem_T *li;
14677 char_u *s = NULL;
14678 long_u len = p - start;
14679
14680 /* Finished a line. Remove CRs before NL. */
14681 if (readlen > 0 && !binary)
14682 {
14683 while (len > 0 && start[len - 1] == '\r')
14684 --len;
14685 /* removal may cross back to the "prev" string */
14686 if (len == 0)
14687 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14688 --prevlen;
14689 }
14690 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014691 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014692 else
14693 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014694 /* Change "prev" buffer to be the right size. This way
14695 * the bytes are only copied once, and very long lines are
14696 * allocated only once. */
14697 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014698 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014699 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014700 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014701 prev = NULL; /* the list will own the string */
14702 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014703 }
14704 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014705 if (s == NULL)
14706 {
14707 do_outofmem_msg((long_u) prevlen + len + 1);
14708 failed = TRUE;
14709 break;
14710 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014711
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014712 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014713 {
14714 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014715 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014716 break;
14717 }
14718 li->li_tv.v_type = VAR_STRING;
14719 li->li_tv.v_lock = 0;
14720 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014721 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014722
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014723 start = p + 1; /* step over newline */
14724 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014725 break;
14726 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014727 else if (*p == NUL)
14728 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014729#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014730 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14731 * when finding the BF and check the previous two bytes. */
14732 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014733 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014734 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14735 * + 1, these may be in the "prev" string. */
14736 char_u back1 = p >= buf + 1 ? p[-1]
14737 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14738 char_u back2 = p >= buf + 2 ? p[-2]
14739 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14740 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014741
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014742 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014743 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014744 char_u *dest = p - 2;
14745
14746 /* Usually a BOM is at the beginning of a file, and so at
14747 * the beginning of a line; then we can just step over it.
14748 */
14749 if (start == dest)
14750 start = p + 1;
14751 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014752 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014753 /* have to shuffle buf to close gap */
14754 int adjust_prevlen = 0;
14755
14756 if (dest < buf)
14757 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014758 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014759 dest = buf;
14760 }
14761 if (readlen > p - buf + 1)
14762 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14763 readlen -= 3 - adjust_prevlen;
14764 prevlen -= adjust_prevlen;
14765 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014766 }
14767 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014768 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014769#endif
14770 } /* for */
14771
14772 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14773 break;
14774 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014775 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014776 /* There's part of a line in buf, store it in "prev". */
14777 if (p - start + prevlen >= prevsize)
14778 {
14779 /* need bigger "prev" buffer */
14780 char_u *newprev;
14781
14782 /* A common use case is ordinary text files and "prev" gets a
14783 * fragment of a line, so the first allocation is made
14784 * small, to avoid repeatedly 'allocing' large and
14785 * 'reallocing' small. */
14786 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014787 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014788 else
14789 {
14790 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014791 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014792 prevsize = grow50pc > growmin ? grow50pc : growmin;
14793 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014794 newprev = prev == NULL ? alloc(prevsize)
14795 : vim_realloc(prev, prevsize);
14796 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014797 {
14798 do_outofmem_msg((long_u)prevsize);
14799 failed = TRUE;
14800 break;
14801 }
14802 prev = newprev;
14803 }
14804 /* Add the line part to end of "prev". */
14805 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014806 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014807 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014808 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014809
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014810 /*
14811 * For a negative line count use only the lines at the end of the file,
14812 * free the rest.
14813 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014814 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014815 while (cnt > -maxline)
14816 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014817 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014818 --cnt;
14819 }
14820
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014821 if (failed)
14822 {
14823 list_free(rettv->vval.v_list, TRUE);
14824 /* readfile doc says an empty list is returned on error */
14825 rettv->vval.v_list = list_alloc();
14826 }
14827
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014828 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014829 fclose(fd);
14830}
14831
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014832#if defined(FEAT_RELTIME)
14833static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14834
14835/*
14836 * Convert a List to proftime_T.
14837 * Return FAIL when there is something wrong.
14838 */
14839 static int
14840list2proftime(arg, tm)
14841 typval_T *arg;
14842 proftime_T *tm;
14843{
14844 long n1, n2;
14845 int error = FALSE;
14846
14847 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14848 || arg->vval.v_list->lv_len != 2)
14849 return FAIL;
14850 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14851 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14852# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014853 tm->HighPart = n1;
14854 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014855# else
14856 tm->tv_sec = n1;
14857 tm->tv_usec = n2;
14858# endif
14859 return error ? FAIL : OK;
14860}
14861#endif /* FEAT_RELTIME */
14862
14863/*
14864 * "reltime()" function
14865 */
14866 static void
14867f_reltime(argvars, rettv)
14868 typval_T *argvars;
14869 typval_T *rettv;
14870{
14871#ifdef FEAT_RELTIME
14872 proftime_T res;
14873 proftime_T start;
14874
14875 if (argvars[0].v_type == VAR_UNKNOWN)
14876 {
14877 /* No arguments: get current time. */
14878 profile_start(&res);
14879 }
14880 else if (argvars[1].v_type == VAR_UNKNOWN)
14881 {
14882 if (list2proftime(&argvars[0], &res) == FAIL)
14883 return;
14884 profile_end(&res);
14885 }
14886 else
14887 {
14888 /* Two arguments: compute the difference. */
14889 if (list2proftime(&argvars[0], &start) == FAIL
14890 || list2proftime(&argvars[1], &res) == FAIL)
14891 return;
14892 profile_sub(&res, &start);
14893 }
14894
14895 if (rettv_list_alloc(rettv) == OK)
14896 {
14897 long n1, n2;
14898
14899# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014900 n1 = res.HighPart;
14901 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014902# else
14903 n1 = res.tv_sec;
14904 n2 = res.tv_usec;
14905# endif
14906 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14907 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14908 }
14909#endif
14910}
14911
14912/*
14913 * "reltimestr()" function
14914 */
14915 static void
14916f_reltimestr(argvars, rettv)
14917 typval_T *argvars;
14918 typval_T *rettv;
14919{
14920#ifdef FEAT_RELTIME
14921 proftime_T tm;
14922#endif
14923
14924 rettv->v_type = VAR_STRING;
14925 rettv->vval.v_string = NULL;
14926#ifdef FEAT_RELTIME
14927 if (list2proftime(&argvars[0], &tm) == OK)
14928 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14929#endif
14930}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014931
Bram Moolenaar0d660222005-01-07 21:51:51 +000014932#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14933static void make_connection __ARGS((void));
14934static int check_connection __ARGS((void));
14935
14936 static void
14937make_connection()
14938{
14939 if (X_DISPLAY == NULL
14940# ifdef FEAT_GUI
14941 && !gui.in_use
14942# endif
14943 )
14944 {
14945 x_force_connect = TRUE;
14946 setup_term_clip();
14947 x_force_connect = FALSE;
14948 }
14949}
14950
14951 static int
14952check_connection()
14953{
14954 make_connection();
14955 if (X_DISPLAY == NULL)
14956 {
14957 EMSG(_("E240: No connection to Vim server"));
14958 return FAIL;
14959 }
14960 return OK;
14961}
14962#endif
14963
14964#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014965static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014966
14967 static void
14968remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014969 typval_T *argvars;
14970 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014971 int expr;
14972{
14973 char_u *server_name;
14974 char_u *keys;
14975 char_u *r = NULL;
14976 char_u buf[NUMBUFLEN];
14977# ifdef WIN32
14978 HWND w;
14979# else
14980 Window w;
14981# endif
14982
14983 if (check_restricted() || check_secure())
14984 return;
14985
14986# ifdef FEAT_X11
14987 if (check_connection() == FAIL)
14988 return;
14989# endif
14990
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014991 server_name = get_tv_string_chk(&argvars[0]);
14992 if (server_name == NULL)
14993 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014994 keys = get_tv_string_buf(&argvars[1], buf);
14995# ifdef WIN32
14996 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14997# else
14998 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14999 < 0)
15000# endif
15001 {
15002 if (r != NULL)
15003 EMSG(r); /* sending worked but evaluation failed */
15004 else
15005 EMSG2(_("E241: Unable to send to %s"), server_name);
15006 return;
15007 }
15008
15009 rettv->vval.v_string = r;
15010
15011 if (argvars[2].v_type != VAR_UNKNOWN)
15012 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015013 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015014 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015015 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015016
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015017 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015018 v.di_tv.v_type = VAR_STRING;
15019 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015020 idvar = get_tv_string_chk(&argvars[2]);
15021 if (idvar != NULL)
15022 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015023 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015024 }
15025}
15026#endif
15027
15028/*
15029 * "remote_expr()" function
15030 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015031 static void
15032f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015033 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015034 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015035{
15036 rettv->v_type = VAR_STRING;
15037 rettv->vval.v_string = NULL;
15038#ifdef FEAT_CLIENTSERVER
15039 remote_common(argvars, rettv, TRUE);
15040#endif
15041}
15042
15043/*
15044 * "remote_foreground()" function
15045 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015046 static void
15047f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015048 typval_T *argvars UNUSED;
15049 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015050{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015051#ifdef FEAT_CLIENTSERVER
15052# ifdef WIN32
15053 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015054 {
15055 char_u *server_name = get_tv_string_chk(&argvars[0]);
15056
15057 if (server_name != NULL)
15058 serverForeground(server_name);
15059 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015060# else
15061 /* Send a foreground() expression to the server. */
15062 argvars[1].v_type = VAR_STRING;
15063 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15064 argvars[2].v_type = VAR_UNKNOWN;
15065 remote_common(argvars, rettv, TRUE);
15066 vim_free(argvars[1].vval.v_string);
15067# endif
15068#endif
15069}
15070
Bram Moolenaar0d660222005-01-07 21:51:51 +000015071 static void
15072f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015073 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015074 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015075{
15076#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015077 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015078 char_u *s = NULL;
15079# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015080 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015081# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015082 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015083
15084 if (check_restricted() || check_secure())
15085 {
15086 rettv->vval.v_number = -1;
15087 return;
15088 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015089 serverid = get_tv_string_chk(&argvars[0]);
15090 if (serverid == NULL)
15091 {
15092 rettv->vval.v_number = -1;
15093 return; /* type error; errmsg already given */
15094 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015095# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015096 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015097 if (n == 0)
15098 rettv->vval.v_number = -1;
15099 else
15100 {
15101 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15102 rettv->vval.v_number = (s != NULL);
15103 }
15104# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015105 if (check_connection() == FAIL)
15106 return;
15107
15108 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015109 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015110# endif
15111
15112 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15113 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015114 char_u *retvar;
15115
Bram Moolenaar33570922005-01-25 22:26:29 +000015116 v.di_tv.v_type = VAR_STRING;
15117 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015118 retvar = get_tv_string_chk(&argvars[1]);
15119 if (retvar != NULL)
15120 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015121 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015122 }
15123#else
15124 rettv->vval.v_number = -1;
15125#endif
15126}
15127
Bram Moolenaar0d660222005-01-07 21:51:51 +000015128 static void
15129f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015130 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015131 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015132{
15133 char_u *r = NULL;
15134
15135#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015136 char_u *serverid = get_tv_string_chk(&argvars[0]);
15137
15138 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015139 {
15140# ifdef WIN32
15141 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015142 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015143
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015144 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015145 if (n != 0)
15146 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15147 if (r == NULL)
15148# else
15149 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015150 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015151# endif
15152 EMSG(_("E277: Unable to read a server reply"));
15153 }
15154#endif
15155 rettv->v_type = VAR_STRING;
15156 rettv->vval.v_string = r;
15157}
15158
15159/*
15160 * "remote_send()" function
15161 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015162 static void
15163f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015164 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015165 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015166{
15167 rettv->v_type = VAR_STRING;
15168 rettv->vval.v_string = NULL;
15169#ifdef FEAT_CLIENTSERVER
15170 remote_common(argvars, rettv, FALSE);
15171#endif
15172}
15173
15174/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015175 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015176 */
15177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015178f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015179 typval_T *argvars;
15180 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015181{
Bram Moolenaar33570922005-01-25 22:26:29 +000015182 list_T *l;
15183 listitem_T *item, *item2;
15184 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015185 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015186 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015187 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015188 dict_T *d;
15189 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015190 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015191
Bram Moolenaar8c711452005-01-14 21:53:12 +000015192 if (argvars[0].v_type == VAR_DICT)
15193 {
15194 if (argvars[2].v_type != VAR_UNKNOWN)
15195 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015196 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015197 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015198 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015199 key = get_tv_string_chk(&argvars[1]);
15200 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015201 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015202 di = dict_find(d, key, -1);
15203 if (di == NULL)
15204 EMSG2(_(e_dictkey), key);
15205 else
15206 {
15207 *rettv = di->di_tv;
15208 init_tv(&di->di_tv);
15209 dictitem_remove(d, di);
15210 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015211 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015212 }
15213 }
15214 else if (argvars[0].v_type != VAR_LIST)
15215 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015216 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015217 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015219 int error = FALSE;
15220
15221 idx = get_tv_number_chk(&argvars[1], &error);
15222 if (error)
15223 ; /* type error: do nothing, errmsg already given */
15224 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015225 EMSGN(_(e_listidx), idx);
15226 else
15227 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015228 if (argvars[2].v_type == VAR_UNKNOWN)
15229 {
15230 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015231 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015232 *rettv = item->li_tv;
15233 vim_free(item);
15234 }
15235 else
15236 {
15237 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015238 end = get_tv_number_chk(&argvars[2], &error);
15239 if (error)
15240 ; /* type error: do nothing */
15241 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015242 EMSGN(_(e_listidx), end);
15243 else
15244 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015245 int cnt = 0;
15246
15247 for (li = item; li != NULL; li = li->li_next)
15248 {
15249 ++cnt;
15250 if (li == item2)
15251 break;
15252 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015253 if (li == NULL) /* didn't find "item2" after "item" */
15254 EMSG(_(e_invrange));
15255 else
15256 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015257 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015258 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015259 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015260 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015261 l->lv_first = item;
15262 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015263 item->li_prev = NULL;
15264 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015265 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015266 }
15267 }
15268 }
15269 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015270 }
15271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272}
15273
15274/*
15275 * "rename({from}, {to})" function
15276 */
15277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015278f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015279 typval_T *argvars;
15280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015281{
15282 char_u buf[NUMBUFLEN];
15283
15284 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015285 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015286 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015287 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15288 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015289}
15290
15291/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015292 * "repeat()" function
15293 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015294 static void
15295f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015296 typval_T *argvars;
15297 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015298{
15299 char_u *p;
15300 int n;
15301 int slen;
15302 int len;
15303 char_u *r;
15304 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015305
15306 n = get_tv_number(&argvars[1]);
15307 if (argvars[0].v_type == VAR_LIST)
15308 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015309 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015310 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015311 if (list_extend(rettv->vval.v_list,
15312 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015313 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015314 }
15315 else
15316 {
15317 p = get_tv_string(&argvars[0]);
15318 rettv->v_type = VAR_STRING;
15319 rettv->vval.v_string = NULL;
15320
15321 slen = (int)STRLEN(p);
15322 len = slen * n;
15323 if (len <= 0)
15324 return;
15325
15326 r = alloc(len + 1);
15327 if (r != NULL)
15328 {
15329 for (i = 0; i < n; i++)
15330 mch_memmove(r + i * slen, p, (size_t)slen);
15331 r[len] = NUL;
15332 }
15333
15334 rettv->vval.v_string = r;
15335 }
15336}
15337
15338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339 * "resolve()" function
15340 */
15341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015342f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015343 typval_T *argvars;
15344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345{
15346 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015347#ifdef HAVE_READLINK
15348 char_u *buf = NULL;
15349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015351 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352#ifdef FEAT_SHORTCUT
15353 {
15354 char_u *v = NULL;
15355
15356 v = mch_resolve_shortcut(p);
15357 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015358 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015360 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361 }
15362#else
15363# ifdef HAVE_READLINK
15364 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365 char_u *cpy;
15366 int len;
15367 char_u *remain = NULL;
15368 char_u *q;
15369 int is_relative_to_current = FALSE;
15370 int has_trailing_pathsep = FALSE;
15371 int limit = 100;
15372
15373 p = vim_strsave(p);
15374
15375 if (p[0] == '.' && (vim_ispathsep(p[1])
15376 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15377 is_relative_to_current = TRUE;
15378
15379 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015380 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015381 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015382 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015383 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385
15386 q = getnextcomp(p);
15387 if (*q != NUL)
15388 {
15389 /* Separate the first path component in "p", and keep the
15390 * remainder (beginning with the path separator). */
15391 remain = vim_strsave(q - 1);
15392 q[-1] = NUL;
15393 }
15394
Bram Moolenaard9462e32011-04-11 21:35:11 +020015395 buf = alloc(MAXPATHL + 1);
15396 if (buf == NULL)
15397 goto fail;
15398
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399 for (;;)
15400 {
15401 for (;;)
15402 {
15403 len = readlink((char *)p, (char *)buf, MAXPATHL);
15404 if (len <= 0)
15405 break;
15406 buf[len] = NUL;
15407
15408 if (limit-- == 0)
15409 {
15410 vim_free(p);
15411 vim_free(remain);
15412 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015413 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414 goto fail;
15415 }
15416
15417 /* Ensure that the result will have a trailing path separator
15418 * if the argument has one. */
15419 if (remain == NULL && has_trailing_pathsep)
15420 add_pathsep(buf);
15421
15422 /* Separate the first path component in the link value and
15423 * concatenate the remainders. */
15424 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15425 if (*q != NUL)
15426 {
15427 if (remain == NULL)
15428 remain = vim_strsave(q - 1);
15429 else
15430 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015431 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015432 if (cpy != NULL)
15433 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434 vim_free(remain);
15435 remain = cpy;
15436 }
15437 }
15438 q[-1] = NUL;
15439 }
15440
15441 q = gettail(p);
15442 if (q > p && *q == NUL)
15443 {
15444 /* Ignore trailing path separator. */
15445 q[-1] = NUL;
15446 q = gettail(p);
15447 }
15448 if (q > p && !mch_isFullName(buf))
15449 {
15450 /* symlink is relative to directory of argument */
15451 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15452 if (cpy != NULL)
15453 {
15454 STRCPY(cpy, p);
15455 STRCPY(gettail(cpy), buf);
15456 vim_free(p);
15457 p = cpy;
15458 }
15459 }
15460 else
15461 {
15462 vim_free(p);
15463 p = vim_strsave(buf);
15464 }
15465 }
15466
15467 if (remain == NULL)
15468 break;
15469
15470 /* Append the first path component of "remain" to "p". */
15471 q = getnextcomp(remain + 1);
15472 len = q - remain - (*q != NUL);
15473 cpy = vim_strnsave(p, STRLEN(p) + len);
15474 if (cpy != NULL)
15475 {
15476 STRNCAT(cpy, remain, len);
15477 vim_free(p);
15478 p = cpy;
15479 }
15480 /* Shorten "remain". */
15481 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015482 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015483 else
15484 {
15485 vim_free(remain);
15486 remain = NULL;
15487 }
15488 }
15489
15490 /* If the result is a relative path name, make it explicitly relative to
15491 * the current directory if and only if the argument had this form. */
15492 if (!vim_ispathsep(*p))
15493 {
15494 if (is_relative_to_current
15495 && *p != NUL
15496 && !(p[0] == '.'
15497 && (p[1] == NUL
15498 || vim_ispathsep(p[1])
15499 || (p[1] == '.'
15500 && (p[2] == NUL
15501 || vim_ispathsep(p[2]))))))
15502 {
15503 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015504 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015505 if (cpy != NULL)
15506 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507 vim_free(p);
15508 p = cpy;
15509 }
15510 }
15511 else if (!is_relative_to_current)
15512 {
15513 /* Strip leading "./". */
15514 q = p;
15515 while (q[0] == '.' && vim_ispathsep(q[1]))
15516 q += 2;
15517 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015518 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519 }
15520 }
15521
15522 /* Ensure that the result will have no trailing path separator
15523 * if the argument had none. But keep "/" or "//". */
15524 if (!has_trailing_pathsep)
15525 {
15526 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015527 if (after_pathsep(p, q))
15528 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529 }
15530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015531 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532 }
15533# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015534 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535# endif
15536#endif
15537
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015538 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539
15540#ifdef HAVE_READLINK
15541fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015542 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015544 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545}
15546
15547/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015548 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549 */
15550 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015551f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015552 typval_T *argvars;
15553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554{
Bram Moolenaar33570922005-01-25 22:26:29 +000015555 list_T *l;
15556 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557
Bram Moolenaar0d660222005-01-07 21:51:51 +000015558 if (argvars[0].v_type != VAR_LIST)
15559 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015560 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015561 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015562 {
15563 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015564 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015565 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015566 while (li != NULL)
15567 {
15568 ni = li->li_prev;
15569 list_append(l, li);
15570 li = ni;
15571 }
15572 rettv->vval.v_list = l;
15573 rettv->v_type = VAR_LIST;
15574 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015575 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577}
15578
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015579#define SP_NOMOVE 0x01 /* don't move cursor */
15580#define SP_REPEAT 0x02 /* repeat to find outer pair */
15581#define SP_RETCOUNT 0x04 /* return matchcount */
15582#define SP_SETPCMARK 0x08 /* set previous context mark */
15583#define SP_START 0x10 /* accept match at start position */
15584#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15585#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015586
Bram Moolenaar33570922005-01-25 22:26:29 +000015587static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015588
15589/*
15590 * Get flags for a search function.
15591 * Possibly sets "p_ws".
15592 * Returns BACKWARD, FORWARD or zero (for an error).
15593 */
15594 static int
15595get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015596 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015597 int *flagsp;
15598{
15599 int dir = FORWARD;
15600 char_u *flags;
15601 char_u nbuf[NUMBUFLEN];
15602 int mask;
15603
15604 if (varp->v_type != VAR_UNKNOWN)
15605 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015606 flags = get_tv_string_buf_chk(varp, nbuf);
15607 if (flags == NULL)
15608 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015609 while (*flags != NUL)
15610 {
15611 switch (*flags)
15612 {
15613 case 'b': dir = BACKWARD; break;
15614 case 'w': p_ws = TRUE; break;
15615 case 'W': p_ws = FALSE; break;
15616 default: mask = 0;
15617 if (flagsp != NULL)
15618 switch (*flags)
15619 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015620 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015621 case 'e': mask = SP_END; break;
15622 case 'm': mask = SP_RETCOUNT; break;
15623 case 'n': mask = SP_NOMOVE; break;
15624 case 'p': mask = SP_SUBPAT; break;
15625 case 'r': mask = SP_REPEAT; break;
15626 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015627 }
15628 if (mask == 0)
15629 {
15630 EMSG2(_(e_invarg2), flags);
15631 dir = 0;
15632 }
15633 else
15634 *flagsp |= mask;
15635 }
15636 if (dir == 0)
15637 break;
15638 ++flags;
15639 }
15640 }
15641 return dir;
15642}
15643
Bram Moolenaar071d4272004-06-13 20:20:40 +000015644/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015645 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015646 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015647 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015648search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015649 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015650 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015651 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015653 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654 char_u *pat;
15655 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015656 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657 int save_p_ws = p_ws;
15658 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015659 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015660 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015661 proftime_T tm;
15662#ifdef FEAT_RELTIME
15663 long time_limit = 0;
15664#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015665 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015666 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015668 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015669 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015670 if (dir == 0)
15671 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015672 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015673 if (flags & SP_START)
15674 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015675 if (flags & SP_END)
15676 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015677
Bram Moolenaar76929292008-01-06 19:07:36 +000015678 /* Optional arguments: line number to stop searching and timeout. */
15679 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015680 {
15681 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15682 if (lnum_stop < 0)
15683 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015684#ifdef FEAT_RELTIME
15685 if (argvars[3].v_type != VAR_UNKNOWN)
15686 {
15687 time_limit = get_tv_number_chk(&argvars[3], NULL);
15688 if (time_limit < 0)
15689 goto theend;
15690 }
15691#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015692 }
15693
Bram Moolenaar76929292008-01-06 19:07:36 +000015694#ifdef FEAT_RELTIME
15695 /* Set the time limit, if there is one. */
15696 profile_setlimit(time_limit, &tm);
15697#endif
15698
Bram Moolenaar231334e2005-07-25 20:46:57 +000015699 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015700 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015701 * Check to make sure only those flags are set.
15702 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15703 * flags cannot be set. Check for that condition also.
15704 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015705 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015706 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015707 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015708 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015709 goto theend;
15710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015711
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015712 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015713 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015714 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015715 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015717 if (flags & SP_SUBPAT)
15718 retval = subpatnum;
15719 else
15720 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015721 if (flags & SP_SETPCMARK)
15722 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015724 if (match_pos != NULL)
15725 {
15726 /* Store the match cursor position */
15727 match_pos->lnum = pos.lnum;
15728 match_pos->col = pos.col + 1;
15729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730 /* "/$" will put the cursor after the end of the line, may need to
15731 * correct that here */
15732 check_cursor();
15733 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015734
15735 /* If 'n' flag is used: restore cursor position. */
15736 if (flags & SP_NOMOVE)
15737 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015738 else
15739 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015740theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015742
15743 return retval;
15744}
15745
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015746#ifdef FEAT_FLOAT
15747/*
15748 * "round({float})" function
15749 */
15750 static void
15751f_round(argvars, rettv)
15752 typval_T *argvars;
15753 typval_T *rettv;
15754{
15755 float_T f;
15756
15757 rettv->v_type = VAR_FLOAT;
15758 if (get_float_arg(argvars, &f) == OK)
15759 /* round() is not in C90, use ceil() or floor() instead. */
15760 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15761 else
15762 rettv->vval.v_float = 0.0;
15763}
15764#endif
15765
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015766/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015767 * "screencol()" function
15768 *
15769 * First column is 1 to be consistent with virtcol().
15770 */
15771 static void
15772f_screencol(argvars, rettv)
15773 typval_T *argvars UNUSED;
15774 typval_T *rettv;
15775{
15776 rettv->vval.v_number = screen_screencol() + 1;
15777}
15778
15779/*
15780 * "screenrow()" function
15781 */
15782 static void
15783f_screenrow(argvars, rettv)
15784 typval_T *argvars UNUSED;
15785 typval_T *rettv;
15786{
15787 rettv->vval.v_number = screen_screenrow() + 1;
15788}
15789
15790/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015791 * "search()" function
15792 */
15793 static void
15794f_search(argvars, rettv)
15795 typval_T *argvars;
15796 typval_T *rettv;
15797{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015798 int flags = 0;
15799
15800 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015801}
15802
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015804 * "searchdecl()" function
15805 */
15806 static void
15807f_searchdecl(argvars, rettv)
15808 typval_T *argvars;
15809 typval_T *rettv;
15810{
15811 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015812 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015813 int error = FALSE;
15814 char_u *name;
15815
15816 rettv->vval.v_number = 1; /* default: FAIL */
15817
15818 name = get_tv_string_chk(&argvars[0]);
15819 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015820 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015821 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015822 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15823 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15824 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015825 if (!error && name != NULL)
15826 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015827 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015828}
15829
15830/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015831 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015832 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015833 static int
15834searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015835 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015836 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015837{
15838 char_u *spat, *mpat, *epat;
15839 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015840 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015841 int dir;
15842 int flags = 0;
15843 char_u nbuf1[NUMBUFLEN];
15844 char_u nbuf2[NUMBUFLEN];
15845 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015846 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015847 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015848 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849
Bram Moolenaar071d4272004-06-13 20:20:40 +000015850 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015851 spat = get_tv_string_chk(&argvars[0]);
15852 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15853 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15854 if (spat == NULL || mpat == NULL || epat == NULL)
15855 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857 /* Handle the optional fourth argument: flags */
15858 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015859 if (dir == 0)
15860 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015861
15862 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015863 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15864 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015865 if ((flags & (SP_END | SP_SUBPAT)) != 0
15866 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015867 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015868 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015869 goto theend;
15870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015872 /* Using 'r' implies 'W', otherwise it doesn't work. */
15873 if (flags & SP_REPEAT)
15874 p_ws = FALSE;
15875
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015876 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015877 if (argvars[3].v_type == VAR_UNKNOWN
15878 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015879 skip = (char_u *)"";
15880 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015881 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015882 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015883 if (argvars[5].v_type != VAR_UNKNOWN)
15884 {
15885 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15886 if (lnum_stop < 0)
15887 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015888#ifdef FEAT_RELTIME
15889 if (argvars[6].v_type != VAR_UNKNOWN)
15890 {
15891 time_limit = get_tv_number_chk(&argvars[6], NULL);
15892 if (time_limit < 0)
15893 goto theend;
15894 }
15895#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015896 }
15897 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015898 if (skip == NULL)
15899 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015901 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015902 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015903
15904theend:
15905 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015906
15907 return retval;
15908}
15909
15910/*
15911 * "searchpair()" function
15912 */
15913 static void
15914f_searchpair(argvars, rettv)
15915 typval_T *argvars;
15916 typval_T *rettv;
15917{
15918 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15919}
15920
15921/*
15922 * "searchpairpos()" function
15923 */
15924 static void
15925f_searchpairpos(argvars, rettv)
15926 typval_T *argvars;
15927 typval_T *rettv;
15928{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015929 pos_T match_pos;
15930 int lnum = 0;
15931 int col = 0;
15932
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015933 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015934 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015935
15936 if (searchpair_cmn(argvars, &match_pos) > 0)
15937 {
15938 lnum = match_pos.lnum;
15939 col = match_pos.col;
15940 }
15941
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015942 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15943 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015944}
15945
15946/*
15947 * Search for a start/middle/end thing.
15948 * Used by searchpair(), see its documentation for the details.
15949 * Returns 0 or -1 for no match,
15950 */
15951 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015952do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15953 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015954 char_u *spat; /* start pattern */
15955 char_u *mpat; /* middle pattern */
15956 char_u *epat; /* end pattern */
15957 int dir; /* BACKWARD or FORWARD */
15958 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015959 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015960 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015961 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015962 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015963{
15964 char_u *save_cpo;
15965 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15966 long retval = 0;
15967 pos_T pos;
15968 pos_T firstpos;
15969 pos_T foundpos;
15970 pos_T save_cursor;
15971 pos_T save_pos;
15972 int n;
15973 int r;
15974 int nest = 1;
15975 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015976 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015977 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015978
15979 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15980 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015981 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015982
Bram Moolenaar76929292008-01-06 19:07:36 +000015983#ifdef FEAT_RELTIME
15984 /* Set the time limit, if there is one. */
15985 profile_setlimit(time_limit, &tm);
15986#endif
15987
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015988 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15989 * start/middle/end (pat3, for the top pair). */
15990 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15991 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15992 if (pat2 == NULL || pat3 == NULL)
15993 goto theend;
15994 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15995 if (*mpat == NUL)
15996 STRCPY(pat3, pat2);
15997 else
15998 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15999 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016000 if (flags & SP_START)
16001 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016002
Bram Moolenaar071d4272004-06-13 20:20:40 +000016003 save_cursor = curwin->w_cursor;
16004 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016005 clearpos(&firstpos);
16006 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016007 pat = pat3;
16008 for (;;)
16009 {
16010 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016011 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16013 /* didn't find it or found the first match again: FAIL */
16014 break;
16015
16016 if (firstpos.lnum == 0)
16017 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016018 if (equalpos(pos, foundpos))
16019 {
16020 /* Found the same position again. Can happen with a pattern that
16021 * has "\zs" at the end and searching backwards. Advance one
16022 * character and try again. */
16023 if (dir == BACKWARD)
16024 decl(&pos);
16025 else
16026 incl(&pos);
16027 }
16028 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016029
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016030 /* clear the start flag to avoid getting stuck here */
16031 options &= ~SEARCH_START;
16032
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033 /* If the skip pattern matches, ignore this match. */
16034 if (*skip != NUL)
16035 {
16036 save_pos = curwin->w_cursor;
16037 curwin->w_cursor = pos;
16038 r = eval_to_bool(skip, &err, NULL, FALSE);
16039 curwin->w_cursor = save_pos;
16040 if (err)
16041 {
16042 /* Evaluating {skip} caused an error, break here. */
16043 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016044 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045 break;
16046 }
16047 if (r)
16048 continue;
16049 }
16050
16051 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16052 {
16053 /* Found end when searching backwards or start when searching
16054 * forward: nested pair. */
16055 ++nest;
16056 pat = pat2; /* nested, don't search for middle */
16057 }
16058 else
16059 {
16060 /* Found end when searching forward or start when searching
16061 * backward: end of (nested) pair; or found middle in outer pair. */
16062 if (--nest == 1)
16063 pat = pat3; /* outer level, search for middle */
16064 }
16065
16066 if (nest == 0)
16067 {
16068 /* Found the match: return matchcount or line number. */
16069 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016070 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016072 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016073 if (flags & SP_SETPCMARK)
16074 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075 curwin->w_cursor = pos;
16076 if (!(flags & SP_REPEAT))
16077 break;
16078 nest = 1; /* search for next unmatched */
16079 }
16080 }
16081
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016082 if (match_pos != NULL)
16083 {
16084 /* Store the match cursor position */
16085 match_pos->lnum = curwin->w_cursor.lnum;
16086 match_pos->col = curwin->w_cursor.col + 1;
16087 }
16088
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016090 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091 curwin->w_cursor = save_cursor;
16092
16093theend:
16094 vim_free(pat2);
16095 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016096 if (p_cpo == empty_option)
16097 p_cpo = save_cpo;
16098 else
16099 /* Darn, evaluating the {skip} expression changed the value. */
16100 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016101
16102 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103}
16104
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016105/*
16106 * "searchpos()" function
16107 */
16108 static void
16109f_searchpos(argvars, rettv)
16110 typval_T *argvars;
16111 typval_T *rettv;
16112{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016113 pos_T match_pos;
16114 int lnum = 0;
16115 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016116 int n;
16117 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016118
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016119 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016120 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016121
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016122 n = search_cmn(argvars, &match_pos, &flags);
16123 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016124 {
16125 lnum = match_pos.lnum;
16126 col = match_pos.col;
16127 }
16128
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016129 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16130 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016131 if (flags & SP_SUBPAT)
16132 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016133}
16134
16135
Bram Moolenaar0d660222005-01-07 21:51:51 +000016136 static void
16137f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016138 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016141#ifdef FEAT_CLIENTSERVER
16142 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016143 char_u *server = get_tv_string_chk(&argvars[0]);
16144 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016145
Bram Moolenaar0d660222005-01-07 21:51:51 +000016146 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016147 if (server == NULL || reply == NULL)
16148 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016149 if (check_restricted() || check_secure())
16150 return;
16151# ifdef FEAT_X11
16152 if (check_connection() == FAIL)
16153 return;
16154# endif
16155
16156 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016158 EMSG(_("E258: Unable to send to client"));
16159 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016161 rettv->vval.v_number = 0;
16162#else
16163 rettv->vval.v_number = -1;
16164#endif
16165}
16166
Bram Moolenaar0d660222005-01-07 21:51:51 +000016167 static void
16168f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016169 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016170 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016171{
16172 char_u *r = NULL;
16173
16174#ifdef FEAT_CLIENTSERVER
16175# ifdef WIN32
16176 r = serverGetVimNames();
16177# else
16178 make_connection();
16179 if (X_DISPLAY != NULL)
16180 r = serverGetVimNames(X_DISPLAY);
16181# endif
16182#endif
16183 rettv->v_type = VAR_STRING;
16184 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185}
16186
16187/*
16188 * "setbufvar()" function
16189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016191f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016192 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016193 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016194{
16195 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016197 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016198 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199 char_u nbuf[NUMBUFLEN];
16200
16201 if (check_restricted() || check_secure())
16202 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016203 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16204 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016205 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 varp = &argvars[2];
16207
16208 if (buf != NULL && varname != NULL && varp != NULL)
16209 {
16210 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016211 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212
16213 if (*varname == '&')
16214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016215 long numval;
16216 char_u *strval;
16217 int error = FALSE;
16218
Bram Moolenaar071d4272004-06-13 20:20:40 +000016219 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016220 numval = get_tv_number_chk(varp, &error);
16221 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016222 if (!error && strval != NULL)
16223 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224 }
16225 else
16226 {
16227 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16228 if (bufvarname != NULL)
16229 {
16230 STRCPY(bufvarname, "b:");
16231 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016232 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233 vim_free(bufvarname);
16234 }
16235 }
16236
16237 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240}
16241
16242/*
16243 * "setcmdpos()" function
16244 */
16245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016246f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016247 typval_T *argvars;
16248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016249{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016250 int pos = (int)get_tv_number(&argvars[0]) - 1;
16251
16252 if (pos >= 0)
16253 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254}
16255
16256/*
16257 * "setline()" function
16258 */
16259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016260f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016261 typval_T *argvars;
16262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263{
16264 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016265 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016266 list_T *l = NULL;
16267 listitem_T *li = NULL;
16268 long added = 0;
16269 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016271 lnum = get_tv_lnum(&argvars[0]);
16272 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016274 l = argvars[1].vval.v_list;
16275 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016277 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016278 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016279
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016280 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016281 for (;;)
16282 {
16283 if (l != NULL)
16284 {
16285 /* list argument, get next string */
16286 if (li == NULL)
16287 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016288 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016289 li = li->li_next;
16290 }
16291
16292 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016293 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016294 break;
16295 if (lnum <= curbuf->b_ml.ml_line_count)
16296 {
16297 /* existing line, replace it */
16298 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16299 {
16300 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016301 if (lnum == curwin->w_cursor.lnum)
16302 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016303 rettv->vval.v_number = 0; /* OK */
16304 }
16305 }
16306 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16307 {
16308 /* lnum is one past the last line, append the line */
16309 ++added;
16310 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16311 rettv->vval.v_number = 0; /* OK */
16312 }
16313
16314 if (l == NULL) /* only one string argument */
16315 break;
16316 ++lnum;
16317 }
16318
16319 if (added > 0)
16320 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321}
16322
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016323static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16324
Bram Moolenaar071d4272004-06-13 20:20:40 +000016325/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016326 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016327 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016328 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016329set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016330 win_T *wp UNUSED;
16331 typval_T *list_arg UNUSED;
16332 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016333 typval_T *rettv;
16334{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016335#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016336 char_u *act;
16337 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016338#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016339
Bram Moolenaar2641f772005-03-25 21:58:17 +000016340 rettv->vval.v_number = -1;
16341
16342#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016343 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016344 EMSG(_(e_listreq));
16345 else
16346 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016347 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016348
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016349 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016350 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016351 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016352 if (act == NULL)
16353 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016354 if (*act == 'a' || *act == 'r')
16355 action = *act;
16356 }
16357
Bram Moolenaar81484f42012-12-05 15:16:47 +010016358 if (l != NULL && set_errorlist(wp, l, action,
16359 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016360 rettv->vval.v_number = 0;
16361 }
16362#endif
16363}
16364
16365/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016366 * "setloclist()" function
16367 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016368 static void
16369f_setloclist(argvars, rettv)
16370 typval_T *argvars;
16371 typval_T *rettv;
16372{
16373 win_T *win;
16374
16375 rettv->vval.v_number = -1;
16376
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016377 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016378 if (win != NULL)
16379 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16380}
16381
16382/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016383 * "setmatches()" function
16384 */
16385 static void
16386f_setmatches(argvars, rettv)
16387 typval_T *argvars;
16388 typval_T *rettv;
16389{
16390#ifdef FEAT_SEARCH_EXTRA
16391 list_T *l;
16392 listitem_T *li;
16393 dict_T *d;
16394
16395 rettv->vval.v_number = -1;
16396 if (argvars[0].v_type != VAR_LIST)
16397 {
16398 EMSG(_(e_listreq));
16399 return;
16400 }
16401 if ((l = argvars[0].vval.v_list) != NULL)
16402 {
16403
16404 /* To some extent make sure that we are dealing with a list from
16405 * "getmatches()". */
16406 li = l->lv_first;
16407 while (li != NULL)
16408 {
16409 if (li->li_tv.v_type != VAR_DICT
16410 || (d = li->li_tv.vval.v_dict) == NULL)
16411 {
16412 EMSG(_(e_invarg));
16413 return;
16414 }
16415 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16416 && dict_find(d, (char_u *)"pattern", -1) != NULL
16417 && dict_find(d, (char_u *)"priority", -1) != NULL
16418 && dict_find(d, (char_u *)"id", -1) != NULL))
16419 {
16420 EMSG(_(e_invarg));
16421 return;
16422 }
16423 li = li->li_next;
16424 }
16425
16426 clear_matches(curwin);
16427 li = l->lv_first;
16428 while (li != NULL)
16429 {
16430 d = li->li_tv.vval.v_dict;
16431 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16432 get_dict_string(d, (char_u *)"pattern", FALSE),
16433 (int)get_dict_number(d, (char_u *)"priority"),
16434 (int)get_dict_number(d, (char_u *)"id"));
16435 li = li->li_next;
16436 }
16437 rettv->vval.v_number = 0;
16438 }
16439#endif
16440}
16441
16442/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016443 * "setpos()" function
16444 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016445 static void
16446f_setpos(argvars, rettv)
16447 typval_T *argvars;
16448 typval_T *rettv;
16449{
16450 pos_T pos;
16451 int fnum;
16452 char_u *name;
16453
Bram Moolenaar08250432008-02-13 11:42:46 +000016454 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016455 name = get_tv_string_chk(argvars);
16456 if (name != NULL)
16457 {
16458 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16459 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016460 if (--pos.col < 0)
16461 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016462 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016463 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016464 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016465 if (fnum == curbuf->b_fnum)
16466 {
16467 curwin->w_cursor = pos;
16468 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016469 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016470 }
16471 else
16472 EMSG(_(e_invarg));
16473 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016474 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16475 {
16476 /* set mark */
16477 if (setmark_pos(name[1], &pos, fnum) == OK)
16478 rettv->vval.v_number = 0;
16479 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016480 else
16481 EMSG(_(e_invarg));
16482 }
16483 }
16484}
16485
16486/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016487 * "setqflist()" function
16488 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016489 static void
16490f_setqflist(argvars, rettv)
16491 typval_T *argvars;
16492 typval_T *rettv;
16493{
16494 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16495}
16496
16497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016498 * "setreg()" function
16499 */
16500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016501f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016502 typval_T *argvars;
16503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016504{
16505 int regname;
16506 char_u *strregname;
16507 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016508 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509 int append;
16510 char_u yank_type;
16511 long block_len;
16512
16513 block_len = -1;
16514 yank_type = MAUTO;
16515 append = FALSE;
16516
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016517 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016518 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016519
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016520 if (strregname == NULL)
16521 return; /* type error; errmsg already given */
16522 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523 if (regname == 0 || regname == '@')
16524 regname = '"';
16525 else if (regname == '=')
16526 return;
16527
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016528 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016530 stropt = get_tv_string_chk(&argvars[2]);
16531 if (stropt == NULL)
16532 return; /* type error */
16533 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016534 switch (*stropt)
16535 {
16536 case 'a': case 'A': /* append */
16537 append = TRUE;
16538 break;
16539 case 'v': case 'c': /* character-wise selection */
16540 yank_type = MCHAR;
16541 break;
16542 case 'V': case 'l': /* line-wise selection */
16543 yank_type = MLINE;
16544 break;
16545#ifdef FEAT_VISUAL
16546 case 'b': case Ctrl_V: /* block-wise selection */
16547 yank_type = MBLOCK;
16548 if (VIM_ISDIGIT(stropt[1]))
16549 {
16550 ++stropt;
16551 block_len = getdigits(&stropt) - 1;
16552 --stropt;
16553 }
16554 break;
16555#endif
16556 }
16557 }
16558
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016559 strval = get_tv_string_chk(&argvars[1]);
16560 if (strval != NULL)
16561 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016562 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016563 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016564}
16565
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016566/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016567 * "settabvar()" function
16568 */
16569 static void
16570f_settabvar(argvars, rettv)
16571 typval_T *argvars;
16572 typval_T *rettv;
16573{
16574 tabpage_T *save_curtab;
16575 char_u *varname, *tabvarname;
16576 typval_T *varp;
16577 tabpage_T *tp;
16578
16579 rettv->vval.v_number = 0;
16580
16581 if (check_restricted() || check_secure())
16582 return;
16583
16584 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16585 varname = get_tv_string_chk(&argvars[1]);
16586 varp = &argvars[2];
16587
16588 if (tp != NULL && varname != NULL && varp != NULL)
16589 {
16590 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016591 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016592
16593 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16594 if (tabvarname != NULL)
16595 {
16596 STRCPY(tabvarname, "t:");
16597 STRCPY(tabvarname + 2, varname);
16598 set_var(tabvarname, varp, TRUE);
16599 vim_free(tabvarname);
16600 }
16601
16602 /* Restore current tabpage */
16603 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016604 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016605 }
16606}
16607
16608/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016609 * "settabwinvar()" function
16610 */
16611 static void
16612f_settabwinvar(argvars, rettv)
16613 typval_T *argvars;
16614 typval_T *rettv;
16615{
16616 setwinvar(argvars, rettv, 1);
16617}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618
16619/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016620 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016623f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016624 typval_T *argvars;
16625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016627 setwinvar(argvars, rettv, 0);
16628}
16629
16630/*
16631 * "setwinvar()" and "settabwinvar()" functions
16632 */
16633 static void
16634setwinvar(argvars, rettv, off)
16635 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016636 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016637 int off;
16638{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639 win_T *win;
16640#ifdef FEAT_WINDOWS
16641 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016642 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643#endif
16644 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016645 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016646 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016647 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648
16649 if (check_restricted() || check_secure())
16650 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016651
16652#ifdef FEAT_WINDOWS
16653 if (off == 1)
16654 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16655 else
16656 tp = curtab;
16657#endif
16658 win = find_win_by_nr(&argvars[off], tp);
16659 varname = get_tv_string_chk(&argvars[off + 1]);
16660 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016661
16662 if (win != NULL && varname != NULL && varp != NULL)
16663 {
16664#ifdef FEAT_WINDOWS
16665 /* set curwin to be our win, temporarily */
16666 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016667 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016668 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016669 if (!win_valid(win))
16670 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016671 curwin = win;
16672 curbuf = curwin->w_buffer;
16673#endif
16674
16675 if (*varname == '&')
16676 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016677 long numval;
16678 char_u *strval;
16679 int error = FALSE;
16680
Bram Moolenaar071d4272004-06-13 20:20:40 +000016681 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016682 numval = get_tv_number_chk(varp, &error);
16683 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016684 if (!error && strval != NULL)
16685 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016686 }
16687 else
16688 {
16689 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16690 if (winvarname != NULL)
16691 {
16692 STRCPY(winvarname, "w:");
16693 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016694 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016695 vim_free(winvarname);
16696 }
16697 }
16698
16699#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016700 /* Restore current tabpage and window, if still valid (autocomands can
16701 * make them invalid). */
16702 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016703 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704 if (win_valid(save_curwin))
16705 {
16706 curwin = save_curwin;
16707 curbuf = curwin->w_buffer;
16708 }
16709#endif
16710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711}
16712
16713/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016714 * "shellescape({string})" function
16715 */
16716 static void
16717f_shellescape(argvars, rettv)
16718 typval_T *argvars;
16719 typval_T *rettv;
16720{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016721 rettv->vval.v_string = vim_strsave_shellescape(
16722 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016723 rettv->v_type = VAR_STRING;
16724}
16725
16726/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016727 * shiftwidth() function
16728 */
16729 static void
16730f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016731 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016732 typval_T *rettv;
16733{
16734 rettv->vval.v_number = get_sw_value();
16735}
16736
16737/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016738 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739 */
16740 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016741f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016742 typval_T *argvars;
16743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016744{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016745 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746
Bram Moolenaar0d660222005-01-07 21:51:51 +000016747 p = get_tv_string(&argvars[0]);
16748 rettv->vval.v_string = vim_strsave(p);
16749 simplify_filename(rettv->vval.v_string); /* simplify in place */
16750 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016751}
16752
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016753#ifdef FEAT_FLOAT
16754/*
16755 * "sin()" function
16756 */
16757 static void
16758f_sin(argvars, rettv)
16759 typval_T *argvars;
16760 typval_T *rettv;
16761{
16762 float_T f;
16763
16764 rettv->v_type = VAR_FLOAT;
16765 if (get_float_arg(argvars, &f) == OK)
16766 rettv->vval.v_float = sin(f);
16767 else
16768 rettv->vval.v_float = 0.0;
16769}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016770
16771/*
16772 * "sinh()" function
16773 */
16774 static void
16775f_sinh(argvars, rettv)
16776 typval_T *argvars;
16777 typval_T *rettv;
16778{
16779 float_T f;
16780
16781 rettv->v_type = VAR_FLOAT;
16782 if (get_float_arg(argvars, &f) == OK)
16783 rettv->vval.v_float = sinh(f);
16784 else
16785 rettv->vval.v_float = 0.0;
16786}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016787#endif
16788
Bram Moolenaar0d660222005-01-07 21:51:51 +000016789static int
16790#ifdef __BORLANDC__
16791 _RTLENTRYF
16792#endif
16793 item_compare __ARGS((const void *s1, const void *s2));
16794static int
16795#ifdef __BORLANDC__
16796 _RTLENTRYF
16797#endif
16798 item_compare2 __ARGS((const void *s1, const void *s2));
16799
16800static int item_compare_ic;
16801static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016802static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016803static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016804#define ITEM_COMPARE_FAIL 999
16805
Bram Moolenaar071d4272004-06-13 20:20:40 +000016806/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016807 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016808 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016809 static int
16810#ifdef __BORLANDC__
16811_RTLENTRYF
16812#endif
16813item_compare(s1, s2)
16814 const void *s1;
16815 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016817 char_u *p1, *p2;
16818 char_u *tofree1, *tofree2;
16819 int res;
16820 char_u numbuf1[NUMBUFLEN];
16821 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016822
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016823 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16824 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016825 if (p1 == NULL)
16826 p1 = (char_u *)"";
16827 if (p2 == NULL)
16828 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016829 if (item_compare_ic)
16830 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016831 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016832 res = STRCMP(p1, p2);
16833 vim_free(tofree1);
16834 vim_free(tofree2);
16835 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836}
16837
16838 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016839#ifdef __BORLANDC__
16840_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016842item_compare2(s1, s2)
16843 const void *s1;
16844 const void *s2;
16845{
16846 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016847 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016848 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016849 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016851 /* shortcut after failure in previous call; compare all items equal */
16852 if (item_compare_func_err)
16853 return 0;
16854
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016855 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16856 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016857 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16858 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016859
16860 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016861 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016862 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16863 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016864 clear_tv(&argv[0]);
16865 clear_tv(&argv[1]);
16866
16867 if (res == FAIL)
16868 res = ITEM_COMPARE_FAIL;
16869 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016870 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16871 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016872 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016873 clear_tv(&rettv);
16874 return res;
16875}
16876
16877/*
16878 * "sort({list})" function
16879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016881f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016882 typval_T *argvars;
16883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884{
Bram Moolenaar33570922005-01-25 22:26:29 +000016885 list_T *l;
16886 listitem_T *li;
16887 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016888 long len;
16889 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890
Bram Moolenaar0d660222005-01-07 21:51:51 +000016891 if (argvars[0].v_type != VAR_LIST)
16892 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016893 else
16894 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016895 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016896 if (l == NULL || tv_check_lock(l->lv_lock,
16897 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016898 return;
16899 rettv->vval.v_list = l;
16900 rettv->v_type = VAR_LIST;
16901 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902
Bram Moolenaar0d660222005-01-07 21:51:51 +000016903 len = list_len(l);
16904 if (len <= 1)
16905 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906
Bram Moolenaar0d660222005-01-07 21:51:51 +000016907 item_compare_ic = FALSE;
16908 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016909 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016910 if (argvars[1].v_type != VAR_UNKNOWN)
16911 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016912 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016913 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016914 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016915 else
16916 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016917 int error = FALSE;
16918
16919 i = get_tv_number_chk(&argvars[1], &error);
16920 if (error)
16921 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016922 if (i == 1)
16923 item_compare_ic = TRUE;
16924 else
16925 item_compare_func = get_tv_string(&argvars[1]);
16926 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016927
16928 if (argvars[2].v_type != VAR_UNKNOWN)
16929 {
16930 /* optional third argument: {dict} */
16931 if (argvars[2].v_type != VAR_DICT)
16932 {
16933 EMSG(_(e_dictreq));
16934 return;
16935 }
16936 item_compare_selfdict = argvars[2].vval.v_dict;
16937 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016939
Bram Moolenaar0d660222005-01-07 21:51:51 +000016940 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016941 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016942 if (ptrs == NULL)
16943 return;
16944 i = 0;
16945 for (li = l->lv_first; li != NULL; li = li->li_next)
16946 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016948 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016949 /* test the compare function */
16950 if (item_compare_func != NULL
16951 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16952 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016953 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016955 {
16956 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016957 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016958 item_compare_func == NULL ? item_compare : item_compare2);
16959
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016960 if (!item_compare_func_err)
16961 {
16962 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016963 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016964 l->lv_len = 0;
16965 for (i = 0; i < len; ++i)
16966 list_append(l, ptrs[i]);
16967 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016968 }
16969
16970 vim_free(ptrs);
16971 }
16972}
16973
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016974/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016975 * "soundfold({word})" function
16976 */
16977 static void
16978f_soundfold(argvars, rettv)
16979 typval_T *argvars;
16980 typval_T *rettv;
16981{
16982 char_u *s;
16983
16984 rettv->v_type = VAR_STRING;
16985 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016986#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016987 rettv->vval.v_string = eval_soundfold(s);
16988#else
16989 rettv->vval.v_string = vim_strsave(s);
16990#endif
16991}
16992
16993/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016994 * "spellbadword()" function
16995 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016996 static void
16997f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016998 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016999 typval_T *rettv;
17000{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017001 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017002 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017003 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017004
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017005 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017006 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017007
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017008#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017009 if (argvars[0].v_type == VAR_UNKNOWN)
17010 {
17011 /* Find the start and length of the badly spelled word. */
17012 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17013 if (len != 0)
17014 word = ml_get_cursor();
17015 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017016 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017017 {
17018 char_u *str = get_tv_string_chk(&argvars[0]);
17019 int capcol = -1;
17020
17021 if (str != NULL)
17022 {
17023 /* Check the argument for spelling. */
17024 while (*str != NUL)
17025 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017026 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017027 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017028 {
17029 word = str;
17030 break;
17031 }
17032 str += len;
17033 }
17034 }
17035 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017036#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017037
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017038 list_append_string(rettv->vval.v_list, word, len);
17039 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017040 attr == HLF_SPB ? "bad" :
17041 attr == HLF_SPR ? "rare" :
17042 attr == HLF_SPL ? "local" :
17043 attr == HLF_SPC ? "caps" :
17044 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017045}
17046
17047/*
17048 * "spellsuggest()" function
17049 */
17050 static void
17051f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017052 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017053 typval_T *rettv;
17054{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017055#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017056 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017057 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017058 int maxcount;
17059 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017060 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017061 listitem_T *li;
17062 int need_capital = FALSE;
17063#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017064
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017065 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017066 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017067
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017068#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017069 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017070 {
17071 str = get_tv_string(&argvars[0]);
17072 if (argvars[1].v_type != VAR_UNKNOWN)
17073 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017074 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017075 if (maxcount <= 0)
17076 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017077 if (argvars[2].v_type != VAR_UNKNOWN)
17078 {
17079 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17080 if (typeerr)
17081 return;
17082 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017083 }
17084 else
17085 maxcount = 25;
17086
Bram Moolenaar4770d092006-01-12 23:22:24 +000017087 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017088
17089 for (i = 0; i < ga.ga_len; ++i)
17090 {
17091 str = ((char_u **)ga.ga_data)[i];
17092
17093 li = listitem_alloc();
17094 if (li == NULL)
17095 vim_free(str);
17096 else
17097 {
17098 li->li_tv.v_type = VAR_STRING;
17099 li->li_tv.v_lock = 0;
17100 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017101 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017102 }
17103 }
17104 ga_clear(&ga);
17105 }
17106#endif
17107}
17108
Bram Moolenaar0d660222005-01-07 21:51:51 +000017109 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017110f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017111 typval_T *argvars;
17112 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017113{
17114 char_u *str;
17115 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017116 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017117 regmatch_T regmatch;
17118 char_u patbuf[NUMBUFLEN];
17119 char_u *save_cpo;
17120 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017121 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017122 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017123 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017124
17125 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17126 save_cpo = p_cpo;
17127 p_cpo = (char_u *)"";
17128
17129 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017130 if (argvars[1].v_type != VAR_UNKNOWN)
17131 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017132 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17133 if (pat == NULL)
17134 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017135 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017136 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017137 }
17138 if (pat == NULL || *pat == NUL)
17139 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017140
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017141 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017143 if (typeerr)
17144 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145
Bram Moolenaar0d660222005-01-07 21:51:51 +000017146 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17147 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017149 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017150 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017151 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017152 if (*str == NUL)
17153 match = FALSE; /* empty item at the end */
17154 else
17155 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017156 if (match)
17157 end = regmatch.startp[0];
17158 else
17159 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017160 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17161 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017162 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017163 if (list_append_string(rettv->vval.v_list, str,
17164 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017165 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017166 }
17167 if (!match)
17168 break;
17169 /* Advance to just after the match. */
17170 if (regmatch.endp[0] > str)
17171 col = 0;
17172 else
17173 {
17174 /* Don't get stuck at the same match. */
17175#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017176 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017177#else
17178 col = 1;
17179#endif
17180 }
17181 str = regmatch.endp[0];
17182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183
Bram Moolenaar0d660222005-01-07 21:51:51 +000017184 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186
Bram Moolenaar0d660222005-01-07 21:51:51 +000017187 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188}
17189
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017190#ifdef FEAT_FLOAT
17191/*
17192 * "sqrt()" function
17193 */
17194 static void
17195f_sqrt(argvars, rettv)
17196 typval_T *argvars;
17197 typval_T *rettv;
17198{
17199 float_T f;
17200
17201 rettv->v_type = VAR_FLOAT;
17202 if (get_float_arg(argvars, &f) == OK)
17203 rettv->vval.v_float = sqrt(f);
17204 else
17205 rettv->vval.v_float = 0.0;
17206}
17207
17208/*
17209 * "str2float()" function
17210 */
17211 static void
17212f_str2float(argvars, rettv)
17213 typval_T *argvars;
17214 typval_T *rettv;
17215{
17216 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17217
17218 if (*p == '+')
17219 p = skipwhite(p + 1);
17220 (void)string2float(p, &rettv->vval.v_float);
17221 rettv->v_type = VAR_FLOAT;
17222}
17223#endif
17224
Bram Moolenaar2c932302006-03-18 21:42:09 +000017225/*
17226 * "str2nr()" function
17227 */
17228 static void
17229f_str2nr(argvars, rettv)
17230 typval_T *argvars;
17231 typval_T *rettv;
17232{
17233 int base = 10;
17234 char_u *p;
17235 long n;
17236
17237 if (argvars[1].v_type != VAR_UNKNOWN)
17238 {
17239 base = get_tv_number(&argvars[1]);
17240 if (base != 8 && base != 10 && base != 16)
17241 {
17242 EMSG(_(e_invarg));
17243 return;
17244 }
17245 }
17246
17247 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017248 if (*p == '+')
17249 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017250 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17251 rettv->vval.v_number = n;
17252}
17253
Bram Moolenaar071d4272004-06-13 20:20:40 +000017254#ifdef HAVE_STRFTIME
17255/*
17256 * "strftime({format}[, {time}])" function
17257 */
17258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017259f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017260 typval_T *argvars;
17261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262{
17263 char_u result_buf[256];
17264 struct tm *curtime;
17265 time_t seconds;
17266 char_u *p;
17267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017268 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017270 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017271 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017272 seconds = time(NULL);
17273 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017274 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275 curtime = localtime(&seconds);
17276 /* MSVC returns NULL for an invalid value of seconds. */
17277 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017278 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279 else
17280 {
17281# ifdef FEAT_MBYTE
17282 vimconv_T conv;
17283 char_u *enc;
17284
17285 conv.vc_type = CONV_NONE;
17286 enc = enc_locale();
17287 convert_setup(&conv, p_enc, enc);
17288 if (conv.vc_type != CONV_NONE)
17289 p = string_convert(&conv, p, NULL);
17290# endif
17291 if (p != NULL)
17292 (void)strftime((char *)result_buf, sizeof(result_buf),
17293 (char *)p, curtime);
17294 else
17295 result_buf[0] = NUL;
17296
17297# ifdef FEAT_MBYTE
17298 if (conv.vc_type != CONV_NONE)
17299 vim_free(p);
17300 convert_setup(&conv, enc, p_enc);
17301 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017302 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303 else
17304# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017305 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306
17307# ifdef FEAT_MBYTE
17308 /* Release conversion descriptors */
17309 convert_setup(&conv, NULL, NULL);
17310 vim_free(enc);
17311# endif
17312 }
17313}
17314#endif
17315
17316/*
17317 * "stridx()" function
17318 */
17319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017320f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017321 typval_T *argvars;
17322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323{
17324 char_u buf[NUMBUFLEN];
17325 char_u *needle;
17326 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017327 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017328 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017329 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017331 needle = get_tv_string_chk(&argvars[1]);
17332 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017333 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017334 if (needle == NULL || haystack == NULL)
17335 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336
Bram Moolenaar33570922005-01-25 22:26:29 +000017337 if (argvars[2].v_type != VAR_UNKNOWN)
17338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017339 int error = FALSE;
17340
17341 start_idx = get_tv_number_chk(&argvars[2], &error);
17342 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017343 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017344 if (start_idx >= 0)
17345 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017346 }
17347
17348 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17349 if (pos != NULL)
17350 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351}
17352
17353/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017354 * "string()" function
17355 */
17356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017357f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017358 typval_T *argvars;
17359 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017360{
17361 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017362 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017363
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017364 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017365 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017366 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017367 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017368 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369}
17370
17371/*
17372 * "strlen()" function
17373 */
17374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017375f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017376 typval_T *argvars;
17377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017379 rettv->vval.v_number = (varnumber_T)(STRLEN(
17380 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381}
17382
17383/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017384 * "strchars()" function
17385 */
17386 static void
17387f_strchars(argvars, rettv)
17388 typval_T *argvars;
17389 typval_T *rettv;
17390{
17391 char_u *s = get_tv_string(&argvars[0]);
17392#ifdef FEAT_MBYTE
17393 varnumber_T len = 0;
17394
17395 while (*s != NUL)
17396 {
17397 mb_cptr2char_adv(&s);
17398 ++len;
17399 }
17400 rettv->vval.v_number = len;
17401#else
17402 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17403#endif
17404}
17405
17406/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017407 * "strdisplaywidth()" function
17408 */
17409 static void
17410f_strdisplaywidth(argvars, rettv)
17411 typval_T *argvars;
17412 typval_T *rettv;
17413{
17414 char_u *s = get_tv_string(&argvars[0]);
17415 int col = 0;
17416
17417 if (argvars[1].v_type != VAR_UNKNOWN)
17418 col = get_tv_number(&argvars[1]);
17419
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017420 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017421}
17422
17423/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017424 * "strwidth()" function
17425 */
17426 static void
17427f_strwidth(argvars, rettv)
17428 typval_T *argvars;
17429 typval_T *rettv;
17430{
17431 char_u *s = get_tv_string(&argvars[0]);
17432
17433 rettv->vval.v_number = (varnumber_T)(
17434#ifdef FEAT_MBYTE
17435 mb_string2cells(s, -1)
17436#else
17437 STRLEN(s)
17438#endif
17439 );
17440}
17441
17442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017443 * "strpart()" function
17444 */
17445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017446f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017447 typval_T *argvars;
17448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017449{
17450 char_u *p;
17451 int n;
17452 int len;
17453 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017454 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017456 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457 slen = (int)STRLEN(p);
17458
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017459 n = get_tv_number_chk(&argvars[1], &error);
17460 if (error)
17461 len = 0;
17462 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017463 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017464 else
17465 len = slen - n; /* default len: all bytes that are available. */
17466
17467 /*
17468 * Only return the overlap between the specified part and the actual
17469 * string.
17470 */
17471 if (n < 0)
17472 {
17473 len += n;
17474 n = 0;
17475 }
17476 else if (n > slen)
17477 n = slen;
17478 if (len < 0)
17479 len = 0;
17480 else if (n + len > slen)
17481 len = slen - n;
17482
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017483 rettv->v_type = VAR_STRING;
17484 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017485}
17486
17487/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017488 * "strridx()" function
17489 */
17490 static void
17491f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017492 typval_T *argvars;
17493 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017494{
17495 char_u buf[NUMBUFLEN];
17496 char_u *needle;
17497 char_u *haystack;
17498 char_u *rest;
17499 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017500 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017501
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017502 needle = get_tv_string_chk(&argvars[1]);
17503 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017504
17505 rettv->vval.v_number = -1;
17506 if (needle == NULL || haystack == NULL)
17507 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017508
17509 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017510 if (argvars[2].v_type != VAR_UNKNOWN)
17511 {
17512 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017513 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017514 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017515 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017516 }
17517 else
17518 end_idx = haystack_len;
17519
Bram Moolenaar0d660222005-01-07 21:51:51 +000017520 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017521 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017522 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017523 lastmatch = haystack + end_idx;
17524 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017525 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017526 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017527 for (rest = haystack; *rest != '\0'; ++rest)
17528 {
17529 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017530 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017531 break;
17532 lastmatch = rest;
17533 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017534 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017535
17536 if (lastmatch == NULL)
17537 rettv->vval.v_number = -1;
17538 else
17539 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17540}
17541
17542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543 * "strtrans()" function
17544 */
17545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017546f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017547 typval_T *argvars;
17548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017549{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017550 rettv->v_type = VAR_STRING;
17551 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552}
17553
17554/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017555 * "submatch()" function
17556 */
17557 static void
17558f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017559 typval_T *argvars;
17560 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017561{
17562 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017563 rettv->vval.v_string =
17564 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017565}
17566
17567/*
17568 * "substitute()" function
17569 */
17570 static void
17571f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017572 typval_T *argvars;
17573 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017574{
17575 char_u patbuf[NUMBUFLEN];
17576 char_u subbuf[NUMBUFLEN];
17577 char_u flagsbuf[NUMBUFLEN];
17578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017579 char_u *str = get_tv_string_chk(&argvars[0]);
17580 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17581 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17582 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17583
Bram Moolenaar0d660222005-01-07 21:51:51 +000017584 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017585 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17586 rettv->vval.v_string = NULL;
17587 else
17588 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017589}
17590
17591/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017592 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017595f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017596 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017597 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598{
17599 int id = 0;
17600#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017601 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602 long col;
17603 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017604 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017606 lnum = get_tv_lnum(argvars); /* -1 on type error */
17607 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17608 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017610 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017611 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017612 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613#endif
17614
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017615 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616}
17617
17618/*
17619 * "synIDattr(id, what [, mode])" function
17620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017622f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017623 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625{
17626 char_u *p = NULL;
17627#ifdef FEAT_SYN_HL
17628 int id;
17629 char_u *what;
17630 char_u *mode;
17631 char_u modebuf[NUMBUFLEN];
17632 int modec;
17633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017634 id = get_tv_number(&argvars[0]);
17635 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017636 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017638 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017640 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017641 modec = 0; /* replace invalid with current */
17642 }
17643 else
17644 {
17645#ifdef FEAT_GUI
17646 if (gui.in_use)
17647 modec = 'g';
17648 else
17649#endif
17650 if (t_colors > 1)
17651 modec = 'c';
17652 else
17653 modec = 't';
17654 }
17655
17656
17657 switch (TOLOWER_ASC(what[0]))
17658 {
17659 case 'b':
17660 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17661 p = highlight_color(id, what, modec);
17662 else /* bold */
17663 p = highlight_has_attr(id, HL_BOLD, modec);
17664 break;
17665
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017666 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667 p = highlight_color(id, what, modec);
17668 break;
17669
17670 case 'i':
17671 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17672 p = highlight_has_attr(id, HL_INVERSE, modec);
17673 else /* italic */
17674 p = highlight_has_attr(id, HL_ITALIC, modec);
17675 break;
17676
17677 case 'n': /* name */
17678 p = get_highlight_name(NULL, id - 1);
17679 break;
17680
17681 case 'r': /* reverse */
17682 p = highlight_has_attr(id, HL_INVERSE, modec);
17683 break;
17684
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017685 case 's':
17686 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17687 p = highlight_color(id, what, modec);
17688 else /* standout */
17689 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690 break;
17691
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017692 case 'u':
17693 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17694 /* underline */
17695 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17696 else
17697 /* undercurl */
17698 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699 break;
17700 }
17701
17702 if (p != NULL)
17703 p = vim_strsave(p);
17704#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017705 rettv->v_type = VAR_STRING;
17706 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707}
17708
17709/*
17710 * "synIDtrans(id)" function
17711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017713f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017714 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716{
17717 int id;
17718
17719#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017720 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017721
17722 if (id > 0)
17723 id = syn_get_final_id(id);
17724 else
17725#endif
17726 id = 0;
17727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017728 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729}
17730
17731/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017732 * "synconcealed(lnum, col)" function
17733 */
17734 static void
17735f_synconcealed(argvars, rettv)
17736 typval_T *argvars UNUSED;
17737 typval_T *rettv;
17738{
17739#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17740 long lnum;
17741 long col;
17742 int syntax_flags = 0;
17743 int cchar;
17744 int matchid = 0;
17745 char_u str[NUMBUFLEN];
17746#endif
17747
17748 rettv->v_type = VAR_LIST;
17749 rettv->vval.v_list = NULL;
17750
17751#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17752 lnum = get_tv_lnum(argvars); /* -1 on type error */
17753 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17754
17755 vim_memset(str, NUL, sizeof(str));
17756
17757 if (rettv_list_alloc(rettv) != FAIL)
17758 {
17759 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17760 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17761 && curwin->w_p_cole > 0)
17762 {
17763 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17764 syntax_flags = get_syntax_info(&matchid);
17765
17766 /* get the conceal character */
17767 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17768 {
17769 cchar = syn_get_sub_char();
17770 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17771 cchar = lcs_conceal;
17772 if (cchar != NUL)
17773 {
17774# ifdef FEAT_MBYTE
17775 if (has_mbyte)
17776 (*mb_char2bytes)(cchar, str);
17777 else
17778# endif
17779 str[0] = cchar;
17780 }
17781 }
17782 }
17783
17784 list_append_number(rettv->vval.v_list,
17785 (syntax_flags & HL_CONCEAL) != 0);
17786 /* -1 to auto-determine strlen */
17787 list_append_string(rettv->vval.v_list, str, -1);
17788 list_append_number(rettv->vval.v_list, matchid);
17789 }
17790#endif
17791}
17792
17793/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017794 * "synstack(lnum, col)" function
17795 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017796 static void
17797f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017798 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017799 typval_T *rettv;
17800{
17801#ifdef FEAT_SYN_HL
17802 long lnum;
17803 long col;
17804 int i;
17805 int id;
17806#endif
17807
17808 rettv->v_type = VAR_LIST;
17809 rettv->vval.v_list = NULL;
17810
17811#ifdef FEAT_SYN_HL
17812 lnum = get_tv_lnum(argvars); /* -1 on type error */
17813 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17814
17815 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017816 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017817 && rettv_list_alloc(rettv) != FAIL)
17818 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017819 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017820 for (i = 0; ; ++i)
17821 {
17822 id = syn_get_stack_item(i);
17823 if (id < 0)
17824 break;
17825 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17826 break;
17827 }
17828 }
17829#endif
17830}
17831
17832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017833 * "system()" function
17834 */
17835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017836f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017837 typval_T *argvars;
17838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017839{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017840 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017842 char_u *infile = NULL;
17843 char_u buf[NUMBUFLEN];
17844 int err = FALSE;
17845 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017847 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017848 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017849
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017850 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017851 {
17852 /*
17853 * Write the string to a temp file, to be used for input of the shell
17854 * command.
17855 */
17856 if ((infile = vim_tempname('i')) == NULL)
17857 {
17858 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017859 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017860 }
17861
17862 fd = mch_fopen((char *)infile, WRITEBIN);
17863 if (fd == NULL)
17864 {
17865 EMSG2(_(e_notopen), infile);
17866 goto done;
17867 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017868 p = get_tv_string_buf_chk(&argvars[1], buf);
17869 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017870 {
17871 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017872 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017873 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017874 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17875 err = TRUE;
17876 if (fclose(fd) != 0)
17877 err = TRUE;
17878 if (err)
17879 {
17880 EMSG(_("E677: Error writing temp file"));
17881 goto done;
17882 }
17883 }
17884
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017885 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17886 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017887
Bram Moolenaar071d4272004-06-13 20:20:40 +000017888#ifdef USE_CR
17889 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017890 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017891 {
17892 char_u *s;
17893
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017894 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017895 {
17896 if (*s == CAR)
17897 *s = NL;
17898 }
17899 }
17900#else
17901# ifdef USE_CRNL
17902 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017903 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904 {
17905 char_u *s, *d;
17906
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017907 d = res;
17908 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017909 {
17910 if (s[0] == CAR && s[1] == NL)
17911 ++s;
17912 *d++ = *s;
17913 }
17914 *d = NUL;
17915 }
17916# endif
17917#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017918
17919done:
17920 if (infile != NULL)
17921 {
17922 mch_remove(infile);
17923 vim_free(infile);
17924 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017925 rettv->v_type = VAR_STRING;
17926 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927}
17928
17929/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017930 * "tabpagebuflist()" function
17931 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017932 static void
17933f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017934 typval_T *argvars UNUSED;
17935 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017936{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017937#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017938 tabpage_T *tp;
17939 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017940
17941 if (argvars[0].v_type == VAR_UNKNOWN)
17942 wp = firstwin;
17943 else
17944 {
17945 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17946 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017947 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017948 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017949 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017950 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017951 for (; wp != NULL; wp = wp->w_next)
17952 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017953 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017954 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017955 }
17956#endif
17957}
17958
17959
17960/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017961 * "tabpagenr()" function
17962 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017963 static void
17964f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017965 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017966 typval_T *rettv;
17967{
17968 int nr = 1;
17969#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017970 char_u *arg;
17971
17972 if (argvars[0].v_type != VAR_UNKNOWN)
17973 {
17974 arg = get_tv_string_chk(&argvars[0]);
17975 nr = 0;
17976 if (arg != NULL)
17977 {
17978 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017979 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017980 else
17981 EMSG2(_(e_invexpr2), arg);
17982 }
17983 }
17984 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017985 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017986#endif
17987 rettv->vval.v_number = nr;
17988}
17989
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017990
17991#ifdef FEAT_WINDOWS
17992static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17993
17994/*
17995 * Common code for tabpagewinnr() and winnr().
17996 */
17997 static int
17998get_winnr(tp, argvar)
17999 tabpage_T *tp;
18000 typval_T *argvar;
18001{
18002 win_T *twin;
18003 int nr = 1;
18004 win_T *wp;
18005 char_u *arg;
18006
18007 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18008 if (argvar->v_type != VAR_UNKNOWN)
18009 {
18010 arg = get_tv_string_chk(argvar);
18011 if (arg == NULL)
18012 nr = 0; /* type error; errmsg already given */
18013 else if (STRCMP(arg, "$") == 0)
18014 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18015 else if (STRCMP(arg, "#") == 0)
18016 {
18017 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18018 if (twin == NULL)
18019 nr = 0;
18020 }
18021 else
18022 {
18023 EMSG2(_(e_invexpr2), arg);
18024 nr = 0;
18025 }
18026 }
18027
18028 if (nr > 0)
18029 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18030 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018031 {
18032 if (wp == NULL)
18033 {
18034 /* didn't find it in this tabpage */
18035 nr = 0;
18036 break;
18037 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018038 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018039 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018040 return nr;
18041}
18042#endif
18043
18044/*
18045 * "tabpagewinnr()" function
18046 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018047 static void
18048f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018049 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018050 typval_T *rettv;
18051{
18052 int nr = 1;
18053#ifdef FEAT_WINDOWS
18054 tabpage_T *tp;
18055
18056 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18057 if (tp == NULL)
18058 nr = 0;
18059 else
18060 nr = get_winnr(tp, &argvars[1]);
18061#endif
18062 rettv->vval.v_number = nr;
18063}
18064
18065
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018066/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018067 * "tagfiles()" function
18068 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018069 static void
18070f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018071 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018072 typval_T *rettv;
18073{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018074 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018075 tagname_T tn;
18076 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018077
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018078 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018079 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018080 fname = alloc(MAXPATHL);
18081 if (fname == NULL)
18082 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018083
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018084 for (first = TRUE; ; first = FALSE)
18085 if (get_tagfname(&tn, first, fname) == FAIL
18086 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018087 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018088 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018089 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018090}
18091
18092/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018093 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018094 */
18095 static void
18096f_taglist(argvars, rettv)
18097 typval_T *argvars;
18098 typval_T *rettv;
18099{
18100 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018101
18102 tag_pattern = get_tv_string(&argvars[0]);
18103
18104 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018105 if (*tag_pattern == NUL)
18106 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018107
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018108 if (rettv_list_alloc(rettv) == OK)
18109 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018110}
18111
18112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 * "tempname()" function
18114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018116f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018117 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119{
18120 static int x = 'A';
18121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018122 rettv->v_type = VAR_STRING;
18123 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018124
18125 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18126 * names. Skip 'I' and 'O', they are used for shell redirection. */
18127 do
18128 {
18129 if (x == 'Z')
18130 x = '0';
18131 else if (x == '9')
18132 x = 'A';
18133 else
18134 {
18135#ifdef EBCDIC
18136 if (x == 'I')
18137 x = 'J';
18138 else if (x == 'R')
18139 x = 'S';
18140 else
18141#endif
18142 ++x;
18143 }
18144 } while (x == 'I' || x == 'O');
18145}
18146
18147/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018148 * "test(list)" function: Just checking the walls...
18149 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018150 static void
18151f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018152 typval_T *argvars UNUSED;
18153 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018154{
18155 /* Used for unit testing. Change the code below to your liking. */
18156#if 0
18157 listitem_T *li;
18158 list_T *l;
18159 char_u *bad, *good;
18160
18161 if (argvars[0].v_type != VAR_LIST)
18162 return;
18163 l = argvars[0].vval.v_list;
18164 if (l == NULL)
18165 return;
18166 li = l->lv_first;
18167 if (li == NULL)
18168 return;
18169 bad = get_tv_string(&li->li_tv);
18170 li = li->li_next;
18171 if (li == NULL)
18172 return;
18173 good = get_tv_string(&li->li_tv);
18174 rettv->vval.v_number = test_edit_score(bad, good);
18175#endif
18176}
18177
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018178#ifdef FEAT_FLOAT
18179/*
18180 * "tan()" function
18181 */
18182 static void
18183f_tan(argvars, rettv)
18184 typval_T *argvars;
18185 typval_T *rettv;
18186{
18187 float_T f;
18188
18189 rettv->v_type = VAR_FLOAT;
18190 if (get_float_arg(argvars, &f) == OK)
18191 rettv->vval.v_float = tan(f);
18192 else
18193 rettv->vval.v_float = 0.0;
18194}
18195
18196/*
18197 * "tanh()" function
18198 */
18199 static void
18200f_tanh(argvars, rettv)
18201 typval_T *argvars;
18202 typval_T *rettv;
18203{
18204 float_T f;
18205
18206 rettv->v_type = VAR_FLOAT;
18207 if (get_float_arg(argvars, &f) == OK)
18208 rettv->vval.v_float = tanh(f);
18209 else
18210 rettv->vval.v_float = 0.0;
18211}
18212#endif
18213
Bram Moolenaard52d9742005-08-21 22:20:28 +000018214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018215 * "tolower(string)" function
18216 */
18217 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018218f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018219 typval_T *argvars;
18220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221{
18222 char_u *p;
18223
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018224 p = vim_strsave(get_tv_string(&argvars[0]));
18225 rettv->v_type = VAR_STRING;
18226 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018227
18228 if (p != NULL)
18229 while (*p != NUL)
18230 {
18231#ifdef FEAT_MBYTE
18232 int l;
18233
18234 if (enc_utf8)
18235 {
18236 int c, lc;
18237
18238 c = utf_ptr2char(p);
18239 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018240 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018241 /* TODO: reallocate string when byte count changes. */
18242 if (utf_char2len(lc) == l)
18243 utf_char2bytes(lc, p);
18244 p += l;
18245 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018246 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247 p += l; /* skip multi-byte character */
18248 else
18249#endif
18250 {
18251 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18252 ++p;
18253 }
18254 }
18255}
18256
18257/*
18258 * "toupper(string)" function
18259 */
18260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018261f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018262 typval_T *argvars;
18263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018265 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018266 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267}
18268
18269/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018270 * "tr(string, fromstr, tostr)" function
18271 */
18272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018273f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018274 typval_T *argvars;
18275 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018276{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018277 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018278 char_u *fromstr;
18279 char_u *tostr;
18280 char_u *p;
18281#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018282 int inlen;
18283 int fromlen;
18284 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018285 int idx;
18286 char_u *cpstr;
18287 int cplen;
18288 int first = TRUE;
18289#endif
18290 char_u buf[NUMBUFLEN];
18291 char_u buf2[NUMBUFLEN];
18292 garray_T ga;
18293
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018294 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018295 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18296 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018297
18298 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018299 rettv->v_type = VAR_STRING;
18300 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018301 if (fromstr == NULL || tostr == NULL)
18302 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018303 ga_init2(&ga, (int)sizeof(char), 80);
18304
18305#ifdef FEAT_MBYTE
18306 if (!has_mbyte)
18307#endif
18308 /* not multi-byte: fromstr and tostr must be the same length */
18309 if (STRLEN(fromstr) != STRLEN(tostr))
18310 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018311#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018312error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018313#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018314 EMSG2(_(e_invarg2), fromstr);
18315 ga_clear(&ga);
18316 return;
18317 }
18318
18319 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018320 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018321 {
18322#ifdef FEAT_MBYTE
18323 if (has_mbyte)
18324 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018325 inlen = (*mb_ptr2len)(in_str);
18326 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018327 cplen = inlen;
18328 idx = 0;
18329 for (p = fromstr; *p != NUL; p += fromlen)
18330 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018331 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018332 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018333 {
18334 for (p = tostr; *p != NUL; p += tolen)
18335 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018336 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018337 if (idx-- == 0)
18338 {
18339 cplen = tolen;
18340 cpstr = p;
18341 break;
18342 }
18343 }
18344 if (*p == NUL) /* tostr is shorter than fromstr */
18345 goto error;
18346 break;
18347 }
18348 ++idx;
18349 }
18350
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018351 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018352 {
18353 /* Check that fromstr and tostr have the same number of
18354 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018355 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018356 first = FALSE;
18357 for (p = tostr; *p != NUL; p += tolen)
18358 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018359 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018360 --idx;
18361 }
18362 if (idx != 0)
18363 goto error;
18364 }
18365
18366 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018367 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018368 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018369
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018370 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018371 }
18372 else
18373#endif
18374 {
18375 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018376 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018377 if (p != NULL)
18378 ga_append(&ga, tostr[p - fromstr]);
18379 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018380 ga_append(&ga, *in_str);
18381 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018382 }
18383 }
18384
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018385 /* add a terminating NUL */
18386 ga_grow(&ga, 1);
18387 ga_append(&ga, NUL);
18388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018389 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018390}
18391
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018392#ifdef FEAT_FLOAT
18393/*
18394 * "trunc({float})" function
18395 */
18396 static void
18397f_trunc(argvars, rettv)
18398 typval_T *argvars;
18399 typval_T *rettv;
18400{
18401 float_T f;
18402
18403 rettv->v_type = VAR_FLOAT;
18404 if (get_float_arg(argvars, &f) == OK)
18405 /* trunc() is not in C90, use floor() or ceil() instead. */
18406 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18407 else
18408 rettv->vval.v_float = 0.0;
18409}
18410#endif
18411
Bram Moolenaar8299df92004-07-10 09:47:34 +000018412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018413 * "type(expr)" function
18414 */
18415 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018416f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018417 typval_T *argvars;
18418 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018420 int n;
18421
18422 switch (argvars[0].v_type)
18423 {
18424 case VAR_NUMBER: n = 0; break;
18425 case VAR_STRING: n = 1; break;
18426 case VAR_FUNC: n = 2; break;
18427 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018428 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018429#ifdef FEAT_FLOAT
18430 case VAR_FLOAT: n = 5; break;
18431#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018432 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18433 }
18434 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435}
18436
18437/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018438 * "undofile(name)" function
18439 */
18440 static void
18441f_undofile(argvars, rettv)
18442 typval_T *argvars;
18443 typval_T *rettv;
18444{
18445 rettv->v_type = VAR_STRING;
18446#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018447 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018448 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018449
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018450 if (*fname == NUL)
18451 {
18452 /* If there is no file name there will be no undo file. */
18453 rettv->vval.v_string = NULL;
18454 }
18455 else
18456 {
18457 char_u *ffname = FullName_save(fname, FALSE);
18458
18459 if (ffname != NULL)
18460 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18461 vim_free(ffname);
18462 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018463 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018464#else
18465 rettv->vval.v_string = NULL;
18466#endif
18467}
18468
18469/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018470 * "undotree()" function
18471 */
18472 static void
18473f_undotree(argvars, rettv)
18474 typval_T *argvars UNUSED;
18475 typval_T *rettv;
18476{
18477 if (rettv_dict_alloc(rettv) == OK)
18478 {
18479 dict_T *dict = rettv->vval.v_dict;
18480 list_T *list;
18481
Bram Moolenaar730cde92010-06-27 05:18:54 +020018482 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018483 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018484 dict_add_nr_str(dict, "save_last",
18485 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018486 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18487 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018488 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018489
18490 list = list_alloc();
18491 if (list != NULL)
18492 {
18493 u_eval_tree(curbuf->b_u_oldhead, list);
18494 dict_add_list(dict, "entries", list);
18495 }
18496 }
18497}
18498
18499/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018500 * "values(dict)" function
18501 */
18502 static void
18503f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018504 typval_T *argvars;
18505 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018506{
18507 dict_list(argvars, rettv, 1);
18508}
18509
18510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018511 * "virtcol(string)" function
18512 */
18513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018514f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018515 typval_T *argvars;
18516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018517{
18518 colnr_T vcol = 0;
18519 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018520 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018522 fp = var2fpos(&argvars[0], FALSE, &fnum);
18523 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18524 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525 {
18526 getvvcol(curwin, fp, NULL, NULL, &vcol);
18527 ++vcol;
18528 }
18529
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018530 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531}
18532
18533/*
18534 * "visualmode()" function
18535 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018537f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018538 typval_T *argvars UNUSED;
18539 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018540{
18541#ifdef FEAT_VISUAL
18542 char_u str[2];
18543
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018544 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545 str[0] = curbuf->b_visual_mode_eval;
18546 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018547 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548
18549 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018550 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552#endif
18553}
18554
18555/*
18556 * "winbufnr(nr)" function
18557 */
18558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018559f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018560 typval_T *argvars;
18561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562{
18563 win_T *wp;
18564
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018565 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018567 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018569 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570}
18571
18572/*
18573 * "wincol()" function
18574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018576f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018577 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579{
18580 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018581 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582}
18583
18584/*
18585 * "winheight(nr)" function
18586 */
18587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018588f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018589 typval_T *argvars;
18590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018591{
18592 win_T *wp;
18593
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018594 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018596 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018598 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018599}
18600
18601/*
18602 * "winline()" function
18603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018605f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018606 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018608{
18609 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018610 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611}
18612
18613/*
18614 * "winnr()" function
18615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018617f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018618 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620{
18621 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018622
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018624 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018626 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627}
18628
18629/*
18630 * "winrestcmd()" function
18631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018633f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018634 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636{
18637#ifdef FEAT_WINDOWS
18638 win_T *wp;
18639 int winnr = 1;
18640 garray_T ga;
18641 char_u buf[50];
18642
18643 ga_init2(&ga, (int)sizeof(char), 70);
18644 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18645 {
18646 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18647 ga_concat(&ga, buf);
18648# ifdef FEAT_VERTSPLIT
18649 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18650 ga_concat(&ga, buf);
18651# endif
18652 ++winnr;
18653 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018654 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018656 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018658 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018660 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661}
18662
18663/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018664 * "winrestview()" function
18665 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018666 static void
18667f_winrestview(argvars, rettv)
18668 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018669 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018670{
18671 dict_T *dict;
18672
18673 if (argvars[0].v_type != VAR_DICT
18674 || (dict = argvars[0].vval.v_dict) == NULL)
18675 EMSG(_(e_invarg));
18676 else
18677 {
18678 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18679 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18680#ifdef FEAT_VIRTUALEDIT
18681 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18682#endif
18683 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018684 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018685
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018686 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018687#ifdef FEAT_DIFF
18688 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18689#endif
18690 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18691 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18692
18693 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018694 win_new_height(curwin, curwin->w_height);
18695# ifdef FEAT_VERTSPLIT
18696 win_new_width(curwin, W_WIDTH(curwin));
18697# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018698 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018699
18700 if (curwin->w_topline == 0)
18701 curwin->w_topline = 1;
18702 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18703 curwin->w_topline = curbuf->b_ml.ml_line_count;
18704#ifdef FEAT_DIFF
18705 check_topfill(curwin, TRUE);
18706#endif
18707 }
18708}
18709
18710/*
18711 * "winsaveview()" function
18712 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018713 static void
18714f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018715 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018716 typval_T *rettv;
18717{
18718 dict_T *dict;
18719
Bram Moolenaara800b422010-06-27 01:15:55 +020018720 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018721 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018722 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018723
18724 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18725 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18726#ifdef FEAT_VIRTUALEDIT
18727 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18728#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018729 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018730 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18731
18732 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18733#ifdef FEAT_DIFF
18734 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18735#endif
18736 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18737 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18738}
18739
18740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741 * "winwidth(nr)" function
18742 */
18743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018744f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018745 typval_T *argvars;
18746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747{
18748 win_T *wp;
18749
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018750 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018752 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753 else
18754#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018755 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018757 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758#endif
18759}
18760
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018762 * "writefile()" function
18763 */
18764 static void
18765f_writefile(argvars, rettv)
18766 typval_T *argvars;
18767 typval_T *rettv;
18768{
18769 int binary = FALSE;
18770 char_u *fname;
18771 FILE *fd;
18772 listitem_T *li;
18773 char_u *s;
18774 int ret = 0;
18775 int c;
18776
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018777 if (check_restricted() || check_secure())
18778 return;
18779
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018780 if (argvars[0].v_type != VAR_LIST)
18781 {
18782 EMSG2(_(e_listarg), "writefile()");
18783 return;
18784 }
18785 if (argvars[0].vval.v_list == NULL)
18786 return;
18787
18788 if (argvars[2].v_type != VAR_UNKNOWN
18789 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18790 binary = TRUE;
18791
18792 /* Always open the file in binary mode, library functions have a mind of
18793 * their own about CR-LF conversion. */
18794 fname = get_tv_string(&argvars[1]);
18795 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18796 {
18797 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18798 ret = -1;
18799 }
18800 else
18801 {
18802 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18803 li = li->li_next)
18804 {
18805 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18806 {
18807 if (*s == '\n')
18808 c = putc(NUL, fd);
18809 else
18810 c = putc(*s, fd);
18811 if (c == EOF)
18812 {
18813 ret = -1;
18814 break;
18815 }
18816 }
18817 if (!binary || li->li_next != NULL)
18818 if (putc('\n', fd) == EOF)
18819 {
18820 ret = -1;
18821 break;
18822 }
18823 if (ret < 0)
18824 {
18825 EMSG(_(e_write));
18826 break;
18827 }
18828 }
18829 fclose(fd);
18830 }
18831
18832 rettv->vval.v_number = ret;
18833}
18834
18835/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018836 * "xor(expr, expr)" function
18837 */
18838 static void
18839f_xor(argvars, rettv)
18840 typval_T *argvars;
18841 typval_T *rettv;
18842{
18843 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18844 ^ get_tv_number_chk(&argvars[1], NULL);
18845}
18846
18847
18848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018850 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018851 */
18852 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018853var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018854 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018855 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018856 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018858 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018860 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861
Bram Moolenaara5525202006-03-02 22:52:09 +000018862 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018863 if (varp->v_type == VAR_LIST)
18864 {
18865 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018866 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018867 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018868 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018869
18870 l = varp->vval.v_list;
18871 if (l == NULL)
18872 return NULL;
18873
18874 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018875 pos.lnum = list_find_nr(l, 0L, &error);
18876 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018877 return NULL; /* invalid line number */
18878
18879 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018880 pos.col = list_find_nr(l, 1L, &error);
18881 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018882 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018883 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018884
18885 /* We accept "$" for the column number: last column. */
18886 li = list_find(l, 1L);
18887 if (li != NULL && li->li_tv.v_type == VAR_STRING
18888 && li->li_tv.vval.v_string != NULL
18889 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18890 pos.col = len + 1;
18891
Bram Moolenaara5525202006-03-02 22:52:09 +000018892 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018893 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018894 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018895 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018896
Bram Moolenaara5525202006-03-02 22:52:09 +000018897#ifdef FEAT_VIRTUALEDIT
18898 /* Get the virtual offset. Defaults to zero. */
18899 pos.coladd = list_find_nr(l, 2L, &error);
18900 if (error)
18901 pos.coladd = 0;
18902#endif
18903
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018904 return &pos;
18905 }
18906
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018907 name = get_tv_string_chk(varp);
18908 if (name == NULL)
18909 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018910 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018912#ifdef FEAT_VISUAL
18913 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18914 {
18915 if (VIsual_active)
18916 return &VIsual;
18917 return &curwin->w_cursor;
18918 }
18919#endif
18920 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018921 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010018922 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18924 return NULL;
18925 return pp;
18926 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018927
18928#ifdef FEAT_VIRTUALEDIT
18929 pos.coladd = 0;
18930#endif
18931
Bram Moolenaar477933c2007-07-17 14:32:23 +000018932 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018933 {
18934 pos.col = 0;
18935 if (name[1] == '0') /* "w0": first visible line */
18936 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018937 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018938 pos.lnum = curwin->w_topline;
18939 return &pos;
18940 }
18941 else if (name[1] == '$') /* "w$": last visible line */
18942 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018943 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018944 pos.lnum = curwin->w_botline - 1;
18945 return &pos;
18946 }
18947 }
18948 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018950 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018951 {
18952 pos.lnum = curbuf->b_ml.ml_line_count;
18953 pos.col = 0;
18954 }
18955 else
18956 {
18957 pos.lnum = curwin->w_cursor.lnum;
18958 pos.col = (colnr_T)STRLEN(ml_get_curline());
18959 }
18960 return &pos;
18961 }
18962 return NULL;
18963}
18964
18965/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018966 * Convert list in "arg" into a position and optional file number.
18967 * When "fnump" is NULL there is no file number, only 3 items.
18968 * Note that the column is passed on as-is, the caller may want to decrement
18969 * it to use 1 for the first column.
18970 * Return FAIL when conversion is not possible, doesn't check the position for
18971 * validity.
18972 */
18973 static int
18974list2fpos(arg, posp, fnump)
18975 typval_T *arg;
18976 pos_T *posp;
18977 int *fnump;
18978{
18979 list_T *l = arg->vval.v_list;
18980 long i = 0;
18981 long n;
18982
Bram Moolenaarbde35262006-07-23 20:12:24 +000018983 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18984 * when "fnump" isn't NULL and "coladd" is optional. */
18985 if (arg->v_type != VAR_LIST
18986 || l == NULL
18987 || l->lv_len < (fnump == NULL ? 2 : 3)
18988 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018989 return FAIL;
18990
18991 if (fnump != NULL)
18992 {
18993 n = list_find_nr(l, i++, NULL); /* fnum */
18994 if (n < 0)
18995 return FAIL;
18996 if (n == 0)
18997 n = curbuf->b_fnum; /* current buffer */
18998 *fnump = n;
18999 }
19000
19001 n = list_find_nr(l, i++, NULL); /* lnum */
19002 if (n < 0)
19003 return FAIL;
19004 posp->lnum = n;
19005
19006 n = list_find_nr(l, i++, NULL); /* col */
19007 if (n < 0)
19008 return FAIL;
19009 posp->col = n;
19010
19011#ifdef FEAT_VIRTUALEDIT
19012 n = list_find_nr(l, i, NULL);
19013 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019014 posp->coladd = 0;
19015 else
19016 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019017#endif
19018
19019 return OK;
19020}
19021
19022/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019023 * Get the length of an environment variable name.
19024 * Advance "arg" to the first character after the name.
19025 * Return 0 for error.
19026 */
19027 static int
19028get_env_len(arg)
19029 char_u **arg;
19030{
19031 char_u *p;
19032 int len;
19033
19034 for (p = *arg; vim_isIDc(*p); ++p)
19035 ;
19036 if (p == *arg) /* no name found */
19037 return 0;
19038
19039 len = (int)(p - *arg);
19040 *arg = p;
19041 return len;
19042}
19043
19044/*
19045 * Get the length of the name of a function or internal variable.
19046 * "arg" is advanced to the first non-white character after the name.
19047 * Return 0 if something is wrong.
19048 */
19049 static int
19050get_id_len(arg)
19051 char_u **arg;
19052{
19053 char_u *p;
19054 int len;
19055
19056 /* Find the end of the name. */
19057 for (p = *arg; eval_isnamec(*p); ++p)
19058 ;
19059 if (p == *arg) /* no name found */
19060 return 0;
19061
19062 len = (int)(p - *arg);
19063 *arg = skipwhite(p);
19064
19065 return len;
19066}
19067
19068/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019069 * Get the length of the name of a variable or function.
19070 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019072 * Return -1 if curly braces expansion failed.
19073 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019074 * If the name contains 'magic' {}'s, expand them and return the
19075 * expanded name in an allocated string via 'alias' - caller must free.
19076 */
19077 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019078get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079 char_u **arg;
19080 char_u **alias;
19081 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019082 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083{
19084 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 char_u *p;
19086 char_u *expr_start;
19087 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019088
19089 *alias = NULL; /* default to no alias */
19090
19091 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19092 && (*arg)[2] == (int)KE_SNR)
19093 {
19094 /* hard coded <SNR>, already translated */
19095 *arg += 3;
19096 return get_id_len(arg) + 3;
19097 }
19098 len = eval_fname_script(*arg);
19099 if (len > 0)
19100 {
19101 /* literal "<SID>", "s:" or "<SNR>" */
19102 *arg += len;
19103 }
19104
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019106 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019108 p = find_name_end(*arg, &expr_start, &expr_end,
19109 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110 if (expr_start != NULL)
19111 {
19112 char_u *temp_string;
19113
19114 if (!evaluate)
19115 {
19116 len += (int)(p - *arg);
19117 *arg = skipwhite(p);
19118 return len;
19119 }
19120
19121 /*
19122 * Include any <SID> etc in the expanded string:
19123 * Thus the -len here.
19124 */
19125 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19126 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019127 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128 *alias = temp_string;
19129 *arg = skipwhite(p);
19130 return (int)STRLEN(temp_string);
19131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132
19133 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019134 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135 EMSG2(_(e_invexpr2), *arg);
19136
19137 return len;
19138}
19139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019140/*
19141 * Find the end of a variable or function name, taking care of magic braces.
19142 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19143 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019144 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019145 * Return a pointer to just after the name. Equal to "arg" if there is no
19146 * valid name.
19147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019148 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019149find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150 char_u *arg;
19151 char_u **expr_start;
19152 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019153 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019155 int mb_nest = 0;
19156 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157 char_u *p;
19158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019159 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019161 *expr_start = NULL;
19162 *expr_end = NULL;
19163 }
19164
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019165 /* Quick check for valid starting character. */
19166 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19167 return arg;
19168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019169 for (p = arg; *p != NUL
19170 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019171 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019172 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019173 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019174 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019175 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019176 if (*p == '\'')
19177 {
19178 /* skip over 'string' to avoid counting [ and ] inside it. */
19179 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19180 ;
19181 if (*p == NUL)
19182 break;
19183 }
19184 else if (*p == '"')
19185 {
19186 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19187 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19188 if (*p == '\\' && p[1] != NUL)
19189 ++p;
19190 if (*p == NUL)
19191 break;
19192 }
19193
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019194 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019195 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019196 if (*p == '[')
19197 ++br_nest;
19198 else if (*p == ']')
19199 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019200 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019201
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019202 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019203 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019204 if (*p == '{')
19205 {
19206 mb_nest++;
19207 if (expr_start != NULL && *expr_start == NULL)
19208 *expr_start = p;
19209 }
19210 else if (*p == '}')
19211 {
19212 mb_nest--;
19213 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19214 *expr_end = p;
19215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217 }
19218
19219 return p;
19220}
19221
19222/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019223 * Expands out the 'magic' {}'s in a variable/function name.
19224 * Note that this can call itself recursively, to deal with
19225 * constructs like foo{bar}{baz}{bam}
19226 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19227 * "in_start" ^
19228 * "expr_start" ^
19229 * "expr_end" ^
19230 * "in_end" ^
19231 *
19232 * Returns a new allocated string, which the caller must free.
19233 * Returns NULL for failure.
19234 */
19235 static char_u *
19236make_expanded_name(in_start, expr_start, expr_end, in_end)
19237 char_u *in_start;
19238 char_u *expr_start;
19239 char_u *expr_end;
19240 char_u *in_end;
19241{
19242 char_u c1;
19243 char_u *retval = NULL;
19244 char_u *temp_result;
19245 char_u *nextcmd = NULL;
19246
19247 if (expr_end == NULL || in_end == NULL)
19248 return NULL;
19249 *expr_start = NUL;
19250 *expr_end = NUL;
19251 c1 = *in_end;
19252 *in_end = NUL;
19253
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019254 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019255 if (temp_result != NULL && nextcmd == NULL)
19256 {
19257 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19258 + (in_end - expr_end) + 1));
19259 if (retval != NULL)
19260 {
19261 STRCPY(retval, in_start);
19262 STRCAT(retval, temp_result);
19263 STRCAT(retval, expr_end + 1);
19264 }
19265 }
19266 vim_free(temp_result);
19267
19268 *in_end = c1; /* put char back for error messages */
19269 *expr_start = '{';
19270 *expr_end = '}';
19271
19272 if (retval != NULL)
19273 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019274 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019275 if (expr_start != NULL)
19276 {
19277 /* Further expansion! */
19278 temp_result = make_expanded_name(retval, expr_start,
19279 expr_end, temp_result);
19280 vim_free(retval);
19281 retval = temp_result;
19282 }
19283 }
19284
19285 return retval;
19286}
19287
19288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019289 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019290 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019291 */
19292 static int
19293eval_isnamec(c)
19294 int c;
19295{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019296 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19297}
19298
19299/*
19300 * Return TRUE if character "c" can be used as the first character in a
19301 * variable or function name (excluding '{' and '}').
19302 */
19303 static int
19304eval_isnamec1(c)
19305 int c;
19306{
19307 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019308}
19309
19310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311 * Set number v: variable to "val".
19312 */
19313 void
19314set_vim_var_nr(idx, val)
19315 int idx;
19316 long val;
19317{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019318 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319}
19320
19321/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019322 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323 */
19324 long
19325get_vim_var_nr(idx)
19326 int idx;
19327{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019328 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329}
19330
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019331/*
19332 * Get string v: variable value. Uses a static buffer, can only be used once.
19333 */
19334 char_u *
19335get_vim_var_str(idx)
19336 int idx;
19337{
19338 return get_tv_string(&vimvars[idx].vv_tv);
19339}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019340
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019342 * Get List v: variable value. Caller must take care of reference count when
19343 * needed.
19344 */
19345 list_T *
19346get_vim_var_list(idx)
19347 int idx;
19348{
19349 return vimvars[idx].vv_list;
19350}
19351
19352/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019353 * Set v:char to character "c".
19354 */
19355 void
19356set_vim_var_char(c)
19357 int c;
19358{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019359 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019360
19361#ifdef FEAT_MBYTE
19362 if (has_mbyte)
19363 buf[(*mb_char2bytes)(c, buf)] = NUL;
19364 else
19365#endif
19366 {
19367 buf[0] = c;
19368 buf[1] = NUL;
19369 }
19370 set_vim_var_string(VV_CHAR, buf, -1);
19371}
19372
19373/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019374 * Set v:count to "count" and v:count1 to "count1".
19375 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376 */
19377 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019378set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 long count;
19380 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019381 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019383 if (set_prevcount)
19384 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019385 vimvars[VV_COUNT].vv_nr = count;
19386 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387}
19388
19389/*
19390 * Set string v: variable to a copy of "val".
19391 */
19392 void
19393set_vim_var_string(idx, val, len)
19394 int idx;
19395 char_u *val;
19396 int len; /* length of "val" to use or -1 (whole string) */
19397{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019398 /* Need to do this (at least) once, since we can't initialize a union.
19399 * Will always be invoked when "v:progname" is set. */
19400 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19401
Bram Moolenaare9a41262005-01-15 22:18:47 +000019402 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019404 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019406 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019408 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409}
19410
19411/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019412 * Set List v: variable to "val".
19413 */
19414 void
19415set_vim_var_list(idx, val)
19416 int idx;
19417 list_T *val;
19418{
19419 list_unref(vimvars[idx].vv_list);
19420 vimvars[idx].vv_list = val;
19421 if (val != NULL)
19422 ++val->lv_refcount;
19423}
19424
19425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019426 * Set v:register if needed.
19427 */
19428 void
19429set_reg_var(c)
19430 int c;
19431{
19432 char_u regname;
19433
19434 if (c == 0 || c == ' ')
19435 regname = '"';
19436 else
19437 regname = c;
19438 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019439 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019440 set_vim_var_string(VV_REG, &regname, 1);
19441}
19442
19443/*
19444 * Get or set v:exception. If "oldval" == NULL, return the current value.
19445 * Otherwise, restore the value to "oldval" and return NULL.
19446 * Must always be called in pairs to save and restore v:exception! Does not
19447 * take care of memory allocations.
19448 */
19449 char_u *
19450v_exception(oldval)
19451 char_u *oldval;
19452{
19453 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019454 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455
Bram Moolenaare9a41262005-01-15 22:18:47 +000019456 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457 return NULL;
19458}
19459
19460/*
19461 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19462 * Otherwise, restore the value to "oldval" and return NULL.
19463 * Must always be called in pairs to save and restore v:throwpoint! Does not
19464 * take care of memory allocations.
19465 */
19466 char_u *
19467v_throwpoint(oldval)
19468 char_u *oldval;
19469{
19470 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019471 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472
Bram Moolenaare9a41262005-01-15 22:18:47 +000019473 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474 return NULL;
19475}
19476
19477#if defined(FEAT_AUTOCMD) || defined(PROTO)
19478/*
19479 * Set v:cmdarg.
19480 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19481 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19482 * Must always be called in pairs!
19483 */
19484 char_u *
19485set_cmdarg(eap, oldarg)
19486 exarg_T *eap;
19487 char_u *oldarg;
19488{
19489 char_u *oldval;
19490 char_u *newval;
19491 unsigned len;
19492
Bram Moolenaare9a41262005-01-15 22:18:47 +000019493 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019494 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019495 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019496 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019497 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019498 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019499 }
19500
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019501 if (eap->force_bin == FORCE_BIN)
19502 len = 6;
19503 else if (eap->force_bin == FORCE_NOBIN)
19504 len = 8;
19505 else
19506 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019507
19508 if (eap->read_edit)
19509 len += 7;
19510
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019511 if (eap->force_ff != 0)
19512 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19513# ifdef FEAT_MBYTE
19514 if (eap->force_enc != 0)
19515 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019516 if (eap->bad_char != 0)
19517 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019518# endif
19519
19520 newval = alloc(len + 1);
19521 if (newval == NULL)
19522 return NULL;
19523
19524 if (eap->force_bin == FORCE_BIN)
19525 sprintf((char *)newval, " ++bin");
19526 else if (eap->force_bin == FORCE_NOBIN)
19527 sprintf((char *)newval, " ++nobin");
19528 else
19529 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019530
19531 if (eap->read_edit)
19532 STRCAT(newval, " ++edit");
19533
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019534 if (eap->force_ff != 0)
19535 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19536 eap->cmd + eap->force_ff);
19537# ifdef FEAT_MBYTE
19538 if (eap->force_enc != 0)
19539 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19540 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019541 if (eap->bad_char == BAD_KEEP)
19542 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19543 else if (eap->bad_char == BAD_DROP)
19544 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19545 else if (eap->bad_char != 0)
19546 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019547# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019548 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019549 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550}
19551#endif
19552
19553/*
19554 * Get the value of internal variable "name".
19555 * Return OK or FAIL.
19556 */
19557 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019558get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019559 char_u *name;
19560 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019561 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019562 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563{
19564 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019565 typval_T *tv = NULL;
19566 typval_T atv;
19567 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019569
19570 /* truncate the name, so that we can use strcmp() */
19571 cc = name[len];
19572 name[len] = NUL;
19573
19574 /*
19575 * Check for "b:changedtick".
19576 */
19577 if (STRCMP(name, "b:changedtick") == 0)
19578 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019579 atv.v_type = VAR_NUMBER;
19580 atv.vval.v_number = curbuf->b_changedtick;
19581 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019582 }
19583
19584 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019585 * Check for user-defined variables.
19586 */
19587 else
19588 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019589 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019591 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019592 }
19593
Bram Moolenaare9a41262005-01-15 22:18:47 +000019594 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019595 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019596 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019597 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598 ret = FAIL;
19599 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019600 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019601 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019602
19603 name[len] = cc;
19604
19605 return ret;
19606}
19607
19608/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019609 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19610 * Also handle function call with Funcref variable: func(expr)
19611 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19612 */
19613 static int
19614handle_subscript(arg, rettv, evaluate, verbose)
19615 char_u **arg;
19616 typval_T *rettv;
19617 int evaluate; /* do more than finding the end */
19618 int verbose; /* give error messages */
19619{
19620 int ret = OK;
19621 dict_T *selfdict = NULL;
19622 char_u *s;
19623 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019624 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019625
19626 while (ret == OK
19627 && (**arg == '['
19628 || (**arg == '.' && rettv->v_type == VAR_DICT)
19629 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19630 && !vim_iswhite(*(*arg - 1)))
19631 {
19632 if (**arg == '(')
19633 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019634 /* need to copy the funcref so that we can clear rettv */
19635 functv = *rettv;
19636 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019637
19638 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019639 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019640 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019641 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19642 &len, evaluate, selfdict);
19643
19644 /* Clear the funcref afterwards, so that deleting it while
19645 * evaluating the arguments is possible (see test55). */
19646 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019647
19648 /* Stop the expression evaluation when immediately aborting on
19649 * error, or when an interrupt occurred or an exception was thrown
19650 * but not caught. */
19651 if (aborting())
19652 {
19653 if (ret == OK)
19654 clear_tv(rettv);
19655 ret = FAIL;
19656 }
19657 dict_unref(selfdict);
19658 selfdict = NULL;
19659 }
19660 else /* **arg == '[' || **arg == '.' */
19661 {
19662 dict_unref(selfdict);
19663 if (rettv->v_type == VAR_DICT)
19664 {
19665 selfdict = rettv->vval.v_dict;
19666 if (selfdict != NULL)
19667 ++selfdict->dv_refcount;
19668 }
19669 else
19670 selfdict = NULL;
19671 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19672 {
19673 clear_tv(rettv);
19674 ret = FAIL;
19675 }
19676 }
19677 }
19678 dict_unref(selfdict);
19679 return ret;
19680}
19681
19682/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019683 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019684 * value).
19685 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019686 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019687alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019688{
Bram Moolenaar33570922005-01-25 22:26:29 +000019689 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019690}
19691
19692/*
19693 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019694 * The string "s" must have been allocated, it is consumed.
19695 * Return NULL for out of memory, the variable otherwise.
19696 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019697 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019698alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699 char_u *s;
19700{
Bram Moolenaar33570922005-01-25 22:26:29 +000019701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019702
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019703 rettv = alloc_tv();
19704 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019706 rettv->v_type = VAR_STRING;
19707 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708 }
19709 else
19710 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019711 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019712}
19713
19714/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019715 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019717 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019718free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019719 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720{
19721 if (varp != NULL)
19722 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019723 switch (varp->v_type)
19724 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019725 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019726 func_unref(varp->vval.v_string);
19727 /*FALLTHROUGH*/
19728 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019729 vim_free(varp->vval.v_string);
19730 break;
19731 case VAR_LIST:
19732 list_unref(varp->vval.v_list);
19733 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019734 case VAR_DICT:
19735 dict_unref(varp->vval.v_dict);
19736 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019737 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019738#ifdef FEAT_FLOAT
19739 case VAR_FLOAT:
19740#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019741 case VAR_UNKNOWN:
19742 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019743 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019744 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019745 break;
19746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747 vim_free(varp);
19748 }
19749}
19750
19751/*
19752 * Free the memory for a variable value and set the value to NULL or 0.
19753 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019754 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019755clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019756 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019757{
19758 if (varp != NULL)
19759 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019760 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019761 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019762 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019763 func_unref(varp->vval.v_string);
19764 /*FALLTHROUGH*/
19765 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019766 vim_free(varp->vval.v_string);
19767 varp->vval.v_string = NULL;
19768 break;
19769 case VAR_LIST:
19770 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019771 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019772 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019773 case VAR_DICT:
19774 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019775 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019776 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019777 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019778 varp->vval.v_number = 0;
19779 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019780#ifdef FEAT_FLOAT
19781 case VAR_FLOAT:
19782 varp->vval.v_float = 0.0;
19783 break;
19784#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019785 case VAR_UNKNOWN:
19786 break;
19787 default:
19788 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019790 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791 }
19792}
19793
19794/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019795 * Set the value of a variable to NULL without freeing items.
19796 */
19797 static void
19798init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019799 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019800{
19801 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019802 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019803}
19804
19805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806 * Get the number value of a variable.
19807 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019808 * For incompatible types, return 0.
19809 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19810 * caller of incompatible types: it sets *denote to TRUE if "denote"
19811 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019812 */
19813 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019814get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019815 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019816{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019817 int error = FALSE;
19818
19819 return get_tv_number_chk(varp, &error); /* return 0L on error */
19820}
19821
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019822 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019823get_tv_number_chk(varp, denote)
19824 typval_T *varp;
19825 int *denote;
19826{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019827 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019828
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019829 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019831 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019832 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019833#ifdef FEAT_FLOAT
19834 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019835 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019836 break;
19837#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019838 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019839 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019840 break;
19841 case VAR_STRING:
19842 if (varp->vval.v_string != NULL)
19843 vim_str2nr(varp->vval.v_string, NULL, NULL,
19844 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019845 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019846 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019847 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019848 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019849 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019850 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019851 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019852 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019853 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019854 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019855 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019856 if (denote == NULL) /* useful for values that must be unsigned */
19857 n = -1;
19858 else
19859 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019860 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861}
19862
19863/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019864 * Get the lnum from the first argument.
19865 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019866 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867 */
19868 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019869get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019870 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871{
Bram Moolenaar33570922005-01-25 22:26:29 +000019872 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019873 linenr_T lnum;
19874
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019875 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876 if (lnum == 0) /* no valid number, try using line() */
19877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019878 rettv.v_type = VAR_NUMBER;
19879 f_line(argvars, &rettv);
19880 lnum = rettv.vval.v_number;
19881 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019882 }
19883 return lnum;
19884}
19885
19886/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019887 * Get the lnum from the first argument.
19888 * Also accepts "$", then "buf" is used.
19889 * Returns 0 on error.
19890 */
19891 static linenr_T
19892get_tv_lnum_buf(argvars, buf)
19893 typval_T *argvars;
19894 buf_T *buf;
19895{
19896 if (argvars[0].v_type == VAR_STRING
19897 && argvars[0].vval.v_string != NULL
19898 && argvars[0].vval.v_string[0] == '$'
19899 && buf != NULL)
19900 return buf->b_ml.ml_line_count;
19901 return get_tv_number_chk(&argvars[0], NULL);
19902}
19903
19904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019905 * Get the string value of a variable.
19906 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019907 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19908 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019909 * If the String variable has never been set, return an empty string.
19910 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019911 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19912 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019913 */
19914 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019915get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019916 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917{
19918 static char_u mybuf[NUMBUFLEN];
19919
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019920 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921}
19922
19923 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019924get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019925 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019926 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019927{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019928 char_u *res = get_tv_string_buf_chk(varp, buf);
19929
19930 return res != NULL ? res : (char_u *)"";
19931}
19932
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019933 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019934get_tv_string_chk(varp)
19935 typval_T *varp;
19936{
19937 static char_u mybuf[NUMBUFLEN];
19938
19939 return get_tv_string_buf_chk(varp, mybuf);
19940}
19941
19942 static char_u *
19943get_tv_string_buf_chk(varp, buf)
19944 typval_T *varp;
19945 char_u *buf;
19946{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019947 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019949 case VAR_NUMBER:
19950 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19951 return buf;
19952 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019953 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019954 break;
19955 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019956 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019957 break;
19958 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019959 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019960 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019961#ifdef FEAT_FLOAT
19962 case VAR_FLOAT:
19963 EMSG(_("E806: using Float as a String"));
19964 break;
19965#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019966 case VAR_STRING:
19967 if (varp->vval.v_string != NULL)
19968 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019969 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019970 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019971 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019972 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019974 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975}
19976
19977/*
19978 * Find variable "name" in the list of variables.
19979 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019980 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019981 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019982 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019983 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019984 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019985find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019987 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019989 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019990 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019991
Bram Moolenaara7043832005-01-21 11:56:39 +000019992 ht = find_var_ht(name, &varname);
19993 if (htp != NULL)
19994 *htp = ht;
19995 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019997 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998}
19999
20000/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020001 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000020002 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020004 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020005find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020006 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000020007 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020008 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020009{
Bram Moolenaar33570922005-01-25 22:26:29 +000020010 hashitem_T *hi;
20011
20012 if (*varname == NUL)
20013 {
20014 /* Must be something like "s:", otherwise "ht" would be NULL. */
20015 switch (varname[-2])
20016 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020017 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020018 case 'g': return &globvars_var;
20019 case 'v': return &vimvars_var;
20020 case 'b': return &curbuf->b_bufvar;
20021 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020022#ifdef FEAT_WINDOWS
20023 case 't': return &curtab->tp_winvar;
20024#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020025 case 'l': return current_funccal == NULL
20026 ? NULL : &current_funccal->l_vars_var;
20027 case 'a': return current_funccal == NULL
20028 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020029 }
20030 return NULL;
20031 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020032
20033 hi = hash_find(ht, varname);
20034 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020035 {
20036 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020037 * worked find the variable again. Don't auto-load a script if it was
20038 * loaded already, otherwise it would be loaded every time when
20039 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020040 if (ht == &globvarht && !writing)
20041 {
20042 /* Note: script_autoload() may make "hi" invalid. It must either
20043 * be obtained again or not used. */
20044 if (!script_autoload(varname, FALSE) || aborting())
20045 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020046 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020047 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020048 if (HASHITEM_EMPTY(hi))
20049 return NULL;
20050 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020051 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020052}
20053
20054/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020055 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020056 * Set "varname" to the start of name without ':'.
20057 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020058 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020059find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 char_u *name;
20061 char_u **varname;
20062{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020063 hashitem_T *hi;
20064
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065 if (name[1] != ':')
20066 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020067 /* The name must not start with a colon or #. */
20068 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069 return NULL;
20070 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020071
20072 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020073 hi = hash_find(&compat_hashtab, name);
20074 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020075 return &compat_hashtab;
20076
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020078 return &globvarht; /* global variable */
20079 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020080 }
20081 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020082 if (*name == 'g') /* global variable */
20083 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020084 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20085 */
20086 if (vim_strchr(name + 2, ':') != NULL
20087 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020088 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000020090 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000020092 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020093#ifdef FEAT_WINDOWS
20094 if (*name == 't') /* tab page variable */
20095 return &curtab->tp_vars.dv_hashtab;
20096#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020097 if (*name == 'v') /* v: variable */
20098 return &vimvarht;
20099 if (*name == 'a' && current_funccal != NULL) /* function argument */
20100 return &current_funccal->l_avars.dv_hashtab;
20101 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20102 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103 if (*name == 's' /* script variable */
20104 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20105 return &SCRIPT_VARS(current_SID);
20106 return NULL;
20107}
20108
20109/*
20110 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020111 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112 * Returns NULL when it doesn't exist.
20113 */
20114 char_u *
20115get_var_value(name)
20116 char_u *name;
20117{
Bram Moolenaar33570922005-01-25 22:26:29 +000020118 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119
Bram Moolenaara7043832005-01-21 11:56:39 +000020120 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121 if (v == NULL)
20122 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020123 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124}
20125
20126/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020127 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128 * sourcing this script and when executing functions defined in the script.
20129 */
20130 void
20131new_script_vars(id)
20132 scid_T id;
20133{
Bram Moolenaara7043832005-01-21 11:56:39 +000020134 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020135 hashtab_T *ht;
20136 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020137
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20139 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020140 /* Re-allocating ga_data means that an ht_array pointing to
20141 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020142 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020143 for (i = 1; i <= ga_scripts.ga_len; ++i)
20144 {
20145 ht = &SCRIPT_VARS(i);
20146 if (ht->ht_mask == HT_INIT_SIZE - 1)
20147 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020148 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020149 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020150 }
20151
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152 while (ga_scripts.ga_len < id)
20153 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020154 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020155 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020156 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158 }
20159 }
20160}
20161
20162/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020163 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20164 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 */
20166 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020167init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020168 dict_T *dict;
20169 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020170 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171{
Bram Moolenaar33570922005-01-25 22:26:29 +000020172 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020173 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020174 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020175 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020176 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020177 dict_var->di_tv.vval.v_dict = dict;
20178 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020179 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020180 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20181 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182}
20183
20184/*
20185 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020186 * Frees all allocated variables and the value they contain.
20187 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188 */
20189 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020190vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020191 hashtab_T *ht;
20192{
20193 vars_clear_ext(ht, TRUE);
20194}
20195
20196/*
20197 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20198 */
20199 static void
20200vars_clear_ext(ht, free_val)
20201 hashtab_T *ht;
20202 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203{
Bram Moolenaara7043832005-01-21 11:56:39 +000020204 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020205 hashitem_T *hi;
20206 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020207
Bram Moolenaar33570922005-01-25 22:26:29 +000020208 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020209 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020210 for (hi = ht->ht_array; todo > 0; ++hi)
20211 {
20212 if (!HASHITEM_EMPTY(hi))
20213 {
20214 --todo;
20215
Bram Moolenaar33570922005-01-25 22:26:29 +000020216 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020217 * ht_array might change then. hash_clear() takes care of it
20218 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020219 v = HI2DI(hi);
20220 if (free_val)
20221 clear_tv(&v->di_tv);
20222 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20223 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020224 }
20225 }
20226 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020227 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228}
20229
Bram Moolenaara7043832005-01-21 11:56:39 +000020230/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020231 * Delete a variable from hashtab "ht" at item "hi".
20232 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020233 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020234 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020235delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020236 hashtab_T *ht;
20237 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238{
Bram Moolenaar33570922005-01-25 22:26:29 +000020239 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020240
20241 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020242 clear_tv(&di->di_tv);
20243 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244}
20245
20246/*
20247 * List the value of one internal variable.
20248 */
20249 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020250list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020251 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020253 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020255 char_u *tofree;
20256 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020257 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020258
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020259 current_copyID += COPYID_INC;
20260 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020261 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020262 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020263 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020264}
20265
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020267list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020268 char_u *prefix;
20269 char_u *name;
20270 int type;
20271 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020272 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020273{
Bram Moolenaar31859182007-08-14 20:41:13 +000020274 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20275 msg_start();
20276 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277 if (name != NULL) /* "a:" vars don't have a name stored */
20278 msg_puts(name);
20279 msg_putchar(' ');
20280 msg_advance(22);
20281 if (type == VAR_NUMBER)
20282 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020283 else if (type == VAR_FUNC)
20284 msg_putchar('*');
20285 else if (type == VAR_LIST)
20286 {
20287 msg_putchar('[');
20288 if (*string == '[')
20289 ++string;
20290 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020291 else if (type == VAR_DICT)
20292 {
20293 msg_putchar('{');
20294 if (*string == '{')
20295 ++string;
20296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020297 else
20298 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020299
Bram Moolenaar071d4272004-06-13 20:20:40 +000020300 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020301
20302 if (type == VAR_FUNC)
20303 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020304 if (*first)
20305 {
20306 msg_clr_eos();
20307 *first = FALSE;
20308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020309}
20310
20311/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020312 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313 * If the variable already exists, the value is updated.
20314 * Otherwise the variable is created.
20315 */
20316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020317set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020319 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020320 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321{
Bram Moolenaar33570922005-01-25 22:26:29 +000020322 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020323 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020324 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020326 ht = find_var_ht(name, &varname);
20327 if (ht == NULL || *varname == NUL)
20328 {
20329 EMSG2(_(e_illvar), name);
20330 return;
20331 }
20332 v = find_var_in_ht(ht, varname, TRUE);
20333
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020334 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20335 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020336
Bram Moolenaar33570922005-01-25 22:26:29 +000020337 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020338 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020339 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020340 if (var_check_ro(v->di_flags, name)
20341 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020342 return;
20343 if (v->di_tv.v_type != tv->v_type
20344 && !((v->di_tv.v_type == VAR_STRING
20345 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020346 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020347 || tv->v_type == VAR_NUMBER))
20348#ifdef FEAT_FLOAT
20349 && !((v->di_tv.v_type == VAR_NUMBER
20350 || v->di_tv.v_type == VAR_FLOAT)
20351 && (tv->v_type == VAR_NUMBER
20352 || tv->v_type == VAR_FLOAT))
20353#endif
20354 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020355 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020356 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020357 return;
20358 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020359
20360 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020361 * Handle setting internal v: variables separately: we don't change
20362 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020363 */
20364 if (ht == &vimvarht)
20365 {
20366 if (v->di_tv.v_type == VAR_STRING)
20367 {
20368 vim_free(v->di_tv.vval.v_string);
20369 if (copy || tv->v_type != VAR_STRING)
20370 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20371 else
20372 {
20373 /* Take over the string to avoid an extra alloc/free. */
20374 v->di_tv.vval.v_string = tv->vval.v_string;
20375 tv->vval.v_string = NULL;
20376 }
20377 }
20378 else if (v->di_tv.v_type != VAR_NUMBER)
20379 EMSG2(_(e_intern2), "set_var()");
20380 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020381 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020382 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020383 if (STRCMP(varname, "searchforward") == 0)
20384 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20385 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020386 return;
20387 }
20388
20389 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390 }
20391 else /* add a new variable */
20392 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020393 /* Can't add "v:" variable. */
20394 if (ht == &vimvarht)
20395 {
20396 EMSG2(_(e_illvar), name);
20397 return;
20398 }
20399
Bram Moolenaar92124a32005-06-17 22:03:40 +000020400 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020401 if (!valid_varname(varname))
20402 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020403
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020404 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20405 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020406 if (v == NULL)
20407 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020408 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020409 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020411 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412 return;
20413 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020414 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020415 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020416
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020417 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020418 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020419 else
20420 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020421 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020422 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020423 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425}
20426
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020427/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020428 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020429 * Also give an error message.
20430 */
20431 static int
20432var_check_ro(flags, name)
20433 int flags;
20434 char_u *name;
20435{
20436 if (flags & DI_FLAGS_RO)
20437 {
20438 EMSG2(_(e_readonlyvar), name);
20439 return TRUE;
20440 }
20441 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20442 {
20443 EMSG2(_(e_readonlysbx), name);
20444 return TRUE;
20445 }
20446 return FALSE;
20447}
20448
20449/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020450 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20451 * Also give an error message.
20452 */
20453 static int
20454var_check_fixed(flags, name)
20455 int flags;
20456 char_u *name;
20457{
20458 if (flags & DI_FLAGS_FIX)
20459 {
20460 EMSG2(_("E795: Cannot delete variable %s"), name);
20461 return TRUE;
20462 }
20463 return FALSE;
20464}
20465
20466/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020467 * Check if a funcref is assigned to a valid variable name.
20468 * Return TRUE and give an error if not.
20469 */
20470 static int
20471var_check_func_name(name, new_var)
20472 char_u *name; /* points to start of variable name */
20473 int new_var; /* TRUE when creating the variable */
20474{
20475 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20476 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20477 ? name[2] : name[0]))
20478 {
20479 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20480 name);
20481 return TRUE;
20482 }
20483 /* Don't allow hiding a function. When "v" is not NULL we might be
20484 * assigning another function to the same var, the type is checked
20485 * below. */
20486 if (new_var && function_exists(name))
20487 {
20488 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20489 name);
20490 return TRUE;
20491 }
20492 return FALSE;
20493}
20494
20495/*
20496 * Check if a variable name is valid.
20497 * Return FALSE and give an error if not.
20498 */
20499 static int
20500valid_varname(varname)
20501 char_u *varname;
20502{
20503 char_u *p;
20504
20505 for (p = varname; *p != NUL; ++p)
20506 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20507 && *p != AUTOLOAD_CHAR)
20508 {
20509 EMSG2(_(e_illvar), varname);
20510 return FALSE;
20511 }
20512 return TRUE;
20513}
20514
20515/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020516 * Return TRUE if typeval "tv" is set to be locked (immutable).
20517 * Also give an error message, using "name".
20518 */
20519 static int
20520tv_check_lock(lock, name)
20521 int lock;
20522 char_u *name;
20523{
20524 if (lock & VAR_LOCKED)
20525 {
20526 EMSG2(_("E741: Value is locked: %s"),
20527 name == NULL ? (char_u *)_("Unknown") : name);
20528 return TRUE;
20529 }
20530 if (lock & VAR_FIXED)
20531 {
20532 EMSG2(_("E742: Cannot change value of %s"),
20533 name == NULL ? (char_u *)_("Unknown") : name);
20534 return TRUE;
20535 }
20536 return FALSE;
20537}
20538
20539/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020540 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020541 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020542 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020543 * It is OK for "from" and "to" to point to the same item. This is used to
20544 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020545 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020546 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020547copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020548 typval_T *from;
20549 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020551 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020552 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020553 switch (from->v_type)
20554 {
20555 case VAR_NUMBER:
20556 to->vval.v_number = from->vval.v_number;
20557 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020558#ifdef FEAT_FLOAT
20559 case VAR_FLOAT:
20560 to->vval.v_float = from->vval.v_float;
20561 break;
20562#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020563 case VAR_STRING:
20564 case VAR_FUNC:
20565 if (from->vval.v_string == NULL)
20566 to->vval.v_string = NULL;
20567 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020568 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020569 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020570 if (from->v_type == VAR_FUNC)
20571 func_ref(to->vval.v_string);
20572 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020573 break;
20574 case VAR_LIST:
20575 if (from->vval.v_list == NULL)
20576 to->vval.v_list = NULL;
20577 else
20578 {
20579 to->vval.v_list = from->vval.v_list;
20580 ++to->vval.v_list->lv_refcount;
20581 }
20582 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020583 case VAR_DICT:
20584 if (from->vval.v_dict == NULL)
20585 to->vval.v_dict = NULL;
20586 else
20587 {
20588 to->vval.v_dict = from->vval.v_dict;
20589 ++to->vval.v_dict->dv_refcount;
20590 }
20591 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020592 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020593 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020594 break;
20595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596}
20597
20598/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020599 * Make a copy of an item.
20600 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020601 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20602 * reference to an already copied list/dict can be used.
20603 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020604 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020605 static int
20606item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020607 typval_T *from;
20608 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020609 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020610 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020611{
20612 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020613 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020614
Bram Moolenaar33570922005-01-25 22:26:29 +000020615 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020616 {
20617 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020618 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020619 }
20620 ++recurse;
20621
20622 switch (from->v_type)
20623 {
20624 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020625#ifdef FEAT_FLOAT
20626 case VAR_FLOAT:
20627#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020628 case VAR_STRING:
20629 case VAR_FUNC:
20630 copy_tv(from, to);
20631 break;
20632 case VAR_LIST:
20633 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020634 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020635 if (from->vval.v_list == NULL)
20636 to->vval.v_list = NULL;
20637 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20638 {
20639 /* use the copy made earlier */
20640 to->vval.v_list = from->vval.v_list->lv_copylist;
20641 ++to->vval.v_list->lv_refcount;
20642 }
20643 else
20644 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20645 if (to->vval.v_list == NULL)
20646 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020647 break;
20648 case VAR_DICT:
20649 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020650 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020651 if (from->vval.v_dict == NULL)
20652 to->vval.v_dict = NULL;
20653 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20654 {
20655 /* use the copy made earlier */
20656 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20657 ++to->vval.v_dict->dv_refcount;
20658 }
20659 else
20660 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20661 if (to->vval.v_dict == NULL)
20662 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020663 break;
20664 default:
20665 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020666 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020667 }
20668 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020669 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020670}
20671
20672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020673 * ":echo expr1 ..." print each argument separated with a space, add a
20674 * newline at the end.
20675 * ":echon expr1 ..." print each argument plain.
20676 */
20677 void
20678ex_echo(eap)
20679 exarg_T *eap;
20680{
20681 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020682 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020683 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684 char_u *p;
20685 int needclr = TRUE;
20686 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020687 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688
20689 if (eap->skip)
20690 ++emsg_skip;
20691 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20692 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020693 /* If eval1() causes an error message the text from the command may
20694 * still need to be cleared. E.g., "echo 22,44". */
20695 need_clr_eos = needclr;
20696
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020698 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020699 {
20700 /*
20701 * Report the invalid expression unless the expression evaluation
20702 * has been cancelled due to an aborting error, an interrupt, or an
20703 * exception.
20704 */
20705 if (!aborting())
20706 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020707 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708 break;
20709 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020710 need_clr_eos = FALSE;
20711
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712 if (!eap->skip)
20713 {
20714 if (atstart)
20715 {
20716 atstart = FALSE;
20717 /* Call msg_start() after eval1(), evaluating the expression
20718 * may cause a message to appear. */
20719 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020720 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020721 /* Mark the saved text as finishing the line, so that what
20722 * follows is displayed on a new line when scrolling back
20723 * at the more prompt. */
20724 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020727 }
20728 else if (eap->cmdidx == CMD_echo)
20729 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020730 current_copyID += COPYID_INC;
20731 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020732 if (p != NULL)
20733 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020735 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020736 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020737 if (*p != TAB && needclr)
20738 {
20739 /* remove any text still there from the command */
20740 msg_clr_eos();
20741 needclr = FALSE;
20742 }
20743 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020744 }
20745 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020746 {
20747#ifdef FEAT_MBYTE
20748 if (has_mbyte)
20749 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020750 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020751
20752 (void)msg_outtrans_len_attr(p, i, echo_attr);
20753 p += i - 1;
20754 }
20755 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020757 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020759 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020760 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020762 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020763 arg = skipwhite(arg);
20764 }
20765 eap->nextcmd = check_nextcmd(arg);
20766
20767 if (eap->skip)
20768 --emsg_skip;
20769 else
20770 {
20771 /* remove text that may still be there from the command */
20772 if (needclr)
20773 msg_clr_eos();
20774 if (eap->cmdidx == CMD_echo)
20775 msg_end();
20776 }
20777}
20778
20779/*
20780 * ":echohl {name}".
20781 */
20782 void
20783ex_echohl(eap)
20784 exarg_T *eap;
20785{
20786 int id;
20787
20788 id = syn_name2id(eap->arg);
20789 if (id == 0)
20790 echo_attr = 0;
20791 else
20792 echo_attr = syn_id2attr(id);
20793}
20794
20795/*
20796 * ":execute expr1 ..." execute the result of an expression.
20797 * ":echomsg expr1 ..." Print a message
20798 * ":echoerr expr1 ..." Print an error
20799 * Each gets spaces around each argument and a newline at the end for
20800 * echo commands
20801 */
20802 void
20803ex_execute(eap)
20804 exarg_T *eap;
20805{
20806 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020807 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020808 int ret = OK;
20809 char_u *p;
20810 garray_T ga;
20811 int len;
20812 int save_did_emsg;
20813
20814 ga_init2(&ga, 1, 80);
20815
20816 if (eap->skip)
20817 ++emsg_skip;
20818 while (*arg != NUL && *arg != '|' && *arg != '\n')
20819 {
20820 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020821 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020822 {
20823 /*
20824 * Report the invalid expression unless the expression evaluation
20825 * has been cancelled due to an aborting error, an interrupt, or an
20826 * exception.
20827 */
20828 if (!aborting())
20829 EMSG2(_(e_invexpr2), p);
20830 ret = FAIL;
20831 break;
20832 }
20833
20834 if (!eap->skip)
20835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020836 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020837 len = (int)STRLEN(p);
20838 if (ga_grow(&ga, len + 2) == FAIL)
20839 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020840 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841 ret = FAIL;
20842 break;
20843 }
20844 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847 ga.ga_len += len;
20848 }
20849
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020850 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851 arg = skipwhite(arg);
20852 }
20853
20854 if (ret != FAIL && ga.ga_data != NULL)
20855 {
20856 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020857 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020859 out_flush();
20860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020861 else if (eap->cmdidx == CMD_echoerr)
20862 {
20863 /* We don't want to abort following commands, restore did_emsg. */
20864 save_did_emsg = did_emsg;
20865 EMSG((char_u *)ga.ga_data);
20866 if (!force_abort)
20867 did_emsg = save_did_emsg;
20868 }
20869 else if (eap->cmdidx == CMD_execute)
20870 do_cmdline((char_u *)ga.ga_data,
20871 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20872 }
20873
20874 ga_clear(&ga);
20875
20876 if (eap->skip)
20877 --emsg_skip;
20878
20879 eap->nextcmd = check_nextcmd(arg);
20880}
20881
20882/*
20883 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20884 * "arg" points to the "&" or '+' when called, to "option" when returning.
20885 * Returns NULL when no option name found. Otherwise pointer to the char
20886 * after the option name.
20887 */
20888 static char_u *
20889find_option_end(arg, opt_flags)
20890 char_u **arg;
20891 int *opt_flags;
20892{
20893 char_u *p = *arg;
20894
20895 ++p;
20896 if (*p == 'g' && p[1] == ':')
20897 {
20898 *opt_flags = OPT_GLOBAL;
20899 p += 2;
20900 }
20901 else if (*p == 'l' && p[1] == ':')
20902 {
20903 *opt_flags = OPT_LOCAL;
20904 p += 2;
20905 }
20906 else
20907 *opt_flags = 0;
20908
20909 if (!ASCII_ISALPHA(*p))
20910 return NULL;
20911 *arg = p;
20912
20913 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20914 p += 4; /* termcap option */
20915 else
20916 while (ASCII_ISALPHA(*p))
20917 ++p;
20918 return p;
20919}
20920
20921/*
20922 * ":function"
20923 */
20924 void
20925ex_function(eap)
20926 exarg_T *eap;
20927{
20928 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020929 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930 int j;
20931 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020932 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020933 char_u *name = NULL;
20934 char_u *p;
20935 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020936 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020937 garray_T newargs;
20938 garray_T newlines;
20939 int varargs = FALSE;
20940 int mustend = FALSE;
20941 int flags = 0;
20942 ufunc_T *fp;
20943 int indent;
20944 int nesting;
20945 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020946 dictitem_T *v;
20947 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020948 static int func_nr = 0; /* number for nameless function */
20949 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020950 hashtab_T *ht;
20951 int todo;
20952 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020953 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954
20955 /*
20956 * ":function" without argument: list functions.
20957 */
20958 if (ends_excmd(*eap->arg))
20959 {
20960 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020961 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020962 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020963 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020964 {
20965 if (!HASHITEM_EMPTY(hi))
20966 {
20967 --todo;
20968 fp = HI2UF(hi);
20969 if (!isdigit(*fp->uf_name))
20970 list_func_head(fp, FALSE);
20971 }
20972 }
20973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974 eap->nextcmd = check_nextcmd(eap->arg);
20975 return;
20976 }
20977
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020978 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020979 * ":function /pat": list functions matching pattern.
20980 */
20981 if (*eap->arg == '/')
20982 {
20983 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20984 if (!eap->skip)
20985 {
20986 regmatch_T regmatch;
20987
20988 c = *p;
20989 *p = NUL;
20990 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20991 *p = c;
20992 if (regmatch.regprog != NULL)
20993 {
20994 regmatch.rm_ic = p_ic;
20995
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020996 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020997 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20998 {
20999 if (!HASHITEM_EMPTY(hi))
21000 {
21001 --todo;
21002 fp = HI2UF(hi);
21003 if (!isdigit(*fp->uf_name)
21004 && vim_regexec(&regmatch, fp->uf_name, 0))
21005 list_func_head(fp, FALSE);
21006 }
21007 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000021008 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021009 }
21010 }
21011 if (*p == '/')
21012 ++p;
21013 eap->nextcmd = check_nextcmd(p);
21014 return;
21015 }
21016
21017 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021018 * Get the function name. There are these situations:
21019 * func normal function name
21020 * "name" == func, "fudi.fd_dict" == NULL
21021 * dict.func new dictionary entry
21022 * "name" == NULL, "fudi.fd_dict" set,
21023 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21024 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021025 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021026 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21027 * dict.func existing dict entry that's not a Funcref
21028 * "name" == NULL, "fudi.fd_dict" set,
21029 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021032 name = trans_function_name(&p, eap->skip, 0, &fudi);
21033 paren = (vim_strchr(p, '(') != NULL);
21034 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035 {
21036 /*
21037 * Return on an invalid expression in braces, unless the expression
21038 * evaluation has been cancelled due to an aborting error, an
21039 * interrupt, or an exception.
21040 */
21041 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021042 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021043 if (!eap->skip && fudi.fd_newkey != NULL)
21044 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021045 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021046 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021048 else
21049 eap->skip = TRUE;
21050 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021051
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052 /* An error in a function call during evaluation of an expression in magic
21053 * braces should not cause the function not to be defined. */
21054 saved_did_emsg = did_emsg;
21055 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021056
21057 /*
21058 * ":function func" with only function name: list function.
21059 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021060 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021061 {
21062 if (!ends_excmd(*skipwhite(p)))
21063 {
21064 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021065 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021066 }
21067 eap->nextcmd = check_nextcmd(p);
21068 if (eap->nextcmd != NULL)
21069 *p = NUL;
21070 if (!eap->skip && !got_int)
21071 {
21072 fp = find_func(name);
21073 if (fp != NULL)
21074 {
21075 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021076 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021078 if (FUNCLINE(fp, j) == NULL)
21079 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021080 msg_putchar('\n');
21081 msg_outnum((long)(j + 1));
21082 if (j < 9)
21083 msg_putchar(' ');
21084 if (j < 99)
21085 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021086 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087 out_flush(); /* show a line at a time */
21088 ui_breakcheck();
21089 }
21090 if (!got_int)
21091 {
21092 msg_putchar('\n');
21093 msg_puts((char_u *)" endfunction");
21094 }
21095 }
21096 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021097 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021099 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100 }
21101
21102 /*
21103 * ":function name(arg1, arg2)" Define function.
21104 */
21105 p = skipwhite(p);
21106 if (*p != '(')
21107 {
21108 if (!eap->skip)
21109 {
21110 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021111 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112 }
21113 /* attempt to continue by skipping some text */
21114 if (vim_strchr(p, '(') != NULL)
21115 p = vim_strchr(p, '(');
21116 }
21117 p = skipwhite(p + 1);
21118
21119 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21120 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21121
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021122 if (!eap->skip)
21123 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021124 /* Check the name of the function. Unless it's a dictionary function
21125 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021126 if (name != NULL)
21127 arg = name;
21128 else
21129 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021130 if (arg != NULL && (fudi.fd_di == NULL
21131 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021132 {
21133 if (*arg == K_SPECIAL)
21134 j = 3;
21135 else
21136 j = 0;
21137 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21138 : eval_isnamec(arg[j])))
21139 ++j;
21140 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021141 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021142 }
21143 }
21144
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145 /*
21146 * Isolate the arguments: "arg1, arg2, ...)"
21147 */
21148 while (*p != ')')
21149 {
21150 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21151 {
21152 varargs = TRUE;
21153 p += 3;
21154 mustend = TRUE;
21155 }
21156 else
21157 {
21158 arg = p;
21159 while (ASCII_ISALNUM(*p) || *p == '_')
21160 ++p;
21161 if (arg == p || isdigit(*arg)
21162 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21163 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21164 {
21165 if (!eap->skip)
21166 EMSG2(_("E125: Illegal argument: %s"), arg);
21167 break;
21168 }
21169 if (ga_grow(&newargs, 1) == FAIL)
21170 goto erret;
21171 c = *p;
21172 *p = NUL;
21173 arg = vim_strsave(arg);
21174 if (arg == NULL)
21175 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021176
21177 /* Check for duplicate argument name. */
21178 for (i = 0; i < newargs.ga_len; ++i)
21179 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21180 {
21181 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21182 goto erret;
21183 }
21184
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21186 *p = c;
21187 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188 if (*p == ',')
21189 ++p;
21190 else
21191 mustend = TRUE;
21192 }
21193 p = skipwhite(p);
21194 if (mustend && *p != ')')
21195 {
21196 if (!eap->skip)
21197 EMSG2(_(e_invarg2), eap->arg);
21198 break;
21199 }
21200 }
21201 ++p; /* skip the ')' */
21202
Bram Moolenaare9a41262005-01-15 22:18:47 +000021203 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021204 for (;;)
21205 {
21206 p = skipwhite(p);
21207 if (STRNCMP(p, "range", 5) == 0)
21208 {
21209 flags |= FC_RANGE;
21210 p += 5;
21211 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021212 else if (STRNCMP(p, "dict", 4) == 0)
21213 {
21214 flags |= FC_DICT;
21215 p += 4;
21216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021217 else if (STRNCMP(p, "abort", 5) == 0)
21218 {
21219 flags |= FC_ABORT;
21220 p += 5;
21221 }
21222 else
21223 break;
21224 }
21225
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021226 /* When there is a line break use what follows for the function body.
21227 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21228 if (*p == '\n')
21229 line_arg = p + 1;
21230 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021231 EMSG(_(e_trailing));
21232
21233 /*
21234 * Read the body of the function, until ":endfunction" is found.
21235 */
21236 if (KeyTyped)
21237 {
21238 /* Check if the function already exists, don't let the user type the
21239 * whole function before telling him it doesn't work! For a script we
21240 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021241 if (!eap->skip && !eap->forceit)
21242 {
21243 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21244 EMSG(_(e_funcdict));
21245 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021246 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021248
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021249 if (!eap->skip && did_emsg)
21250 goto erret;
21251
Bram Moolenaar071d4272004-06-13 20:20:40 +000021252 msg_putchar('\n'); /* don't overwrite the function name */
21253 cmdline_row = msg_row;
21254 }
21255
21256 indent = 2;
21257 nesting = 0;
21258 for (;;)
21259 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021260 if (KeyTyped)
21261 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021262 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021263 sourcing_lnum_off = sourcing_lnum;
21264
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021265 if (line_arg != NULL)
21266 {
21267 /* Use eap->arg, split up in parts by line breaks. */
21268 theline = line_arg;
21269 p = vim_strchr(theline, '\n');
21270 if (p == NULL)
21271 line_arg += STRLEN(line_arg);
21272 else
21273 {
21274 *p = NUL;
21275 line_arg = p + 1;
21276 }
21277 }
21278 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279 theline = getcmdline(':', 0L, indent);
21280 else
21281 theline = eap->getline(':', eap->cookie, indent);
21282 if (KeyTyped)
21283 lines_left = Rows - 1;
21284 if (theline == NULL)
21285 {
21286 EMSG(_("E126: Missing :endfunction"));
21287 goto erret;
21288 }
21289
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021290 /* Detect line continuation: sourcing_lnum increased more than one. */
21291 if (sourcing_lnum > sourcing_lnum_off + 1)
21292 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21293 else
21294 sourcing_lnum_off = 0;
21295
Bram Moolenaar071d4272004-06-13 20:20:40 +000021296 if (skip_until != NULL)
21297 {
21298 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21299 * don't check for ":endfunc". */
21300 if (STRCMP(theline, skip_until) == 0)
21301 {
21302 vim_free(skip_until);
21303 skip_until = NULL;
21304 }
21305 }
21306 else
21307 {
21308 /* skip ':' and blanks*/
21309 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21310 ;
21311
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021312 /* Check for "endfunction". */
21313 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021315 if (line_arg == NULL)
21316 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021317 break;
21318 }
21319
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021320 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021321 * at "end". */
21322 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21323 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021324 else if (STRNCMP(p, "if", 2) == 0
21325 || STRNCMP(p, "wh", 2) == 0
21326 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 || STRNCMP(p, "try", 3) == 0)
21328 indent += 2;
21329
21330 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021331 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021333 if (*p == '!')
21334 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021335 p += eval_fname_script(p);
21336 if (ASCII_ISALPHA(*p))
21337 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021338 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021339 if (*skipwhite(p) == '(')
21340 {
21341 ++nesting;
21342 indent += 2;
21343 }
21344 }
21345 }
21346
21347 /* Check for ":append" or ":insert". */
21348 p = skip_range(p, NULL);
21349 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21350 || (p[0] == 'i'
21351 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21352 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21353 skip_until = vim_strsave((char_u *)".");
21354
21355 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21356 arg = skipwhite(skiptowhite(p));
21357 if (arg[0] == '<' && arg[1] =='<'
21358 && ((p[0] == 'p' && p[1] == 'y'
21359 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21360 || (p[0] == 'p' && p[1] == 'e'
21361 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21362 || (p[0] == 't' && p[1] == 'c'
21363 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021364 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21365 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021366 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21367 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021368 || (p[0] == 'm' && p[1] == 'z'
21369 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370 ))
21371 {
21372 /* ":python <<" continues until a dot, like ":append" */
21373 p = skipwhite(arg + 2);
21374 if (*p == NUL)
21375 skip_until = vim_strsave((char_u *)".");
21376 else
21377 skip_until = vim_strsave(p);
21378 }
21379 }
21380
21381 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021382 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021383 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021384 if (line_arg == NULL)
21385 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021387 }
21388
21389 /* Copy the line to newly allocated memory. get_one_sourceline()
21390 * allocates 250 bytes per line, this saves 80% on average. The cost
21391 * is an extra alloc/free. */
21392 p = vim_strsave(theline);
21393 if (p != NULL)
21394 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021395 if (line_arg == NULL)
21396 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021397 theline = p;
21398 }
21399
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021400 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21401
21402 /* Add NULL lines for continuation lines, so that the line count is
21403 * equal to the index in the growarray. */
21404 while (sourcing_lnum_off-- > 0)
21405 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021406
21407 /* Check for end of eap->arg. */
21408 if (line_arg != NULL && *line_arg == NUL)
21409 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410 }
21411
21412 /* Don't define the function when skipping commands or when an error was
21413 * detected. */
21414 if (eap->skip || did_emsg)
21415 goto erret;
21416
21417 /*
21418 * If there are no errors, add the function
21419 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021420 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021421 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021422 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021423 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021424 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021425 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021426 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021427 goto erret;
21428 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021429
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021430 fp = find_func(name);
21431 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021433 if (!eap->forceit)
21434 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021435 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021436 goto erret;
21437 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021438 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021439 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021440 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021441 name);
21442 goto erret;
21443 }
21444 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021445 ga_clear_strings(&(fp->uf_args));
21446 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021447 vim_free(name);
21448 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450 }
21451 else
21452 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021453 char numbuf[20];
21454
21455 fp = NULL;
21456 if (fudi.fd_newkey == NULL && !eap->forceit)
21457 {
21458 EMSG(_(e_funcdict));
21459 goto erret;
21460 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021461 if (fudi.fd_di == NULL)
21462 {
21463 /* Can't add a function to a locked dictionary */
21464 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21465 goto erret;
21466 }
21467 /* Can't change an existing function if it is locked */
21468 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21469 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021470
21471 /* Give the function a sequential number. Can only be used with a
21472 * Funcref! */
21473 vim_free(name);
21474 sprintf(numbuf, "%d", ++func_nr);
21475 name = vim_strsave((char_u *)numbuf);
21476 if (name == NULL)
21477 goto erret;
21478 }
21479
21480 if (fp == NULL)
21481 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021482 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021483 {
21484 int slen, plen;
21485 char_u *scriptname;
21486
21487 /* Check that the autoload name matches the script name. */
21488 j = FAIL;
21489 if (sourcing_name != NULL)
21490 {
21491 scriptname = autoload_name(name);
21492 if (scriptname != NULL)
21493 {
21494 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021495 plen = (int)STRLEN(p);
21496 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021497 if (slen > plen && fnamecmp(p,
21498 sourcing_name + slen - plen) == 0)
21499 j = OK;
21500 vim_free(scriptname);
21501 }
21502 }
21503 if (j == FAIL)
21504 {
21505 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21506 goto erret;
21507 }
21508 }
21509
21510 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021511 if (fp == NULL)
21512 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021513
21514 if (fudi.fd_dict != NULL)
21515 {
21516 if (fudi.fd_di == NULL)
21517 {
21518 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021519 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021520 if (fudi.fd_di == NULL)
21521 {
21522 vim_free(fp);
21523 goto erret;
21524 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021525 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21526 {
21527 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021528 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021529 goto erret;
21530 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021531 }
21532 else
21533 /* overwrite existing dict entry */
21534 clear_tv(&fudi.fd_di->di_tv);
21535 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021536 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021537 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021538 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021539
21540 /* behave like "dict" was used */
21541 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021542 }
21543
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021545 STRCPY(fp->uf_name, name);
21546 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021548 fp->uf_args = newargs;
21549 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021550#ifdef FEAT_PROFILE
21551 fp->uf_tml_count = NULL;
21552 fp->uf_tml_total = NULL;
21553 fp->uf_tml_self = NULL;
21554 fp->uf_profiling = FALSE;
21555 if (prof_def_func())
21556 func_do_profile(fp);
21557#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021558 fp->uf_varargs = varargs;
21559 fp->uf_flags = flags;
21560 fp->uf_calls = 0;
21561 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021562 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021563
21564erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021565 ga_clear_strings(&newargs);
21566 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021567ret_free:
21568 vim_free(skip_until);
21569 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021571 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572}
21573
21574/*
21575 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021576 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021578 * flags:
21579 * TFN_INT: internal function name OK
21580 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581 * Advances "pp" to just after the function name (if no error).
21582 */
21583 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021584trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585 char_u **pp;
21586 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021587 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021588 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021590 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591 char_u *start;
21592 char_u *end;
21593 int lead;
21594 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021596 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021597
21598 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021599 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021600 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021601
21602 /* Check for hard coded <SNR>: already translated function ID (from a user
21603 * command). */
21604 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21605 && (*pp)[2] == (int)KE_SNR)
21606 {
21607 *pp += 3;
21608 len = get_id_len(pp) + 3;
21609 return vim_strnsave(start, len);
21610 }
21611
21612 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21613 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021615 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021617
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021618 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21619 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021620 if (end == start)
21621 {
21622 if (!skip)
21623 EMSG(_("E129: Function name required"));
21624 goto theend;
21625 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021626 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021627 {
21628 /*
21629 * Report an invalid expression in braces, unless the expression
21630 * evaluation has been cancelled due to an aborting error, an
21631 * interrupt, or an exception.
21632 */
21633 if (!aborting())
21634 {
21635 if (end != NULL)
21636 EMSG2(_(e_invarg2), start);
21637 }
21638 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021639 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021640 goto theend;
21641 }
21642
21643 if (lv.ll_tv != NULL)
21644 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021645 if (fdp != NULL)
21646 {
21647 fdp->fd_dict = lv.ll_dict;
21648 fdp->fd_newkey = lv.ll_newkey;
21649 lv.ll_newkey = NULL;
21650 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021651 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021652 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21653 {
21654 name = vim_strsave(lv.ll_tv->vval.v_string);
21655 *pp = end;
21656 }
21657 else
21658 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021659 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21660 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021661 EMSG(_(e_funcref));
21662 else
21663 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021664 name = NULL;
21665 }
21666 goto theend;
21667 }
21668
21669 if (lv.ll_name == NULL)
21670 {
21671 /* Error found, but continue after the function name. */
21672 *pp = end;
21673 goto theend;
21674 }
21675
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021676 /* Check if the name is a Funcref. If so, use the value. */
21677 if (lv.ll_exp_name != NULL)
21678 {
21679 len = (int)STRLEN(lv.ll_exp_name);
21680 name = deref_func_name(lv.ll_exp_name, &len);
21681 if (name == lv.ll_exp_name)
21682 name = NULL;
21683 }
21684 else
21685 {
21686 len = (int)(end - *pp);
21687 name = deref_func_name(*pp, &len);
21688 if (name == *pp)
21689 name = NULL;
21690 }
21691 if (name != NULL)
21692 {
21693 name = vim_strsave(name);
21694 *pp = end;
21695 goto theend;
21696 }
21697
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021698 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021699 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021700 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021701 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21702 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21703 {
21704 /* When there was "s:" already or the name expanded to get a
21705 * leading "s:" then remove it. */
21706 lv.ll_name += 2;
21707 len -= 2;
21708 lead = 2;
21709 }
21710 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021711 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021712 {
21713 if (lead == 2) /* skip over "s:" */
21714 lv.ll_name += 2;
21715 len = (int)(end - lv.ll_name);
21716 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021717
21718 /*
21719 * Copy the function name to allocated memory.
21720 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21721 * Accept <SNR>123_name() outside a script.
21722 */
21723 if (skip)
21724 lead = 0; /* do nothing */
21725 else if (lead > 0)
21726 {
21727 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021728 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21729 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021730 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021731 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021732 if (current_SID <= 0)
21733 {
21734 EMSG(_(e_usingsid));
21735 goto theend;
21736 }
21737 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21738 lead += (int)STRLEN(sid_buf);
21739 }
21740 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021741 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021742 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021743 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021744 goto theend;
21745 }
21746 name = alloc((unsigned)(len + lead + 1));
21747 if (name != NULL)
21748 {
21749 if (lead > 0)
21750 {
21751 name[0] = K_SPECIAL;
21752 name[1] = KS_EXTRA;
21753 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021754 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021755 STRCPY(name + 3, sid_buf);
21756 }
21757 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21758 name[len + lead] = NUL;
21759 }
21760 *pp = end;
21761
21762theend:
21763 clear_lval(&lv);
21764 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765}
21766
21767/*
21768 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21769 * Return 2 if "p" starts with "s:".
21770 * Return 0 otherwise.
21771 */
21772 static int
21773eval_fname_script(p)
21774 char_u *p;
21775{
21776 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21777 || STRNICMP(p + 1, "SNR>", 4) == 0))
21778 return 5;
21779 if (p[0] == 's' && p[1] == ':')
21780 return 2;
21781 return 0;
21782}
21783
21784/*
21785 * Return TRUE if "p" starts with "<SID>" or "s:".
21786 * Only works if eval_fname_script() returned non-zero for "p"!
21787 */
21788 static int
21789eval_fname_sid(p)
21790 char_u *p;
21791{
21792 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21793}
21794
21795/*
21796 * List the head of the function: "name(arg1, arg2)".
21797 */
21798 static void
21799list_func_head(fp, indent)
21800 ufunc_T *fp;
21801 int indent;
21802{
21803 int j;
21804
21805 msg_start();
21806 if (indent)
21807 MSG_PUTS(" ");
21808 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021809 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021810 {
21811 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021812 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021813 }
21814 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021815 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021817 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818 {
21819 if (j)
21820 MSG_PUTS(", ");
21821 msg_puts(FUNCARG(fp, j));
21822 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021823 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824 {
21825 if (j)
21826 MSG_PUTS(", ");
21827 MSG_PUTS("...");
21828 }
21829 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021830 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021831 if (p_verbose > 0)
21832 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021833}
21834
21835/*
21836 * Find a function by name, return pointer to it in ufuncs.
21837 * Return NULL for unknown function.
21838 */
21839 static ufunc_T *
21840find_func(name)
21841 char_u *name;
21842{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021843 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021844
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021845 hi = hash_find(&func_hashtab, name);
21846 if (!HASHITEM_EMPTY(hi))
21847 return HI2UF(hi);
21848 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021849}
21850
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021851#if defined(EXITFREE) || defined(PROTO)
21852 void
21853free_all_functions()
21854{
21855 hashitem_T *hi;
21856
21857 /* Need to start all over every time, because func_free() may change the
21858 * hash table. */
21859 while (func_hashtab.ht_used > 0)
21860 for (hi = func_hashtab.ht_array; ; ++hi)
21861 if (!HASHITEM_EMPTY(hi))
21862 {
21863 func_free(HI2UF(hi));
21864 break;
21865 }
21866}
21867#endif
21868
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021869/*
21870 * Return TRUE if a function "name" exists.
21871 */
21872 static int
21873function_exists(name)
21874 char_u *name;
21875{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021876 char_u *nm = name;
21877 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021878 int n = FALSE;
21879
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021880 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021881 nm = skipwhite(nm);
21882
21883 /* Only accept "funcname", "funcname ", "funcname (..." and
21884 * "funcname(...", not "funcname!...". */
21885 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021886 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021887 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021888 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021889 else
21890 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021891 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021892 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021893 return n;
21894}
21895
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021896/*
21897 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021898 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021899 */
21900 static int
21901builtin_function(name)
21902 char_u *name;
21903{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021904 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21905 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021906}
21907
Bram Moolenaar05159a02005-02-26 23:04:13 +000021908#if defined(FEAT_PROFILE) || defined(PROTO)
21909/*
21910 * Start profiling function "fp".
21911 */
21912 static void
21913func_do_profile(fp)
21914 ufunc_T *fp;
21915{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021916 int len = fp->uf_lines.ga_len;
21917
21918 if (len == 0)
21919 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021920 fp->uf_tm_count = 0;
21921 profile_zero(&fp->uf_tm_self);
21922 profile_zero(&fp->uf_tm_total);
21923 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021924 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021925 if (fp->uf_tml_total == NULL)
21926 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021927 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021928 if (fp->uf_tml_self == NULL)
21929 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021930 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021931 fp->uf_tml_idx = -1;
21932 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21933 || fp->uf_tml_self == NULL)
21934 return; /* out of memory */
21935
21936 fp->uf_profiling = TRUE;
21937}
21938
21939/*
21940 * Dump the profiling results for all functions in file "fd".
21941 */
21942 void
21943func_dump_profile(fd)
21944 FILE *fd;
21945{
21946 hashitem_T *hi;
21947 int todo;
21948 ufunc_T *fp;
21949 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021950 ufunc_T **sorttab;
21951 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021952
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021953 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021954 if (todo == 0)
21955 return; /* nothing to dump */
21956
Bram Moolenaar73830342005-02-28 22:48:19 +000021957 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21958
Bram Moolenaar05159a02005-02-26 23:04:13 +000021959 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21960 {
21961 if (!HASHITEM_EMPTY(hi))
21962 {
21963 --todo;
21964 fp = HI2UF(hi);
21965 if (fp->uf_profiling)
21966 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021967 if (sorttab != NULL)
21968 sorttab[st_len++] = fp;
21969
Bram Moolenaar05159a02005-02-26 23:04:13 +000021970 if (fp->uf_name[0] == K_SPECIAL)
21971 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21972 else
21973 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21974 if (fp->uf_tm_count == 1)
21975 fprintf(fd, "Called 1 time\n");
21976 else
21977 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21978 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21979 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21980 fprintf(fd, "\n");
21981 fprintf(fd, "count total (s) self (s)\n");
21982
21983 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21984 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021985 if (FUNCLINE(fp, i) == NULL)
21986 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021987 prof_func_line(fd, fp->uf_tml_count[i],
21988 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021989 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21990 }
21991 fprintf(fd, "\n");
21992 }
21993 }
21994 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021995
21996 if (sorttab != NULL && st_len > 0)
21997 {
21998 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21999 prof_total_cmp);
22000 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22001 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22002 prof_self_cmp);
22003 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22004 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022005
22006 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022007}
Bram Moolenaar73830342005-02-28 22:48:19 +000022008
22009 static void
22010prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22011 FILE *fd;
22012 ufunc_T **sorttab;
22013 int st_len;
22014 char *title;
22015 int prefer_self; /* when equal print only self time */
22016{
22017 int i;
22018 ufunc_T *fp;
22019
22020 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22021 fprintf(fd, "count total (s) self (s) function\n");
22022 for (i = 0; i < 20 && i < st_len; ++i)
22023 {
22024 fp = sorttab[i];
22025 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22026 prefer_self);
22027 if (fp->uf_name[0] == K_SPECIAL)
22028 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22029 else
22030 fprintf(fd, " %s()\n", fp->uf_name);
22031 }
22032 fprintf(fd, "\n");
22033}
22034
22035/*
22036 * Print the count and times for one function or function line.
22037 */
22038 static void
22039prof_func_line(fd, count, total, self, prefer_self)
22040 FILE *fd;
22041 int count;
22042 proftime_T *total;
22043 proftime_T *self;
22044 int prefer_self; /* when equal print only self time */
22045{
22046 if (count > 0)
22047 {
22048 fprintf(fd, "%5d ", count);
22049 if (prefer_self && profile_equal(total, self))
22050 fprintf(fd, " ");
22051 else
22052 fprintf(fd, "%s ", profile_msg(total));
22053 if (!prefer_self && profile_equal(total, self))
22054 fprintf(fd, " ");
22055 else
22056 fprintf(fd, "%s ", profile_msg(self));
22057 }
22058 else
22059 fprintf(fd, " ");
22060}
22061
22062/*
22063 * Compare function for total time sorting.
22064 */
22065 static int
22066#ifdef __BORLANDC__
22067_RTLENTRYF
22068#endif
22069prof_total_cmp(s1, s2)
22070 const void *s1;
22071 const void *s2;
22072{
22073 ufunc_T *p1, *p2;
22074
22075 p1 = *(ufunc_T **)s1;
22076 p2 = *(ufunc_T **)s2;
22077 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22078}
22079
22080/*
22081 * Compare function for self time sorting.
22082 */
22083 static int
22084#ifdef __BORLANDC__
22085_RTLENTRYF
22086#endif
22087prof_self_cmp(s1, s2)
22088 const void *s1;
22089 const void *s2;
22090{
22091 ufunc_T *p1, *p2;
22092
22093 p1 = *(ufunc_T **)s1;
22094 p2 = *(ufunc_T **)s2;
22095 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22096}
22097
Bram Moolenaar05159a02005-02-26 23:04:13 +000022098#endif
22099
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022100/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022101 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022102 * Return TRUE if a package was loaded.
22103 */
22104 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022105script_autoload(name, reload)
22106 char_u *name;
22107 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022108{
22109 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022110 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022111 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022112 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022113
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022114 /* Return quickly when autoload disabled. */
22115 if (no_autoload)
22116 return FALSE;
22117
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022118 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022119 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022120 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022121 return FALSE;
22122
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022123 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022124
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022125 /* Find the name in the list of previously loaded package names. Skip
22126 * "autoload/", it's always the same. */
22127 for (i = 0; i < ga_loaded.ga_len; ++i)
22128 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22129 break;
22130 if (!reload && i < ga_loaded.ga_len)
22131 ret = FALSE; /* was loaded already */
22132 else
22133 {
22134 /* Remember the name if it wasn't loaded already. */
22135 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22136 {
22137 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22138 tofree = NULL;
22139 }
22140
22141 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022142 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022143 ret = TRUE;
22144 }
22145
22146 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022147 return ret;
22148}
22149
22150/*
22151 * Return the autoload script name for a function or variable name.
22152 * Returns NULL when out of memory.
22153 */
22154 static char_u *
22155autoload_name(name)
22156 char_u *name;
22157{
22158 char_u *p;
22159 char_u *scriptname;
22160
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022161 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022162 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22163 if (scriptname == NULL)
22164 return FALSE;
22165 STRCPY(scriptname, "autoload/");
22166 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022167 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022168 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022169 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022170 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022171 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022172}
22173
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22175
22176/*
22177 * Function given to ExpandGeneric() to obtain the list of user defined
22178 * function names.
22179 */
22180 char_u *
22181get_user_func_name(xp, idx)
22182 expand_T *xp;
22183 int idx;
22184{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022185 static long_u done;
22186 static hashitem_T *hi;
22187 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188
22189 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022190 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022191 done = 0;
22192 hi = func_hashtab.ht_array;
22193 }
22194 if (done < func_hashtab.ht_used)
22195 {
22196 if (done++ > 0)
22197 ++hi;
22198 while (HASHITEM_EMPTY(hi))
22199 ++hi;
22200 fp = HI2UF(hi);
22201
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022202 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022203 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022205 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22206 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022207
22208 cat_func_name(IObuff, fp);
22209 if (xp->xp_context != EXPAND_USER_FUNC)
22210 {
22211 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022212 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022213 STRCAT(IObuff, ")");
22214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022215 return IObuff;
22216 }
22217 return NULL;
22218}
22219
22220#endif /* FEAT_CMDL_COMPL */
22221
22222/*
22223 * Copy the function name of "fp" to buffer "buf".
22224 * "buf" must be able to hold the function name plus three bytes.
22225 * Takes care of script-local function names.
22226 */
22227 static void
22228cat_func_name(buf, fp)
22229 char_u *buf;
22230 ufunc_T *fp;
22231{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022232 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233 {
22234 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022235 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022236 }
22237 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022238 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022239}
22240
22241/*
22242 * ":delfunction {name}"
22243 */
22244 void
22245ex_delfunction(eap)
22246 exarg_T *eap;
22247{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022248 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022249 char_u *p;
22250 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022251 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022252
22253 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022254 name = trans_function_name(&p, eap->skip, 0, &fudi);
22255 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022256 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022257 {
22258 if (fudi.fd_dict != NULL && !eap->skip)
22259 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262 if (!ends_excmd(*skipwhite(p)))
22263 {
22264 vim_free(name);
22265 EMSG(_(e_trailing));
22266 return;
22267 }
22268 eap->nextcmd = check_nextcmd(p);
22269 if (eap->nextcmd != NULL)
22270 *p = NUL;
22271
22272 if (!eap->skip)
22273 fp = find_func(name);
22274 vim_free(name);
22275
22276 if (!eap->skip)
22277 {
22278 if (fp == NULL)
22279 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022280 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022281 return;
22282 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022283 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 {
22285 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22286 return;
22287 }
22288
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022289 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022291 /* Delete the dict item that refers to the function, it will
22292 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022293 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022295 else
22296 func_free(fp);
22297 }
22298}
22299
22300/*
22301 * Free a function and remove it from the list of functions.
22302 */
22303 static void
22304func_free(fp)
22305 ufunc_T *fp;
22306{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022307 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022308
22309 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022310 ga_clear_strings(&(fp->uf_args));
22311 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022312#ifdef FEAT_PROFILE
22313 vim_free(fp->uf_tml_count);
22314 vim_free(fp->uf_tml_total);
22315 vim_free(fp->uf_tml_self);
22316#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022317
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022318 /* remove the function from the function hashtable */
22319 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22320 if (HASHITEM_EMPTY(hi))
22321 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022322 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022323 hash_remove(&func_hashtab, hi);
22324
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022325 vim_free(fp);
22326}
22327
22328/*
22329 * Unreference a Function: decrement the reference count and free it when it
22330 * becomes zero. Only for numbered functions.
22331 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022332 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022333func_unref(name)
22334 char_u *name;
22335{
22336 ufunc_T *fp;
22337
22338 if (name != NULL && isdigit(*name))
22339 {
22340 fp = find_func(name);
22341 if (fp == NULL)
22342 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022343 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022344 {
22345 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022346 * when "uf_calls" becomes zero. */
22347 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022348 func_free(fp);
22349 }
22350 }
22351}
22352
22353/*
22354 * Count a reference to a Function.
22355 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022356 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022357func_ref(name)
22358 char_u *name;
22359{
22360 ufunc_T *fp;
22361
22362 if (name != NULL && isdigit(*name))
22363 {
22364 fp = find_func(name);
22365 if (fp == NULL)
22366 EMSG2(_(e_intern2), "func_ref()");
22367 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022368 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369 }
22370}
22371
22372/*
22373 * Call a user function.
22374 */
22375 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022376call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022377 ufunc_T *fp; /* pointer to function */
22378 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022379 typval_T *argvars; /* arguments */
22380 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022381 linenr_T firstline; /* first line of range */
22382 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022383 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022384{
Bram Moolenaar33570922005-01-25 22:26:29 +000022385 char_u *save_sourcing_name;
22386 linenr_T save_sourcing_lnum;
22387 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022388 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022389 int save_did_emsg;
22390 static int depth = 0;
22391 dictitem_T *v;
22392 int fixvar_idx = 0; /* index in fixvar[] */
22393 int i;
22394 int ai;
22395 char_u numbuf[NUMBUFLEN];
22396 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022397#ifdef FEAT_PROFILE
22398 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022399 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401
22402 /* If depth of calling is getting too high, don't execute the function */
22403 if (depth >= p_mfd)
22404 {
22405 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022406 rettv->v_type = VAR_NUMBER;
22407 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408 return;
22409 }
22410 ++depth;
22411
22412 line_breakcheck(); /* check for CTRL-C hit */
22413
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022414 fc = (funccall_T *)alloc(sizeof(funccall_T));
22415 fc->caller = current_funccal;
22416 current_funccal = fc;
22417 fc->func = fp;
22418 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022419 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022420 fc->linenr = 0;
22421 fc->returned = FALSE;
22422 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022423 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022424 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22425 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426
Bram Moolenaar33570922005-01-25 22:26:29 +000022427 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022428 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022429 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22430 * each argument variable and saves a lot of time.
22431 */
22432 /*
22433 * Init l: variables.
22434 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022435 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022436 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022437 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022438 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22439 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022440 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022441 name = v->di_key;
22442 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022443 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022444 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022445 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022446 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022447 v->di_tv.vval.v_dict = selfdict;
22448 ++selfdict->dv_refcount;
22449 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022450
Bram Moolenaar33570922005-01-25 22:26:29 +000022451 /*
22452 * Init a: variables.
22453 * Set a:0 to "argcount".
22454 * Set a:000 to a list with room for the "..." arguments.
22455 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022456 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022457 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022458 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022459 /* Use "name" to avoid a warning from some compiler that checks the
22460 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022461 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022462 name = v->di_key;
22463 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022464 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022465 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022466 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022467 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022468 v->di_tv.vval.v_list = &fc->l_varlist;
22469 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22470 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22471 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022472
22473 /*
22474 * Set a:firstline to "firstline" and a:lastline to "lastline".
22475 * Set a:name to named arguments.
22476 * Set a:N to the "..." arguments.
22477 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022478 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022479 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022480 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022481 (varnumber_T)lastline);
22482 for (i = 0; i < argcount; ++i)
22483 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022484 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022485 if (ai < 0)
22486 /* named argument a:name */
22487 name = FUNCARG(fp, i);
22488 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022489 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022490 /* "..." argument a:1, a:2, etc. */
22491 sprintf((char *)numbuf, "%d", ai + 1);
22492 name = numbuf;
22493 }
22494 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22495 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022496 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022497 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22498 }
22499 else
22500 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022501 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22502 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022503 if (v == NULL)
22504 break;
22505 v->di_flags = DI_FLAGS_RO;
22506 }
22507 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022508 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022509
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022510 /* Note: the values are copied directly to avoid alloc/free.
22511 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022512 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022513 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022514
22515 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22516 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022517 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22518 fc->l_listitems[ai].li_tv = argvars[i];
22519 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022520 }
22521 }
22522
Bram Moolenaar071d4272004-06-13 20:20:40 +000022523 /* Don't redraw while executing the function. */
22524 ++RedrawingDisabled;
22525 save_sourcing_name = sourcing_name;
22526 save_sourcing_lnum = sourcing_lnum;
22527 sourcing_lnum = 1;
22528 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022529 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022530 if (sourcing_name != NULL)
22531 {
22532 if (save_sourcing_name != NULL
22533 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22534 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22535 else
22536 STRCPY(sourcing_name, "function ");
22537 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22538
22539 if (p_verbose >= 12)
22540 {
22541 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022542 verbose_enter_scroll();
22543
Bram Moolenaar555b2802005-05-19 21:08:39 +000022544 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545 if (p_verbose >= 14)
22546 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022548 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022549 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022550 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022551
22552 msg_puts((char_u *)"(");
22553 for (i = 0; i < argcount; ++i)
22554 {
22555 if (i > 0)
22556 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022557 if (argvars[i].v_type == VAR_NUMBER)
22558 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022559 else
22560 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022561 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22562 if (s != NULL)
22563 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022564 if (vim_strsize(s) > MSG_BUF_CLEN)
22565 {
22566 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22567 s = buf;
22568 }
22569 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022570 vim_free(tofree);
22571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022572 }
22573 }
22574 msg_puts((char_u *)")");
22575 }
22576 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022577
22578 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579 --no_wait_return;
22580 }
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)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022584 {
22585 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22586 func_do_profile(fp);
22587 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022588 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022589 {
22590 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022591 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022592 profile_zero(&fp->uf_tm_children);
22593 }
22594 script_prof_save(&wait_start);
22595 }
22596#endif
22597
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022599 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022600 save_did_emsg = did_emsg;
22601 did_emsg = FALSE;
22602
22603 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022604 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22606
22607 --RedrawingDisabled;
22608
22609 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022610 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022611 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022612 clear_tv(rettv);
22613 rettv->v_type = VAR_NUMBER;
22614 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022615 }
22616
Bram Moolenaar05159a02005-02-26 23:04:13 +000022617#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022618 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022619 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022620 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022621 profile_end(&call_start);
22622 profile_sub_wait(&wait_start, &call_start);
22623 profile_add(&fp->uf_tm_total, &call_start);
22624 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022625 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022626 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022627 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22628 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022629 }
22630 }
22631#endif
22632
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633 /* when being verbose, mention the return value */
22634 if (p_verbose >= 12)
22635 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022637 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022640 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022641 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022642 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022643 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022644 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022646 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022647 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022648 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022649 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022650
Bram Moolenaar555b2802005-05-19 21:08:39 +000022651 /* The value may be very long. Skip the middle part, so that we
22652 * have some idea how it starts and ends. smsg() would always
22653 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022654 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022655 if (s != NULL)
22656 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022657 if (vim_strsize(s) > MSG_BUF_CLEN)
22658 {
22659 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22660 s = buf;
22661 }
22662 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022663 vim_free(tofree);
22664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 }
22666 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022667
22668 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022669 --no_wait_return;
22670 }
22671
22672 vim_free(sourcing_name);
22673 sourcing_name = save_sourcing_name;
22674 sourcing_lnum = save_sourcing_lnum;
22675 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022676#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022677 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022678 script_prof_restore(&wait_start);
22679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022680
22681 if (p_verbose >= 12 && sourcing_name != NULL)
22682 {
22683 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022684 verbose_enter_scroll();
22685
Bram Moolenaar555b2802005-05-19 21:08:39 +000022686 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022687 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022688
22689 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690 --no_wait_return;
22691 }
22692
22693 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022694 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022695 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022696
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022697 /* If the a:000 list and the l: and a: dicts are not referenced we can
22698 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022699 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22700 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22701 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22702 {
22703 free_funccal(fc, FALSE);
22704 }
22705 else
22706 {
22707 hashitem_T *hi;
22708 listitem_T *li;
22709 int todo;
22710
22711 /* "fc" is still in use. This can happen when returning "a:000" or
22712 * assigning "l:" to a global variable.
22713 * Link "fc" in the list for garbage collection later. */
22714 fc->caller = previous_funccal;
22715 previous_funccal = fc;
22716
22717 /* Make a copy of the a: variables, since we didn't do that above. */
22718 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22719 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22720 {
22721 if (!HASHITEM_EMPTY(hi))
22722 {
22723 --todo;
22724 v = HI2DI(hi);
22725 copy_tv(&v->di_tv, &v->di_tv);
22726 }
22727 }
22728
22729 /* Make a copy of the a:000 items, since we didn't do that above. */
22730 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22731 copy_tv(&li->li_tv, &li->li_tv);
22732 }
22733}
22734
22735/*
22736 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022737 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022738 */
22739 static int
22740can_free_funccal(fc, copyID)
22741 funccall_T *fc;
22742 int copyID;
22743{
22744 return (fc->l_varlist.lv_copyID != copyID
22745 && fc->l_vars.dv_copyID != copyID
22746 && fc->l_avars.dv_copyID != copyID);
22747}
22748
22749/*
22750 * Free "fc" and what it contains.
22751 */
22752 static void
22753free_funccal(fc, free_val)
22754 funccall_T *fc;
22755 int free_val; /* a: vars were allocated */
22756{
22757 listitem_T *li;
22758
22759 /* The a: variables typevals may not have been allocated, only free the
22760 * allocated variables. */
22761 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22762
22763 /* free all l: variables */
22764 vars_clear(&fc->l_vars.dv_hashtab);
22765
22766 /* Free the a:000 variables if they were allocated. */
22767 if (free_val)
22768 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22769 clear_tv(&li->li_tv);
22770
22771 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022772}
22773
22774/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022775 * Add a number variable "name" to dict "dp" with value "nr".
22776 */
22777 static void
22778add_nr_var(dp, v, name, nr)
22779 dict_T *dp;
22780 dictitem_T *v;
22781 char *name;
22782 varnumber_T nr;
22783{
22784 STRCPY(v->di_key, name);
22785 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22786 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22787 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022788 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022789 v->di_tv.vval.v_number = nr;
22790}
22791
22792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022793 * ":return [expr]"
22794 */
22795 void
22796ex_return(eap)
22797 exarg_T *eap;
22798{
22799 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022800 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022801 int returning = FALSE;
22802
22803 if (current_funccal == NULL)
22804 {
22805 EMSG(_("E133: :return not inside a function"));
22806 return;
22807 }
22808
22809 if (eap->skip)
22810 ++emsg_skip;
22811
22812 eap->nextcmd = NULL;
22813 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022814 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022815 {
22816 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022817 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022818 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022819 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022820 }
22821 /* It's safer to return also on error. */
22822 else if (!eap->skip)
22823 {
22824 /*
22825 * Return unless the expression evaluation has been cancelled due to an
22826 * aborting error, an interrupt, or an exception.
22827 */
22828 if (!aborting())
22829 returning = do_return(eap, FALSE, TRUE, NULL);
22830 }
22831
22832 /* When skipping or the return gets pending, advance to the next command
22833 * in this line (!returning). Otherwise, ignore the rest of the line.
22834 * Following lines will be ignored by get_func_line(). */
22835 if (returning)
22836 eap->nextcmd = NULL;
22837 else if (eap->nextcmd == NULL) /* no argument */
22838 eap->nextcmd = check_nextcmd(arg);
22839
22840 if (eap->skip)
22841 --emsg_skip;
22842}
22843
22844/*
22845 * Return from a function. Possibly makes the return pending. Also called
22846 * for a pending return at the ":endtry" or after returning from an extra
22847 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022848 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022849 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022850 * FALSE when the return gets pending.
22851 */
22852 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022853do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854 exarg_T *eap;
22855 int reanimate;
22856 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022857 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022858{
22859 int idx;
22860 struct condstack *cstack = eap->cstack;
22861
22862 if (reanimate)
22863 /* Undo the return. */
22864 current_funccal->returned = FALSE;
22865
22866 /*
22867 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22868 * not in its finally clause (which then is to be executed next) is found.
22869 * In this case, make the ":return" pending for execution at the ":endtry".
22870 * Otherwise, return normally.
22871 */
22872 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22873 if (idx >= 0)
22874 {
22875 cstack->cs_pending[idx] = CSTP_RETURN;
22876
22877 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022878 /* A pending return again gets pending. "rettv" points to an
22879 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022881 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882 else
22883 {
22884 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022885 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022886 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022887 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022889 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 {
22891 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022892 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022893 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022894 else
22895 EMSG(_(e_outofmem));
22896 }
22897 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022898 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022899
22900 if (reanimate)
22901 {
22902 /* The pending return value could be overwritten by a ":return"
22903 * without argument in a finally clause; reset the default
22904 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022905 current_funccal->rettv->v_type = VAR_NUMBER;
22906 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907 }
22908 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022909 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910 }
22911 else
22912 {
22913 current_funccal->returned = TRUE;
22914
22915 /* If the return is carried out now, store the return value. For
22916 * a return immediately after reanimation, the value is already
22917 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022918 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022920 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022921 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022922 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022923 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022924 }
22925 }
22926
22927 return idx < 0;
22928}
22929
22930/*
22931 * Free the variable with a pending return value.
22932 */
22933 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022934discard_pending_return(rettv)
22935 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936{
Bram Moolenaar33570922005-01-25 22:26:29 +000022937 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938}
22939
22940/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022941 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022942 * is an allocated string. Used by report_pending() for verbose messages.
22943 */
22944 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022945get_return_cmd(rettv)
22946 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022948 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022949 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022950 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022952 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022953 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022954 if (s == NULL)
22955 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022956
22957 STRCPY(IObuff, ":return ");
22958 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22959 if (STRLEN(s) + 8 >= IOSIZE)
22960 STRCPY(IObuff + IOSIZE - 4, "...");
22961 vim_free(tofree);
22962 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022963}
22964
22965/*
22966 * Get next function line.
22967 * Called by do_cmdline() to get the next line.
22968 * Returns allocated string, or NULL for end of function.
22969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022970 char_u *
22971get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022972 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022973 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022974 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975{
Bram Moolenaar33570922005-01-25 22:26:29 +000022976 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022977 ufunc_T *fp = fcp->func;
22978 char_u *retval;
22979 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980
22981 /* If breakpoints have been added/deleted need to check for it. */
22982 if (fcp->dbg_tick != debug_tick)
22983 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022984 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 sourcing_lnum);
22986 fcp->dbg_tick = debug_tick;
22987 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022988#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022989 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022990 func_line_end(cookie);
22991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022992
Bram Moolenaar05159a02005-02-26 23:04:13 +000022993 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022994 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22995 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996 retval = NULL;
22997 else
22998 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022999 /* Skip NULL lines (continuation lines). */
23000 while (fcp->linenr < gap->ga_len
23001 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23002 ++fcp->linenr;
23003 if (fcp->linenr >= gap->ga_len)
23004 retval = NULL;
23005 else
23006 {
23007 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23008 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023009#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023010 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023011 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023012#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023014 }
23015
23016 /* Did we encounter a breakpoint? */
23017 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23018 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023019 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023020 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023021 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022 sourcing_lnum);
23023 fcp->dbg_tick = debug_tick;
23024 }
23025
23026 return retval;
23027}
23028
Bram Moolenaar05159a02005-02-26 23:04:13 +000023029#if defined(FEAT_PROFILE) || defined(PROTO)
23030/*
23031 * Called when starting to read a function line.
23032 * "sourcing_lnum" must be correct!
23033 * When skipping lines it may not actually be executed, but we won't find out
23034 * until later and we need to store the time now.
23035 */
23036 void
23037func_line_start(cookie)
23038 void *cookie;
23039{
23040 funccall_T *fcp = (funccall_T *)cookie;
23041 ufunc_T *fp = fcp->func;
23042
23043 if (fp->uf_profiling && sourcing_lnum >= 1
23044 && sourcing_lnum <= fp->uf_lines.ga_len)
23045 {
23046 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023047 /* Skip continuation lines. */
23048 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23049 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023050 fp->uf_tml_execed = FALSE;
23051 profile_start(&fp->uf_tml_start);
23052 profile_zero(&fp->uf_tml_children);
23053 profile_get_wait(&fp->uf_tml_wait);
23054 }
23055}
23056
23057/*
23058 * Called when actually executing a function line.
23059 */
23060 void
23061func_line_exec(cookie)
23062 void *cookie;
23063{
23064 funccall_T *fcp = (funccall_T *)cookie;
23065 ufunc_T *fp = fcp->func;
23066
23067 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23068 fp->uf_tml_execed = TRUE;
23069}
23070
23071/*
23072 * Called when done with a function line.
23073 */
23074 void
23075func_line_end(cookie)
23076 void *cookie;
23077{
23078 funccall_T *fcp = (funccall_T *)cookie;
23079 ufunc_T *fp = fcp->func;
23080
23081 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23082 {
23083 if (fp->uf_tml_execed)
23084 {
23085 ++fp->uf_tml_count[fp->uf_tml_idx];
23086 profile_end(&fp->uf_tml_start);
23087 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023088 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023089 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23090 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023091 }
23092 fp->uf_tml_idx = -1;
23093 }
23094}
23095#endif
23096
Bram Moolenaar071d4272004-06-13 20:20:40 +000023097/*
23098 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023099 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023100 */
23101 int
23102func_has_ended(cookie)
23103 void *cookie;
23104{
Bram Moolenaar33570922005-01-25 22:26:29 +000023105 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023106
23107 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23108 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023109 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110 || fcp->returned);
23111}
23112
23113/*
23114 * return TRUE if cookie indicates a function which "abort"s on errors.
23115 */
23116 int
23117func_has_abort(cookie)
23118 void *cookie;
23119{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023120 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023121}
23122
23123#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23124typedef enum
23125{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023126 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23127 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23128 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023129} var_flavour_T;
23130
23131static var_flavour_T var_flavour __ARGS((char_u *varname));
23132
23133 static var_flavour_T
23134var_flavour(varname)
23135 char_u *varname;
23136{
23137 char_u *p = varname;
23138
23139 if (ASCII_ISUPPER(*p))
23140 {
23141 while (*(++p))
23142 if (ASCII_ISLOWER(*p))
23143 return VAR_FLAVOUR_SESSION;
23144 return VAR_FLAVOUR_VIMINFO;
23145 }
23146 else
23147 return VAR_FLAVOUR_DEFAULT;
23148}
23149#endif
23150
23151#if defined(FEAT_VIMINFO) || defined(PROTO)
23152/*
23153 * Restore global vars that start with a capital from the viminfo file
23154 */
23155 int
23156read_viminfo_varlist(virp, writing)
23157 vir_T *virp;
23158 int writing;
23159{
23160 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023161 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023162 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023163
23164 if (!writing && (find_viminfo_parameter('!') != NULL))
23165 {
23166 tab = vim_strchr(virp->vir_line + 1, '\t');
23167 if (tab != NULL)
23168 {
23169 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023170 switch (*tab)
23171 {
23172 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023173#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023174 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023175#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023176 case 'D': type = VAR_DICT; break;
23177 case 'L': type = VAR_LIST; break;
23178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023179
23180 tab = vim_strchr(tab, '\t');
23181 if (tab != NULL)
23182 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023183 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023184 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023185 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023187#ifdef FEAT_FLOAT
23188 else if (type == VAR_FLOAT)
23189 (void)string2float(tab + 1, &tv.vval.v_float);
23190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023191 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023192 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023193 if (type == VAR_DICT || type == VAR_LIST)
23194 {
23195 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23196
23197 if (etv == NULL)
23198 /* Failed to parse back the dict or list, use it as a
23199 * string. */
23200 tv.v_type = VAR_STRING;
23201 else
23202 {
23203 vim_free(tv.vval.v_string);
23204 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023205 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023206 }
23207 }
23208
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023209 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023210
23211 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023212 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023213 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23214 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023215 }
23216 }
23217 }
23218
23219 return viminfo_readline(virp);
23220}
23221
23222/*
23223 * Write global vars that start with a capital to the viminfo file
23224 */
23225 void
23226write_viminfo_varlist(fp)
23227 FILE *fp;
23228{
Bram Moolenaar33570922005-01-25 22:26:29 +000023229 hashitem_T *hi;
23230 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023231 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023232 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023233 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023234 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023235 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023236
23237 if (find_viminfo_parameter('!') == NULL)
23238 return;
23239
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023240 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023241
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023242 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023243 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023244 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023245 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023246 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023247 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023248 this_var = HI2DI(hi);
23249 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023250 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023251 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023252 {
23253 case VAR_STRING: s = "STR"; break;
23254 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023255#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023256 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023257#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023258 case VAR_DICT: s = "DIC"; break;
23259 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023260 default: continue;
23261 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023262 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023263 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023264 if (p != NULL)
23265 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023266 vim_free(tofree);
23267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023268 }
23269 }
23270}
23271#endif
23272
23273#if defined(FEAT_SESSION) || defined(PROTO)
23274 int
23275store_session_globals(fd)
23276 FILE *fd;
23277{
Bram Moolenaar33570922005-01-25 22:26:29 +000023278 hashitem_T *hi;
23279 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023280 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023281 char_u *p, *t;
23282
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023283 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023284 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023285 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023286 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023287 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023288 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023289 this_var = HI2DI(hi);
23290 if ((this_var->di_tv.v_type == VAR_NUMBER
23291 || this_var->di_tv.v_type == VAR_STRING)
23292 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023293 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023294 /* Escape special characters with a backslash. Turn a LF and
23295 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023296 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023297 (char_u *)"\\\"\n\r");
23298 if (p == NULL) /* out of memory */
23299 break;
23300 for (t = p; *t != NUL; ++t)
23301 if (*t == '\n')
23302 *t = 'n';
23303 else if (*t == '\r')
23304 *t = 'r';
23305 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023306 this_var->di_key,
23307 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23308 : ' ',
23309 p,
23310 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23311 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023312 || put_eol(fd) == FAIL)
23313 {
23314 vim_free(p);
23315 return FAIL;
23316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023317 vim_free(p);
23318 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023319#ifdef FEAT_FLOAT
23320 else if (this_var->di_tv.v_type == VAR_FLOAT
23321 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23322 {
23323 float_T f = this_var->di_tv.vval.v_float;
23324 int sign = ' ';
23325
23326 if (f < 0)
23327 {
23328 f = -f;
23329 sign = '-';
23330 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023331 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023332 this_var->di_key, sign, f) < 0)
23333 || put_eol(fd) == FAIL)
23334 return FAIL;
23335 }
23336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023337 }
23338 }
23339 return OK;
23340}
23341#endif
23342
Bram Moolenaar661b1822005-07-28 22:36:45 +000023343/*
23344 * Display script name where an item was last set.
23345 * Should only be invoked when 'verbose' is non-zero.
23346 */
23347 void
23348last_set_msg(scriptID)
23349 scid_T scriptID;
23350{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023351 char_u *p;
23352
Bram Moolenaar661b1822005-07-28 22:36:45 +000023353 if (scriptID != 0)
23354 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023355 p = home_replace_save(NULL, get_scriptname(scriptID));
23356 if (p != NULL)
23357 {
23358 verbose_enter();
23359 MSG_PUTS(_("\n\tLast set from "));
23360 MSG_PUTS(p);
23361 vim_free(p);
23362 verbose_leave();
23363 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023364 }
23365}
23366
Bram Moolenaard812df62008-11-09 12:46:09 +000023367/*
23368 * List v:oldfiles in a nice way.
23369 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023370 void
23371ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023372 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023373{
23374 list_T *l = vimvars[VV_OLDFILES].vv_list;
23375 listitem_T *li;
23376 int nr = 0;
23377
23378 if (l == NULL)
23379 msg((char_u *)_("No old files"));
23380 else
23381 {
23382 msg_start();
23383 msg_scroll = TRUE;
23384 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23385 {
23386 msg_outnum((long)++nr);
23387 MSG_PUTS(": ");
23388 msg_outtrans(get_tv_string(&li->li_tv));
23389 msg_putchar('\n');
23390 out_flush(); /* output one line at a time */
23391 ui_breakcheck();
23392 }
23393 /* Assume "got_int" was set to truncate the listing. */
23394 got_int = FALSE;
23395
23396#ifdef FEAT_BROWSE_CMD
23397 if (cmdmod.browse)
23398 {
23399 quit_more = FALSE;
23400 nr = prompt_for_number(FALSE);
23401 msg_starthere();
23402 if (nr > 0)
23403 {
23404 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23405 (long)nr);
23406
23407 if (p != NULL)
23408 {
23409 p = expand_env_save(p);
23410 eap->arg = p;
23411 eap->cmdidx = CMD_edit;
23412 cmdmod.browse = FALSE;
23413 do_exedit(eap, NULL);
23414 vim_free(p);
23415 }
23416 }
23417 }
23418#endif
23419 }
23420}
23421
Bram Moolenaar071d4272004-06-13 20:20:40 +000023422#endif /* FEAT_EVAL */
23423
Bram Moolenaar071d4272004-06-13 20:20:40 +000023424
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023425#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426
23427#ifdef WIN3264
23428/*
23429 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23430 */
23431static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23432static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23433static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23434
23435/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023436 * Get the short path (8.3) for the filename in "fnamep".
23437 * Only works for a valid file name.
23438 * When the path gets longer "fnamep" is changed and the allocated buffer
23439 * is put in "bufp".
23440 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23441 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023442 */
23443 static int
23444get_short_pathname(fnamep, bufp, fnamelen)
23445 char_u **fnamep;
23446 char_u **bufp;
23447 int *fnamelen;
23448{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023449 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023450 char_u *newbuf;
23451
23452 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023453 l = GetShortPathName(*fnamep, *fnamep, len);
23454 if (l > len - 1)
23455 {
23456 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023457 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023458 newbuf = vim_strnsave(*fnamep, l);
23459 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023460 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023461
23462 vim_free(*bufp);
23463 *fnamep = *bufp = newbuf;
23464
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023465 /* Really should always succeed, as the buffer is big enough. */
23466 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023467 }
23468
23469 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023470 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023471}
23472
23473/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023474 * Get the short path (8.3) for the filename in "fname". The converted
23475 * path is returned in "bufp".
23476 *
23477 * Some of the directories specified in "fname" may not exist. This function
23478 * will shorten the existing directories at the beginning of the path and then
23479 * append the remaining non-existing path.
23480 *
23481 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023482 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023483 * bufp - Pointer to an allocated buffer for the filename.
23484 * fnamelen - Length of the filename pointed to by fname
23485 *
23486 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023487 */
23488 static int
23489shortpath_for_invalid_fname(fname, bufp, fnamelen)
23490 char_u **fname;
23491 char_u **bufp;
23492 int *fnamelen;
23493{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023494 char_u *short_fname, *save_fname, *pbuf_unused;
23495 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023496 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023497 int old_len, len;
23498 int new_len, sfx_len;
23499 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500
23501 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023502 old_len = *fnamelen;
23503 save_fname = vim_strnsave(*fname, old_len);
23504 pbuf_unused = NULL;
23505 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023506
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023507 endp = save_fname + old_len - 1; /* Find the end of the copy */
23508 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023509
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023510 /*
23511 * Try shortening the supplied path till it succeeds by removing one
23512 * directory at a time from the tail of the path.
23513 */
23514 len = 0;
23515 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023516 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023517 /* go back one path-separator */
23518 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23519 --endp;
23520 if (endp <= save_fname)
23521 break; /* processed the complete path */
23522
23523 /*
23524 * Replace the path separator with a NUL and try to shorten the
23525 * resulting path.
23526 */
23527 ch = *endp;
23528 *endp = 0;
23529 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023530 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023531 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23532 {
23533 retval = FAIL;
23534 goto theend;
23535 }
23536 *endp = ch; /* preserve the string */
23537
23538 if (len > 0)
23539 break; /* successfully shortened the path */
23540
23541 /* failed to shorten the path. Skip the path separator */
23542 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023543 }
23544
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023545 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023547 /*
23548 * Succeeded in shortening the path. Now concatenate the shortened
23549 * path with the remaining path at the tail.
23550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023552 /* Compute the length of the new path. */
23553 sfx_len = (int)(save_endp - endp) + 1;
23554 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023556 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023557 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023558 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023559 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023560 /* There is not enough space in the currently allocated string,
23561 * copy it to a buffer big enough. */
23562 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023563 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023564 {
23565 retval = FAIL;
23566 goto theend;
23567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023568 }
23569 else
23570 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023571 /* Transfer short_fname to the main buffer (it's big enough),
23572 * unless get_short_pathname() did its work in-place. */
23573 *fname = *bufp = save_fname;
23574 if (short_fname != save_fname)
23575 vim_strncpy(save_fname, short_fname, len);
23576 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023578
23579 /* concat the not-shortened part of the path */
23580 vim_strncpy(*fname + len, endp, sfx_len);
23581 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023582 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023583
23584theend:
23585 vim_free(pbuf_unused);
23586 vim_free(save_fname);
23587
23588 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023589}
23590
23591/*
23592 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023593 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023594 */
23595 static int
23596shortpath_for_partial(fnamep, bufp, fnamelen)
23597 char_u **fnamep;
23598 char_u **bufp;
23599 int *fnamelen;
23600{
23601 int sepcount, len, tflen;
23602 char_u *p;
23603 char_u *pbuf, *tfname;
23604 int hasTilde;
23605
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023606 /* Count up the path separators from the RHS.. so we know which part
23607 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023609 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023610 if (vim_ispathsep(*p))
23611 ++sepcount;
23612
23613 /* Need full path first (use expand_env() to remove a "~/") */
23614 hasTilde = (**fnamep == '~');
23615 if (hasTilde)
23616 pbuf = tfname = expand_env_save(*fnamep);
23617 else
23618 pbuf = tfname = FullName_save(*fnamep, FALSE);
23619
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023620 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023621
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023622 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23623 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023624
23625 if (len == 0)
23626 {
23627 /* Don't have a valid filename, so shorten the rest of the
23628 * path if we can. This CAN give us invalid 8.3 filenames, but
23629 * there's not a lot of point in guessing what it might be.
23630 */
23631 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023632 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23633 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 }
23635
23636 /* Count the paths backward to find the beginning of the desired string. */
23637 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023638 {
23639#ifdef FEAT_MBYTE
23640 if (has_mbyte)
23641 p -= mb_head_off(tfname, p);
23642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023643 if (vim_ispathsep(*p))
23644 {
23645 if (sepcount == 0 || (hasTilde && sepcount == 1))
23646 break;
23647 else
23648 sepcount --;
23649 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023651 if (hasTilde)
23652 {
23653 --p;
23654 if (p >= tfname)
23655 *p = '~';
23656 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023657 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658 }
23659 else
23660 ++p;
23661
23662 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23663 vim_free(*bufp);
23664 *fnamelen = (int)STRLEN(p);
23665 *bufp = pbuf;
23666 *fnamep = p;
23667
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023668 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023669}
23670#endif /* WIN3264 */
23671
23672/*
23673 * Adjust a filename, according to a string of modifiers.
23674 * *fnamep must be NUL terminated when called. When returning, the length is
23675 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023676 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023677 * When there is an error, *fnamep is set to NULL.
23678 */
23679 int
23680modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23681 char_u *src; /* string with modifiers */
23682 int *usedlen; /* characters after src that are used */
23683 char_u **fnamep; /* file name so far */
23684 char_u **bufp; /* buffer for allocated file name or NULL */
23685 int *fnamelen; /* length of fnamep */
23686{
23687 int valid = 0;
23688 char_u *tail;
23689 char_u *s, *p, *pbuf;
23690 char_u dirname[MAXPATHL];
23691 int c;
23692 int has_fullname = 0;
23693#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023694 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023695 int has_shortname = 0;
23696#endif
23697
23698repeat:
23699 /* ":p" - full path/file_name */
23700 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23701 {
23702 has_fullname = 1;
23703
23704 valid |= VALID_PATH;
23705 *usedlen += 2;
23706
23707 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23708 if ((*fnamep)[0] == '~'
23709#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23710 && ((*fnamep)[1] == '/'
23711# ifdef BACKSLASH_IN_FILENAME
23712 || (*fnamep)[1] == '\\'
23713# endif
23714 || (*fnamep)[1] == NUL)
23715
23716#endif
23717 )
23718 {
23719 *fnamep = expand_env_save(*fnamep);
23720 vim_free(*bufp); /* free any allocated file name */
23721 *bufp = *fnamep;
23722 if (*fnamep == NULL)
23723 return -1;
23724 }
23725
23726 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023727 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023728 {
23729 if (vim_ispathsep(*p)
23730 && p[1] == '.'
23731 && (p[2] == NUL
23732 || vim_ispathsep(p[2])
23733 || (p[2] == '.'
23734 && (p[3] == NUL || vim_ispathsep(p[3])))))
23735 break;
23736 }
23737
23738 /* FullName_save() is slow, don't use it when not needed. */
23739 if (*p != NUL || !vim_isAbsName(*fnamep))
23740 {
23741 *fnamep = FullName_save(*fnamep, *p != NUL);
23742 vim_free(*bufp); /* free any allocated file name */
23743 *bufp = *fnamep;
23744 if (*fnamep == NULL)
23745 return -1;
23746 }
23747
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023748#ifdef WIN3264
23749# if _WIN32_WINNT >= 0x0500
23750 if (vim_strchr(*fnamep, '~') != NULL)
23751 {
23752 /* Expand 8.3 filename to full path. Needed to make sure the same
23753 * file does not have two different names.
23754 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23755 p = alloc(_MAX_PATH + 1);
23756 if (p != NULL)
23757 {
23758 if (GetLongPathName(*fnamep, p, MAXPATHL))
23759 {
23760 vim_free(*bufp);
23761 *bufp = *fnamep = p;
23762 }
23763 else
23764 vim_free(p);
23765 }
23766 }
23767# endif
23768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023769 /* Append a path separator to a directory. */
23770 if (mch_isdir(*fnamep))
23771 {
23772 /* Make room for one or two extra characters. */
23773 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23774 vim_free(*bufp); /* free any allocated file name */
23775 *bufp = *fnamep;
23776 if (*fnamep == NULL)
23777 return -1;
23778 add_pathsep(*fnamep);
23779 }
23780 }
23781
23782 /* ":." - path relative to the current directory */
23783 /* ":~" - path relative to the home directory */
23784 /* ":8" - shortname path - postponed till after */
23785 while (src[*usedlen] == ':'
23786 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23787 {
23788 *usedlen += 2;
23789 if (c == '8')
23790 {
23791#ifdef WIN3264
23792 has_shortname = 1; /* Postpone this. */
23793#endif
23794 continue;
23795 }
23796 pbuf = NULL;
23797 /* Need full path first (use expand_env() to remove a "~/") */
23798 if (!has_fullname)
23799 {
23800 if (c == '.' && **fnamep == '~')
23801 p = pbuf = expand_env_save(*fnamep);
23802 else
23803 p = pbuf = FullName_save(*fnamep, FALSE);
23804 }
23805 else
23806 p = *fnamep;
23807
23808 has_fullname = 0;
23809
23810 if (p != NULL)
23811 {
23812 if (c == '.')
23813 {
23814 mch_dirname(dirname, MAXPATHL);
23815 s = shorten_fname(p, dirname);
23816 if (s != NULL)
23817 {
23818 *fnamep = s;
23819 if (pbuf != NULL)
23820 {
23821 vim_free(*bufp); /* free any allocated file name */
23822 *bufp = pbuf;
23823 pbuf = NULL;
23824 }
23825 }
23826 }
23827 else
23828 {
23829 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23830 /* Only replace it when it starts with '~' */
23831 if (*dirname == '~')
23832 {
23833 s = vim_strsave(dirname);
23834 if (s != NULL)
23835 {
23836 *fnamep = s;
23837 vim_free(*bufp);
23838 *bufp = s;
23839 }
23840 }
23841 }
23842 vim_free(pbuf);
23843 }
23844 }
23845
23846 tail = gettail(*fnamep);
23847 *fnamelen = (int)STRLEN(*fnamep);
23848
23849 /* ":h" - head, remove "/file_name", can be repeated */
23850 /* Don't remove the first "/" or "c:\" */
23851 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23852 {
23853 valid |= VALID_HEAD;
23854 *usedlen += 2;
23855 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023856 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023857 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023858 *fnamelen = (int)(tail - *fnamep);
23859#ifdef VMS
23860 if (*fnamelen > 0)
23861 *fnamelen += 1; /* the path separator is part of the path */
23862#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023863 if (*fnamelen == 0)
23864 {
23865 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23866 p = vim_strsave((char_u *)".");
23867 if (p == NULL)
23868 return -1;
23869 vim_free(*bufp);
23870 *bufp = *fnamep = tail = p;
23871 *fnamelen = 1;
23872 }
23873 else
23874 {
23875 while (tail > s && !after_pathsep(s, tail))
23876 mb_ptr_back(*fnamep, tail);
23877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023878 }
23879
23880 /* ":8" - shortname */
23881 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23882 {
23883 *usedlen += 2;
23884#ifdef WIN3264
23885 has_shortname = 1;
23886#endif
23887 }
23888
23889#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023890 /*
23891 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892 */
23893 if (has_shortname)
23894 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023895 /* Copy the string if it is shortened by :h and when it wasn't copied
23896 * yet, because we are going to change it in place. Avoids changing
23897 * the buffer name for "%:8". */
23898 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023899 {
23900 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023901 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023902 return -1;
23903 vim_free(*bufp);
23904 *bufp = *fnamep = p;
23905 }
23906
23907 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023908 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023909 if (!has_fullname && !vim_isAbsName(*fnamep))
23910 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023911 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023912 return -1;
23913 }
23914 else
23915 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023916 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023917
Bram Moolenaardc935552011-08-17 15:23:23 +020023918 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023919 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023920 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023921 return -1;
23922
23923 if (l == 0)
23924 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023925 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023926 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023927 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023928 return -1;
23929 }
23930 *fnamelen = l;
23931 }
23932 }
23933#endif /* WIN3264 */
23934
23935 /* ":t" - tail, just the basename */
23936 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23937 {
23938 *usedlen += 2;
23939 *fnamelen -= (int)(tail - *fnamep);
23940 *fnamep = tail;
23941 }
23942
23943 /* ":e" - extension, can be repeated */
23944 /* ":r" - root, without extension, can be repeated */
23945 while (src[*usedlen] == ':'
23946 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23947 {
23948 /* find a '.' in the tail:
23949 * - for second :e: before the current fname
23950 * - otherwise: The last '.'
23951 */
23952 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23953 s = *fnamep - 2;
23954 else
23955 s = *fnamep + *fnamelen - 1;
23956 for ( ; s > tail; --s)
23957 if (s[0] == '.')
23958 break;
23959 if (src[*usedlen + 1] == 'e') /* :e */
23960 {
23961 if (s > tail)
23962 {
23963 *fnamelen += (int)(*fnamep - (s + 1));
23964 *fnamep = s + 1;
23965#ifdef VMS
23966 /* cut version from the extension */
23967 s = *fnamep + *fnamelen - 1;
23968 for ( ; s > *fnamep; --s)
23969 if (s[0] == ';')
23970 break;
23971 if (s > *fnamep)
23972 *fnamelen = s - *fnamep;
23973#endif
23974 }
23975 else if (*fnamep <= tail)
23976 *fnamelen = 0;
23977 }
23978 else /* :r */
23979 {
23980 if (s > tail) /* remove one extension */
23981 *fnamelen = (int)(s - *fnamep);
23982 }
23983 *usedlen += 2;
23984 }
23985
23986 /* ":s?pat?foo?" - substitute */
23987 /* ":gs?pat?foo?" - global substitute */
23988 if (src[*usedlen] == ':'
23989 && (src[*usedlen + 1] == 's'
23990 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23991 {
23992 char_u *str;
23993 char_u *pat;
23994 char_u *sub;
23995 int sep;
23996 char_u *flags;
23997 int didit = FALSE;
23998
23999 flags = (char_u *)"";
24000 s = src + *usedlen + 2;
24001 if (src[*usedlen + 1] == 'g')
24002 {
24003 flags = (char_u *)"g";
24004 ++s;
24005 }
24006
24007 sep = *s++;
24008 if (sep)
24009 {
24010 /* find end of pattern */
24011 p = vim_strchr(s, sep);
24012 if (p != NULL)
24013 {
24014 pat = vim_strnsave(s, (int)(p - s));
24015 if (pat != NULL)
24016 {
24017 s = p + 1;
24018 /* find end of substitution */
24019 p = vim_strchr(s, sep);
24020 if (p != NULL)
24021 {
24022 sub = vim_strnsave(s, (int)(p - s));
24023 str = vim_strnsave(*fnamep, *fnamelen);
24024 if (sub != NULL && str != NULL)
24025 {
24026 *usedlen = (int)(p + 1 - src);
24027 s = do_string_sub(str, pat, sub, flags);
24028 if (s != NULL)
24029 {
24030 *fnamep = s;
24031 *fnamelen = (int)STRLEN(s);
24032 vim_free(*bufp);
24033 *bufp = s;
24034 didit = TRUE;
24035 }
24036 }
24037 vim_free(sub);
24038 vim_free(str);
24039 }
24040 vim_free(pat);
24041 }
24042 }
24043 /* after using ":s", repeat all the modifiers */
24044 if (didit)
24045 goto repeat;
24046 }
24047 }
24048
24049 return valid;
24050}
24051
24052/*
24053 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24054 * "flags" can be "g" to do a global substitute.
24055 * Returns an allocated string, NULL for error.
24056 */
24057 char_u *
24058do_string_sub(str, pat, sub, flags)
24059 char_u *str;
24060 char_u *pat;
24061 char_u *sub;
24062 char_u *flags;
24063{
24064 int sublen;
24065 regmatch_T regmatch;
24066 int i;
24067 int do_all;
24068 char_u *tail;
24069 garray_T ga;
24070 char_u *ret;
24071 char_u *save_cpo;
24072
24073 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24074 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024075 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024076
24077 ga_init2(&ga, 1, 200);
24078
24079 do_all = (flags[0] == 'g');
24080
24081 regmatch.rm_ic = p_ic;
24082 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24083 if (regmatch.regprog != NULL)
24084 {
24085 tail = str;
24086 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24087 {
24088 /*
24089 * Get some space for a temporary buffer to do the substitution
24090 * into. It will contain:
24091 * - The text up to where the match is.
24092 * - The substituted text.
24093 * - The text after the match.
24094 */
24095 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24096 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24097 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24098 {
24099 ga_clear(&ga);
24100 break;
24101 }
24102
24103 /* copy the text up to where the match is */
24104 i = (int)(regmatch.startp[0] - tail);
24105 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24106 /* add the substituted text */
24107 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24108 + ga.ga_len + i, TRUE, TRUE, FALSE);
24109 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024110 /* avoid getting stuck on a match with an empty string */
24111 if (tail == regmatch.endp[0])
24112 {
24113 if (*tail == NUL)
24114 break;
24115 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24116 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024117 }
24118 else
24119 {
24120 tail = regmatch.endp[0];
24121 if (*tail == NUL)
24122 break;
24123 }
24124 if (!do_all)
24125 break;
24126 }
24127
24128 if (ga.ga_data != NULL)
24129 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24130
24131 vim_free(regmatch.regprog);
24132 }
24133
24134 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24135 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024136 if (p_cpo == empty_option)
24137 p_cpo = save_cpo;
24138 else
24139 /* Darn, evaluating {sub} expression changed the value. */
24140 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024141
24142 return ret;
24143}
24144
24145#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */